-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lifecycle-operator): introduce functions for SchedulingGates fun…
…ctionality (#2140) Signed-off-by: odubajDT <[email protected]> Signed-off-by: odubajDT <[email protected]> Co-authored-by: Florian Bacher <[email protected]> Co-authored-by: RealAnna <[email protected]> Co-authored-by: Giovanni Liva <[email protected]>
- Loading branch information
1 parent
a8c170e
commit 5753c46
Showing
15 changed files
with
1,030 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,7 @@ rules: | |
verbs: | ||
- get | ||
- list | ||
- update | ||
- watch | ||
- apiGroups: | ||
- "" | ||
|
115 changes: 115 additions & 0 deletions
115
lifecycle-operator/controllers/common/fake/schedulinggateshandler_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
lifecycle-operator/controllers/common/schedulinggateshandler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" | ||
apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" | ||
controllererrors "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/errors" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
//go:generate moq -pkg fake -skip-ensure -out ./fake/schedulinggateshandler_mock.go . ISchedulingGatesHandler | ||
type ISchedulingGatesHandler interface { | ||
RemoveGates(ctx context.Context, workloadInstance *klcv1alpha3.KeptnWorkloadInstance) error | ||
Enabled() bool | ||
} | ||
|
||
type RemoveGatesFunc func(ctx context.Context, c client.Client, podName string, podNamespace string) error | ||
type GetPodsFunc func(ctx context.Context, c client.Client, ownerUID types.UID, ownerKind string, namespace string) ([]string, error) | ||
|
||
type SchedulingGatesHandler struct { | ||
client.Client | ||
logr.Logger | ||
enabled bool | ||
removeGates RemoveGatesFunc | ||
getPods GetPodsFunc | ||
} | ||
|
||
func NewSchedulingGatesHandler(c client.Client, l logr.Logger, enabled bool) *SchedulingGatesHandler { | ||
return &SchedulingGatesHandler{ | ||
Client: c, | ||
Logger: l, | ||
enabled: enabled, | ||
removeGates: removePodGates, | ||
getPods: getPodsOfOwner, | ||
} | ||
} | ||
|
||
func (h *SchedulingGatesHandler) RemoveGates(ctx context.Context, workloadInstance *klcv1alpha3.KeptnWorkloadInstance) error { | ||
switch workloadInstance.Spec.ResourceReference.Kind { | ||
case "Pod": | ||
return h.removeGates(ctx, h.Client, workloadInstance.Spec.ResourceReference.Name, workloadInstance.Namespace) | ||
case "ReplicaSet", "StatefulSet", "DaemonSet": | ||
podList, err := h.getPods(ctx, h.Client, workloadInstance.Spec.ResourceReference.UID, workloadInstance.Spec.ResourceReference.Kind, workloadInstance.Namespace) | ||
if err != nil { | ||
h.Logger.Error(err, "cannot get pods") | ||
return err | ||
} | ||
for _, pod := range podList { | ||
err := h.removeGates(ctx, h.Client, pod, workloadInstance.Namespace) | ||
if err != nil { | ||
h.Logger.Error(err, "cannot remove gates from pod") | ||
return err | ||
} | ||
} | ||
default: | ||
return controllererrors.ErrUnsupportedWorkloadInstanceResourceReference | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *SchedulingGatesHandler) Enabled() bool { | ||
return h.enabled | ||
} | ||
|
||
func removePodGates(ctx context.Context, c client.Client, podName string, podNamespace string) error { | ||
pod := &v1.Pod{} | ||
err := c.Get(ctx, types.NamespacedName{Namespace: podNamespace, Name: podName}, pod) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if pod.Annotations[apicommon.SchedulingGateRemoved] != "" { | ||
return nil | ||
} | ||
|
||
if len(pod.Annotations) == 0 { | ||
pod.Annotations = make(map[string]string, 1) | ||
} | ||
pod.Annotations[apicommon.SchedulingGateRemoved] = "true" | ||
pod.Spec.SchedulingGates = nil | ||
return c.Update(ctx, pod) | ||
} | ||
|
||
func getPodsOfOwner(ctx context.Context, c client.Client, ownerUID types.UID, ownerKind string, namespace string) ([]string, error) { | ||
pods := &v1.PodList{} | ||
err := c.List(ctx, pods, client.InNamespace(namespace)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var resultPods []string | ||
|
||
for _, pod := range pods.Items { | ||
for _, owner := range pod.OwnerReferences { | ||
if owner.Kind == ownerKind && owner.UID == ownerUID { | ||
resultPods = append(resultPods, pod.Name) | ||
break | ||
} | ||
} | ||
} | ||
|
||
return resultPods, nil | ||
} |
Oops, something went wrong.