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 Nov 6, 2024
1 parent 7320f6c commit 6a5775d
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 11 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.

24 changes: 19 additions & 5 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 @@ -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
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 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 = "FAILED"
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 []PodStatus, status PodStatus) 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 PodStatus
var podsStatus []PodStatus
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)
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 string(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 6a5775d

Please sign in to comment.