Skip to content

Commit 90abb94

Browse files
committed
Move kubebuilder annotations to YAML comments
1 parent b1b6652 commit 90abb94

6 files changed

+28
-9
lines changed

config/crd/bases/postgres-operator.crunchydata.com_crunchybridgeclusters.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
---
2+
# controller-gen.kubebuilder.io/version: v0.17.3
23
apiVersion: apiextensions.k8s.io/v1
34
kind: CustomResourceDefinition
45
metadata:
5-
annotations:
6-
controller-gen.kubebuilder.io/version: v0.17.3
76
name: crunchybridgeclusters.postgres-operator.crunchydata.com
87
spec:
98
group: postgres-operator.crunchydata.com

config/crd/bases/postgres-operator.crunchydata.com_pgadmins.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
---
2+
# controller-gen.kubebuilder.io/version: v0.17.3
23
apiVersion: apiextensions.k8s.io/v1
34
kind: CustomResourceDefinition
45
metadata:
5-
annotations:
6-
controller-gen.kubebuilder.io/version: v0.17.3
76
name: pgadmins.postgres-operator.crunchydata.com
87
spec:
98
group: postgres-operator.crunchydata.com

config/crd/bases/postgres-operator.crunchydata.com_pgupgrades.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
---
2+
# controller-gen.kubebuilder.io/version: v0.17.3
23
apiVersion: apiextensions.k8s.io/v1
34
kind: CustomResourceDefinition
45
metadata:
5-
annotations:
6-
controller-gen.kubebuilder.io/version: v0.17.3
76
name: pgupgrades.postgres-operator.crunchydata.com
87
spec:
98
group: postgres-operator.crunchydata.com

config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
---
2+
# controller-gen.kubebuilder.io/version: v0.17.3
23
apiVersion: apiextensions.k8s.io/v1
34
kind: CustomResourceDefinition
45
metadata:
5-
annotations:
6-
controller-gen.kubebuilder.io/version: v0.17.3
76
name: postgresclusters.postgres-operator.crunchydata.com
87
spec:
98
group: postgres-operator.crunchydata.com

internal/crd/post-process.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"log/slog"
1313
"os"
1414
"path/filepath"
15+
"regexp"
1516

1617
"github.com/itchyny/gojq"
1718
"sigs.k8s.io/yaml"
@@ -44,8 +45,12 @@ func main() {
4445
panic(err)
4546
}
4647

48+
// Turn top-level strings that start with octothorpe U+0023 into YAML comments by removing their quotes.
49+
yamlData := need(yaml.Marshal(v))
50+
yamlData = regexp.MustCompile(`(?m)^'(#[^']*)'(.*)$`).ReplaceAll(yamlData, []byte("$1$2"))
51+
4752
slog.Info("Writing", "file", yamlName)
48-
must(os.WriteFile(yamlPath, append([]byte("---\n"), need(yaml.Marshal(v))...), 0o644))
53+
must(os.WriteFile(yamlPath, append([]byte("---\n"), yamlData...), 0o644))
4954
}
5055

5156
if _, ok := result.Next(); ok {

internal/crd/post-process.jq

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,22 @@ reduce paths(try .["x-kubernetes-int-or-string"] == true) as $path (.;
6464
end
6565
) |
6666

67+
# Rename Kubebuilder annotations and move them to the top-level.
68+
# The caller can turn these into YAML comments.
69+
. += (.metadata.annotations | with_entries(select(.key | startswith("controller-gen.kubebuilder")) | .key = "# \(.key)")) |
70+
.metadata.annotations |= with_entries(select(.key | startswith("controller-gen.kubebuilder") | not)) |
71+
72+
# Remove nulls and empty objects from metadata.
73+
# Some very old generators would set a null creationTimestamp.
74+
#
75+
# https://github.com/kubernetes-sigs/controller-tools/issues/402
76+
# https://issue.k8s.io/67610
77+
del(.metadata | .. | select(length == 0)) |
78+
79+
# Remove status to avoid conflicts with the CRD controller.
80+
# Some very old generators would set this field.
81+
#
82+
# https://github.com/kubernetes-sigs/controller-tools/issues/456
83+
del(.status) |
84+
6785
.

0 commit comments

Comments
 (0)