-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AWS instance type patch (#217)
Depends on #216 Tested manually: ``` - apiVersion: infrastructure.cluster.x-k8s.io/v1beta2 kind: AWSMachineTemplate metadata: name: aws-quick-start-control-plane-dlj45 namespace: default template: spec: iamInstanceProfile: control-plane.cluster-api-provider-aws.sigs.k8s.io instanceType: m5.2xlarge - apiVersion: infrastructure.cluster.x-k8s.io/v1beta2 kind: AWSMachineTemplate metadata name: aws-quick-start-md-0-infra-kpc8m namespace: default spec: template: spec: iamInstanceProfile: nodes.cluster-api-provider-aws.sigs.k8s.io instanceType: m5.4xlarge ```
- Loading branch information
Showing
12 changed files
with
463 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
+++ | ||
title = "Instance type" | ||
+++ | ||
|
||
The instance type customization allows the user to specify the profile to use for control-plane | ||
and worker Machines. | ||
|
||
This customization will be available when the | ||
[provider-specific cluster configuration patch]({{< ref "..">}}) is included in the `ClusterClass`. | ||
|
||
## Example | ||
|
||
To specify the instance type, use the following configuration: | ||
|
||
```yaml | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Cluster | ||
metadata: | ||
name: <NAME> | ||
spec: | ||
topology: | ||
variables: | ||
- name: clusterConfig | ||
value: | ||
controlPlane: | ||
aws: | ||
instanceType: m5.xlarge | ||
- name: workerConfig | ||
value: | ||
aws: | ||
instanceType: m5.2xlarge | ||
``` | ||
Applying this configuration will result in the following value being set: | ||
- control-plane `AWSMachineTemplate`: | ||
|
||
- ```yaml | ||
spec: | ||
template: | ||
spec: | ||
instanceType: m5.xlarge | ||
``` | ||
|
||
- worker `AWSMachineTemplate`: | ||
|
||
- ```yaml | ||
spec: | ||
template: | ||
spec: | ||
instanceType: m5.2xlarge | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
pkg/handlers/aws/mutation/instancetype/inject_control_plane.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package instancetype | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
|
||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/apis" | ||
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches" | ||
"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" | ||
) | ||
|
||
const ( | ||
// VariableName is the external patch variable name. | ||
VariableName = "instanceType" | ||
|
||
// WorkerHandlerNamePatch is the name of the inject handler. | ||
ControlPlaneHandlerNamePatch = "AWSInstanceTypeControlPlanePatch" | ||
) | ||
|
||
type awsInstanceTypeControlPlanePatchHandler struct { | ||
variableName string | ||
variableFieldPath []string | ||
} | ||
|
||
var ( | ||
_ commonhandlers.Named = &awsInstanceTypeControlPlanePatchHandler{} | ||
_ mutation.GeneratePatches = &awsInstanceTypeControlPlanePatchHandler{} | ||
_ mutation.MetaMutator = &awsInstanceTypeControlPlanePatchHandler{} | ||
) | ||
|
||
func NewControlPlaneMetaPatch() *awsInstanceTypeControlPlanePatchHandler { | ||
return newAWSInstanceTypeControlPlanePatchHandler( | ||
clusterconfig.MetaVariableName, | ||
clusterconfig.MetaControlPlaneConfigName, | ||
awsclusterconfig.AWSVariableName, | ||
VariableName, | ||
) | ||
} | ||
|
||
func newAWSInstanceTypeControlPlanePatchHandler( | ||
variableName string, | ||
variableFieldPath ...string, | ||
) *awsInstanceTypeControlPlanePatchHandler { | ||
return &awsInstanceTypeControlPlanePatchHandler{ | ||
variableName: variableName, | ||
variableFieldPath: variableFieldPath, | ||
} | ||
} | ||
|
||
func (h *awsInstanceTypeControlPlanePatchHandler) Name() string { | ||
return ControlPlaneHandlerNamePatch | ||
} | ||
|
||
func (h *awsInstanceTypeControlPlanePatchHandler) Mutate( | ||
ctx context.Context, | ||
obj runtime.Object, | ||
vars map[string]apiextensionsv1.JSON, | ||
holderRef runtimehooksv1.HolderReference, | ||
_ client.ObjectKey, | ||
) error { | ||
log := ctrl.LoggerFrom(ctx).WithValues( | ||
"holderRef", holderRef, | ||
) | ||
|
||
instanceTypeVar, found, err := variables.Get[v1alpha1.InstanceType]( | ||
vars, | ||
h.variableName, | ||
h.variableFieldPath..., | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
log.V(5).Info("AWS instance type variable for control-plane not defined") | ||
return nil | ||
} | ||
|
||
log = log.WithValues( | ||
"variableName", | ||
h.variableName, | ||
"variableFieldPath", | ||
h.variableFieldPath, | ||
"variableValue", | ||
instanceTypeVar, | ||
) | ||
|
||
return patches.Generate( | ||
obj, | ||
vars, | ||
&holderRef, | ||
selectors.InfrastructureControlPlaneMachines( | ||
"v1beta2", | ||
"AWSMachineTemplate", | ||
), | ||
log, | ||
func(obj *capav1.AWSMachineTemplate) error { | ||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", client.ObjectKeyFromObject(obj), | ||
).Info("setting instance type in control plane AWSMachineTemplate spec") | ||
|
||
obj.Spec.Template.Spec.InstanceType = string(instanceTypeVar) | ||
|
||
return nil | ||
}, | ||
) | ||
} | ||
|
||
func (h *awsInstanceTypeControlPlanePatchHandler) GeneratePatches( | ||
ctx context.Context, | ||
req *runtimehooksv1.GeneratePatchesRequest, | ||
resp *runtimehooksv1.GeneratePatchesResponse, | ||
) { | ||
handlers.GeneratePatches(ctx, req, resp, apis.CAPADecoder(), h.Mutate) | ||
} |
130 changes: 130 additions & 0 deletions
130
pkg/handlers/aws/mutation/instancetype/inject_worker.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package instancetype | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
|
||
awsworkerconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/workerconfig" | ||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/workerconfig" | ||
|
||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/apis" | ||
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches" | ||
"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" | ||
) | ||
|
||
const ( | ||
// WorkerHandlerNamePatch is the name of the inject handler. | ||
WorkerHandlerNamePatch = "AWSInstanceTypeWorkerPatch" | ||
) | ||
|
||
type awsInstanceTypeWorkerPatchHandler struct { | ||
variableName string | ||
variableFieldPath []string | ||
} | ||
|
||
var ( | ||
_ commonhandlers.Named = &awsInstanceTypeWorkerPatchHandler{} | ||
_ mutation.GeneratePatches = &awsInstanceTypeWorkerPatchHandler{} | ||
_ mutation.MetaMutator = &awsInstanceTypeWorkerPatchHandler{} | ||
) | ||
|
||
func NewWorkerMetaPatch() *awsInstanceTypeWorkerPatchHandler { | ||
return newAWSInstanceTypeWorkerPatchHandler( | ||
workerconfig.MetaVariableName, | ||
awsworkerconfig.AWSVariableName, | ||
VariableName, | ||
) | ||
} | ||
|
||
func newAWSInstanceTypeWorkerPatchHandler( | ||
variableName string, | ||
variableFieldPath ...string, | ||
) *awsInstanceTypeWorkerPatchHandler { | ||
return &awsInstanceTypeWorkerPatchHandler{ | ||
variableName: variableName, | ||
variableFieldPath: variableFieldPath, | ||
} | ||
} | ||
|
||
func (h *awsInstanceTypeWorkerPatchHandler) Name() string { | ||
return WorkerHandlerNamePatch | ||
} | ||
|
||
func (h *awsInstanceTypeWorkerPatchHandler) Mutate( | ||
ctx context.Context, | ||
obj runtime.Object, | ||
vars map[string]apiextensionsv1.JSON, | ||
holderRef runtimehooksv1.HolderReference, | ||
_ client.ObjectKey, | ||
) error { | ||
log := ctrl.LoggerFrom(ctx).WithValues( | ||
"holderRef", holderRef, | ||
) | ||
|
||
instanceTypeVar, found, err := variables.Get[v1alpha1.InstanceType]( | ||
vars, | ||
h.variableName, | ||
h.variableFieldPath..., | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
log.V(5).Info("AWS instance type variable for worker not defined") | ||
return nil | ||
} | ||
|
||
log = log.WithValues( | ||
"variableName", | ||
h.variableName, | ||
"variableFieldPath", | ||
h.variableFieldPath, | ||
"variableValue", | ||
instanceTypeVar, | ||
) | ||
|
||
return patches.Generate( | ||
obj, | ||
vars, | ||
&holderRef, | ||
selectors.InfrastructureWorkerMachineTemplates( | ||
"v1beta2", | ||
"AWSMachineTemplate", | ||
), | ||
log, | ||
func(obj *capav1.AWSMachineTemplate) error { | ||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", client.ObjectKeyFromObject(obj), | ||
).Info("setting instance type in workers AWSMachineTemplate spec") | ||
|
||
obj.Spec.Template.Spec.InstanceType = string(instanceTypeVar) | ||
|
||
return nil | ||
}, | ||
) | ||
} | ||
|
||
func (h *awsInstanceTypeWorkerPatchHandler) GeneratePatches( | ||
ctx context.Context, | ||
req *runtimehooksv1.GeneratePatchesRequest, | ||
resp *runtimehooksv1.GeneratePatchesResponse, | ||
) { | ||
handlers.GeneratePatches(ctx, req, resp, apis.CAPADecoder(), h.Mutate) | ||
} |
Oops, something went wrong.