Skip to content

Commit 5921994

Browse files
Merge pull request openstack-k8s-operators#246 from stuggi/8063
Add CR name length validation
2 parents f46a2aa + 4f04456 commit 5921994

File tree

10 files changed

+334
-16
lines changed

10 files changed

+334
-16
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ test: manifests generate gowork fmt vet envtest ginkgo ## Run tests.
131131
if [ -f test/functional/suite_test.go ]; then \
132132
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) -v debug --bin-dir $(LOCALBIN) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) --trace --cover --coverprofile cover.out --covermode=atomic --coverpkg=../../pkg/mariadb,../../controllers,../../api/v1beta1 ${PROC_CMD} $(GINKGO_ARGS) ./test/... || exit 1; \
133133
fi; \
134-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -v ./... --cover --coverprofile cover.out --covermode=atomic || exit 1; \
134+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) --bin-dir $(LOCALBIN) use $(ENVTEST_K8S_VERSION) -p path)" go test -v ./... --cover --coverprofile cover.out --covermode=atomic || exit 1; \
135135
popd ; \
136136
done
137137

api/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ require (
66
github.com/go-logr/logr v1.4.2
77
github.com/onsi/ginkgo/v2 v2.19.0
88
github.com/onsi/gomega v1.33.1
9-
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240709153313-544a55696489
9+
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240715111029-358ba8041494
1010
k8s.io/api v0.28.11
1111
k8s.io/apimachinery v0.28.11
1212
k8s.io/client-go v0.28.11
13-
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
13+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
1414
sigs.k8s.io/controller-runtime v0.16.6
1515
)
1616

api/go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
8383
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
8484
github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7 h1:rncLxJBpFGqBztyxCMwNRnMjhhIDOWHJowi6q8G6koI=
8585
github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7/go.mod h1:ctXNyWanKEjGj8sss1KjjHQ3ENKFm33FFnS5BKaIPh4=
86-
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240709153313-544a55696489 h1:4X/xF7z62rCHEui1yUVaE3/zdCleOcHsrsg6++oOrkY=
87-
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240709153313-544a55696489/go.mod h1:k9KuWN2LBtLbKHgcyh/0lrwk3Kr0cOAhiR3hi/mrwOQ=
86+
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240715111029-358ba8041494 h1:JZnRj8RIIsFpsJ5oonvqUEAKNDEkD9C3aeclu1V8JrA=
87+
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240715111029-358ba8041494/go.mod h1:s6ANiH9MoDBntRxnBzMP5fWBq2+NxFSl9CoXilGbLFY=
8888
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
8989
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
9090
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -211,8 +211,8 @@ k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
211211
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
212212
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
213213
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
214-
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 h1:jgGTlFYnhF1PM1Ax/lAlxUPE+KfCIXHaathvJg1C3ak=
215-
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
214+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1J8+AsQnQCKsi8A=
215+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
216216
sigs.k8s.io/controller-runtime v0.16.6 h1:FiXwTuFF5ZJKmozfP2Z0j7dh6kmxP4Ou1KLfxgKKC3I=
217217
sigs.k8s.io/controller-runtime v0.16.6/go.mod h1:+dQzkZxnylD0u49e0a+7AR+vlibEBaThmPca7lTyUsI=
218218
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=

api/v1beta1/galera_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ const (
3131
GaleraContainerImage = "quay.io/podified-antelope-centos9/openstack-mariadb:current-podified"
3232

3333
storageRequestProdMin = "5G"
34+
35+
// CrMaxLengthCorrection - DNS1123LabelMaxLength (63) - CrMaxLengthCorrection used in validation to
36+
// omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
37+
// Int32 is a 10 character + hyphen = 11 + len(-galera) = 17
38+
CrMaxLengthCorrection = 17
3439
)
3540

3641
// AdoptionRedirectSpec defines redirection to a different DB instance during Adoption

api/v1beta1/galera_webhook.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ var _ webhook.Validator = &Galera{}
8181
func (r *Galera) ValidateCreate() (admission.Warnings, error) {
8282
galeralog.Info("validate create", "name", r.Name)
8383

84-
allErrs := field.ErrorList{}
8584
allWarn := []string{}
8685
basePath := field.NewPath("spec")
8786

87+
allErrs := common_webhook.ValidateDNS1123Label(
88+
field.NewPath("metadata").Child("name"),
89+
[]string{r.Name},
90+
CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
91+
8892
warn, err := r.Spec.ValidateCreate(basePath)
8993

9094
if err != nil {

go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ go 1.20
44

55
require (
66
github.com/go-logr/logr v1.4.2
7+
github.com/google/uuid v1.6.0
78
github.com/onsi/ginkgo/v2 v2.19.0
89
github.com/onsi/gomega v1.33.1
9-
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240709153313-544a55696489
10+
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240715111029-358ba8041494
1011
github.com/openstack-k8s-operators/mariadb-operator/api v0.0.0-00010101000000-000000000000
12+
go.uber.org/zap v1.27.0
1113
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3
1214
k8s.io/api v0.28.11
1315
k8s.io/apimachinery v0.28.11
1416
k8s.io/client-go v0.28.11
1517
k8s.io/kubectl v0.28.11
16-
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
18+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
1719
sigs.k8s.io/controller-runtime v0.16.6
1820
)
1921

@@ -36,24 +38,24 @@ require (
3638
github.com/google/go-cmp v0.6.0 // indirect
3739
github.com/google/gofuzz v1.2.0 // indirect
3840
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
39-
github.com/google/uuid v1.6.0 // indirect
4041
github.com/imdario/mergo v0.3.12 // indirect
4142
github.com/josharian/intern v1.0.0 // indirect
4243
github.com/json-iterator/go v1.1.12 // indirect
44+
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 // indirect
4345
github.com/mailru/easyjson v0.7.7 // indirect
4446
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
4547
github.com/moby/spdystream v0.2.0 // indirect
4648
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4749
github.com/modern-go/reflect2 v1.0.2 // indirect
4850
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
51+
github.com/openshift/api v3.9.0+incompatible // indirect
4952
github.com/pkg/errors v0.9.1 // indirect
5053
github.com/prometheus/client_golang v1.16.0 // indirect
5154
github.com/prometheus/client_model v0.4.0 // indirect
5255
github.com/prometheus/common v0.44.0 // indirect
5356
github.com/prometheus/procfs v0.10.1 // indirect
5457
github.com/spf13/pflag v1.0.5 // indirect
5558
go.uber.org/multierr v1.11.0 // indirect
56-
go.uber.org/zap v1.27.0 // indirect
5759
golang.org/x/net v0.25.0 // indirect
5860
golang.org/x/oauth2 v0.8.0 // indirect
5961
golang.org/x/sys v0.20.0 // indirect

go.sum

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
5757
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
5858
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
5959
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
60+
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 h1:VzM3TYHDgqPkettiP6I6q2jOeQFL4nrJM+UcAc4f6Fs=
61+
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0/go.mod h1:nqCI7aelBJU61wiBeeZWJ6oi4bJy5nrjkM6lWIMA4j0=
6062
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
6163
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
6264
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -83,8 +85,10 @@ github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA
8385
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
8486
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
8587
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
86-
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240709153313-544a55696489 h1:4X/xF7z62rCHEui1yUVaE3/zdCleOcHsrsg6++oOrkY=
87-
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240709153313-544a55696489/go.mod h1:k9KuWN2LBtLbKHgcyh/0lrwk3Kr0cOAhiR3hi/mrwOQ=
88+
github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7 h1:rncLxJBpFGqBztyxCMwNRnMjhhIDOWHJowi6q8G6koI=
89+
github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7/go.mod h1:ctXNyWanKEjGj8sss1KjjHQ3ENKFm33FFnS5BKaIPh4=
90+
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240715111029-358ba8041494 h1:JZnRj8RIIsFpsJ5oonvqUEAKNDEkD9C3aeclu1V8JrA=
91+
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240715111029-358ba8041494/go.mod h1:s6ANiH9MoDBntRxnBzMP5fWBq2+NxFSl9CoXilGbLFY=
8892
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
8993
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
9094
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -213,8 +217,8 @@ k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5Ohx
213217
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
214218
k8s.io/kubectl v0.28.11 h1:9kJ715IY/QbnnrZRJVryL3uoVm0bDq88Br5HI10lavM=
215219
k8s.io/kubectl v0.28.11/go.mod h1:yMBK/33GCQftCP5vpJH4CDutPxyRW4cMM8mC0bhGgm8=
216-
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 h1:jgGTlFYnhF1PM1Ax/lAlxUPE+KfCIXHaathvJg1C3ak=
217-
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
220+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1J8+AsQnQCKsi8A=
221+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
218222
sigs.k8s.io/controller-runtime v0.16.6 h1:FiXwTuFF5ZJKmozfP2Z0j7dh6kmxP4Ou1KLfxgKKC3I=
219223
sigs.k8s.io/controller-runtime v0.16.6/go.mod h1:+dQzkZxnylD0u49e0a+7AR+vlibEBaThmPca7lTyUsI=
220224
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=

tests/functional/base_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package functional_test
18+
19+
import (
20+
"time"
21+
22+
. "github.com/onsi/gomega" //revive:disable:dot-imports
23+
24+
"github.com/google/uuid"
25+
26+
"k8s.io/apimachinery/pkg/types"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
29+
mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
30+
)
31+
32+
const (
33+
timeout = 10 * time.Second
34+
interval = timeout / 100
35+
)
36+
37+
func CreateGaleraConfig(namespace string, spec map[string]interface{}) client.Object {
38+
name := uuid.New().String()
39+
40+
raw := map[string]interface{}{
41+
"apiVersion": "mariadb.openstack.org/v1beta1",
42+
"kind": "Galera",
43+
"metadata": map[string]interface{}{
44+
"name": name,
45+
"namespace": namespace,
46+
},
47+
"spec": spec,
48+
}
49+
50+
return th.CreateUnstructured(raw)
51+
}
52+
53+
func GetDefaultGaleraSpec() map[string]interface{} {
54+
return map[string]interface{}{
55+
"replicas": 1,
56+
"logToDisk": false,
57+
"secret": "osp-secret",
58+
"storageClass": "local-storage",
59+
"storageRequest": "500M",
60+
}
61+
}
62+
63+
func GetGalera(name types.NamespacedName) *mariadbv1.Galera {
64+
instance := &mariadbv1.Galera{}
65+
Eventually(func(g Gomega) {
66+
g.Expect(k8sClient.Get(ctx, name, instance)).Should(Succeed())
67+
}, timeout, interval).Should(Succeed())
68+
return instance
69+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package functional_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports
21+
. "github.com/onsi/gomega" //revive:disable:dot-imports
22+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
23+
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"k8s.io/apimachinery/pkg/types"
26+
)
27+
28+
var _ = Describe("Galera webhook", func() {
29+
var galeraName types.NamespacedName
30+
31+
When("a default Galera gets created", func() {
32+
BeforeEach(func() {
33+
galera := CreateGaleraConfig(namespace, GetDefaultGaleraSpec())
34+
galeraName.Name = galera.GetName()
35+
galeraName.Namespace = galera.GetNamespace()
36+
DeferCleanup(th.DeleteInstance, galera)
37+
})
38+
39+
It("should have created a Galera", func() {
40+
Eventually(func(_ Gomega) {
41+
GetGalera(galeraName)
42+
}, timeout, interval).Should(Succeed())
43+
})
44+
})
45+
46+
When("a Galera gets created with a name longer then 46 chars", func() {
47+
It("gets blocked by the webhook and fail", func() {
48+
49+
raw := map[string]interface{}{
50+
"apiVersion": "mariadb.openstack.org/v1beta1",
51+
"kind": "Galera",
52+
"metadata": map[string]interface{}{
53+
"name": "foo-1234567890-1234567890-1234567890-1234567890",
54+
"namespace": namespace,
55+
},
56+
"spec": GetDefaultGaleraSpec(),
57+
}
58+
59+
unstructuredObj := &unstructured.Unstructured{Object: raw}
60+
_, err := controllerutil.CreateOrPatch(
61+
th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil })
62+
Expect(err).To(HaveOccurred())
63+
})
64+
})
65+
})

0 commit comments

Comments
 (0)