Skip to content

Commit 845826a

Browse files
authored
refactor: aggregate types to be used by clients (#672)
**What problem does this PR solve?**: This change make it possible to reuse this common to marshal and unmarshal functions using the aggregate types in other projects that import the API. To do this this PR moves `ControlPlaneConfig`(renamed to `ControlPlaneConfigSpec`) and `WorkerNodeConfigSpec` to the `api/` module. **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> Unit and e2e tests, but mainly the fact that the project builds. **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. --> Majority of changes are changing the package name for the different variables.
1 parent c3b1183 commit 845826a

File tree

105 files changed

+294
-367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+294
-367
lines changed

api/v1alpha1/constants.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,27 @@
44
package v1alpha1
55

66
const (
7+
// ClusterConfigVariableName is the meta cluster config patch variable name.
8+
ClusterConfigVariableName = "clusterConfig"
9+
// ControlPlaneConfigVariableName is the control-plane config patch variable name.
10+
ControlPlaneConfigVariableName = "controlPlane"
11+
// WorkerConfigVariableName is the meta worker config patch variable name.
12+
WorkerConfigVariableName = "workerConfig"
13+
14+
// AWSVariableName is the AWS config patch variable name.
15+
AWSVariableName = "aws"
16+
// DockerVariableName is the Docker config patch variable name.
17+
DockerVariableName = "docker"
18+
// NutanixVariableName is the Nutanix config patch variable name.
19+
NutanixVariableName = "nutanix"
20+
721
// CNIVariableName is the CNI external patch variable name.
822
CNIVariableName = "cni"
923
// NFDVariableName is the NFD external patch variable name.
1024
NFDVariableName = "nfd"
25+
1126
// ClusterAutoscalerVariableName is the cluster-autoscaler external patch variable name.
1227
ClusterAutoscalerVariableName = "clusterAutoscaler"
13-
// AWSVariableName is the AWS config patch variable name.
14-
AWSVariableName = "aws"
15-
// NutanixVariableName is the Nutanix config patch variable name.
16-
NutanixVariableName = "nutanix"
17-
// ServiceLoadBalancerName is the Service LoadBalancer config patch variable name.
28+
// ServiceLoadBalancerVariableName is the Service LoadBalancer config patch variable name.
1829
ServiceLoadBalancerVariableName = "serviceLoadBalancer"
1930
)

api/v1alpha1/zz_generated.deepcopy.go

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// Copyright 2024 Nutanix. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package clusterconfig
4+
package variables
55

66
import carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
77

8-
// ClusterConfig is a type to be used internally to simplify the handling of cluster configurations for different
8+
// The types here are to be used internally to simplify the handling of cluster configurations for different
99
// providers. It is not meant to be used as a CRD.
1010
// By including all the possible configurations for all the providers, we can easily switch between providers in code
1111
// without type assertions/switches and avoids passing around `interface{}` or `any` types.
1212
// Every provider-specific cluster config variable will successfully unmarshal to this type and so it is safe to use
1313
// this internally when a handler provides functionality for multiple providers but exhibits different behaviour per
1414
// provider.
15-
type ClusterConfig struct {
15+
16+
type ClusterConfigSpec struct {
1617
AWS *carenv1.AWSSpec `json:"aws,omitempty"`
1718

1819
Docker *carenv1.DockerSpec `json:"docker,omitempty"`
@@ -21,15 +22,23 @@ type ClusterConfig struct {
2122

2223
carenv1.GenericClusterConfigSpec `json:",inline"`
2324

24-
ExtraAPIServerCertSANs []string `json:"extraAPIServerCertSANs,omitempty"`
25+
ControlPlane *ControlPlaneNodeConfigSpec `json:"controlPlane,omitempty"`
2526

26-
ControlPlane *ControlPlaneConfig `json:"controlPlane,omitempty"`
27+
ExtraAPIServerCertSANs []string `json:"extraAPIServerCertSANs,omitempty"`
2728
}
2829

29-
type ControlPlaneConfig struct {
30+
type ControlPlaneNodeConfigSpec struct {
3031
AWS *carenv1.AWSControlPlaneNodeSpec `json:"aws,omitempty"`
3132

32-
Docker *carenv1.DockerNodeConfigSpec `json:"docker,omitempty"`
33+
Docker *carenv1.DockerNodeSpec `json:"docker,omitempty"`
34+
35+
Nutanix *carenv1.NutanixNodeSpec `json:"nutanix,omitempty"`
36+
}
37+
38+
type WorkerNodeConfigSpec struct {
39+
AWS *carenv1.AWSWorkerNodeSpec `json:"aws,omitempty"`
40+
41+
Docker *carenv1.DockerNodeSpec `json:"docker,omitempty"`
3342

34-
Nutanix *carenv1.NutanixNodeConfigSpec `json:"nutanix,omitempty"`
43+
Nutanix *carenv1.NutanixNodeSpec `json:"nutanix,omitempty"`
3544
}

api/variables/json.go

+62
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"fmt"
99

1010
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
11+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
12+
13+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1114
)
1215

1316
func MustMarshal(val any) *apiextensionsv1.JSON {
@@ -18,3 +21,62 @@ func MustMarshal(val any) *apiextensionsv1.JSON {
1821

1922
return &apiextensionsv1.JSON{Raw: marshaled}
2023
}
24+
25+
func MarshalToClusterVariable[T any](name string, obj T) (*clusterv1.ClusterVariable, error) {
26+
marshaled, err := json.Marshal(obj)
27+
if err != nil {
28+
return nil, fmt.Errorf("failed to marshal variable value %q: %w", name, err)
29+
}
30+
return &clusterv1.ClusterVariable{
31+
Name: name,
32+
Value: apiextensionsv1.JSON{Raw: marshaled},
33+
}, nil
34+
}
35+
36+
func UnmarshalClusterConfigVariable(clusterVariables []clusterv1.ClusterVariable) (*ClusterConfigSpec, error) {
37+
variableName := v1alpha1.ClusterConfigVariableName
38+
clusterConfig := GetClusterVariableByName(variableName, clusterVariables)
39+
if clusterConfig == nil {
40+
return nil, nil
41+
}
42+
spec := &ClusterConfigSpec{}
43+
err := UnmarshalClusterVariable(clusterConfig, spec)
44+
if err != nil {
45+
return nil, fmt.Errorf("failed to unmarshal cluster variable %q: %w", variableName, err)
46+
}
47+
48+
return spec, nil
49+
}
50+
51+
func UnmarshalWorkerConfigVariable(clusterVariables []clusterv1.ClusterVariable) (*WorkerNodeConfigSpec, error) {
52+
variableName := v1alpha1.WorkerConfigVariableName
53+
workerConfig := GetClusterVariableByName(variableName, clusterVariables)
54+
if workerConfig == nil {
55+
return nil, nil
56+
}
57+
spec := &WorkerNodeConfigSpec{}
58+
err := UnmarshalClusterVariable(workerConfig, spec)
59+
if err != nil {
60+
return nil, fmt.Errorf("failed to unmarshal cluster variable %q: %w", variableName, err)
61+
}
62+
63+
return spec, nil
64+
}
65+
66+
func UnmarshalClusterVariable[T any](clusterVariable *clusterv1.ClusterVariable, obj *T) error {
67+
err := json.Unmarshal(clusterVariable.Value.Raw, obj)
68+
if err != nil {
69+
return fmt.Errorf("failed to unmarshal json: %w", err)
70+
}
71+
72+
return nil
73+
}
74+
75+
func GetClusterVariableByName(name string, clusterVariables []clusterv1.ClusterVariable) *clusterv1.ClusterVariable {
76+
for _, clusterVar := range clusterVariables {
77+
if clusterVar.Name == name {
78+
return &clusterVar
79+
}
80+
}
81+
return nil
82+
}

common/pkg/capi/clustertopology/variables/json.go

-32
This file was deleted.

pkg/handlers/aws/clusterconfig/variables.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1313
commonhandlers "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
1414
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
15-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
1615
)
1716

1817
var (
@@ -41,7 +40,7 @@ func (h *awsClusterConfigVariableHandler) DiscoverVariables(
4140
resp *runtimehooksv1.DiscoverVariablesResponse,
4241
) {
4342
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{
44-
Name: clusterconfig.MetaVariableName,
43+
Name: v1alpha1.ClusterConfigVariableName,
4544
Required: true,
4645
Schema: v1alpha1.AWSClusterConfig{}.VariableSchema(),
4746
})

pkg/handlers/aws/mutation/ami/inject_control_plane.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import (
77
capav1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
88
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
99
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
10-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
1110
)
1211

1312
func NewControlPlanePatch() *awsAMISpecPatchHandler {
1413
return newAWSAMISpecPatchHandler(
15-
clusterconfig.MetaVariableName,
14+
v1alpha1.ClusterConfigVariableName,
1615
[]string{
17-
clusterconfig.MetaControlPlaneConfigName,
16+
v1alpha1.ControlPlaneConfigVariableName,
1817
v1alpha1.AWSVariableName,
1918
VariableName,
2019
},

pkg/handlers/aws/mutation/ami/inject_control_plane_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1313
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
1414
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
15-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
1615
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
1716
)
1817

@@ -29,9 +28,9 @@ var _ = Describe("Generate AMI patches for ControlPlane", func() {
2928
Name: "AMI set for control plane",
3029
Vars: []runtimehooksv1.Variable{
3130
capitest.VariableWithValue(
32-
clusterconfig.MetaVariableName,
31+
v1alpha1.ClusterConfigVariableName,
3332
v1alpha1.AMISpec{ID: "ami-controlplane"},
34-
clusterconfig.MetaControlPlaneConfigName,
33+
v1alpha1.ControlPlaneConfigVariableName,
3534
v1alpha1.AWSVariableName,
3635
VariableName,
3736
),
@@ -49,15 +48,15 @@ var _ = Describe("Generate AMI patches for ControlPlane", func() {
4948
Name: "AMI lookup format set for control plane",
5049
Vars: []runtimehooksv1.Variable{
5150
capitest.VariableWithValue(
52-
clusterconfig.MetaVariableName,
51+
v1alpha1.ClusterConfigVariableName,
5352
v1alpha1.AMISpec{
5453
Lookup: &v1alpha1.AMILookup{
5554
Format: "test-{{.kubernetesVersion}}-format",
5655
Org: "1234",
5756
BaseOS: "testOS",
5857
},
5958
},
60-
clusterconfig.MetaControlPlaneConfigName,
59+
v1alpha1.ControlPlaneConfigVariableName,
6160
v1alpha1.AWSVariableName,
6261
VariableName,
6362
),

pkg/handlers/aws/mutation/ami/inject_worker.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
capav1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
77
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
88
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
9-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
109
)
1110

1211
func NewWorkerPatch() *awsAMISpecPatchHandler {
1312
return newAWSAMISpecPatchHandler(
14-
workerconfig.MetaVariableName,
13+
v1alpha1.WorkerConfigVariableName,
1514
[]string{
1615
v1alpha1.AWSVariableName,
1716
VariableName,

pkg/handlers/aws/mutation/ami/inject_worker_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1414
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
1515
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
16-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
1716
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
1817
)
1918

@@ -27,7 +26,7 @@ var _ = Describe("Generate AMI patches for Worker", func() {
2726
Name: "AMI set for workers",
2827
Vars: []runtimehooksv1.Variable{
2928
capitest.VariableWithValue(
30-
workerconfig.MetaVariableName,
29+
v1alpha1.WorkerConfigVariableName,
3130
v1alpha1.AMISpec{ID: "ami-controlplane"},
3231
v1alpha1.AWSVariableName,
3332
VariableName,
@@ -52,7 +51,7 @@ var _ = Describe("Generate AMI patches for Worker", func() {
5251
Name: "AMI lookup format set for worker",
5352
Vars: []runtimehooksv1.Variable{
5453
capitest.VariableWithValue(
55-
workerconfig.MetaVariableName,
54+
v1alpha1.WorkerConfigVariableName,
5655
v1alpha1.AMISpec{
5756
Lookup: &v1alpha1.AMILookup{
5857
Format: "test-{{.kubernetesVersion}}-format",

pkg/handlers/aws/mutation/ami/variables_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import (
1111
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1212
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
1313
awsclusterconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/clusterconfig"
14-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
1514
)
1615

1716
func TestVariableValidation(t *testing.T) {
1817
capitest.ValidateDiscoverVariables(
1918
t,
20-
clusterconfig.MetaVariableName,
19+
v1alpha1.ClusterConfigVariableName,
2120
ptr.To(v1alpha1.AWSClusterConfig{}.VariableSchema()),
2221
true,
2322
awsclusterconfig.NewVariable,

pkg/handlers/aws/mutation/cni/calico/inject.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches"
2121
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
2222
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
23-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
2423
)
2524

2625
const (
@@ -35,7 +34,7 @@ type calicoPatchHandler struct {
3534

3635
func NewPatch() *calicoPatchHandler {
3736
return newCalicoPatchHandler(
38-
clusterconfig.MetaVariableName,
37+
v1alpha1.ClusterConfigVariableName,
3938
"addons",
4039
v1alpha1.CNIVariableName,
4140
)

pkg/handlers/aws/mutation/cni/calico/inject_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1616
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
1717
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
18-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
1918
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
2019
)
2120

@@ -37,7 +36,7 @@ var _ = Describe("Generate AWS Calico CNI ingress patches", func() {
3736
Name: "provider set with AWSClusterTemplate",
3837
Vars: []runtimehooksv1.Variable{
3938
capitest.VariableWithValue(
40-
clusterconfig.MetaVariableName,
39+
v1alpha1.ClusterConfigVariableName,
4140
v1alpha1.CNI{
4241
Provider: v1alpha1.CNIProviderCalico,
4342
},
@@ -105,7 +104,7 @@ var _ = Describe("Generate AWS Calico CNI ingress patches", func() {
105104
Name: "provider set with AWSClusterTemplate pre-existing rules",
106105
Vars: []runtimehooksv1.Variable{
107106
capitest.VariableWithValue(
108-
clusterconfig.MetaVariableName,
107+
v1alpha1.ClusterConfigVariableName,
109108
v1alpha1.CNI{
110109
Provider: v1alpha1.CNIProviderCalico,
111110
},
@@ -198,7 +197,7 @@ var _ = Describe("Generate AWS Calico CNI ingress patches", func() {
198197
Name: "provider set with AWSClusterTemplate conflicting pre-existing rules",
199198
Vars: []runtimehooksv1.Variable{
200199
capitest.VariableWithValue(
201-
clusterconfig.MetaVariableName,
200+
v1alpha1.ClusterConfigVariableName,
202201
v1alpha1.CNI{
203202
Provider: v1alpha1.CNIProviderCalico,
204203
},

pkg/handlers/aws/mutation/controlplaneloadbalancer/inject.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches"
1919
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
2020
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
21-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
2221
)
2322

2423
const (
@@ -33,7 +32,7 @@ type awsControlPlaneLoadBalancer struct {
3332

3433
func NewPatch() *awsControlPlaneLoadBalancer {
3534
return newAWSControlPlaneLoadBalancer(
36-
clusterconfig.MetaVariableName,
35+
v1alpha1.ClusterConfigVariableName,
3736
v1alpha1.AWSVariableName,
3837
VariableName,
3938
)

0 commit comments

Comments
 (0)