Skip to content

Commit 5f9cf76

Browse files
committed
add adopt types and adopt option in KongService
1 parent 8d39ac7 commit 5f9cf76

File tree

9 files changed

+275
-0
lines changed

9 files changed

+275
-0
lines changed

api/common/v1alpha1/adopt_types.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package v1alpha1
2+
3+
// AdoptOptions is the options for CRDs to attach to an existing Kong entity.
4+
// +kubebuilder:object:generate=true
5+
// +kubebuilder:validation:XValidation:rule="self.from == oldSelf.from",message="'from'(adopt source) is immutable"
6+
// +kubebuilder:validation:XValidation:rule="self.from == 'konnect' ? has(self.konnect) : true",message="Must specify Konnect options when from='konnect'"
7+
// +kubebuilder:validation:XValidation:rule="has(self.konnect) ? (self.konnect.id == oldSelf.konnect.id) : true",message="konnect.id is immutable"
8+
// +apireference:kgo:include
9+
type AdoptOptions struct {
10+
// From is the source of the entity to adopt from.
11+
// Now 'konnect' is supported.
12+
// +required
13+
// +kubebuilder:validation:Enum=konnect
14+
From AdoptSource `json:"from"`
15+
// Konnect is the options for adopting the entity from Konnect.
16+
// Required when from == 'konnect'.
17+
// +optional
18+
Konnect *AdoptKonnectOptions `json:"konnect,omitempty"`
19+
}
20+
21+
// AdoptSource is the type to define the source of the entity to adopt from.
22+
type AdoptSource string
23+
24+
const (
25+
// AdoptSourceKonnect indicates that the entity is adopted from Konnect.
26+
AdoptSourceKonnect AdoptSource = "konnect"
27+
)
28+
29+
// AdoptKonnectOptions specifies the options for adopting the entity from Konnect.
30+
// +kubebuilder:object:generate=true
31+
// +apireference:kgo:include
32+
type AdoptKonnectOptions struct {
33+
// ID is the Konnect ID of the entity.
34+
// +required
35+
ID string `json:"id"`
36+
}

api/common/v1alpha1/zz_generated.deepcopy.go

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/configuration/v1alpha1/kongservice_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,19 @@ type KongService struct {
5454

5555
// KongServiceSpec defines specification of a Kong Service.
5656
// +kubebuilder:validation:XValidation:rule="!has(self.controlPlaneRef) ? true : self.controlPlaneRef.type != 'kic'", message="KIC is not supported as control plane"
57+
// +kubebuilder:validation:XValidation:rule="!has(self.adopt) ? true : (has(self.controlPlaneRef) && self.controlPlaneRef.type == 'konnectNamespacedRef')", message="spec.adopt is allowed only when controlPlaneRef is konnectNamespacedRef"
58+
// +kubebuilder:validation:XValidation:rule="(has(oldSelf.adopt) && has(self.adopt)) || (!has(oldSelf.adopt) && !has(self.adopt))", message="Cannot set or unset spec.adopt in updates"
5759
// +apireference:kgo:include
5860
type KongServiceSpec struct {
5961
// ControlPlaneRef is a reference to a ControlPlane this KongService is associated with.
6062
// +kubebuilder:validation:XValidation:message="'konnectID' type is not supported", rule="self.type != 'konnectID'"
6163
// +required
6264
ControlPlaneRef *commonv1alpha1.ControlPlaneRef `json:"controlPlaneRef"`
6365

66+
// Adopt is the options for adopting a service from an existing service in Konnect.
67+
// +optional
68+
Adopt *commonv1alpha1.AdoptOptions `json:"adopt,omitempty"`
69+
6470
KongServiceAPISpec `json:",inline"`
6571
}
6672

api/configuration/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,39 @@ spec:
5656
spec:
5757
description: KongServiceSpec defines specification of a Kong Service.
5858
properties:
59+
adopt:
60+
description: Adopt is the options for adopting a service from an existing
61+
service in Konnect.
62+
properties:
63+
from:
64+
description: |-
65+
From is the source of the entity to adopt from.
66+
Now 'konnect' is supported.
67+
enum:
68+
- konnect
69+
type: string
70+
konnect:
71+
description: |-
72+
Konnect is the options for adopting the entity from Konnect.
73+
Required when from == 'konnect'.
74+
properties:
75+
id:
76+
description: ID is the Konnect ID of the entity.
77+
type: string
78+
required:
79+
- id
80+
type: object
81+
required:
82+
- from
83+
type: object
84+
x-kubernetes-validations:
85+
- message: '''from''(adopt source) is immutable'
86+
rule: self.from == oldSelf.from
87+
- message: Must specify Konnect options when from='konnect'
88+
rule: 'self.from == ''konnect'' ? has(self.konnect) : true'
89+
- message: konnect.id is immutable
90+
rule: 'has(self.konnect) ? (self.konnect.id == oldSelf.konnect.id)
91+
: true'
5992
connect_timeout:
6093
description: The timeout in milliseconds for establishing a connection
6194
to the upstream server.
@@ -198,6 +231,12 @@ spec:
198231
- message: KIC is not supported as control plane
199232
rule: '!has(self.controlPlaneRef) ? true : self.controlPlaneRef.type
200233
!= ''kic'''
234+
- message: spec.adopt is allowed only when controlPlaneRef is konnectNamespacedRef
235+
rule: '!has(self.adopt) ? true : (has(self.controlPlaneRef) && self.controlPlaneRef.type
236+
== ''konnectNamespacedRef'')'
237+
- message: Cannot set or unset spec.adopt in updates
238+
rule: (has(oldSelf.adopt) && has(self.adopt)) || (!has(oldSelf.adopt)
239+
&& !has(self.adopt))
201240
status:
202241
default:
203242
conditions:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
kind: KonnectAPIAuthConfiguration
2+
apiVersion: konnect.konghq.com/v1alpha1
3+
metadata:
4+
name: konnect-api-auth-1
5+
namespace: default
6+
spec:
7+
type: token
8+
token: kpat_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
9+
serverURL: eu.api.konghq.com
10+
---
11+
kind: KonnectGatewayControlPlane
12+
apiVersion: konnect.konghq.com/v1alpha1
13+
metadata:
14+
name: test1
15+
namespace: default
16+
spec:
17+
name: test1
18+
labels:
19+
app: test1
20+
key1: test1
21+
konnect:
22+
authRef:
23+
name: konnect-api-auth-1
24+
---
25+
kind: KongService
26+
apiVersion: configuration.konghq.com/v1alpha1
27+
metadata:
28+
name: service-adopted-1
29+
namespace: default
30+
spec:
31+
adopt:
32+
from: konnect
33+
konnect:
34+
id: "aaaabbbb-cccc-dddd-eeee-000011112222"
35+
name: service-1
36+
host: example.com
37+
controlPlaneRef:
38+
type: konnectNamespacedRef
39+
konnectNamespacedRef:
40+
name: test1

docs/all-api-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ KongServiceSpec defines specification of a Kong Service.
12841284
| Field | Description |
12851285
| --- | --- |
12861286
| `controlPlaneRef` _[ControlPlaneRef](#controlplaneref)_ | ControlPlaneRef is a reference to a ControlPlane this KongService is associated with. |
1287+
| `adopt` _[AdoptOptions](#adoptoptions)_ | Adopt is the options for adopting a service from an existing service in Konnect. |
12871288
| `url` _string_ | Helper field to set `protocol`, `host`, `port` and `path` using a URL. This field is write-only and is not returned in responses. |
12881289
| `connect_timeout` _integer_ | The timeout in milliseconds for establishing a connection to the upstream server. |
12891290
| `enabled` _boolean_ | Whether the Service is active. If set to `false`, the proxy behavior will be as if any routes attached to it do not exist (404). Default: `true`. |

docs/configuration-api-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,7 @@ KongServiceSpec defines specification of a Kong Service.
12781278
| Field | Description |
12791279
| --- | --- |
12801280
| `controlPlaneRef` _[ControlPlaneRef](#controlplaneref)_ | ControlPlaneRef is a reference to a ControlPlane this KongService is associated with. |
1281+
| `adopt` _[AdoptOptions](#adoptoptions)_ | Adopt is the options for adopting a service from an existing service in Konnect. |
12811282
| `url` _string_ | Helper field to set `protocol`, `host`, `port` and `path` using a URL. This field is write-only and is not returned in responses. |
12821283
| `connect_timeout` _integer_ | The timeout in milliseconds for establishing a connection to the upstream server. |
12831284
| `enabled` _boolean_ | Whether the Service is active. If set to `false`, the proxy behavior will be as if any routes attached to it do not exist (404). Default: `true`. |

test/crdsvalidation/configuration.konghq.com/kongservice_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,116 @@ func TestKongService(t *testing.T) {
100100
},
101101
}.Run(t)
102102
})
103+
104+
t.Run("adopt options testing", func(t *testing.T) {
105+
common.TestCasesGroup[*configurationv1alpha1.KongService]{
106+
{
107+
Name: "valid adopt options",
108+
TestObject: &configurationv1alpha1.KongService{
109+
ObjectMeta: common.CommonObjectMeta,
110+
Spec: configurationv1alpha1.KongServiceSpec{
111+
ControlPlaneRef: &commonv1alpha1.ControlPlaneRef{
112+
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
113+
KonnectNamespacedRef: &commonv1alpha1.KonnectNamespacedRef{
114+
Name: "test-konnect-control-plane",
115+
},
116+
},
117+
Adopt: &commonv1alpha1.AdoptOptions{
118+
From: commonv1alpha1.AdoptSourceKonnect,
119+
Konnect: &commonv1alpha1.AdoptKonnectOptions{
120+
ID: "test-konnect-id",
121+
},
122+
},
123+
},
124+
},
125+
},
126+
{
127+
Name: "invalid adopt.from",
128+
TestObject: &configurationv1alpha1.KongService{
129+
ObjectMeta: common.CommonObjectMeta,
130+
Spec: configurationv1alpha1.KongServiceSpec{
131+
ControlPlaneRef: &commonv1alpha1.ControlPlaneRef{
132+
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
133+
KonnectNamespacedRef: &commonv1alpha1.KonnectNamespacedRef{
134+
Name: "test-konnect-control-plane",
135+
},
136+
},
137+
Adopt: &commonv1alpha1.AdoptOptions{
138+
From: "invalid-adopt-from",
139+
Konnect: &commonv1alpha1.AdoptKonnectOptions{
140+
ID: "test-konnect-id",
141+
},
142+
},
143+
},
144+
},
145+
ExpectedErrorMessage: lo.ToPtr("spec.adopt.from: Unsupported value"),
146+
},
147+
{
148+
Name: "missing adopt.konnect",
149+
TestObject: &configurationv1alpha1.KongService{
150+
ObjectMeta: common.CommonObjectMeta,
151+
Spec: configurationv1alpha1.KongServiceSpec{
152+
ControlPlaneRef: &commonv1alpha1.ControlPlaneRef{
153+
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
154+
KonnectNamespacedRef: &commonv1alpha1.KonnectNamespacedRef{
155+
Name: "test-konnect-control-plane",
156+
},
157+
},
158+
Adopt: &commonv1alpha1.AdoptOptions{
159+
From: commonv1alpha1.AdoptSourceKonnect,
160+
},
161+
},
162+
},
163+
ExpectedErrorMessage: lo.ToPtr("Must specify Konnect options when from='konnect'"),
164+
},
165+
{
166+
Name: "adopt.konnect.id is immutable",
167+
TestObject: &configurationv1alpha1.KongService{
168+
ObjectMeta: common.CommonObjectMeta,
169+
Spec: configurationv1alpha1.KongServiceSpec{
170+
ControlPlaneRef: &commonv1alpha1.ControlPlaneRef{
171+
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
172+
KonnectNamespacedRef: &commonv1alpha1.KonnectNamespacedRef{
173+
Name: "test-konnect-control-plane",
174+
},
175+
},
176+
Adopt: &commonv1alpha1.AdoptOptions{
177+
From: commonv1alpha1.AdoptSourceKonnect,
178+
Konnect: &commonv1alpha1.AdoptKonnectOptions{
179+
ID: "test-konnect-id",
180+
},
181+
},
182+
},
183+
},
184+
Update: func(ks *configurationv1alpha1.KongService) {
185+
ks.Spec.Adopt.Konnect.ID = "test-konnect-id-2"
186+
},
187+
ExpectedUpdateErrorMessage: lo.ToPtr("konnect.id is immutable"),
188+
},
189+
{
190+
Name: "Cannot unset adopt",
191+
TestObject: &configurationv1alpha1.KongService{
192+
ObjectMeta: common.CommonObjectMeta,
193+
Spec: configurationv1alpha1.KongServiceSpec{
194+
ControlPlaneRef: &commonv1alpha1.ControlPlaneRef{
195+
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
196+
KonnectNamespacedRef: &commonv1alpha1.KonnectNamespacedRef{
197+
Name: "test-konnect-control-plane",
198+
},
199+
},
200+
Adopt: &commonv1alpha1.AdoptOptions{
201+
From: commonv1alpha1.AdoptSourceKonnect,
202+
Konnect: &commonv1alpha1.AdoptKonnectOptions{
203+
ID: "test-konnect-id",
204+
},
205+
},
206+
},
207+
},
208+
Update: func(ks *configurationv1alpha1.KongService) {
209+
ks.Spec.Adopt = nil
210+
},
211+
ExpectedUpdateErrorMessage: lo.ToPtr("Cannot set or unset spec.adopt in updates"),
212+
},
213+
}.Run(t)
214+
})
103215
}

0 commit comments

Comments
 (0)