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-unnecessary-radix-diployment-calls #1171

Merged
merged 3 commits into from
Aug 20, 2024
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
4 changes: 2 additions & 2 deletions charts/radix-operator/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: radix-operator
version: 1.37.6
appVersion: 1.57.14
version: 1.37.7
appVersion: 1.57.15
kubeVersion: ">=1.24.0"
description: Radix Operator
keywords:
Expand Down
32 changes: 14 additions & 18 deletions pkg/apis/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,16 @@ func (job *Job) setStatusOfJob(ctx context.Context) error {
return err
}
log.Ctx(ctx).Debug().Msgf("Got %d RadixJob steps", len(steps))
environments, err := job.getJobEnvironments(ctx)
jobStatusCondition, err := job.getJobConditionFromJobStatus(ctx, pipelineJob.Status)
if err != nil {
return err
}
jobStatusCondition, err := job.getJobConditionFromJobStatus(ctx, pipelineJob.Status)

environments, err := job.getJobEnvironments(ctx)
if err != nil {
return err
}

err = job.updateRadixJobStatusWithMetrics(ctx, job.radixJob, job.originalRadixJobCondition, func(currStatus *v1.RadixJobStatus) {
if err = job.updateRadixJobStatusWithMetrics(ctx, job.radixJob, job.originalRadixJobCondition, func(currStatus *v1.RadixJobStatus) {
log.Ctx(ctx).Debug().Msgf("Update RadixJob status with %d steps and the condition %s", len(steps), jobStatusCondition)
currStatus.Steps = steps
currStatus.Condition = jobStatusCondition
Expand All @@ -300,13 +300,11 @@ func (job *Job) setStatusOfJob(ctx context.Context) error {
if len(environments) > 0 {
currStatus.TargetEnvs = environments
}
})
if err != nil {
}); err != nil {
return err
}
if isJobConditionDone(jobStatusCondition) {
err = job.setNextJobToRunning(ctx)
if err != nil {
if err = job.setNextJobToRunning(ctx); err != nil {
return err
}
}
Expand Down Expand Up @@ -679,19 +677,17 @@ func getJobStep(podName, containerName string, containerStatus *corev1.Container
}

func (job *Job) getJobEnvironments(ctx context.Context) ([]string, error) {
deploymentsLinkedToJob, err := job.radixclient.RadixV1().RadixDeployments(corev1.NamespaceAll).List(
ctx,
metav1.ListOptions{LabelSelector: job.getRadixJobNameLabelSelector()})
radixDeployments, err := job.kubeutil.GetRadixDeploymentsForApp(ctx, job.radixJob.Spec.AppName, job.getRadixJobNameLabelSelector())
if err != nil {
return nil, err
}

var environments []string
for _, deployment := range deploymentsLinkedToJob.Items {
environments = append(environments, deployment.Spec.Environment)
}

return environments, nil
environmentsMap := slice.Reduce(radixDeployments, make(map[string]struct{}), func(acc map[string]struct{}, rd v1.RadixDeployment) map[string]struct{} {
if rd.GetLabels()[kube.RadixJobNameLabel] == job.radixJob.Name {
acc[rd.Spec.Environment] = struct{}{}
}
return acc
})
return maps.GetKeysFromMap(environmentsMap), nil
}

func (job *Job) updateRadixJobStatusWithMetrics(ctx context.Context, savingRadixJob *v1.RadixJob, originalRadixJobCondition v1.RadixJobCondition, changeStatusFunc func(currStatus *v1.RadixJobStatus)) error {
Expand Down
50 changes: 50 additions & 0 deletions pkg/apis/kube/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"encoding/json"
"fmt"

"github.com/equinor/radix-common/utils/slice"
"github.com/rs/zerolog/log"
corev1 "k8s.io/api/core/v1"
k8errs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kubelabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
)
Expand Down Expand Up @@ -84,3 +88,49 @@ func (kubeutil *Kube) getNamespace(ctx context.Context, name string) (*corev1.Na

return namespace, nil
}

// ListNamespacesWithSelector List namespaces with selector
func (kubeutil *Kube) ListNamespacesWithSelector(ctx context.Context, labelSelectorString string) ([]*corev1.Namespace, error) {
if kubeutil.NamespaceLister != nil {
selector, err := labels.Parse(labelSelectorString)
if err != nil {
return nil, err
}
return kubeutil.NamespaceLister.List(selector)
}

list, err := kubeutil.kubeClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{LabelSelector: labelSelectorString})
if err != nil {
return nil, err
}

return slice.PointersOf(list.Items).([]*corev1.Namespace), nil

}

// GetEnvNamespacesForApp Get all env namespaces for an application
func (kubeutil *Kube) GetEnvNamespacesForApp(ctx context.Context, appName string) ([]*corev1.Namespace, error) {
return kubeutil.ListNamespacesWithSelector(ctx, envNamespacesLabelForApp(appName).String())
}

func envNamespacesLabelForApp(appName string) kubelabels.Selector {
return labels.NewSelector().
Add(*requirementRadixAppNameLabel(appName)).
Add(*requirementNotRadixAppNamespaceLabel())
}

func requirementRadixAppNameLabel(appName string) *labels.Requirement {
requirement, err := kubelabels.NewRequirement(RadixAppLabel, selection.Equals, []string{appName})
if err != nil {
panic(err)
}
return requirement
}

func requirementNotRadixAppNamespaceLabel() *labels.Requirement {
requirement, err := kubelabels.NewRequirement(RadixEnvLabel, selection.NotEquals, []string{"app"})
if err != nil {
panic(err)
}
return requirement
}
18 changes: 18 additions & 0 deletions pkg/apis/kube/radix_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ func (kubeutil *Kube) GetActiveDeployment(ctx context.Context, namespace string)
}
return nil, nil
}

// GetRadixDeploymentsForApp Get all Radix deployments for an application
func (kubeutil *Kube) GetRadixDeploymentsForApp(ctx context.Context, appName string, labelSelector string) ([]v1.RadixDeployment, error) {
envNamespaces, err := kubeutil.GetEnvNamespacesForApp(ctx, appName)
if err != nil {
return nil, err
}
var radixDeployments []v1.RadixDeployment
for _, namespace := range envNamespaces {
radixDeploymentList, err := kubeutil.RadixClient().RadixV1().RadixDeployments(namespace.GetName()).List(ctx,
metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, err
}
radixDeployments = append(radixDeployments, radixDeploymentList.Items...)
}
return radixDeployments, nil
}