Skip to content

Commit 8353d00

Browse files
authored
chore: use alias types for ControlPlane related types in v2alpha1 (#1977)
1 parent 2a1efa2 commit 8353d00

File tree

11 files changed

+61
-52
lines changed

11 files changed

+61
-52
lines changed

.golangci.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ run:
77
- conformance_tests
88
- istio_tests
99
- envtest
10+
- performance_tests
1011
linters:
1112
enable:
1213
- asciicheck
@@ -54,13 +55,16 @@ linters:
5455
default-signifies-exhaustive: true
5556
forbidigo:
5657
forbid:
57-
# Uncomment or remove - TODO: https://github.com/Kong/kong-operator/issues/1847
58+
# Uncomment or remove - TODO: https://github.com/Kong/kong-operator/issues/1847
5859
# - pattern: ^.*Dataplane[^G].*$
5960
# msg: Please use camel case 'DataPlane' instead of 'Dataplane'
6061
- pattern: ^.*Controlplane.*$
6162
msg: Please use camel case 'ControlPlane' instead of 'Controlplane'
6263
- pattern: ^.*operatorv1beta1.ControlPlane.*$
6364
msg: Please use v2alpha1 ControlPlane (through github.com/kong/kong-operator/internal/types)
65+
# NOTE: When all references use the types package, this rule can use .* instead of listing types.
66+
- pattern: ^.*operatorv2alpha1.(ControlPlane|ControlPlaneStatus|ControlPlaneList|ControlPlaneSpec|ControlPlaneOptions|ControlPlaneDataPlaneTarget\w*)\b.*$
67+
msg: Please use the types package to refer to the type (through github.com/kong/kong-operator/internal/types)
6468
gomodguard:
6569
blocked:
6670
modules:
@@ -195,7 +199,10 @@ linters:
195199
- linters:
196200
- gosec
197201
text: integer overflow conversion
198-
# end of section.
202+
- linters:
203+
- forbidigo
204+
path: internal/types/operatortypes.go
205+
text: use of `operatorv2alpha1.(ControlPlane\w*)` forbidden
199206
paths:
200207
- pkg/clientset
201208
- config/

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ govulncheck: download.govulncheck
248248

249249
GOLANGCI_LINT_CONFIG ?= $(PROJECT_DIR)/.golangci.yaml
250250
.PHONY: lint
251-
lint: golangci-lint lint.modernize
251+
lint: lint.golangci-lint lint.modernize
252+
253+
.PHONY: lint.golangci-lint
254+
lint.golangci-lint: golangci-lint
252255
$(GOLANGCI_LINT) run -v --config $(GOLANGCI_LINT_CONFIG) $(GOLANGCI_LINT_FLAGS)
253256

254257
.PHONY: lint.modernize

controller/controlplane/controller_reconciler_utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/kong/kong-operator/controller/pkg/op"
2121
"github.com/kong/kong-operator/controller/pkg/secrets"
22+
gwtypes "github.com/kong/kong-operator/internal/types"
2223
"github.com/kong/kong-operator/pkg/consts"
2324
k8sutils "github.com/kong/kong-operator/pkg/utils/kubernetes"
2425
)
@@ -54,7 +55,7 @@ func (r *Reconciler) ensureDataPlaneStatus(
5455
dataplane *operatorv1beta1.DataPlane,
5556
) (dataplaneIsSet bool, err error) {
5657
switch cp.Spec.DataPlane.Type {
57-
case operatorv2alpha1.ControlPlaneDataPlaneTargetRefType:
58+
case gwtypes.ControlPlaneDataPlaneTargetRefType:
5859
dataplaneIsSet = cp.Spec.DataPlane.Ref != nil && cp.Spec.DataPlane.Ref.Name == dataplane.Name
5960

6061
var newCondition metav1.Condition
@@ -91,7 +92,7 @@ func (r *Reconciler) ensureDataPlaneStatus(
9192
// communication between the ControlPlane and the DataPlane.
9293
func (r *Reconciler) ensureAdminMTLSCertificateSecret(
9394
ctx context.Context,
94-
cp *operatorv2alpha1.ControlPlane,
95+
cp *gwtypes.ControlPlane,
9596
) (
9697
op.Result,
9798
*corev1.Secret,

controller/controlplane/controller_reconciler_utils_test.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
operatorv1beta1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v1beta1"
1414
operatorv2alpha1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v2alpha1"
1515

16+
gwtypes "github.com/kong/kong-operator/internal/types"
1617
"github.com/kong/kong-operator/modules/manager/scheme"
1718
)
1819

@@ -266,8 +267,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
266267
Name: "test-cp",
267268
Namespace: "cp-ns",
268269
},
269-
Spec: operatorv2alpha1.ControlPlaneSpec{
270-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
270+
Spec: gwtypes.ControlPlaneSpec{
271+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
271272
WatchNamespaces: nil,
272273
},
273274
},
@@ -282,8 +283,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
282283
Name: "test-cp",
283284
Namespace: "cp-ns",
284285
},
285-
Spec: operatorv2alpha1.ControlPlaneSpec{
286-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
286+
Spec: gwtypes.ControlPlaneSpec{
287+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
287288
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
288289
Type: operatorv2alpha1.WatchNamespacesTypeAll,
289290
},
@@ -300,8 +301,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
300301
Name: "test-cp",
301302
Namespace: "cp-ns",
302303
},
303-
Spec: operatorv2alpha1.ControlPlaneSpec{
304-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
304+
Spec: gwtypes.ControlPlaneSpec{
305+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
305306
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
306307
Type: operatorv2alpha1.WatchNamespacesTypeAll,
307308
},
@@ -319,8 +320,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
319320
Name: "test-cp",
320321
Namespace: "cp-ns",
321322
},
322-
Spec: operatorv2alpha1.ControlPlaneSpec{
323-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
323+
Spec: gwtypes.ControlPlaneSpec{
324+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
324325
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
325326
Type: operatorv2alpha1.WatchNamespacesTypeOwn,
326327
},
@@ -337,8 +338,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
337338
Name: "test-cp",
338339
Namespace: "cp-ns",
339340
},
340-
Spec: operatorv2alpha1.ControlPlaneSpec{
341-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
341+
Spec: gwtypes.ControlPlaneSpec{
342+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
342343
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
343344
Type: operatorv2alpha1.WatchNamespacesTypeOwn,
344345
},
@@ -355,8 +356,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
355356
Name: "test-cp",
356357
Namespace: "cp-ns",
357358
},
358-
Spec: operatorv2alpha1.ControlPlaneSpec{
359-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
359+
Spec: gwtypes.ControlPlaneSpec{
360+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
360361
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
361362
Type: operatorv2alpha1.WatchNamespacesTypeOwn,
362363
},
@@ -374,8 +375,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
374375
Name: "test-cp",
375376
Namespace: "cp-ns",
376377
},
377-
Spec: operatorv2alpha1.ControlPlaneSpec{
378-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
378+
Spec: gwtypes.ControlPlaneSpec{
379+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
379380
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
380381
Type: operatorv2alpha1.WatchNamespacesTypeList,
381382
List: []string{"ns1", "ns2"},
@@ -393,8 +394,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
393394
Name: "test-cp",
394395
Namespace: "cp-ns",
395396
},
396-
Spec: operatorv2alpha1.ControlPlaneSpec{
397-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
397+
Spec: gwtypes.ControlPlaneSpec{
398+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
398399
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
399400
Type: operatorv2alpha1.WatchNamespacesTypeList,
400401
List: []string{"ns1", "ns2"},
@@ -412,8 +413,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
412413
Name: "test-cp",
413414
Namespace: "cp-ns",
414415
},
415-
Spec: operatorv2alpha1.ControlPlaneSpec{
416-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
416+
Spec: gwtypes.ControlPlaneSpec{
417+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
417418
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
418419
Type: operatorv2alpha1.WatchNamespacesTypeList,
419420
List: []string{"ns1", "ns2", "ns3"},
@@ -432,8 +433,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
432433
Name: "test-cp",
433434
Namespace: "cp-ns",
434435
},
435-
Spec: operatorv2alpha1.ControlPlaneSpec{
436-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
436+
Spec: gwtypes.ControlPlaneSpec{
437+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
437438
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
438439
Type: operatorv2alpha1.WatchNamespacesTypeList,
439440
List: []string{"ns1", "ns2"},
@@ -452,8 +453,8 @@ func TestValidateWatchNamespaces(t *testing.T) {
452453
Name: "test-cp",
453454
Namespace: "cp-ns",
454455
},
455-
Spec: operatorv2alpha1.ControlPlaneSpec{
456-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
456+
Spec: gwtypes.ControlPlaneSpec{
457+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
457458
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
458459
Type: operatorv2alpha1.WatchNamespacesTypeList,
459460
List: []string{"forbidden-ns"},

controller/controlplane/controller_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
operatorv2alpha1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v2alpha1"
2121

2222
"github.com/kong/kong-operator/ingress-controller/pkg/manager/multiinstance"
23+
gwtypes "github.com/kong/kong-operator/internal/types"
2324
"github.com/kong/kong-operator/modules/manager/scheme"
2425
"github.com/kong/kong-operator/pkg/consts"
2526
k8sutils "github.com/kong/kong-operator/pkg/utils/kubernetes"
@@ -70,20 +71,20 @@ func TestReconciler_Reconcile(t *testing.T) {
7071
string(ControlPlaneFinalizerCleanupValidatingWebhookConfiguration),
7172
},
7273
},
73-
Spec: operatorv2alpha1.ControlPlaneSpec{
74-
DataPlane: operatorv2alpha1.ControlPlaneDataPlaneTarget{
75-
Type: operatorv2alpha1.ControlPlaneDataPlaneTargetRefType,
76-
Ref: &operatorv2alpha1.ControlPlaneDataPlaneTargetRef{
74+
Spec: gwtypes.ControlPlaneSpec{
75+
DataPlane: gwtypes.ControlPlaneDataPlaneTarget{
76+
Type: gwtypes.ControlPlaneDataPlaneTargetRefType,
77+
Ref: &gwtypes.ControlPlaneDataPlaneTargetRef{
7778
Name: "test-dataplane",
7879
},
7980
},
80-
ControlPlaneOptions: operatorv2alpha1.ControlPlaneOptions{
81+
ControlPlaneOptions: gwtypes.ControlPlaneOptions{
8182
WatchNamespaces: &operatorv2alpha1.WatchNamespaces{
8283
Type: operatorv2alpha1.WatchNamespacesTypeAll,
8384
},
8485
},
8586
},
86-
Status: operatorv2alpha1.ControlPlaneStatus{
87+
Status: gwtypes.ControlPlaneStatus{
8788
Conditions: []metav1.Condition{
8889
{
8990
Type: string(kcfgcontrolplane.ConditionTypeProvisioned),

controller/controlplane/controller_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/client"
99

1010
operatorv1beta1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v1beta1"
11-
operatorv2alpha1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v2alpha1"
1211

1312
operatorerrors "github.com/kong/kong-operator/internal/errors"
13+
gwtypes "github.com/kong/kong-operator/internal/types"
1414
)
1515

1616
// GetDataPlaneForControlPlane retrieves the DataPlane object referenced by a ControlPlane.
@@ -20,7 +20,7 @@ func GetDataPlaneForControlPlane(
2020
cp *ControlPlane,
2121
) (*operatorv1beta1.DataPlane, error) {
2222
switch cp.Spec.DataPlane.Type {
23-
case operatorv2alpha1.ControlPlaneDataPlaneTargetRefType:
23+
case gwtypes.ControlPlaneDataPlaneTargetRefType:
2424
if cp.Spec.DataPlane.Ref == nil || cp.Spec.DataPlane.Ref.Name == "" {
2525
return nil, fmt.Errorf("%w, controlplane = %s/%s", operatorerrors.ErrDataPlaneNotSet, cp.Namespace, cp.Name)
2626
}

controller/controlplane/controller_watch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
operatorv2alpha1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v2alpha1"
1515

1616
operatorerrors "github.com/kong/kong-operator/internal/errors"
17+
gwtypes "github.com/kong/kong-operator/internal/types"
1718
)
1819

1920
func (r *Reconciler) listControlPlanesForWatchNamespaceGrants(
@@ -39,7 +40,7 @@ func (r *Reconciler) listControlPlanesForWatchNamespaceGrants(
3940

4041
var recs []reconcile.Request
4142
for _, from := range fromsForControlPlane {
42-
var controlPlaneList operatorv2alpha1.ControlPlaneList
43+
var controlPlaneList gwtypes.ControlPlaneList
4344
if err := r.List(ctx, &controlPlaneList,
4445
client.InNamespace(from.Namespace),
4546
); err != nil {

controller/controlplane_extensions/controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
configurationv1 "github.com/kong/kubernetes-configuration/v2/api/configuration/v1"
2727
operatorv1alpha1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v1alpha1"
2828
operatorv1beta1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v1beta1"
29-
operatorv2alpha1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v2alpha1"
3029

3130
"github.com/kong/kong-operator/controller/pkg/extensions"
3231
"github.com/kong/kong-operator/controller/pkg/log"
@@ -83,7 +82,7 @@ func (r *Reconciler) SetupWithManager(_ context.Context, mgr ctrl.Manager) error
8382
CacheSyncTimeout: r.CacheSyncTimeout,
8483
}).
8584
// Watch for changes to owned ControlPlane that had DataPlane.
86-
For(&operatorv2alpha1.ControlPlane{},
85+
For(&gwtypes.ControlPlane{},
8786
builder.WithPredicates(
8887
ControlPlaneDataPlanePluginsSpecChangedPredicate{},
8988
),

ingress-controller/test/e2e/performance_basic_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestResourceApplyAndUpdatePerf(t *testing.T) {
166166
for j := i; j < i+batchSize && j < defaultResNum; j++ {
167167
resourceYaml += fmt.Sprintf(rulesTpl, j, j, j, j, base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(consumerUsername, j))), j, j, fmt.Sprintf(consumerUsername, j), j)
168168
}
169-
err = applyResourceWithKubectl(ctx, t, kubeconfig, resourceYaml)
169+
err = applyResourceWithKubectl(ctx, kubeconfig, resourceYaml)
170170
require.NoError(t, err)
171171
}
172172
completionTime := time.Now()
@@ -200,11 +200,10 @@ func TestResourceApplyAndUpdatePerf(t *testing.T) {
200200
t.Logf("time to make %d ingress rules take effect: %v", defaultResNum, effectTime.Sub(completionTime))
201201
writeResultToTempFile(t, allResourceTakeEffectReport, defaultResNum, int(effectTime.Sub(completionTime).Milliseconds()))
202202

203-
rand.Seed(time.Now().UnixNano())
204203
randomInt := rand.Intn(10000)
205204

206205
startTime = time.Now()
207-
err = applyResourceWithKubectl(ctx, t, kubeconfig, fmt.Sprintf(updatedIngressTpl, randomInt, randomInt, randomInt))
206+
err = applyResourceWithKubectl(ctx, kubeconfig, fmt.Sprintf(updatedIngressTpl, randomInt, randomInt, randomInt))
208207
require.NoError(t, err)
209208
completionTime = time.Now()
210209

@@ -254,7 +253,7 @@ func isRouteActive(ctx context.Context, t *testing.T, client *http.Client, proxy
254253
return false
255254
}
256255

257-
func applyResourceWithKubectl(ctx context.Context, t *testing.T, kubeconfig, resourceYAML string) error {
256+
func applyResourceWithKubectl(ctx context.Context, kubeconfig, resourceYAML string) error {
258257
cmd := exec.CommandContext(ctx, "kubectl", "--kubeconfig", kubeconfig, "apply", "-f", "-")
259258
cmd.Stdin = strings.NewReader(resourceYAML)
260259
err := cmd.Run()
@@ -279,7 +278,7 @@ func writeResultToTempFile(t *testing.T, filename string, resourceNum, time int)
279278
}
280279
defer file.Close()
281280

282-
_, err = file.WriteString(fmt.Sprintf("%d %d\n", resourceNum, time))
281+
_, err = fmt.Fprintf(file, "%d %d\n", resourceNum, time)
283282
if err != nil {
284283
t.Logf("failed to write to file: %v", err)
285284
return

internal/utils/index/controlplane.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package index
33
import (
44
"sigs.k8s.io/controller-runtime/pkg/client"
55

6-
operatorv2alpha1 "github.com/kong/kubernetes-configuration/v2/api/gateway-operator/v2alpha1"
7-
86
gwtypes "github.com/kong/kong-operator/internal/types"
97
)
108

@@ -43,7 +41,7 @@ func dataPlaneNameOnControlPlane(o client.Object) []string {
4341
}
4442
dp := controlPlane.Spec.DataPlane
4543
switch dp.Type {
46-
case operatorv2alpha1.ControlPlaneDataPlaneTargetRefType:
44+
case gwtypes.ControlPlaneDataPlaneTargetRefType:
4745
// Note: .Name is a pointer, enforced to be non nil at the CRD level.
4846
return []string{controlPlane.Spec.DataPlane.Ref.Name}
4947
// TODO(pmalek): implement DataPlane external URL type

0 commit comments

Comments
 (0)