Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
feat: set default instance type for aws cp and workers
Browse files Browse the repository at this point in the history
  • Loading branch information
supershal committed Apr 10, 2024
1 parent cdf1979 commit 3f673f9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 13 deletions.
43 changes: 40 additions & 3 deletions api/v1alpha1/aws_node_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
package v1alpha1

import (
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/variables"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

const (
AWSControlPlaneInstanceType InstanceType = "m5.xlarge"
AWSWorkerInstanceType InstanceType = "m5.xlarge"
)

type AWSNodeSpec struct {
// +optional
IAMInstanceProfile *IAMInstanceProfile `json:"iamInstanceProfile,omitempty"`
Expand Down Expand Up @@ -49,21 +56,38 @@ func (AdditionalSecurityGroup) VariableSchema() clusterv1.VariableSchema {
}
}

func (AWSNodeSpec) VariableSchema() clusterv1.VariableSchema {
func (a AWSNodeSpec) VariableSchema() clusterv1.VariableSchema {
instanceType := InstanceType("")
if a.InstanceType != nil {
instanceType = *a.InstanceType
}

return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Description: "AWS Node configuration",
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"iamInstanceProfile": IAMInstanceProfile("").VariableSchema().OpenAPIV3Schema,
"instanceType": InstanceType("").VariableSchema().OpenAPIV3Schema,
"instanceType": instanceType.VariableSchema().OpenAPIV3Schema,
"ami": AMISpec{}.VariableSchema().OpenAPIV3Schema,
"additionalSecurityGroups": AdditionalSecurityGroup{}.VariableSchema().OpenAPIV3Schema,
},
},
}
}

func AWSControlPlaneNodeSpec() *AWSNodeSpec {
return &AWSNodeSpec{
InstanceType: ptr.To(AWSControlPlaneInstanceType),
}
}

func AWSWorkerNodeSpec() *AWSNodeSpec {
return &AWSNodeSpec{
InstanceType: ptr.To(AWSWorkerInstanceType),
}
}

type IAMInstanceProfile string

func (IAMInstanceProfile) VariableSchema() clusterv1.VariableSchema {
Expand All @@ -77,11 +101,24 @@ func (IAMInstanceProfile) VariableSchema() clusterv1.VariableSchema {

type InstanceType string

func (InstanceType) VariableSchema() clusterv1.VariableSchema {
func (i InstanceType) VariableSchema() clusterv1.VariableSchema {
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "string",
Description: "The AWS instance type to use for the cluster Machines",
Default: variables.MustMarshal(string(i)),
},
}
}

type ControlPlaneInstanceType string

func (ControlPlaneInstanceType) VariableSchema() clusterv1.VariableSchema {
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "string",
Description: "The AWS instance type to use for the cluster Machines",
Default: variables.MustMarshal("m5.xlarge"),
},
}
}
Expand Down
19 changes: 14 additions & 5 deletions api/v1alpha1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ type ClusterConfigSpec struct {

func (s ClusterConfigSpec) VariableSchema() clusterv1.VariableSchema { //nolint:gocritic,lll // Passed by value for no potential side-effect.
clusterConfigProps := GenericClusterConfig{}.VariableSchema()

if s.ControlPlane == nil {
s.ControlPlane = &NodeConfigSpec{}
}
switch {
case s.AWS != nil:
maps.Copy(
clusterConfigProps.OpenAPIV3Schema.Properties,
map[string]clusterv1.JSONSchemaProps{
AWSVariableName: AWSSpec{}.VariableSchema().OpenAPIV3Schema,
"controlPlane": NodeConfigSpec{
AWS: &AWSNodeSpec{},
}.VariableSchema().OpenAPIV3Schema,
AWSVariableName: s.AWS.VariableSchema().OpenAPIV3Schema,
"controlPlane": s.ControlPlane.VariableSchema().OpenAPIV3Schema,
},
)
case s.Docker != nil:
Expand Down Expand Up @@ -94,6 +94,15 @@ func (s ClusterConfigSpec) VariableSchema() clusterv1.VariableSchema { //nolint:
return clusterConfigProps
}

func DefaultAWSClusterConfigSpec() *ClusterConfigSpec {
return &ClusterConfigSpec{
AWS: &AWSSpec{},
ControlPlane: &NodeConfigSpec{
AWS: AWSControlPlaneNodeSpec(),
},
}
}

// GenericClusterConfig defines the generic cluster configdesired.
type GenericClusterConfig struct {
// +optional
Expand Down
8 changes: 7 additions & 1 deletion api/v1alpha1/node_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s NodeConfigSpec) VariableSchema() clusterv1.VariableSchema {
maps.Copy(
nodeConfigProps.OpenAPIV3Schema.Properties,
map[string]clusterv1.JSONSchemaProps{
AWSVariableName: AWSNodeSpec{}.VariableSchema().OpenAPIV3Schema,
AWSVariableName: s.AWS.VariableSchema().OpenAPIV3Schema,
},
)
case s.Docker != nil:
Expand All @@ -63,6 +63,12 @@ func (s NodeConfigSpec) VariableSchema() clusterv1.VariableSchema {
return nodeConfigProps
}

func DefaultAWSWorkerConfigSpec() *NodeConfigSpec {
return &NodeConfigSpec{
AWS: AWSWorkerNodeSpec(),
}
}

type GenericNodeConfig struct{}

func (GenericNodeConfig) VariableSchema() clusterv1.VariableSchema {
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/aws/clusterconfig/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (h *awsClusterConfigVariableHandler) DiscoverVariables(
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{
Name: clusterconfig.MetaVariableName,
Required: true,
Schema: v1alpha1.ClusterConfigSpec{AWS: &v1alpha1.AWSSpec{}}.VariableSchema(),
Schema: v1alpha1.DefaultAWSClusterConfigSpec().VariableSchema(),
})
resp.SetStatus(runtimehooksv1.ResponseStatusSuccess)
}
2 changes: 1 addition & 1 deletion pkg/handlers/aws/mutation/instancetype/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestVariableValidation(t *testing.T) {
capitest.ValidateDiscoverVariables(
t,
clusterconfig.MetaVariableName,
ptr.To(v1alpha1.ClusterConfigSpec{AWS: &v1alpha1.AWSSpec{}}.VariableSchema()),
ptr.To(v1alpha1.DefaultAWSClusterConfigSpec().VariableSchema()),
true,
awsclusterconfig.NewVariable,
capitest.VariableTestDef{
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/aws/workerconfig/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (h *awsWorkerConfigVariableHandler) DiscoverVariables(
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{
Name: workerconfig.MetaVariableName,
Required: false,
Schema: v1alpha1.NodeConfigSpec{AWS: &v1alpha1.AWSNodeSpec{}}.VariableSchema(),
Schema: v1alpha1.DefaultAWSWorkerConfigSpec().VariableSchema(),
})
resp.SetStatus(runtimehooksv1.ResponseStatusSuccess)
}
2 changes: 1 addition & 1 deletion pkg/handlers/aws/workerconfig/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestVariableValidation(t *testing.T) {
capitest.ValidateDiscoverVariables(
t,
workerconfig.MetaVariableName,
ptr.To(v1alpha1.NodeConfigSpec{AWS: &v1alpha1.AWSNodeSpec{}}.VariableSchema()),
ptr.To(v1alpha1.DefaultAWSWorkerConfigSpec().VariableSchema()),
false,
NewVariable,
capitest.VariableTestDef{
Expand Down

0 comments on commit 3f673f9

Please sign in to comment.