Skip to content

Commit 258b768

Browse files
surajssdDongsu Park
authored andcommitted
api/v1beta1: add Ignition field to AWSMachineSpec
This commit adds new Ignition block to AWSMachineSpec struct, which will allow different way of handling user data. If either bootstrap data has format defined as Ignition or user explicitly specify to use Ignition as a bootstrap format, machine controller will handle things accordingly. Co-authored-by: Dongsu Park <[email protected]> Signed-off-by: Mateusz Gozdek <[email protected]>
1 parent aa4f25e commit 258b768

8 files changed

+111
-3
lines changed

api/v1alpha3/awsmachine_conversion.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func (r *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
4040

4141
restoreSpec(&restored.Spec, &dst.Spec)
4242

43+
dst.Spec.Ignition = restored.Spec.Ignition
44+
4345
return nil
4446
}
4547

@@ -100,6 +102,7 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
100102
}
101103

102104
dst.Spec.Template.ObjectMeta = restored.Spec.Template.ObjectMeta
105+
dst.Spec.Template.Spec.Ignition = restored.Spec.Template.Spec.Ignition
103106

104107
restoreSpec(&restored.Spec.Template.Spec, &dst.Spec.Template.Spec)
105108

api/v1alpha3/zz_generated.conversion.go

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

api/v1alpha4/awsmachine_conversion.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1alpha4
1818

1919
import (
2020
apiconversion "k8s.io/apimachinery/pkg/conversion"
21+
"sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
2122
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
2223
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2324
"sigs.k8s.io/controller-runtime/pkg/conversion"
@@ -26,14 +27,31 @@ import (
2627
// ConvertTo converts the v1alpha4 AWSMachine receiver to a v1beta1 AWSMachine.
2728
func (src *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
2829
dst := dstRaw.(*infrav1.AWSMachine)
29-
return Convert_v1alpha4_AWSMachine_To_v1beta1_AWSMachine(src, dst, nil)
30+
if err := Convert_v1alpha4_AWSMachine_To_v1beta1_AWSMachine(src, dst, nil); err != nil {
31+
return err
32+
}
33+
34+
// Manually restore data.
35+
restored := &v1beta1.AWSMachine{}
36+
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
37+
return err
38+
}
39+
40+
dst.Spec.Ignition = restored.Spec.Ignition
41+
42+
return nil
3043
}
3144

3245
// ConvertFrom converts the v1beta1 AWSMachine to a v1alpha4 AWSMachine.
3346
func (dst *AWSMachine) ConvertFrom(srcRaw conversion.Hub) error {
3447
src := srcRaw.(*infrav1.AWSMachine)
3548

36-
return Convert_v1beta1_AWSMachine_To_v1alpha4_AWSMachine(src, dst, nil)
49+
if err := Convert_v1beta1_AWSMachine_To_v1alpha4_AWSMachine(src, dst, nil); err != nil {
50+
return err
51+
}
52+
53+
// Preserve Hub data on down-conversion except for metadata.
54+
return utilconversion.MarshalData(src, dst)
3755
}
3856

3957
// ConvertTo converts the v1alpha4 AWSMachineList receiver to a v1beta1 AWSMachineList.
@@ -64,6 +82,7 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
6482
}
6583

6684
dst.Spec.Template.ObjectMeta = restored.Spec.Template.ObjectMeta
85+
dst.Spec.Template.Spec.Ignition = restored.Spec.Template.Spec.Ignition
6786

6887
return nil
6988
}
@@ -100,3 +119,7 @@ func (dst *AWSMachineTemplateList) ConvertFrom(srcRaw conversion.Hub) error {
100119
func Convert_v1beta1_AWSMachineTemplateResource_To_v1alpha4_AWSMachineTemplateResource(in *infrav1.AWSMachineTemplateResource, out *AWSMachineTemplateResource, s apiconversion.Scope) error {
101120
return autoConvert_v1beta1_AWSMachineTemplateResource_To_v1alpha4_AWSMachineTemplateResource(in, out, s)
102121
}
122+
123+
func Convert_v1beta1_AWSMachineSpec_To_v1alpha4_AWSMachineSpec(in *v1beta1.AWSMachineSpec, out *AWSMachineSpec, s apiconversion.Scope) error {
124+
return autoConvert_v1beta1_AWSMachineSpec_To_v1alpha4_AWSMachineSpec(in, out, s)
125+
}

api/v1beta1/awsmachine_types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const (
2727
// MachineFinalizer allows ReconcileAWSMachine to clean up AWS resources associated with AWSMachine before
2828
// removing it from the apiserver.
2929
MachineFinalizer = "awsmachine.infrastructure.cluster.x-k8s.io"
30+
31+
// DefaultIgnitionVersion represents default Ignition version generated for machine userdata.
32+
DefaultIgnitionVersion = "2.3"
3033
)
3134

3235
// SecretBackend defines variants for backend secret storage.
@@ -142,6 +145,10 @@ type AWSMachineSpec struct {
142145
// +optional
143146
CloudInit CloudInit `json:"cloudInit,omitempty"`
144147

148+
// Ignition defined options related to the bootstrapping systems where Ignition is used.
149+
// +optional
150+
Ignition *Ignition `json:"ignition,omitempty"`
151+
145152
// SpotMarketOptions allows users to configure instances to be run using AWS Spot instances.
146153
// +optional
147154
SpotMarketOptions *SpotMarketOptions `json:"spotMarketOptions,omitempty"`
@@ -179,6 +186,16 @@ type CloudInit struct {
179186
SecureSecretsBackend SecretBackend `json:"secureSecretsBackend,omitempty"`
180187
}
181188

189+
// Ignition defines options related to the bootstrapping systems where Ignition is used.
190+
type Ignition struct {
191+
// Version defines which version of Ignition will be used to generate bootstrap data.
192+
//
193+
// +optional
194+
// +kubebuilder:default="2.3"
195+
// +kubebuilder:validation:Enum="2.3"
196+
Version string `json:"version,omitempty"`
197+
}
198+
182199
// AWSMachineStatus defines the observed state of AWSMachine.
183200
type AWSMachineStatus struct {
184201
// Ready is true when the provider resource is ready.

api/v1beta1/awsmachine_webhook.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (r *AWSMachine) ValidateCreate() error {
5050
var allErrs field.ErrorList
5151

5252
allErrs = append(allErrs, r.validateCloudInitSecret()...)
53+
allErrs = append(allErrs, r.validateIgnitionAndCloudInit()...)
5354
allErrs = append(allErrs, r.validateRootVolume()...)
5455
allErrs = append(allErrs, r.validateNonRootVolumes()...)
5556
allErrs = append(allErrs, r.validateSSHKeyName()...)
@@ -140,6 +141,31 @@ func (r *AWSMachine) validateCloudInitSecret() field.ErrorList {
140141
return allErrs
141142
}
142143

144+
func (r *AWSMachine) cloudInitConfigured() bool {
145+
configured := false
146+
147+
configured = configured || r.Spec.CloudInit.SecretPrefix != ""
148+
configured = configured || r.Spec.CloudInit.SecretCount != 0
149+
configured = configured || r.Spec.CloudInit.SecureSecretsBackend != ""
150+
configured = configured || r.Spec.CloudInit.InsecureSkipSecretsManager
151+
152+
return configured
153+
}
154+
155+
func (r *AWSMachine) ignitionEnabled() bool {
156+
return r.Spec.Ignition != nil
157+
}
158+
159+
func (r *AWSMachine) validateIgnitionAndCloudInit() field.ErrorList {
160+
var allErrs field.ErrorList
161+
162+
if r.ignitionEnabled() && r.cloudInitConfigured() {
163+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "cloudInit"), "cannot be set if spec.ignition is set"))
164+
}
165+
166+
return allErrs
167+
}
168+
143169
func (r *AWSMachine) validateRootVolume() field.ErrorList {
144170
var allErrs field.ErrorList
145171

@@ -200,9 +226,17 @@ func (r *AWSMachine) ValidateDelete() error {
200226
// Default implements webhook.Defaulter such that an empty CloudInit will be defined with a default
201227
// SecureSecretsBackend as SecretBackendSecretsManager iff InsecureSkipSecretsManager is unset.
202228
func (r *AWSMachine) Default() {
203-
if !r.Spec.CloudInit.InsecureSkipSecretsManager && r.Spec.CloudInit.SecureSecretsBackend == "" {
229+
if !r.Spec.CloudInit.InsecureSkipSecretsManager && r.Spec.CloudInit.SecureSecretsBackend == "" && !r.ignitionEnabled() {
204230
r.Spec.CloudInit.SecureSecretsBackend = SecretBackendSecretsManager
205231
}
232+
233+
if r.ignitionEnabled() && r.Spec.Ignition.Version == "" {
234+
if r.Spec.Ignition == nil {
235+
r.Spec.Ignition = &Ignition{}
236+
}
237+
238+
r.Spec.Ignition.Version = DefaultIgnitionVersion
239+
}
206240
}
207241

208242
func (r *AWSMachine) validateAdditionalSecurityGroups() field.ErrorList {

api/v1beta1/awsmachinetemplate_webhook.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ func (r *AWSMachineTemplate) ValidateCreate() error {
113113
allErrs = append(allErrs, r.validateRootVolume()...)
114114
allErrs = append(allErrs, r.validateNonRootVolumes()...)
115115

116+
cloudInitConfigured := spec.CloudInit.SecureSecretsBackend != "" || spec.CloudInit.InsecureSkipSecretsManager
117+
if cloudInitConfigured && spec.Ignition != nil {
118+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "cloudInit"),
119+
"cannot be set if spec.template.spec.ignition is set"))
120+
}
121+
116122
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
117123
}
118124

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachines.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,18 @@ spec:
10851085
description: IAMInstanceProfile is a name of an IAM instance profile
10861086
to assign to the instance
10871087
type: string
1088+
ignition:
1089+
description: Ignition defined options related to the bootstrapping
1090+
systems where Ignition is used.
1091+
properties:
1092+
version:
1093+
default: "2.3"
1094+
description: Version defines which version of Ignition will be
1095+
used to generate bootstrap data.
1096+
enum:
1097+
- "2.3"
1098+
type: string
1099+
type: object
10881100
imageLookupBaseOS:
10891101
description: ImageLookupBaseOS is the name of the base operating system
10901102
to use for image lookup the AMI is not set.

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinetemplates.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,18 @@ spec:
891891
description: IAMInstanceProfile is a name of an IAM instance
892892
profile to assign to the instance
893893
type: string
894+
ignition:
895+
description: Ignition defined options related to the bootstrapping
896+
systems where Ignition is used.
897+
properties:
898+
version:
899+
default: "2.3"
900+
description: Version defines which version of Ignition
901+
will be used to generate bootstrap data.
902+
enum:
903+
- "2.3"
904+
type: string
905+
type: object
894906
imageLookupBaseOS:
895907
description: ImageLookupBaseOS is the name of the base operating
896908
system to use for image lookup the AMI is not set.

0 commit comments

Comments
 (0)