Skip to content

Commit 8d4207b

Browse files
Shalin Pateljimmidyson
Shalin Patel
andauthored
feat: variables and patches for AWS AMI spec (#225)
Co-authored-by: Jimmi Dyson <[email protected]>
1 parent 0939eea commit 8d4207b

File tree

16 files changed

+646
-59
lines changed

16 files changed

+646
-59
lines changed

api/v1alpha1/aws_clusterconfig_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
type AWSSpec struct {
11+
// AWS region to create cluster in.
1112
// +optional
1213
Region *Region `json:"region,omitempty"`
1314
}

api/v1alpha1/aws_node_types.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33

44
package v1alpha1
55

6-
import clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
6+
import (
7+
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
8+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
9+
)
710

811
type AWSNodeSpec struct {
912
// +optional
1013
IAMInstanceProfile *IAMInstanceProfile `json:"iamInstanceProfile,omitempty"`
1114

1215
// +optional
1316
InstanceType *InstanceType `json:"instanceType,omitempty"`
17+
18+
// AMI or AMI Lookup arguments for machine image of a AWS machine.
19+
// If both AMI ID and AMI lookup arguments are provided then AMI ID takes precedence
20+
//+optional
21+
AMISpec *AMISpec `json:"ami,omitempty"`
1422
}
1523

1624
func (AWSNodeSpec) VariableSchema() clusterv1.VariableSchema {
@@ -21,6 +29,7 @@ func (AWSNodeSpec) VariableSchema() clusterv1.VariableSchema {
2129
Properties: map[string]clusterv1.JSONSchemaProps{
2230
"iamInstanceProfile": IAMInstanceProfile("").VariableSchema().OpenAPIV3Schema,
2331
"instanceType": InstanceType("").VariableSchema().OpenAPIV3Schema,
32+
"ami": AMISpec{}.VariableSchema().OpenAPIV3Schema,
2433
},
2534
},
2635
}
@@ -47,3 +56,69 @@ func (InstanceType) VariableSchema() clusterv1.VariableSchema {
4756
},
4857
}
4958
}
59+
60+
type AMISpec struct {
61+
// ID is an explicit AMI to use.
62+
// +optional
63+
ID string `json:"id,omitempty"`
64+
65+
// Lookup is the lookup arguments for the AMI.
66+
// +optional
67+
Lookup *AMILookup `json:"lookup,omitempty"`
68+
}
69+
70+
func (AMISpec) VariableSchema() clusterv1.VariableSchema {
71+
return clusterv1.VariableSchema{
72+
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
73+
Type: "object",
74+
Default: &v1.JSON{},
75+
Description: "AMI or AMI Lookup arguments for machine image of a AWS machine." +
76+
"If both AMI ID and AMI lookup arguments are provided then AMI ID takes precedence",
77+
Properties: map[string]clusterv1.JSONSchemaProps{
78+
"id": {
79+
Type: "string",
80+
Description: "AMI ID is the reference to the AMI from which to create the machine instance.",
81+
},
82+
"lookup": AMILookup{}.VariableSchema().OpenAPIV3Schema,
83+
},
84+
},
85+
}
86+
}
87+
88+
type AMILookup struct {
89+
// Format is the AMI naming format
90+
// +optional
91+
Format string `json:"format,omitempty"`
92+
93+
// Org is the AWS Organization ID to use for image lookup
94+
// +optional
95+
Org string `json:"org,omitempty"`
96+
97+
// BaseOS is the name of the base os for image lookup
98+
// +optional
99+
BaseOS string `json:"baseOS,omitempty"`
100+
}
101+
102+
func (AMILookup) VariableSchema() clusterv1.VariableSchema {
103+
return clusterv1.VariableSchema{
104+
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
105+
Type: "object",
106+
Default: &v1.JSON{},
107+
Properties: map[string]clusterv1.JSONSchemaProps{
108+
"format": {
109+
Type: "string",
110+
Description: "AMI naming format. Supports substitutions for {{.BaseOS}} and {{.K8sVersion}} with the" +
111+
"base OS and kubernetes version. example: capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*",
112+
},
113+
"org": {
114+
Type: "string",
115+
Description: "The AWS Organization ID to use for image lookup",
116+
},
117+
"baseOS": {
118+
Type: "string",
119+
Description: "The name of the base os for image lookup",
120+
},
121+
},
122+
},
123+
}
124+
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/pkg/capi/clustertopology/patches/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func MutateIfApplicable[T runtime.Object](
7474
return fmt.Errorf("failed to serialize unmodified typed object: %w", err)
7575
}
7676
if err := serializer.Encode(typed, &modifiedTypedBuf); err != nil {
77-
return fmt.Errorf("failed to serialize unmodified typed object: %w", err)
77+
return fmt.Errorf("failed to serialize modified typed object: %w", err)
7878
}
7979

8080
jsonOps, err := jsonpatch.CreatePatch(unmodifiedTypedBuf.Bytes(), modifiedTypedBuf.Bytes())

docs/content/customization/aws/ami.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
+++
2+
title = "AWS AMI ID and Format spec"
3+
+++
4+
5+
The AWS AMI customization allows the user to specify the AMI or AMI Lookup arguments for a AWS machine.
6+
The AMI customization can be applied to both control plane and nodepool machines.
7+
This customization will be available when the
8+
[provider-specific cluster configuration patch]({{< ref "..">}}) is included in the `ClusterClass`.
9+
10+
## Example
11+
12+
To specify the AMI ID or format for all control plane and nodepools, use the following configuration:
13+
14+
```yaml
15+
apiVersion: cluster.x-k8s.io/v1beta1
16+
kind: Cluster
17+
metadata:
18+
name: <NAME>
19+
spec:
20+
topology:
21+
variables:
22+
- name: clusterConfig
23+
value:
24+
controlPlane:
25+
aws:
26+
ami:
27+
# Specify one of id or lookup.
28+
id: "ami-controlplane"
29+
# lookup:
30+
# format: "my-cp-ami-{{.BaseOS}}-?{{.K8sVersion}}-*"
31+
# org: "123456789"
32+
# baseOS: "ubuntu-20.04"
33+
- name: workerConfig
34+
value:
35+
aws:
36+
ami:
37+
# Specify one of id or lookup.
38+
id: "ami-allWorkers"
39+
# lookup:
40+
# format: "my-default-workers-ami-{{.BaseOS}}-?{{.K8sVersion}}-*"
41+
# org: "123456789"
42+
# baseOS: "ubuntu-20.04"
43+
```
44+
45+
We can further customize individual MachineDeployments by using the overrides field with the following configuration:
46+
47+
```yaml
48+
spec:
49+
topology:
50+
# ...
51+
workers:
52+
machineDeployments:
53+
- class: default-worker
54+
name: md-0
55+
variables:
56+
overrides:
57+
- name: workerConfig
58+
value:
59+
ami:
60+
# Specify one of id or lookup.
61+
id: "ami-customWorker"
62+
# lookup:
63+
# format: "gpu-workers-ami-{{.BaseOS}}-?{{.K8sVersion}}-*"
64+
# org: "123456789"
65+
# baseOS: "ubuntu-20.04"
66+
```
67+
68+
Applying this configuration will result in the following value being set:
69+
70+
- control-plane `AWSMachineTemplate`:
71+
72+
- ```yaml
73+
spec:
74+
template:
75+
spec:
76+
ami: ami-controlplane
77+
# lookupFormat: "my-default-workers-ami-{{.BaseOS}}-?{{.K8sVersion}}-*"
78+
# lookupOrg: "123456789"
79+
# lookupBaseOS: "ubuntu-20.04"
80+
```
81+
82+
- worker `AWSMachineTemplate`:
83+
84+
- ```yaml
85+
spec:
86+
template:
87+
spec:
88+
ami: ami-customWorker
89+
# lookupFormat: "gpu-workers-ami-{{.BaseOS}}-?{{.K8sVersion}}-*"
90+
# lookupOrg: "123456789"
91+
# lookupBaseOS: "ubuntu-20.04"
92+
```

examples/capi-quick-start/aws-cluster-ami.yaml

Lines changed: 0 additions & 52 deletions
This file was deleted.

pkg/handlers/aws/clusterconfig/variables_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,22 @@ func TestVariableValidation(t *testing.T) {
5252
},
5353
},
5454
},
55+
capitest.VariableTestDef{
56+
Name: "AMI specification",
57+
Vals: v1alpha1.ClusterConfigSpec{
58+
ControlPlane: &v1alpha1.NodeConfigSpec{
59+
AWS: &v1alpha1.AWSNodeSpec{
60+
AMISpec: &v1alpha1.AMISpec{
61+
ID: "ami-1234",
62+
Lookup: &v1alpha1.AMILookup{
63+
Format: "capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*",
64+
BaseOS: "rhel-8.4",
65+
Org: "12345678",
66+
},
67+
},
68+
},
69+
},
70+
},
71+
},
5572
)
5673
}

0 commit comments

Comments
 (0)