-
Notifications
You must be signed in to change notification settings - Fork 583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add instance details to AWSMachine, enables more than one instance type for instance creation #4112
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,8 +45,12 @@ func (r *AWSMachineTemplateWebhook) SetupWebhookWithManager(mgr ctrl.Manager) er | |
type AWSMachineTemplateWebhook struct{} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-awsmachinetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsmachinetemplates,versions=v1beta2,name=validation.awsmachinetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 | ||
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-awsmachinetemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=awsmachinetemplate,versions=v1beta2,name=mawsmachinetemplate.kb.io,name=mutation.awsmachinetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 | ||
|
||
var _ webhook.CustomValidator = &AWSMachineTemplateWebhook{} | ||
var ( | ||
_ webhook.CustomValidator = &AWSMachineTemplateWebhook{} | ||
_ webhook.Defaulter = &AWSMachineTemplate{} | ||
) | ||
|
||
func (r *AWSMachineTemplate) validateRootVolume() field.ErrorList { | ||
var allErrs field.ErrorList | ||
|
@@ -226,6 +230,12 @@ func (r *AWSMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw r | |
|
||
var allErrs field.ErrorList | ||
|
||
// allow limited changes to instanceDetails if we are defaulting and the field was missing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like I saw this sentence already... :D |
||
// because we know what the field should contain | ||
if len(oldAWSMachineTemplate.Spec.Template.Spec.InstanceDetails) == 0 && len(newAWSMachineTemplate.Spec.Template.Spec.InstanceDetails) == 1 { | ||
oldAWSMachineTemplate.Default() | ||
} | ||
|
||
if !topology.ShouldSkipImmutabilityChecks(req, newAWSMachineTemplate) && !cmp.Equal(newAWSMachineTemplate.Spec, oldAWSMachineTemplate.Spec) { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec"), newAWSMachineTemplate, "AWSMachineTemplate.Spec is immutable")) | ||
} | ||
|
@@ -237,3 +247,16 @@ func (r *AWSMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw r | |
func (r *AWSMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) error { | ||
return nil | ||
} | ||
|
||
// Default implements webhook.Defaulter so that a mutating webhook will be registered for the type. | ||
func (r *AWSMachineTemplate) Default() { | ||
// always encode the instance type and spot market options into instance details if unset | ||
if len(r.Spec.Template.Spec.InstanceDetails) == 0 { | ||
r.Spec.Template.Spec.InstanceDetails = []AWSInstanceDetails{ | ||
{ | ||
InstanceType: r.Spec.Template.Spec.InstanceType, | ||
SpotMarketOptions: r.Spec.Template.Spec.SpotMarketOptions, | ||
}, | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,36 @@ func TestAWSMachineTemplateValidateUpdate(t *testing.T) { | |
}, | ||
wantError: true, | ||
}, | ||
{ | ||
name: "allow setting instance details to the same instance types", | ||
modifiedTemplate: &AWSMachineTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{}, | ||
Spec: AWSMachineTemplateSpec{ | ||
Template: AWSMachineTemplateResource{ | ||
Spec: AWSMachineSpec{ | ||
InstanceType: "test", | ||
InstanceDetails: []AWSInstanceDetails{{InstanceType: "test"}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantError: false, | ||
}, | ||
{ | ||
name: "disallow setting instance details to the new instance types", | ||
modifiedTemplate: &AWSMachineTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{}, | ||
Spec: AWSMachineTemplateSpec{ | ||
Template: AWSMachineTemplateResource{ | ||
Spec: AWSMachineSpec{ | ||
InstanceType: "test", | ||
InstanceDetails: []AWSInstanceDetails{{InstanceType: "test2"}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantError: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
@@ -128,7 +158,6 @@ func TestAWSMachineTemplateValidateUpdate(t *testing.T) { | |
Spec: AWSMachineTemplateSpec{ | ||
Template: AWSMachineTemplateResource{ | ||
Spec: AWSMachineSpec{ | ||
CloudInit: CloudInit{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh..? This isn't needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I discovered #4116 working on this issue, and in fixing it, we don't need the CloudInit struct anymore, we removed the previous legacy code path for cloudinit in the awsmachinewebhook validateUpdate handler. |
||
InstanceType: "test", | ||
}, | ||
}, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Errr, correct me if I'm wrong, but if the previous thing changes something in the machine spec because of the re-order, shouldn't this thing fail here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow your question, but I did catch an issue in reviewing this part of the webhook and refactored the logic to used here to try and match the style of the awsmachinetemplate webhook. I also added some explicit awsmachine_webhook tests.