Skip to content

Commit c5eb253

Browse files
committed
Fix the version checker job update errors out with field is immutable
Currently, In version checker job we reconcile the job and create the job if not present and update the job if there is some change in existing job. However updating the template of the job is not allowed and we get the "Field is immutable" error. I have fixed it by recreating the job instead of updating it.
1 parent 1c13e91 commit c5eb253

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

.bazelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
build --workspace_status_command hack/build/print-workspace-status.sh
2-
test --test_output=errors --test_timeout=-1,-1,-1,2400
2+
test --test_output=errors --test_timeout=-1,-1,-1,3600

hack/bin/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ genrule(
4242
name = "fetch_preflight",
4343
srcs = select({
4444
":k8": ["@preflight_linux//file"],
45+
":m1": ["@preflight_linux//file"],
4546
}),
4647
outs = ["preflight"],
4748
cmd = "cp $(SRCS) $@",

pkg/actor/validate_version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (v *versionChecker) Act(ctx context.Context, cluster *resource.Cluster, log
8686

8787
log.V(DEBUGLEVEL).Info("starting to check the crdb version of the container provided")
8888

89-
r := resource.NewManagedKubeResource(ctx, v.client, cluster, kube.AnnotatingPersister)
89+
r := resource.NewManagedKubeResource(ctx, v.client, cluster, kube.RecreatingPersister)
9090
owner := cluster.Unwrap()
9191

9292
// If the image.name is set use that value and do not check that the

pkg/kube/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
importpath = "github.com/cockroachdb/cockroach-operator/pkg/kube",
1212
visibility = ["//visibility:public"],
1313
deps = [
14+
"//pkg/ptr:go_default_library",
1415
"@com_github_banzaicloud_k8s_objectmatcher//patch:go_default_library",
1516
"@com_github_cenkalti_backoff//:go_default_library",
1617
"@com_github_cockroachdb_errors//:go_default_library",

pkg/kube/helpers.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/banzaicloud/k8s-objectmatcher/patch"
2828
"github.com/cenkalti/backoff"
29+
"github.com/cockroachdb/cockroach-operator/pkg/ptr"
2930
"github.com/cockroachdb/errors"
3031
"github.com/go-logr/logr"
3132
"go.uber.org/zap/zapcore"
@@ -140,6 +141,12 @@ var AnnotatingPersister PersistFn = func(ctx context.Context, cl client.Client,
140141
})
141142
}
142143

144+
var RecreatingPersister PersistFn = func(ctx context.Context, cl client.Client, obj client.Object, f MutateFn) (upserted bool, err error) {
145+
return ReCreateAnnotated(ctx, cl, obj, func() error {
146+
return f()
147+
})
148+
}
149+
143150
// MutateFn is a function which mutates the existing object into it's desired state.
144151
type MutateFn func() error
145152

@@ -190,6 +197,49 @@ func CreateOrUpdateAnnotated(ctx context.Context, c client.Client, obj client.Ob
190197
return true, nil
191198
}
192199

200+
func ReCreateAnnotated(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (upserted bool, err error) {
201+
key := client.ObjectKeyFromObject(obj)
202+
203+
if err := c.Get(ctx, key, obj); err == nil {
204+
existing := obj.DeepCopyObject()
205+
if err := mutate(f, key, obj); err != nil {
206+
return false, err
207+
}
208+
209+
changed, err := ObjectChanged(existing, obj)
210+
if err != nil {
211+
return false, err
212+
}
213+
if !changed {
214+
return false, nil
215+
}
216+
217+
dp := metav1.DeletePropagationForeground
218+
if err := c.Delete(ctx, existing.(client.Object), &client.DeleteOptions{GracePeriodSeconds: ptr.Int64(0),
219+
PropagationPolicy: &dp}); err != nil {
220+
return false, err
221+
}
222+
223+
return false, nil
224+
} else if err != nil && !apierrors.IsNotFound(err) {
225+
return false, err
226+
}
227+
228+
if err := mutate(f, key, obj); err != nil {
229+
return false, err
230+
}
231+
232+
if err := annotator.SetLastAppliedAnnotation(obj); err != nil {
233+
return false, err
234+
}
235+
236+
if err := c.Create(ctx, obj); err != nil {
237+
return false, err
238+
}
239+
240+
return true, nil
241+
}
242+
193243
func ObjectChanged(current, updated runtime.Object) (bool, error) {
194244
opts := []patch.CalculateOption{
195245
patch.IgnoreStatusFields(),

0 commit comments

Comments
 (0)