Skip to content

Commit

Permalink
fix: update operator and image version status when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
redhatrises committed Oct 19, 2023
1 parent c9f180d commit e6127e8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
11 changes: 4 additions & 7 deletions controllers/admission/falconadmission_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
13 changes: 6 additions & 7 deletions controllers/admission/image_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion controllers/falcon_container/falconcontainer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions controllers/falcon_container/image_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions controllers/falcon_node/falconnodesensor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions pkg/common/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit e6127e8

Please sign in to comment.