Skip to content

Commit 32d6d0a

Browse files
authored
Fix serving CRD at runtime (#3031)
* Fix serving CRD at runtime Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de> * Correctly string quote version enum Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de> --------- Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
1 parent 97115d6 commit 32d6d0a

File tree

5 files changed

+13
-22
lines changed

5 files changed

+13
-22
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ GITSTATUS = $(shell git status --porcelain || echo "no changes")
2121
SOURCES = cmd/main.go
2222
VERSION ?= $(shell git describe --tags --always --dirty)
2323
CRD_SOURCES = $(shell find pkg/apis/zalando.org pkg/apis/acid.zalan.do -name '*.go' -not -name '*.deepcopy.go')
24-
GENERATED_CRDS = manifests/postgresteam.crd.yaml manifests/postgresql.crd.yaml
24+
GENERATED_CRDS = manifests/postgresteam.crd.yaml manifests/postgresql.crd.yaml pkg/apis/acid.zalan.do/v1/postgresql.crd.yaml
2525
GENERATED = pkg/apis/zalando.org/v1/zz_generated.deepcopy.go pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go
2626
DIRS := cmd pkg
2727
PKG := `go list ./... | grep -v /vendor/`
@@ -55,7 +55,7 @@ default: local
5555
clean:
5656
rm -rf build
5757
rm $(GENERATED)
58-
rm pkg/apis/acid.zalan.do/v1/postgresql.crd.yaml
58+
rm $(GENERATED_CRDS)
5959

6060
verify:
6161
hack/verify-codegen.sh

manifests/postgresql.crd.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,11 +3523,11 @@ spec:
35233523
type: object
35243524
version:
35253525
enum:
3526-
- 13
3527-
- 14
3528-
- 15
3529-
- 16
3530-
- 17
3526+
- "13"
3527+
- "14"
3528+
- "15"
3529+
- "16"
3530+
- "17"
35313531
type: string
35323532
required:
35333533
- version

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
_ "embed"
55
"fmt"
66

7-
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
8-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9-
10-
"github.com/stretchr/testify/assert/yaml"
117
acidzalando "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do"
128
"github.com/zalando/postgres-operator/pkg/util"
9+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"sigs.k8s.io/yaml"
1312
)
1413

1514
// CRDResource* define names necesssary for the k8s CRD API

pkg/apis/acid.zalan.do/v1/postgresql_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ type AdditionalVolume struct {
185185

186186
// PostgresqlParam describes PostgreSQL version and pairs of configuration parameter name - values.
187187
type PostgresqlParam struct {
188-
// +kubebuilder:validation:Enum=13;14;15;16;17
188+
// +kubebuilder:validation:Enum="13";"14";"15";"16";"17"
189189
PgVersion string `json:"version"`
190190
Parameters map[string]string `json:"parameters,omitempty"`
191191
}

pkg/controller/util.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package controller
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"strings"
87

98
v1 "k8s.io/api/core/v1"
109
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
"k8s.io/apimachinery/pkg/types"
1311
"k8s.io/apimachinery/pkg/util/wait"
1412

1513
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
@@ -65,15 +63,9 @@ func (c *Controller) createOperatorCRD(desiredCrd *apiextv1.CustomResourceDefini
6563
}
6664
if crd != nil {
6765
c.logger.Infof("customResourceDefinition %q is already registered and will only be updated", crd.Name)
68-
// copy annotations and labels from existing CRD since we do not define them
69-
desiredCrd.Annotations = crd.Annotations
70-
desiredCrd.Labels = crd.Labels
71-
patch, err := json.Marshal(desiredCrd)
66+
crd.Spec = desiredCrd.Spec
67+
_, err := c.KubeClient.CustomResourceDefinitions().Update(context.TODO(), crd, metav1.UpdateOptions{})
7268
if err != nil {
73-
return fmt.Errorf("could not marshal new customResourceDefintion %q: %v", desiredCrd.Name, err)
74-
}
75-
if _, err := c.KubeClient.CustomResourceDefinitions().Patch(
76-
context.TODO(), crd.Name, types.MergePatchType, patch, metav1.PatchOptions{}); err != nil {
7769
return fmt.Errorf("could not update customResourceDefinition %q: %v", crd.Name, err)
7870
}
7971
}

0 commit comments

Comments
 (0)