Skip to content

Commit 712eec7

Browse files
committed
Fix openstackclient reconile logic
1 parent 78c7700 commit 712eec7

1 file changed

Lines changed: 7 additions & 39 deletions

File tree

controllers/client/openstackclient_controller.go

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ import (
1717
"context"
1818
"errors"
1919
"fmt"
20-
"reflect"
2120
"time"
2221

2322
"github.com/go-logr/logr"
24-
"golang.org/x/exp/slices"
2523

2624
corev1 "k8s.io/api/core/v1"
2725
rbacv1 "k8s.io/api/rbac/v1"
2826
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
2927
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3028
"k8s.io/apimachinery/pkg/fields"
31-
"k8s.io/apimachinery/pkg/runtime/schema"
3229
"k8s.io/apimachinery/pkg/types"
3330

3431
"k8s.io/apimachinery/pkg/runtime"
@@ -308,45 +305,16 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
308305
},
309306
}
310307

308+
spec := openstackclient.ClientPodSpec(ctx, instance, helper, configVarsHash)
309+
311310
op, err := controllerutil.CreateOrPatch(ctx, r.Client, osclient, func() error {
312311
isPodUpdate := !osclient.ObjectMeta.CreationTimestamp.IsZero()
313312
if !isPodUpdate {
314-
osclient.Spec = openstackclient.ClientPodSpec(ctx, instance, helper, configVarsHash)
313+
osclient.Spec = spec
315314
} else {
316-
hashupdate := false
317-
318-
f := func(e corev1.EnvVar) bool {
319-
return e.Name == "CONFIG_HASH"
320-
}
321-
idx := slices.IndexFunc(osclient.Spec.Containers[0].Env, f)
322-
323-
if idx >= 0 && osclient.Spec.Containers[0].Env[idx].Value != configVarsHash {
324-
hashupdate = true
325-
}
326-
327-
switch {
328-
case osclient.Spec.Containers[0].Image != instance.Spec.ContainerImage:
329-
// if container image change force re-create by triggering NewForbidden
330-
return k8s_errors.NewForbidden(
331-
schema.GroupResource{Group: "", Resource: "pods"}, // Specify the group and resource type
332-
osclient.Name,
333-
errors.New("Cannot update Pod spec field - Spec.Containers[0].Image"), // Specify the error message
334-
)
335-
case hashupdate:
336-
// if config hash changed, recreate the pod to use new config
337-
return k8s_errors.NewForbidden(
338-
schema.GroupResource{Group: "", Resource: "pods"}, // Specify the group and resource type
339-
osclient.Name,
340-
errors.New("Config changed recreate pod"), // Specify the error message
341-
)
342-
case !reflect.DeepEqual(osclient.Spec.NodeSelector, instance.Spec.NodeSelector):
343-
// if NodeSelector change force re-create by triggering NewForbidden
344-
return k8s_errors.NewForbidden(
345-
schema.GroupResource{Group: "", Resource: "pods"}, // Specify the group and resource type
346-
osclient.Name,
347-
errors.New("Cannot update Pod spec field - Spec.NodeSelector"), // Specify the error message
348-
)
349-
}
315+
osclient.Spec.Containers[0].Env = spec.Containers[0].Env
316+
osclient.Spec.NodeSelector = spec.NodeSelector
317+
osclient.Spec.Containers[0].Image = instance.Spec.ContainerImage
350318
}
351319

352320
osclient.Labels = util.MergeStringMaps(osclient.Labels, clientLabels)
@@ -376,7 +344,7 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
376344
}
377345
Log.Info(fmt.Sprintf("OpenStackClient pod deleted due to change %s", err.Error()))
378346

379-
return ctrl.Result{}, nil
347+
return ctrl.Result{Requeue: true}, nil
380348
}
381349

382350
return ctrl.Result{}, fmt.Errorf("Failed to create or update pod %s: %w", osclient.Name, err)

0 commit comments

Comments
 (0)