Skip to content

Commit a721d9e

Browse files
committed
feat: do not allow kic ControlPlane ref type for types unsupported by KIC
1 parent 96347ff commit a721d9e

18 files changed

+193
-113
lines changed

api/configuration/v1alpha1/kongroute_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type KongRoute struct {
5656
}
5757

5858
// KongRouteSpec defines specification of a Kong Route.
59+
// +kubebuilder:validation:XValidation:rule="!has(self.controlPlaneRef) ? true : self.controlPlaneRef.type != 'kic'", message="KongRoute is not supported by kic control plane"
5960
// +apireference:kgo:include
6061
type KongRouteSpec struct {
6162
// ControlPlaneRef is a reference to a ControlPlane this KongRoute is associated with.

api/configuration/v1alpha1/kongservice_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type KongService struct {
5252
}
5353

5454
// KongServiceSpec defines specification of a Kong Route.
55+
// +kubebuilder:validation:XValidation:rule="!has(self.controlPlaneRef) ? true : self.controlPlaneRef.type != 'kic'", message="KongRoute is not supported by kic control plane"
5556
// +apireference:kgo:include
5657
type KongServiceSpec struct {
5758
// ControlPlaneRef is a reference to a ControlPlane this KongService is associated with.

config/crd/gateway-operator/configuration.konghq.com_kongroutes.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ spec:
266266
- message: tags entries must not be longer than 128 characters
267267
rule: self.all(tag, size(tag) >= 1 && size(tag) <= 128)
268268
type: object
269+
x-kubernetes-validations:
270+
- message: KongRoute is not supported by kic control plane
271+
rule: '!has(self.controlPlaneRef) ? true : self.controlPlaneRef.type
272+
!= ''kic'''
269273
status:
270274
default:
271275
conditions:

config/crd/gateway-operator/configuration.konghq.com_kongservices.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ spec:
187187
required:
188188
- host
189189
type: object
190+
x-kubernetes-validations:
191+
- message: KongRoute is not supported by kic control plane
192+
rule: '!has(self.controlPlaneRef) ? true : self.controlPlaneRef.type
193+
!= ''kic'''
190194
status:
191195
default:
192196
conditions:

test/crdsvalidation/kongcacertificate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ func TestKongCACertificate(t *testing.T) {
4545
},
4646
}
4747

48-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
48+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
4949
})
5050
}

test/crdsvalidation/kongcertificate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestKongCertificate(t *testing.T) {
2626
},
2727
}
2828

29-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
29+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
3030
})
3131

3232
t.Run("required fields", func(t *testing.T) {

test/crdsvalidation/kongconsumer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestKongConsumer(t *testing.T) {
2222
Username: "username-1",
2323
}
2424

25-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
25+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, SupportedByKIC).Run(t)
2626
})
2727

2828
t.Run("required fields", func(t *testing.T) {

test/crdsvalidation/kongconsumergroup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestKongConsumerGroup(t *testing.T) {
2525
},
2626
}
2727

28-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
28+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
2929
})
3030

3131
t.Run("cp ref update", func(t *testing.T) {

test/crdsvalidation/kongdataplaneclientcertificate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestKongDataPlaneClientCertificate(t *testing.T) {
2424
},
2525
}
2626

27-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
27+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
2828
})
2929

3030
t.Run("spec", func(t *testing.T) {

test/crdsvalidation/kongkey_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestKongKey(t *testing.T) {
2626
},
2727
}
2828

29-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
29+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
3030
})
3131
t.Run("pem/cp ref", func(t *testing.T) {
3232
obj := &configurationv1alpha1.KongKey{
@@ -46,7 +46,7 @@ func TestKongKey(t *testing.T) {
4646
},
4747
}
4848

49-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
49+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
5050
})
5151

5252
t.Run("spec", func(t *testing.T) {

test/crdsvalidation/kongkeyset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestKongKeySet(t *testing.T) {
2525
},
2626
}
2727

28-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
28+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
2929
})
3030

3131
t.Run("spec", func(t *testing.T) {

test/crdsvalidation/kongpluginbindings_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestKongPluginBindings(t *testing.T) {
3131
},
3232
}
3333

34-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
34+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
3535
})
3636

3737
t.Run("plugin ref", func(t *testing.T) {

test/crdsvalidation/kongroute_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ import (
1313
)
1414

1515
func TestKongRoute(t *testing.T) {
16+
obj := &configurationv1alpha1.KongRoute{
17+
TypeMeta: metav1.TypeMeta{
18+
Kind: "KongRoute",
19+
APIVersion: configurationv1alpha1.GroupVersion.String(),
20+
},
21+
ObjectMeta: commonObjectMeta,
22+
}
23+
1624
t.Run("cp ref", func(t *testing.T) {
17-
obj := &configurationv1alpha1.KongRoute{
18-
TypeMeta: metav1.TypeMeta{
19-
Kind: "KongRoute",
20-
APIVersion: configurationv1alpha1.GroupVersion.String(),
21-
},
22-
ObjectMeta: commonObjectMeta,
23-
}
25+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
26+
})
2427

25-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
28+
t.Run("cp ref - kic", func(t *testing.T) {
29+
NewCRDValidationTestCasesGroupCPRefChangeKICUnsupportedTypes(t, obj).Run(t)
2630
})
2731

2832
t.Run("protocols", func(t *testing.T) {

test/crdsvalidation/kongservice_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ import (
1111
)
1212

1313
func TestKongService(t *testing.T) {
14+
obj := &configurationv1alpha1.KongService{
15+
TypeMeta: metav1.TypeMeta{
16+
Kind: "KongService",
17+
APIVersion: configurationv1alpha1.GroupVersion.String(),
18+
},
19+
ObjectMeta: commonObjectMeta,
20+
}
21+
1422
t.Run("cp ref", func(t *testing.T) {
15-
obj := &configurationv1alpha1.KongService{
16-
TypeMeta: metav1.TypeMeta{
17-
Kind: "KongService",
18-
APIVersion: configurationv1alpha1.GroupVersion.String(),
19-
},
20-
ObjectMeta: commonObjectMeta,
21-
}
23+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
24+
})
2225

23-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
26+
t.Run("cp ref - kic", func(t *testing.T) {
27+
NewCRDValidationTestCasesGroupCPRefChangeKICUnsupportedTypes(t, obj).Run(t)
2428
})
2529

2630
t.Run("tags validation", func(t *testing.T) {

test/crdsvalidation/kongupstream_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestKongUpstream(t *testing.T) {
2222
ObjectMeta: commonObjectMeta,
2323
}
2424

25-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
25+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, NotSupportedByKIC).Run(t)
2626
})
2727

2828
t.Run("required fields", func(t *testing.T) {

test/crdsvalidation/kongvault_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestKongVault(t *testing.T) {
2424
},
2525
}
2626

27-
NewCRDValidationTestCasesGroupCPRefChange(t, obj).Run(t)
27+
NewCRDValidationTestCasesGroupCPRefChange(t, obj, SupportedByKIC).Run(t)
2828
})
2929

3030
t.Run("spec", func(t *testing.T) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package crdsvalidation_test
2+
3+
import (
4+
"testing"
5+
6+
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
7+
"github.com/samber/lo"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
)
11+
12+
func NewCRDValidationTestCasesGroupCPRefChangeKICUnsupportedTypes[
13+
T interface {
14+
client.Object
15+
DeepCopy() T
16+
SetConditions([]metav1.Condition)
17+
SetControlPlaneRef(*configurationv1alpha1.ControlPlaneRef)
18+
GetControlPlaneRef() *configurationv1alpha1.ControlPlaneRef
19+
},
20+
](
21+
t *testing.T,
22+
obj T,
23+
) CRDValidationTestCasesGroup[T] {
24+
ret := CRDValidationTestCasesGroup[T]{}
25+
26+
{
27+
obj := obj.DeepCopy()
28+
obj.SetControlPlaneRef(&configurationv1alpha1.ControlPlaneRef{
29+
Type: configurationv1alpha1.ControlPlaneRefKIC,
30+
})
31+
ret = append(ret, CRDValidationTestCase[T]{
32+
Name: "kic control plane ref is not allowed",
33+
TestObject: obj,
34+
ExpectedErrorMessage: lo.ToPtr("is not supported by kic control plane"),
35+
})
36+
}
37+
38+
return ret
39+
}

0 commit comments

Comments
 (0)