-
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 IAM instance profile patch
- Loading branch information
Showing
19 changed files
with
694 additions
and
71 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
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 = "IAM Instance Profile" | ||
+++ | ||
|
||
The IAM instance profile 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 custom image, use the following configuration: | ||
|
||
```yaml | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Cluster | ||
metadata: | ||
name: <NAME> | ||
spec: | ||
topology: | ||
variables: | ||
- name: clusterConfig | ||
value: | ||
controlPlane: | ||
aws: | ||
iamInstanceProfile: custom-control-plane.cluster-api-provider-aws.sigs.k8s.io | ||
- name: workerConfig | ||
value: | ||
aws: | ||
iamInstanceProfile: custom-nodes.cluster-api-provider-aws.sigs.k8s.io | ||
``` | ||
|
||
Applying this configuration will result in the following value being set: | ||
|
||
- control-plane `AWSMachineTemplte`: | ||
|
||
- ```yaml | ||
spec: | ||
template: | ||
spec: | ||
iamInstanceProfile: custom-control-plane.cluster-api-provider-aws.sigs.k8s.io | ||
``` | ||
|
||
- worker `AWSMachineTemplte`: | ||
|
||
- ```yaml | ||
spec: | ||
template: | ||
spec: | ||
iamInstanceProfile: custom-nodes.cluster-api-provider-aws.sigs.k8s.io | ||
``` |
133 changes: 133 additions & 0 deletions
133
pkg/handlers/aws/mutation/iaminstanceprofile/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 iaminstanceprofile | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
|
||
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" | ||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers" | ||
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig" | ||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig" | ||
) | ||
|
||
const ( | ||
// ControlPlaneHandlerNamePatch is the name of the inject handler. | ||
ControlPlaneHandlerNamePatch = "AWSIAMInstanceProfileControlPlanePatch" | ||
) | ||
|
||
type awsIAMInstanceProfileControlPlanePatchHandler struct { | ||
variableName string | ||
variableFieldPath []string | ||
} | ||
|
||
var ( | ||
_ commonhandlers.Named = &awsIAMInstanceProfileControlPlanePatchHandler{} | ||
_ mutation.GeneratePatches = &awsIAMInstanceProfileControlPlanePatchHandler{} | ||
_ mutation.MetaMutator = &awsIAMInstanceProfileControlPlanePatchHandler{} | ||
) | ||
|
||
func NewControlPlanePatch() *awsIAMInstanceProfileControlPlanePatchHandler { | ||
return newAWSIAMInstanceProfileControlPlanePatchHandler(VariableName) | ||
} | ||
|
||
func NewControlPlaneMetaPatch() *awsIAMInstanceProfileControlPlanePatchHandler { | ||
return newAWSIAMInstanceProfileControlPlanePatchHandler( | ||
clusterconfig.MetaVariableName, | ||
clusterconfig.MetaControlPlaneConfigName, | ||
awsclusterconfig.AWSVariableName, | ||
VariableName, | ||
) | ||
} | ||
|
||
func newAWSIAMInstanceProfileControlPlanePatchHandler( | ||
variableName string, | ||
variableFieldPath ...string, | ||
) *awsIAMInstanceProfileControlPlanePatchHandler { | ||
return &awsIAMInstanceProfileControlPlanePatchHandler{ | ||
variableName: variableName, | ||
variableFieldPath: variableFieldPath, | ||
} | ||
} | ||
|
||
func (h *awsIAMInstanceProfileControlPlanePatchHandler) Name() string { | ||
return ControlPlaneHandlerNamePatch | ||
} | ||
|
||
func (h *awsIAMInstanceProfileControlPlanePatchHandler) 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, | ||
) | ||
|
||
iamInstanceProfileVar, found, err := variables.Get[v1alpha1.IAMInstanceProfile]( | ||
vars, | ||
h.variableName, | ||
h.variableFieldPath..., | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
log.V(5).Info("AWS IAM instance profile variable for control-plane not defined") | ||
return nil | ||
} | ||
|
||
log = log.WithValues( | ||
"variableName", | ||
h.variableName, | ||
"variableFieldPath", | ||
h.variableFieldPath, | ||
"variableValue", | ||
iamInstanceProfileVar, | ||
) | ||
|
||
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 IAM instance profile in control plane AWSMachineTemplate spec") | ||
|
||
obj.Spec.Template.Spec.IAMInstanceProfile = string(iamInstanceProfileVar) | ||
|
||
return nil | ||
}, | ||
) | ||
} | ||
|
||
func (h *awsIAMInstanceProfileControlPlanePatchHandler) GeneratePatches( | ||
ctx context.Context, | ||
req *runtimehooksv1.GeneratePatchesRequest, | ||
resp *runtimehooksv1.GeneratePatchesResponse, | ||
) { | ||
handlers.GeneratePatches(ctx, req, resp, apis.CAPADecoder(), h.Mutate) | ||
} |
21 changes: 21 additions & 0 deletions
21
pkg/handlers/aws/mutation/iaminstanceprofile/inject_control_plane_test.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,21 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package iaminstanceprofile | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation" | ||
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/iaminstanceprofile/tests" | ||
) | ||
|
||
func TestControlPlaneGeneratePatches(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests.TestControlPlaneGeneratePatches( | ||
t, | ||
func() mutation.GeneratePatches { return NewControlPlanePatch() }, | ||
VariableName, | ||
) | ||
} |
Oops, something went wrong.