diff --git a/operator/config/rbac/role.yaml b/operator/config/rbac/role.yaml index 08dc24d2e9..264f40276a 100644 --- a/operator/config/rbac/role.yaml +++ b/operator/config/rbac/role.yaml @@ -8,7 +8,9 @@ rules: - apiGroups: - apps resources: + - deployments - replicasets + - statefulsets verbs: - get - list diff --git a/operator/controllers/keptnworkloadinstance/controller.go b/operator/controllers/keptnworkloadinstance/controller.go index 8319f3dc67..78df2aee78 100644 --- a/operator/controllers/keptnworkloadinstance/controller.go +++ b/operator/controllers/keptnworkloadinstance/controller.go @@ -65,7 +65,7 @@ type KeptnWorkloadInstanceReconciler struct { //+kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptntasks/finalizers,verbs=update //+kubebuilder:rbac:groups=core,resources=events,verbs=create;watch //+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch -//+kubebuilder:rbac:groups=apps,resources=replicasets,verbs=get;list;watch +//+kubebuilder:rbac:groups=apps,resources=replicasets;deployments;statefulsets,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. @@ -192,7 +192,11 @@ func (r *KeptnWorkloadInstanceReconciler) IsReplicaSetRunning(ctx context.Contex } for _, re := range replica.Items { if re.UID == resource.UID { - if re.Status.ReadyReplicas == *re.Spec.Replicas { + replicas, err := r.GetDesiredReplicas(ctx, re.OwnerReferences[0], namespace) + if err != nil { + return false, err + } + if re.Status.ReadyReplicas == replicas { return true, nil } return false, nil diff --git a/operator/controllers/keptnworkloadinstance/reconcile_postdeployment.go b/operator/controllers/keptnworkloadinstance/reconcile_postdeployment.go index 93cda88e75..5012e3e64e 100644 --- a/operator/controllers/keptnworkloadinstance/reconcile_postdeployment.go +++ b/operator/controllers/keptnworkloadinstance/reconcile_postdeployment.go @@ -2,9 +2,11 @@ package keptnworkloadinstance import ( "context" - klcv1alpha1 "github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1" "github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/common" + appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" ) @@ -24,3 +26,26 @@ func (r *KeptnWorkloadInstanceReconciler) reconcilePostDeployment(ctx context.Co } return nil } + +func (r *KeptnWorkloadInstanceReconciler) GetDesiredReplicas(ctx context.Context, reference v1.OwnerReference, namespace string) (int32, error) { + var replicas *int32 + switch reference.Kind { + case "Deployment": + dep := appsv1.Deployment{} + err := r.Client.Get(ctx, types.NamespacedName{Name: reference.Name, Namespace: namespace}, &dep) + if err != nil { + return 0, err + } + replicas = dep.Spec.Replicas + case "StatefulSet": + sts := appsv1.StatefulSet{} + err := r.Client.Get(ctx, types.NamespacedName{Name: reference.Name, Namespace: namespace}, &sts) + if err != nil { + return 0, err + } + replicas = sts.Spec.Replicas + } + + return *replicas, nil + +}