Skip to content

Commit 2397c75

Browse files
authored
[Bugfix] Fix Schema Apply Checksum (#1652)
1 parent bb45038 commit 2397c75

25 files changed

+284
-340
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- (Maintenance) Bump Prometheus API Version
55
- (Bugfix) Prevent unexpected rotation in case of SecurityContext change
66
- (Bugfix) Ensure PDB is created
7+
- (Bugfix) Fix Schema Apply Checksum
78

89
## [1.2.40](https://github.com/arangodb/kube-arangodb/tree/1.2.40) (2024-04-10)
910
- (Feature) Add Core fields to the Scheduler Container Spec

cmd/cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import (
6767
"github.com/arangodb/kube-arangodb/pkg/util/metrics"
6868
"github.com/arangodb/kube-arangodb/pkg/util/probe"
6969
"github.com/arangodb/kube-arangodb/pkg/util/retry"
70+
"github.com/arangodb/kube-arangodb/pkg/util/shutdown"
7071
"github.com/arangodb/kube-arangodb/pkg/version"
7172
)
7273

@@ -378,7 +379,7 @@ func executeMain(cmd *cobra.Command, args []string) {
378379
}
379380

380381
if crdOptions.install {
381-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
382+
ctx, cancel := context.WithTimeout(shutdown.Context(), time.Minute)
382383
defer cancel()
383384

384385
crdOpts, err := prepareCRDOptions(crdOptions.validationSchema)

cmd/crd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/arangodb/kube-arangodb/pkg/crd/crds"
3535
"github.com/arangodb/kube-arangodb/pkg/util/errors"
3636
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
37+
"github.com/arangodb/kube-arangodb/pkg/util/shutdown"
3738
)
3839

3940
var (
@@ -108,7 +109,7 @@ func cmdCRDInstallRun(cmd *cobra.Command, args []string) {
108109
return
109110
}
110111

111-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
112+
ctx, cancel := context.WithTimeout(shutdown.Context(), time.Minute)
112113
defer cancel()
113114

114115
err = crd.EnsureCRDWithOptions(ctx, client, crd.EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts, ForceUpdate: crdInstallOptions.force})

pkg/crd/apply.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import (
2828
"k8s.io/apimachinery/pkg/api/errors"
2929
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
3030

31-
"github.com/arangodb/go-driver"
32-
3331
"github.com/arangodb/kube-arangodb/pkg/crd/crds"
3432
"github.com/arangodb/kube-arangodb/pkg/logging"
3533
kresources "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources"
@@ -65,25 +63,33 @@ func EnsureCRDWithOptions(ctx context.Context, client kclient.Client, opts Ensur
6563
continue
6664
}
6765

68-
var opt *crds.CRDOptions
66+
var opt = &crdReg.defaultOpts
6967
if o, ok := opts.CRDOptions[crdName]; ok {
7068
opt = &o
7169
}
7270
def := crdReg.getter(opt)
7371

74-
err := tryApplyCRD(ctx, client, def, opts.ForceUpdate)
72+
err := tryApplyCRD(ctx, client, def, opt, opts.ForceUpdate)
7573
if !opts.IgnoreErrors && err != nil {
7674
return err
7775
}
7876
}
7977
return nil
8078
}
8179

82-
func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition, forceUpdate bool) error {
80+
func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition, opts *crds.CRDOptions, forceUpdate bool) error {
8381
crdDefinitions := client.KubernetesExtensions().ApiextensionsV1().CustomResourceDefinitions()
8482

8583
crdName := def.CRD.Name
8684

85+
definitionVersion, definitionSchemaVersion := def.DefinitionData.Checksum()
86+
87+
logger := logger.Str("version", definitionVersion)
88+
89+
if opts.GetWithSchema() {
90+
logger = logger.Str("schema", definitionSchemaVersion)
91+
}
92+
8793
c, err := crdDefinitions.Get(ctx, crdName, meta.GetOptions{})
8894
if err != nil {
8995
if !errors.IsNotFound(err) {
@@ -102,12 +108,16 @@ func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition
102108
ObjectMeta: meta.ObjectMeta{
103109
Name: crdName,
104110
Labels: map[string]string{
105-
Version: string(def.Version),
111+
Version: definitionVersion,
106112
},
107113
},
108114
Spec: def.CRD.Spec,
109115
}
110116

117+
if opts.GetWithSchema() {
118+
c.Labels[Schema] = definitionSchemaVersion
119+
}
120+
111121
if _, err := crdDefinitions.Create(ctx, c, meta.CreateOptions{}); err != nil {
112122
logger.Err(err).Str("crd", crdName).Warn("Create Operations is not allowed due to error")
113123
return err
@@ -127,14 +137,20 @@ func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition
127137
c.ObjectMeta.Labels = map[string]string{}
128138
}
129139

130-
if v, ok := c.ObjectMeta.Labels[Version]; ok && v != "" {
131-
if !forceUpdate && !isUpdateRequired(def.Version, driver.Version(v)) {
132-
logger.Str("crd", crdName).Info("CRD Update not required")
133-
return nil
140+
if !forceUpdate {
141+
if v, ok := c.ObjectMeta.Labels[Version]; ok && v == definitionVersion {
142+
if v, ok := c.ObjectMeta.Labels[Schema]; (opts.GetWithSchema() && (ok && v == definitionSchemaVersion)) || (!opts.GetWithSchema() && !ok) {
143+
logger.Str("crd", crdName).Info("CRD Update not required")
144+
return nil
145+
}
134146
}
135147
}
136148

137-
c.ObjectMeta.Labels[Version] = string(def.Version)
149+
c.ObjectMeta.Labels[Version] = definitionVersion
150+
delete(c.ObjectMeta.Labels, Schema)
151+
if opts.GetWithSchema() {
152+
c.ObjectMeta.Labels[Schema] = definitionSchemaVersion
153+
}
138154
c.Spec = def.CRD.Spec
139155

140156
if _, err := crdDefinitions.Update(ctx, c, meta.UpdateOptions{}); err != nil {

pkg/crd/apply_test.go

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ package crd
2222

2323
import (
2424
"context"
25+
"encoding/json"
2526
"testing"
2627
"time"
2728

@@ -51,6 +52,11 @@ func dropLogMessages(t *testing.T, s tests.LogScanner) map[string]string {
5152
lines[p] = m
5253
}
5354

55+
d, err := json.Marshal(lines)
56+
require.NoError(t, err)
57+
58+
t.Logf("Lines: %s", string(d))
59+
5460
return lines
5561
}
5662

@@ -133,27 +139,88 @@ func runApply(t *testing.T, crdOpts map[string]crds.CRDOptions) {
133139

134140
t.Run("Create", func(t *testing.T) {
135141
d := crds.AllDefinitions()[0]
142+
136143
q := d.CRD.DeepCopy()
137144
q.Labels = map[string]string{
138-
Version: string(d.Version),
145+
Version: "version",
139146
}
140147
_, err := c.KubernetesExtensions().ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), q, meta.CreateOptions{})
141148
require.NoError(t, err)
142149
})
143150

144-
t.Run("Ensure", func(t *testing.T) {
151+
t.Run("Ensure without schema", func(t *testing.T) {
152+
crdOpts = updateMap(t, crdOpts, func(t *testing.T, key string, el crds.CRDOptions) crds.CRDOptions {
153+
el.WithSchema = false
154+
return el
155+
})
156+
145157
require.NoError(t, EnsureCRDWithOptions(context.Background(), c, EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts}))
146158

147159
for k, v := range dropLogMessages(t, s) {
148160
t.Run(k, func(t *testing.T) {
149161
if k == crds.AllDefinitions()[0].CRD.GetName() {
150-
require.Equal(t, "CRD Update not required", v)
162+
require.Equal(t, "CRD Updated", v)
151163
} else {
152164
require.Equal(t, "CRD Created", v)
153165
}
154166
})
155167
}
156168
})
169+
170+
t.Run("Rerun without schema", func(t *testing.T) {
171+
crdOpts = updateMap(t, crdOpts, func(t *testing.T, key string, el crds.CRDOptions) crds.CRDOptions {
172+
el.WithSchema = false
173+
return el
174+
})
175+
176+
require.NoError(t, EnsureCRDWithOptions(context.Background(), c, EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts}))
177+
178+
for k, v := range dropLogMessages(t, s) {
179+
t.Run(k, func(t *testing.T) {
180+
require.Equal(t, "CRD Update not required", v)
181+
})
182+
}
183+
})
184+
185+
t.Run("Ensure with schema", func(t *testing.T) {
186+
crdOpts = updateMap(t, crdOpts, func(t *testing.T, key string, el crds.CRDOptions) crds.CRDOptions {
187+
el.WithSchema = true
188+
return el
189+
})
190+
191+
require.NoError(t, EnsureCRDWithOptions(context.Background(), c, EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts}))
192+
193+
for k, v := range dropLogMessages(t, s) {
194+
t.Run(k, func(t *testing.T) {
195+
require.Equal(t, "CRD Updated", v)
196+
})
197+
}
198+
})
199+
200+
t.Run("Rerun with schema", func(t *testing.T) {
201+
crdOpts = updateMap(t, crdOpts, func(t *testing.T, key string, el crds.CRDOptions) crds.CRDOptions {
202+
el.WithSchema = true
203+
return el
204+
})
205+
206+
require.NoError(t, EnsureCRDWithOptions(context.Background(), c, EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts}))
207+
208+
for k, v := range dropLogMessages(t, s) {
209+
t.Run(k, func(t *testing.T) {
210+
require.Equal(t, "CRD Update not required", v)
211+
})
212+
}
213+
})
157214
})
158215
})
159216
}
217+
218+
func updateMap[T comparable](t *testing.T, in map[string]T, f func(t *testing.T, key string, el T) T) map[string]T {
219+
r := make(map[string]T, len(in))
220+
221+
for k, v := range in {
222+
r[k] = f(t, k, v)
223+
}
224+
225+
return r
226+
}

pkg/crd/crds/apps-job.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ import (
2424
_ "embed"
2525

2626
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
27-
28-
"github.com/arangodb/go-driver"
29-
30-
"github.com/arangodb/kube-arangodb/pkg/util"
31-
)
32-
33-
const (
34-
AppsJobVersion = driver.Version("1.0.1")
3527
)
3628

3729
// Deprecated: use AppsJobWithOptions instead
@@ -40,7 +32,7 @@ func AppsJob() *apiextensions.CustomResourceDefinition {
4032
}
4133

4234
func AppsJobWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
43-
return getCRD(appsJobsCRD, appsJobsCRDSchemas, opts...)
35+
return getCRD(AppsJobDefinitionData(), opts...)
4436
}
4537

4638
// Deprecated: use AppsJobDefinitionWithOptions instead
@@ -50,13 +42,17 @@ func AppsJobDefinition() Definition {
5042

5143
func AppsJobDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
5244
return Definition{
53-
Version: AppsJobVersion,
54-
CRD: AppsJobWithOptions(opts...),
45+
DefinitionData: AppsJobDefinitionData(),
46+
CRD: AppsJobWithOptions(opts...),
5547
}
5648
}
5749

58-
var appsJobsCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](appsJobs)
59-
var appsJobsCRDSchemas = util.NewYamlLoader[crdSchemas](appsJobsSchemaRaw)
50+
func AppsJobDefinitionData() DefinitionData {
51+
return DefinitionData{
52+
definition: appsJobs,
53+
schemaDefinition: appsJobsSchemaRaw,
54+
}
55+
}
6056

6157
//go:embed apps-job.yaml
6258
var appsJobs []byte

pkg/crd/crds/backups-backup.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ import (
2424
_ "embed"
2525

2626
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
27-
28-
"github.com/arangodb/go-driver"
29-
30-
"github.com/arangodb/kube-arangodb/pkg/util"
31-
)
32-
33-
const (
34-
BackupsBackupVersion = driver.Version("1.0.1")
3527
)
3628

3729
// Deprecated: use BackupsBackupWithOptions instead
@@ -40,7 +32,7 @@ func BackupsBackup() *apiextensions.CustomResourceDefinition {
4032
}
4133

4234
func BackupsBackupWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
43-
return getCRD(backupsBackupCRD, backupsBackupCRDSchemas, opts...)
35+
return getCRD(BackupsBackupDefinitionData(), opts...)
4436
}
4537

4638
// Deprecated: use BackupsBackupDefinitionWithOptions instead
@@ -50,13 +42,17 @@ func BackupsBackupDefinition() Definition {
5042

5143
func BackupsBackupDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
5244
return Definition{
53-
Version: BackupsBackupVersion,
54-
CRD: BackupsBackupWithOptions(opts...),
45+
DefinitionData: BackupsBackupDefinitionData(),
46+
CRD: BackupsBackupWithOptions(opts...),
5547
}
5648
}
5749

58-
var backupsBackupCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](backupsBackup)
59-
var backupsBackupCRDSchemas = util.NewYamlLoader[crdSchemas](backupsBackupSchemaRaw)
50+
func BackupsBackupDefinitionData() DefinitionData {
51+
return DefinitionData{
52+
definition: backupsBackup,
53+
schemaDefinition: backupsBackupSchemaRaw,
54+
}
55+
}
6056

6157
//go:embed backups-backup.yaml
6258
var backupsBackup []byte

pkg/crd/crds/backups-backuppolicy.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ import (
2424
_ "embed"
2525

2626
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
27-
28-
"github.com/arangodb/go-driver"
29-
30-
"github.com/arangodb/kube-arangodb/pkg/util"
31-
)
32-
33-
const (
34-
BackupsBackupPolicyPolicyVersion = driver.Version("1.0.1")
3527
)
3628

3729
// Deprecated: use BackupsBackupPolicyPolicyWithOptions instead
@@ -40,7 +32,7 @@ func BackupsBackupPolicyPolicy() *apiextensions.CustomResourceDefinition {
4032
}
4133

4234
func BackupsBackupPolicyPolicyWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
43-
return getCRD(backupsBackupPolicyCRD, backupsBackupPolicyCRDSchemas, opts...)
35+
return getCRD(BackupsBackupPolicyDefinitionData(), opts...)
4436
}
4537

4638
// Deprecated: use func BackupsBackupPolicyDefinitionWithOptions instead
@@ -50,13 +42,17 @@ func BackupsBackupPolicyDefinition() Definition {
5042

5143
func BackupsBackupPolicyDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
5244
return Definition{
53-
Version: BackupsBackupPolicyPolicyVersion,
54-
CRD: BackupsBackupPolicyPolicyWithOptions(opts...),
45+
DefinitionData: BackupsBackupPolicyDefinitionData(),
46+
CRD: BackupsBackupPolicyPolicyWithOptions(opts...),
5547
}
5648
}
5749

58-
var backupsBackupPolicyCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](backupsBackupPolicy)
59-
var backupsBackupPolicyCRDSchemas = util.NewYamlLoader[crdSchemas](backupsBackupPolicySchemaRaw)
50+
func BackupsBackupPolicyDefinitionData() DefinitionData {
51+
return DefinitionData{
52+
definition: backupsBackupPolicy,
53+
schemaDefinition: backupsBackupPolicySchemaRaw,
54+
}
55+
}
6056

6157
//go:embed backups-backuppolicy.yaml
6258
var backupsBackupPolicy []byte

0 commit comments

Comments
 (0)