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

feat(lifecycle-operator): introduce functions for SchedulingGates functionality #2140

Merged
merged 15 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const K8sRecommendedAppAnnotations = "app.kubernetes.io/part-of"
const K8sRecommendedManagedByAnnotations = "app.kubernetes.io/managed-by"
const PreDeploymentEvaluationAnnotation = "keptn.sh/pre-deployment-evaluations"
const PostDeploymentEvaluationAnnotation = "keptn.sh/post-deployment-evaluations"
const SchedulingGateRemoved = "keptn.sh/scheduling-gate-removed"
const TaskNameAnnotation = "keptn.sh/task-name"
const NamespaceEnabledAnnotation = "keptn.sh/lifecycle-toolkit"
const CreateAppTaskSpanName = "create_%s_app_task"
Expand Down
1 change: 1 addition & 0 deletions lifecycle-operator/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ rules:
verbs:
- get
- list
- update
- watch
- apiGroups:
- ""
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions lifecycle-operator/controllers/common/schedulinggateshandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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
IsSchedullingEnabled() bool
odubajDT marked this conversation as resolved.
Show resolved Hide resolved
}

type RemoveGatesFunc func(ctx context.Context, c client.Client, log logr.Logger, podName string, podNamespace string) error
type GetPodsFunc func(ctx context.Context, c client.Client, log logr.Logger, 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, h.Logger, workloadInstance.Spec.ResourceReference.Name, workloadInstance.Namespace)
case "ReplicaSet", "StatefulSet", "DaemonSet":
podList, err := h.getPods(ctx, h.Client, h.Logger, workloadInstance.Spec.ResourceReference.UID, workloadInstance.Spec.ResourceReference.Kind, workloadInstance.Namespace)
RealAnna marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
h.Logger.Error(err, "cannot get pods")
return err
}
for _, pod := range podList {
err := h.removeGates(ctx, h.Client, h.Logger, 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) IsSchedullingEnabled() bool {
return h.enabled
}

func removePodGates(ctx context.Context, c client.Client, log logr.Logger, podName string, podNamespace string) error {
odubajDT marked this conversation as resolved.
Show resolved Hide resolved
pod := &v1.Pod{}
err := c.Get(ctx, types.NamespacedName{Namespace: podNamespace, Name: podName}, pod)
if err != nil {
log.Error(err, "cannot remove gates from pod")
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
thisthat marked this conversation as resolved.
Show resolved Hide resolved
return c.Update(ctx, pod)
}

func getPodsOfOwner(ctx context.Context, c client.Client, log logr.Logger, ownerUID types.UID, ownerKind string, namespace string) ([]string, error) {
pods := &v1.PodList{}
err := c.List(ctx, pods, client.InNamespace(namespace))
if err != nil {
log.Error(err, "cannot list pods")
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
}
Loading
Loading