Skip to content

refactor: aggregate types to be used by clients #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions api/v1alpha1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@
package v1alpha1

const (
// ClusterConfigVariableName is the meta cluster config patch variable name.
ClusterConfigVariableName = "clusterConfig"
// ControlPlaneConfigVariableName is the control-plane config patch variable name.
ControlPlaneConfigVariableName = "controlPlane"
// WorkerConfigVariableName is the meta worker config patch variable name.
WorkerConfigVariableName = "workerConfig"

// AWSVariableName is the AWS config patch variable name.
AWSVariableName = "aws"
// DockerVariableName is the Docker config patch variable name.
DockerVariableName = "docker"
// NutanixVariableName is the Nutanix config patch variable name.
NutanixVariableName = "nutanix"

// CNIVariableName is the CNI external patch variable name.
CNIVariableName = "cni"
// NFDVariableName is the NFD external patch variable name.
NFDVariableName = "nfd"

// ClusterAutoscalerVariableName is the cluster-autoscaler external patch variable name.
ClusterAutoscalerVariableName = "clusterAutoscaler"
// AWSVariableName is the AWS config patch variable name.
AWSVariableName = "aws"
// NutanixVariableName is the Nutanix config patch variable name.
NutanixVariableName = "nutanix"
// ServiceLoadBalancerName is the Service LoadBalancer config patch variable name.
// ServiceLoadBalancerVariableName is the Service LoadBalancer config patch variable name.
ServiceLoadBalancerVariableName = "serviceLoadBalancer"
)
4 changes: 1 addition & 3 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// Copyright 2024 Nutanix. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package clusterconfig
package variables

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

// ClusterConfig is a type to be used internally to simplify the handling of cluster configurations for different
// The types here are to be used internally to simplify the handling of cluster configurations for different
// providers. It is not meant to be used as a CRD.
// By including all the possible configurations for all the providers, we can easily switch between providers in code
// without type assertions/switches and avoids passing around `interface{}` or `any` types.
// Every provider-specific cluster config variable will successfully unmarshal to this type and so it is safe to use
// this internally when a handler provides functionality for multiple providers but exhibits different behaviour per
// provider.
type ClusterConfig struct {

type ClusterConfigSpec struct {
AWS *carenv1.AWSSpec `json:"aws,omitempty"`

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

carenv1.GenericClusterConfigSpec `json:",inline"`

ExtraAPIServerCertSANs []string `json:"extraAPIServerCertSANs,omitempty"`
ControlPlane *ControlPlaneNodeConfigSpec `json:"controlPlane,omitempty"`

ControlPlane *ControlPlaneConfig `json:"controlPlane,omitempty"`
ExtraAPIServerCertSANs []string `json:"extraAPIServerCertSANs,omitempty"`
}

type ControlPlaneConfig struct {
type ControlPlaneNodeConfigSpec struct {
AWS *carenv1.AWSControlPlaneNodeSpec `json:"aws,omitempty"`

Docker *carenv1.DockerNodeConfigSpec `json:"docker,omitempty"`
Docker *carenv1.DockerNodeSpec `json:"docker,omitempty"`

Nutanix *carenv1.NutanixNodeSpec `json:"nutanix,omitempty"`
}

type WorkerNodeConfigSpec struct {
AWS *carenv1.AWSWorkerNodeSpec `json:"aws,omitempty"`

Docker *carenv1.DockerNodeSpec `json:"docker,omitempty"`

Nutanix *carenv1.NutanixNodeConfigSpec `json:"nutanix,omitempty"`
Nutanix *carenv1.NutanixNodeSpec `json:"nutanix,omitempty"`
}
62 changes: 62 additions & 0 deletions api/variables/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"fmt"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
)

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

return &apiextensionsv1.JSON{Raw: marshaled}
}

func MarshalToClusterVariable[T any](name string, obj T) (*clusterv1.ClusterVariable, error) {
marshaled, err := json.Marshal(obj)
if err != nil {
return nil, fmt.Errorf("failed to marshal variable value %q: %w", name, err)
}
return &clusterv1.ClusterVariable{
Name: name,
Value: apiextensionsv1.JSON{Raw: marshaled},
}, nil
}

func UnmarshalClusterConfigVariable(clusterVariables []clusterv1.ClusterVariable) (*ClusterConfigSpec, error) {
variableName := v1alpha1.ClusterConfigVariableName
clusterConfig := GetClusterVariableByName(variableName, clusterVariables)
if clusterConfig == nil {
return nil, nil
}
spec := &ClusterConfigSpec{}
err := UnmarshalClusterVariable(clusterConfig, spec)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal cluster variable %q: %w", variableName, err)
}

return spec, nil
}

func UnmarshalWorkerConfigVariable(clusterVariables []clusterv1.ClusterVariable) (*WorkerNodeConfigSpec, error) {
variableName := v1alpha1.WorkerConfigVariableName
workerConfig := GetClusterVariableByName(variableName, clusterVariables)
if workerConfig == nil {
return nil, nil
}
spec := &WorkerNodeConfigSpec{}
err := UnmarshalClusterVariable(workerConfig, spec)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal cluster variable %q: %w", variableName, err)
}

return spec, nil
}

func UnmarshalClusterVariable[T any](clusterVariable *clusterv1.ClusterVariable, obj *T) error {
err := json.Unmarshal(clusterVariable.Value.Raw, obj)
if err != nil {
return fmt.Errorf("failed to unmarshal json: %w", err)
}

return nil
}

func GetClusterVariableByName(name string, clusterVariables []clusterv1.ClusterVariable) *clusterv1.ClusterVariable {
for _, clusterVar := range clusterVariables {
if clusterVar.Name == name {
return &clusterVar
}
}
return nil
}
32 changes: 0 additions & 32 deletions common/pkg/capi/clustertopology/variables/json.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/handlers/aws/clusterconfig/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
commonhandlers "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
)

var (
Expand Down Expand Up @@ -41,7 +40,7 @@ func (h *awsClusterConfigVariableHandler) DiscoverVariables(
resp *runtimehooksv1.DiscoverVariablesResponse,
) {
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{
Name: clusterconfig.MetaVariableName,
Name: v1alpha1.ClusterConfigVariableName,
Required: true,
Schema: v1alpha1.AWSClusterConfig{}.VariableSchema(),
})
Expand Down
5 changes: 2 additions & 3 deletions pkg/handlers/aws/mutation/ami/inject_control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
capav1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
)

func NewControlPlanePatch() *awsAMISpecPatchHandler {
return newAWSAMISpecPatchHandler(
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
[]string{
clusterconfig.MetaControlPlaneConfigName,
v1alpha1.ControlPlaneConfigVariableName,
v1alpha1.AWSVariableName,
VariableName,
},
Expand Down
9 changes: 4 additions & 5 deletions pkg/handlers/aws/mutation/ami/inject_control_plane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
)

Expand All @@ -29,9 +28,9 @@ var _ = Describe("Generate AMI patches for ControlPlane", func() {
Name: "AMI set for control plane",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
v1alpha1.AMISpec{ID: "ami-controlplane"},
clusterconfig.MetaControlPlaneConfigName,
v1alpha1.ControlPlaneConfigVariableName,
v1alpha1.AWSVariableName,
VariableName,
),
Expand All @@ -49,15 +48,15 @@ var _ = Describe("Generate AMI patches for ControlPlane", func() {
Name: "AMI lookup format set for control plane",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
v1alpha1.AMISpec{
Lookup: &v1alpha1.AMILookup{
Format: "test-{{.kubernetesVersion}}-format",
Org: "1234",
BaseOS: "testOS",
},
},
clusterconfig.MetaControlPlaneConfigName,
v1alpha1.ControlPlaneConfigVariableName,
v1alpha1.AWSVariableName,
VariableName,
),
Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/aws/mutation/ami/inject_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
capav1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
)

func NewWorkerPatch() *awsAMISpecPatchHandler {
return newAWSAMISpecPatchHandler(
workerconfig.MetaVariableName,
v1alpha1.WorkerConfigVariableName,
[]string{
v1alpha1.AWSVariableName,
VariableName,
Expand Down
5 changes: 2 additions & 3 deletions pkg/handlers/aws/mutation/ami/inject_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
)

Expand All @@ -27,7 +26,7 @@ var _ = Describe("Generate AMI patches for Worker", func() {
Name: "AMI set for workers",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
workerconfig.MetaVariableName,
v1alpha1.WorkerConfigVariableName,
v1alpha1.AMISpec{ID: "ami-controlplane"},
v1alpha1.AWSVariableName,
VariableName,
Expand All @@ -52,7 +51,7 @@ var _ = Describe("Generate AMI patches for Worker", func() {
Name: "AMI lookup format set for worker",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
workerconfig.MetaVariableName,
v1alpha1.WorkerConfigVariableName,
v1alpha1.AMISpec{
Lookup: &v1alpha1.AMILookup{
Format: "test-{{.kubernetesVersion}}-format",
Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/aws/mutation/ami/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
awsclusterconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/clusterconfig"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
)

func TestVariableValidation(t *testing.T) {
capitest.ValidateDiscoverVariables(
t,
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
ptr.To(v1alpha1.AWSClusterConfig{}.VariableSchema()),
true,
awsclusterconfig.NewVariable,
Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/aws/mutation/cni/calico/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
)

const (
Expand All @@ -35,7 +34,7 @@ type calicoPatchHandler struct {

func NewPatch() *calicoPatchHandler {
return newCalicoPatchHandler(
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
"addons",
v1alpha1.CNIVariableName,
)
Expand Down
7 changes: 3 additions & 4 deletions pkg/handlers/aws/mutation/cni/calico/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
)

Expand All @@ -37,7 +36,7 @@ var _ = Describe("Generate AWS Calico CNI ingress patches", func() {
Name: "provider set with AWSClusterTemplate",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
v1alpha1.CNI{
Provider: v1alpha1.CNIProviderCalico,
},
Expand Down Expand Up @@ -105,7 +104,7 @@ var _ = Describe("Generate AWS Calico CNI ingress patches", func() {
Name: "provider set with AWSClusterTemplate pre-existing rules",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
v1alpha1.CNI{
Provider: v1alpha1.CNIProviderCalico,
},
Expand Down Expand Up @@ -198,7 +197,7 @@ var _ = Describe("Generate AWS Calico CNI ingress patches", func() {
Name: "provider set with AWSClusterTemplate conflicting pre-existing rules",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
v1alpha1.CNI{
Provider: v1alpha1.CNIProviderCalico,
},
Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/aws/mutation/controlplaneloadbalancer/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
)

const (
Expand All @@ -33,7 +32,7 @@ type awsControlPlaneLoadBalancer struct {

func NewPatch() *awsControlPlaneLoadBalancer {
return newAWSControlPlaneLoadBalancer(
clusterconfig.MetaVariableName,
v1alpha1.ClusterConfigVariableName,
v1alpha1.AWSVariableName,
VariableName,
)
Expand Down
Loading
Loading