From e6127e8b746217fc40672cc42a29e7dc23521c8c Mon Sep 17 00:00:00 2001 From: Gabe Alford Date: Thu, 19 Oct 2023 16:09:35 -0600 Subject: [PATCH] fix: update operator and image version status when changed --- controllers/admission/falconadmission_controller.go | 11 ++++------- controllers/admission/image_push.go | 13 ++++++------- .../falcon_container/falconcontainer_controller.go | 2 +- controllers/falcon_container/image_push.go | 7 +++---- .../falcon_node/falconnodesensor_controller.go | 8 ++++---- internal/controller/common/utils.go | 2 +- pkg/common/funcs.go | 13 +++++++++++++ 7 files changed, 32 insertions(+), 24 deletions(-) diff --git a/controllers/admission/falconadmission_controller.go b/controllers/admission/falconadmission_controller.go index d129ea32..5188bd8a 100644 --- a/controllers/admission/falconadmission_controller.go +++ b/controllers/admission/falconadmission_controller.go @@ -108,7 +108,7 @@ func (r *FalconAdmissionReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, err } - // Let's re-fetch the memcached Custom Resource after update the status + // Let's re-fetch the Custom Resource after update the status // so that we have the latest state of the resource on the cluster and we will avoid // raise the issue "the object has been modified, please apply // your changes to the latest version and try again" which would re-trigger the reconciliation @@ -119,7 +119,7 @@ func (r *FalconAdmissionReconciler) Reconcile(ctx context.Context, req ctrl.Requ } } - if falconAdmission.Status.Version == "" || falconAdmission.Status.Version != version.Get() { + if falconAdmission.Status.Version != version.Get() { falconAdmission.Status.Version = version.Get() err = r.Status().Update(ctx, falconAdmission) @@ -130,7 +130,7 @@ func (r *FalconAdmissionReconciler) Reconcile(ctx context.Context, req ctrl.Requ err = r.Get(ctx, req.NamespacedName, falconAdmission) if err != nil { - log.Error(err, "Failed to re-fetch FalconAdmission for status update 2") + log.Error(err, "Failed to re-fetch FalconAdmission for status update") return ctrl.Result{}, err } } @@ -259,8 +259,7 @@ func (r *FalconAdmissionReconciler) Reconcile(ctx context.Context, req ctrl.Requ Message: "FalconAdmission installation completed", ObservedGeneration: falconAdmission.GetGeneration(), }); err != nil { - log.Error(err, "Failed to update FalconAdmission installation completion condition") - return ctrl.Result{}, err + return ctrl.Result{}, fmt.Errorf("failed to update FalconAdmission installation completion condition: %v", err) } return ctrl.Result{}, nil @@ -470,8 +469,6 @@ func (r *FalconAdmissionReconciler) reconcileAdmissionValidatingWebHook(ctx cont } if !reflect.DeepEqual(webhook.Webhooks[0].NamespaceSelector, existingWebhook.Webhooks[0].NamespaceSelector) { - fmt.Println(existingWebhook.Webhooks[0].NamespaceSelector) - fmt.Println(webhook.Webhooks[0].NamespaceSelector) updated = true } diff --git a/controllers/admission/image_push.go b/controllers/admission/image_push.go index 6e9147ee..f1b3a5aa 100644 --- a/controllers/admission/image_push.go +++ b/controllers/admission/image_push.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "strings" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -104,9 +103,9 @@ func (r *FalconAdmissionReconciler) imageUri(ctx context.Context, falconAdmissio return falconAdmission.Spec.Image, nil } - sidecarImage := os.Getenv("RELATED_IMAGE_ADMISSION_CONTROLLER") - if sidecarImage != "" && falconAdmission.Spec.FalconAPI == nil { - return sidecarImage, nil + admissionImage := os.Getenv("RELATED_IMAGE_ADMISSION_CONTROLLER") + if admissionImage != "" && falconAdmission.Spec.FalconAPI == nil { + return admissionImage, nil } registryUri, err := r.registryUri(ctx, falconAdmission) @@ -140,14 +139,14 @@ func (r *FalconAdmissionReconciler) setImageTag(ctx context.Context, falconAdmis // If an Image URI is set, use it for our version if falconAdmission.Spec.Image != "" { - falconAdmission.Status.Sensor = &strings.Split(falconAdmission.Spec.Image, ":")[1] + falconAdmission.Status.Sensor = common.ImageVersion(falconAdmission.Spec.Image) return *falconAdmission.Status.Sensor, r.Client.Status().Update(ctx, falconAdmission) } if os.Getenv("RELATED_IMAGE_ADMISSION_CONTROLLER") != "" && falconAdmission.Spec.FalconAPI == nil { image := os.Getenv("RELATED_IMAGE_ADMISSION_CONTROLLER") - falconAdmission.Status.Sensor = &strings.Split(image, ":")[1] + falconAdmission.Status.Sensor = common.ImageVersion(image) return *falconAdmission.Status.Sensor, r.Client.Status().Update(ctx, falconAdmission) } @@ -160,7 +159,7 @@ func (r *FalconAdmissionReconciler) setImageTag(ctx context.Context, falconAdmis tag, err := registry.LastContainerTag(ctx, common.SensorTypeKac, falconAdmission.Spec.Version) if err == nil { - falconAdmission.Status.Sensor = &tag + falconAdmission.Status.Sensor = common.ImageVersion(tag) } return tag, err diff --git a/controllers/falcon_container/falconcontainer_controller.go b/controllers/falcon_container/falconcontainer_controller.go index 03e64906..df8f357f 100644 --- a/controllers/falcon_container/falconcontainer_controller.go +++ b/controllers/falcon_container/falconcontainer_controller.go @@ -95,7 +95,7 @@ func (r *FalconContainerReconciler) Reconcile(ctx context.Context, req ctrl.Requ } } - if falconContainer.Status.Version == "" { + if falconContainer.Status.Version != version.Get() { falconContainer.Status.Version = version.Get() err := r.Status().Update(ctx, falconContainer) if err != nil { diff --git a/controllers/falcon_container/image_push.go b/controllers/falcon_container/image_push.go index afb0b247..e226a23e 100644 --- a/controllers/falcon_container/image_push.go +++ b/controllers/falcon_container/image_push.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "strings" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -181,14 +180,14 @@ func (r *FalconContainerReconciler) setImageTag(ctx context.Context, falconConta // If an Image URI is set, use it for our version if falconContainer.Spec.Image != nil && *falconContainer.Spec.Image != "" { - falconContainer.Status.Sensor = &strings.Split(*falconContainer.Spec.Image, ":")[1] + falconContainer.Status.Sensor = common.ImageVersion(*falconContainer.Spec.Image) return *falconContainer.Status.Sensor, r.Client.Status().Update(ctx, falconContainer) } if os.Getenv("RELATED_IMAGE_SIDECAR_SENSOR") != "" && falconContainer.Spec.FalconAPI == nil { image := os.Getenv("RELATED_IMAGE_SIDECAR_SENSOR") - falconContainer.Status.Sensor = &strings.Split(image, ":")[1] + falconContainer.Status.Sensor = common.ImageVersion(image) return *falconContainer.Status.Sensor, r.Client.Status().Update(ctx, falconContainer) } @@ -201,7 +200,7 @@ func (r *FalconContainerReconciler) setImageTag(ctx context.Context, falconConta tag, err := registry.LastContainerTag(ctx, common.SensorTypeSidecar, falconContainer.Spec.Version) if err == nil { - falconContainer.Status.Sensor = &tag + falconContainer.Status.Sensor = common.ImageVersion(tag) } return tag, err diff --git a/controllers/falcon_node/falconnodesensor_controller.go b/controllers/falcon_node/falconnodesensor_controller.go index 5479188b..186abee8 100644 --- a/controllers/falcon_node/falconnodesensor_controller.go +++ b/controllers/falcon_node/falconnodesensor_controller.go @@ -3,7 +3,6 @@ package falcon import ( "context" "reflect" - "strings" falconv1alpha1 "github.com/crowdstrike/falcon-operator/api/falcon/v1alpha1" "github.com/crowdstrike/falcon-operator/internal/controller/assets" @@ -98,7 +97,7 @@ func (r *FalconNodeSensorReconciler) Reconcile(ctx context.Context, req ctrl.Req } } - if nodesensor.Status.Version == "" { + if nodesensor.Status.Version != version.Get() { nodesensor.Status.Version = version.Get() err = r.Status().Update(ctx, nodesensor) if err != nil { @@ -276,8 +275,9 @@ func (r *FalconNodeSensorReconciler) Reconcile(ctx context.Context, req ctrl.Req } } - if nodesensor.Status.Sensor == nil { - nodesensor.Status.Sensor = &strings.Split(image, ":")[1] + imgVer := common.ImageVersion(image) + if nodesensor.Status.Sensor != imgVer { + nodesensor.Status.Sensor = imgVer err = r.Status().Update(ctx, nodesensor) if err != nil { log.Error(err, "Failed to update FalconNodeSensor status for nodesensor.Status.Sensor") diff --git a/internal/controller/common/utils.go b/internal/controller/common/utils.go index bf1a7394..294263bc 100644 --- a/internal/controller/common/utils.go +++ b/internal/controller/common/utils.go @@ -164,7 +164,7 @@ func ConditionsUpdate(r client.Client, ctx context.Context, req ctrl.Request, lo if !meta.IsStatusConditionPresentAndEqual(falconStatus.Conditions, falconCondition.Type, falconCondition.Status) { fgvk := falconObject.GetObjectKind().GroupVersionKind() - // Re-fetch the memcached Custom Resource before update the status + // Re-fetch the Custom Resource before update the status // so that we have the latest state of the resource on the cluster and we will avoid // raise the issue "the object has been modified, please apply // your changes to the latest version and try again" which would re-trigger the reconciliation diff --git a/pkg/common/funcs.go b/pkg/common/funcs.go index 0f8245dc..c7047af3 100644 --- a/pkg/common/funcs.go +++ b/pkg/common/funcs.go @@ -178,3 +178,16 @@ func UpdateEnvVars(envVars []corev1.EnvVar, updateEnvVars []corev1.EnvVar) []cor return envVars } + +func ImageVersion(image string) *string { + switch { + case strings.Contains(image, ":"): + versionTag := strings.Split(image, ":") + return &versionTag[1] + case strings.Contains(image, "@"): + versionDigest := strings.Split(image, "@") + return &versionDigest[1] + default: + return &image + } +}