Skip to content

Commit

Permalink
RSCT operator status changes
Browse files Browse the repository at this point in the history
Signed-off-by: manju956 <[email protected]>

Address review comments

Signed-off-by: manju956 <[email protected]>
  • Loading branch information
manju956 committed Oct 24, 2024
1 parent 7746908 commit 703c7bf
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 13 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/rsct_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type RSCTSpec struct {
type RSCTStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
State *string `json:"state,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
7 changes: 6 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions bundle/manifests/rsct-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ metadata:
},
"name": "rsct",
"namespace": "rsct-operator-system"
}
},
"spec": {}
}
]
capabilities: Basic Install
createdAt: "2024-07-10T12:35:54Z"
categories: OpenShift Optional
containerImage: ghcr.io/ocp-power-automation/rsct-operator:latest
createdAt: "2024-10-24T06:29:11Z"
description: Deploys RSCT daemonset on all nodes of an OpenShift cluster
operators.operatorframework.io/builder: operator-sdk-v1.34.1
operators.operatorframework.io/project_layout: go.kubebuilder.io/v4
repository: https://github.com/ocp-power-automation/rsct-operator
support: IBM
name: rsct-operator.v0.0.1
namespace: placeholder
namespace: rsct-operator-system
spec:
apiservicedefinitions: {}
customresourcedefinitions:
Expand All @@ -35,7 +41,7 @@ spec:
kind: RSCT
name: rscts.rsct.ibm.com
version: v1alpha1
description: Deploys RSCT on all nodes of an OpenShift cluster.
description: Deploys custom resource RSCT on all nodes of an OpenShift cluster.
displayName: RSCT Operator for IBM Power Virtual Server
icon:
- base64data: ""
Expand All @@ -44,6 +50,14 @@ spec:
spec:
clusterPermissions:
- rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down Expand Up @@ -173,7 +187,7 @@ spec:
- --leader-elect
command:
- /manager
image: ghcr.io/ocp-power-automation/rsct-operator:latest
image: ghcr.io/ocp-power-automation/rsct-operator:0.0.1
livenessProbe:
httpGet:
path: /healthz
Expand Down Expand Up @@ -255,7 +269,7 @@ spec:
- power
links:
- name: Rsct Operator
url: https://rsct-operator.domain
url: https://github.com/ocp-power-automation/rsct-operator
maintainers:
- email: [email protected]
name: Michael Turek
Expand Down
5 changes: 5 additions & 0 deletions bundle/manifests/rsct.ibm.com_rscts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ spec:
type: object
status:
description: RSCTStatus defines the observed state of RSCT
properties:
state:
description: |-
state reflects current observed state of RSCT resource
type: string
type: object
type: object
served: true
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/rsct.ibm.com_rscts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ spec:
type: object
status:
description: RSCTStatus defines the observed state of RSCT
properties:
state:
description: |-
state reflects current observed state of RSCT resource
type: string
type: object
type: object
served: true
Expand Down
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ kind: Kustomization
images:
- name: controller
newName: ghcr.io/ocp-power-automation/rsct-operator
newTag: latest
newTag: 0.0.1
10 changes: 5 additions & 5 deletions config/manifests/bases/rsct-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ metadata:
annotations:
alm-examples: '[]'
capabilities: Basic Install
support: IBM
categories: "OpenShift Optional"
containerImage: "ghcr.io/ocp-power-automation/rsct-operator:latest"
repository: "https://github.com/ocp-power-automation/rsct-operator"
categories: OpenShift Optional
containerImage: ghcr.io/ocp-power-automation/rsct-operator:latest
description: Deploys RSCT daemonset on all nodes of an OpenShift cluster
repository: https://github.com/ocp-power-automation/rsct-operator
support: IBM
name: rsct-operator.v0.0.0
namespace: rsct-operator-system
spec:
apiservicedefinitions: {}
customresourcedefinitions:
owned:
- description: RSCT custom resource is the schema for the rscts API
- description: RSCT is the Schema for the rscts API
displayName: RSCT
kind: RSCT
name: rscts.rsct.ibm.com
Expand Down
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ kind: ClusterRole
metadata:
name: manager-role
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
1 change: 1 addition & 0 deletions internal/controller/rsct_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type RSCTReconciler struct {
//+kubebuilder:rbac:groups=rsct.ibm.com,resources=rscts/finalizers,verbs=update
//+kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch;

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down
82 changes: 82 additions & 0 deletions internal/controller/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,93 @@ package controller

import (
"context"
"fmt"
"slices"

rsctv1alpha1 "github.com/ocp-power-automation/rsct-operator/api/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type PodStatus string

const (
PENDING PodStatus = "PENDING"
RUNNING PodStatus = "RUNNING"
FAILED PodStatus = "RUNNING"
PARTIALLY_RUNNING PodStatus = "PARTIALLY_RUNNING"
UNKNOWN PodStatus = "UNKNOWN"
)

// matchPodsStatus checks if status of all the pods in slice are same or not
func matchPodsStatus(podsStatus []string, status string) bool {
for _, ps := range podsStatus {
if ps == status {
continue
}
return false
}
return true
}

// evalOperatorStatus determines operator status based on the pods status
func evalOperatorStatus(podList *corev1.PodList) string {
var effectiveStatus string
var podsStatus []string
for _, pod := range podList.Items {
switch {
case pod.Status.Phase == corev1.PodPending:
podsStatus = append(podsStatus, string(PENDING))
case pod.Status.Phase == corev1.PodFailed:
podsStatus = append(podsStatus, string(FAILED))
case pod.Status.Phase == corev1.PodRunning:
podsStatus = append(podsStatus, string(RUNNING))
default:
podsStatus = append(podsStatus, string(UNKNOWN))
}
}

if slices.Contains(podsStatus, string(RUNNING)) {
if slices.Contains(podsStatus, string(FAILED)) || slices.Contains(podsStatus, string(PENDING)) || slices.Contains(podsStatus, string(UNKNOWN)) {
effectiveStatus = string(PARTIALLY_RUNNING)
} else {
effectiveStatus = string(RUNNING)
}
} else if matchPodsStatus(podsStatus, string(FAILED)) {
effectiveStatus = string(FAILED)
} else if matchPodsStatus(podsStatus, string(PENDING)) {
effectiveStatus = string(PENDING)
}
return effectiveStatus
}

// updateRSCTStatus updates RSCT operator status
func (r *RSCTReconciler) updateRSCTStatus(ctx context.Context, rsct *rsctv1alpha1.RSCT, currentDaemonSet *appsv1.DaemonSet) error {
// Operator status:
// 1. PENDING
// 2. RUNNING
// 3. PARTIALLY-RUNNING
// 4. FAILED

pods := &corev1.PodList{}

labelSelector := labels.SelectorFromSet(map[string]string{"app": currentDaemonSet.Name})
listOpts := &client.ListOptions{Namespace: rsct.Namespace, LabelSelector: labelSelector}
listOpts.ApplyOptions([]client.ListOption{})

if err := r.List(ctx, pods, listOpts); err != nil {
return fmt.Errorf("failed to get list of rsct operator pods: %w", err)
}

operatorStatus := evalOperatorStatus(pods)
rsct.Status.State = &operatorStatus

err := r.Status().Update(ctx, rsct)
if err != nil {
return err
}

return nil
}

0 comments on commit 703c7bf

Please sign in to comment.