Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(operator): Get desired amount of replicas from upper level resource #89

Merged
merged 5 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions operator/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ rules:
- apiGroups:
mowies marked this conversation as resolved.
Show resolved Hide resolved
- apps
resources:
- deployments
- replicasets
- statefulsets
verbs:
- get
- list
Expand Down
8 changes: 6 additions & 2 deletions operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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

}