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 4 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
16 changes: 16 additions & 0 deletions operator/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ metadata:
creationTimestamp: null
name: manager-role
rules:
- apiGroups:
mowies marked this conversation as resolved.
Show resolved Hide resolved
- apps
resources:
- deployments
verbs:
- get
- list
- watch
- apiGroups:
- apps
resources:
Expand All @@ -13,6 +21,14 @@ rules:
- get
- list
- watch
- apiGroups:
- apps
resources:
- statefulsets
verbs:
- get
- list
- watch
- apiGroups:
- batch
resources:
Expand Down
8 changes: 7 additions & 1 deletion operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type KeptnWorkloadInstanceReconciler struct {
//+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=deployments,verbs=get;list;watch
//+kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch
thschue marked this conversation as resolved.
Show resolved Hide resolved

// 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 +194,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

}