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]>
  • Loading branch information
manju956 committed Oct 14, 2024
1 parent 4bb2d28 commit 606b665
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 3 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
CurrentStatus *string `json:"current_status,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.

13 changes: 11 additions & 2 deletions bundle/manifests/rsct-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ metadata:
},
"name": "rsct",
"namespace": "rsct-operator-system"
}
},
"spec": {}
}
]
capabilities: Basic Install
createdAt: "2024-07-10T12:35:54Z"
createdAt: "2024-10-07T14:37:59Z"
operators.operatorframework.io/builder: operator-sdk-v1.34.1
operators.operatorframework.io/project_layout: go.kubebuilder.io/v4
name: rsct-operator.v0.0.1
Expand All @@ -44,6 +45,14 @@ spec:
spec:
clusterPermissions:
- rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
6 changes: 6 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,12 @@ spec:
type: object
status:
description: RSCTStatus defines the observed state of RSCT
properties:
current_status:
description: |-
INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Important: Run "make" to regenerate code after modifying this file
type: string
type: object
type: object
served: true
Expand Down
6 changes: 6 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,12 @@ spec:
type: object
status:
description: RSCTStatus defines the observed state of RSCT
properties:
current_status:
description: |-
INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Important: Run "make" to regenerate code after modifying this file
type: string
type: object
type: object
served: true
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
2 changes: 2 additions & 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 All @@ -62,6 +63,7 @@ type RSCTReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *RSCTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {

_ = log.FromContext(ctx)
rsct := &rsctv1alpha1.RSCT{}
if err := r.Client.Get(ctx, req.NamespacedName, rsct); err != nil {
Expand Down
73 changes: 73 additions & 0 deletions internal/controller/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,84 @@ 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"
)

// 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, "PENDING")
case pod.Status.Phase == corev1.PodFailed:
podsStatus = append(podsStatus, "FAILED")
case pod.Status.Phase == corev1.PodRunning:
podsStatus = append(podsStatus, "RUNNING")
continue
default:
podsStatus = append(podsStatus, "UNKNOWN")
}
}

if slices.Contains(podsStatus, "RUNNING") {
if slices.Contains(podsStatus, "FAILED") || slices.Contains(podsStatus, "PENDING") || slices.Contains(podsStatus, "UNKNOWN") {
effectiveStatus = "PARTIALLY_RUNNING"
} else {
effectiveStatus = "RUNNING"
}
} else if matchPodsStatus(podsStatus, "FAILED") {
effectiveStatus = "FAILED"
} else if matchPodsStatus(podsStatus, "PENDING") {
effectiveStatus = "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.CurrentStatus = &operatorStatus

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

return nil
}

0 comments on commit 606b665

Please sign in to comment.