Skip to content

Commit

Permalink
feat: add VPC ID and Subnet IDs patch (#220)
Browse files Browse the repository at this point in the history
Fixes #201
Fixes #200

Depends on #219

Tested manually (at-least that the values are set in `AWSCluster`)
```
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
  name: <NAME>
spec:
  topology:
    variables:
      - name: clusterConfig
        value:
          aws:
            network:
              vpc:
                id: vpc-1234567890
              subnets:
                - id: subnet-1
                - id: subnet-2
                - id: subnet-3
```

This is what the `AWSCluster` looked like:
```
  spec:
    network:
      subnets:
      - id: subnet-1
        isPublic: false
      - id: subnet-2
        isPublic: false
      - id: subnet-3
        isPublic: false
      vpc:
        availabilityZoneSelection: Ordered
        availabilityZoneUsageLimit: 3
        id: vpc-1234567890
    region: us-west-2
```
  • Loading branch information
dkoshkin committed Oct 17, 2023
1 parent f1ba6bb commit 349bf97
Show file tree
Hide file tree
Showing 25 changed files with 550 additions and 46 deletions.
83 changes: 81 additions & 2 deletions api/v1alpha1/aws_clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type AWSSpec struct {
// AWS region to create cluster in.
// +optional
Region *Region `json:"region,omitempty"`
// +optional
Network *AWSNetwork `json:"network,omitempty"`
}

func (AWSSpec) VariableSchema() clusterv1.VariableSchema {
Expand All @@ -19,7 +21,8 @@ func (AWSSpec) VariableSchema() clusterv1.VariableSchema {
Description: "AWS cluster configuration",
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"region": Region("").VariableSchema().OpenAPIV3Schema,
"region": Region("").VariableSchema().OpenAPIV3Schema,
"network": AWSNetwork{}.VariableSchema().OpenAPIV3Schema,
},
},
}
Expand All @@ -30,8 +33,84 @@ type Region string
func (Region) VariableSchema() clusterv1.VariableSchema {
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "string",
Description: "AWS region to create cluster in",
Type: "string",
},
}
}

type AWSNetwork struct {
// +optional
VPC *VPC `json:"vpc,omitempty"`

// +optional
Subnets Subnets `json:"subnets,omitempty"`
}

func (AWSNetwork) VariableSchema() clusterv1.VariableSchema {
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Description: "AWS network configuration",
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"vpc": VPC{}.VariableSchema().OpenAPIV3Schema,
"subnets": Subnets{}.VariableSchema().OpenAPIV3Schema,
},
},
}
}

type VPC struct {
// ID is the vpc-id of the VPC this provider should use to create resources.
ID string `json:"id,omitempty"`
}

func (VPC) VariableSchema() clusterv1.VariableSchema {
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Description: "AWS VPC configuration",
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"id": {
Description: "Existing VPC ID to use for the cluster",
Type: "string",
},
},
},
}
}

type Subnets []SubnetSpec

func (Subnets) VariableSchema() clusterv1.VariableSchema {
resourceSchema := SubnetSpec{}.VariableSchema().OpenAPIV3Schema

return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Description: "AWS Subnet configurations",
Type: "array",
Items: &resourceSchema,
},
}
}

// SubnetSpec configures an AWS Subnet.
type SubnetSpec struct {
// ID defines a unique identifier to reference this resource.
ID string `json:"id"`
}

func (SubnetSpec) VariableSchema() clusterv1.VariableSchema {
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Description: "An AWS Subnet configuration",
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"id": {
Description: "Existing Subnet ID to use for the cluster",
Type: "string",
},
},
},
}
}
2 changes: 1 addition & 1 deletion api/v1alpha1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s ClusterConfigSpec) VariableSchema() clusterv1.VariableSchema { //nolint:
maps.Copy(
clusterConfigProps.OpenAPIV3Schema.Properties,
map[string]clusterv1.JSONSchemaProps{
"aws": AWSSpec{}.VariableSchema().OpenAPIV3Schema,
AWSVariableName: AWSSpec{}.VariableSchema().OpenAPIV3Schema,
"controlPlane": NodeConfigSpec{
AWS: &AWSNodeSpec{},
}.VariableSchema().OpenAPIV3Schema,
Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ package v1alpha1
const (
// CNIVariableName is the external patch variable name.
CNIVariableName = "cni"
// AWSVariableName is the AWS config patch variable name.
AWSVariableName = "aws"
)
2 changes: 1 addition & 1 deletion api/v1alpha1/node_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s NodeConfigSpec) VariableSchema() clusterv1.VariableSchema {
maps.Copy(
nodeConfigProps.OpenAPIV3Schema.Properties,
map[string]clusterv1.JSONSchemaProps{
"aws": AWSNodeSpec{}.VariableSchema().OpenAPIV3Schema,
AWSVariableName: AWSNodeSpec{}.VariableSchema().OpenAPIV3Schema,
},
)
case s.Docker != nil:
Expand Down
79 changes: 79 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

65 changes: 65 additions & 0 deletions docs/content/customization/aws/network.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
+++
title = "Network"
+++

The network customization allows the user to specify existing infrastructure to use for the cluster.

This customization will be available when the
[provider-specific cluster configuration patch]({{< ref "..">}}) is included in the `ClusterClass`.

## Example

To specify existing AWS VPC, use the following configuration:

```yaml
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: <NAME>
spec:
topology:
variables:
- name: clusterConfig
value:
aws:
network:
vpc:
id: vpc-1234567890
```

To also specify existing AWS Subnets, use the following configuration:

```yaml
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: <NAME>
spec:
topology:
variables:
- name: clusterConfig
value:
aws:
network:
vpc:
id: vpc-1234567890
subnets:
- id: subnet-1
- id: subnet-2
- id: subnet-3
```

Applying this configuration will result in the following value being set:

- `AWSClusterTemplate`:

- ```yaml
spec:
network:
subnets:
- id: subnet-1
- id: subnet-2
- id: subnet-3
vpc:
id: vpc-1234567890
```
3 changes: 0 additions & 3 deletions pkg/handlers/aws/clusterconfig/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ var (
const (
// HandlerNameVariable is the name of the variable handler.
HandlerNameVariable = "AWSClusterConfigVars"

// AWSVariableName is the AWS config patch variable name.
AWSVariableName = "aws"
)

func NewVariable() *awsClusterConfigVariableHandler {
Expand Down
6 changes: 2 additions & 4 deletions pkg/handlers/aws/mutation/ami/inject_control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
package ami

import (
_ "embed"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

Expand All @@ -17,7 +15,7 @@ func NewControlPlanePatch() *awsAMISpecPatchHandler {
clusterconfig.MetaVariableName,
[]string{
clusterconfig.MetaControlPlaneConfigName,
awsclusterconfig.AWSVariableName,
v1alpha1.AWSVariableName,
VariableName,
},
selectors.InfrastructureControlPlaneMachines(
Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/aws/mutation/ami/inject_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
package ami

import (
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/workerconfig"
)

func NewWorkerPatch() *awsAMISpecPatchHandler {
return newAWSAMISpecPatchHandler(
workerconfig.MetaVariableName,
[]string{
awsclusterconfig.AWSVariableName,
v1alpha1.AWSVariableName,
VariableName,
},
selectors.InfrastructureWorkerMachineTemplates(
Expand Down
1 change: 0 additions & 1 deletion pkg/handlers/aws/mutation/cni/calico/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package calico

import (
"context"
_ "embed"
"slices"

"github.com/go-logr/logr"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package iaminstanceprofile

import (
"context"
_ "embed"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -18,7 +17,6 @@ import (
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

Expand All @@ -36,7 +34,7 @@ func NewControlPlanePatch() *awsIAMInstanceProfileControlPlanePatchHandler {
return newAWSIAMInstanceProfileControlPlanePatchHandler(
clusterconfig.MetaVariableName,
clusterconfig.MetaControlPlaneConfigName,
awsclusterconfig.AWSVariableName,
v1alpha1.AWSVariableName,
VariableName,
)
}
Expand Down
Loading

0 comments on commit 349bf97

Please sign in to comment.