Skip to content

Commit 4450fad

Browse files
committed
AWS dedicated host webhook updates
1 parent a41deb9 commit 4450fad

1 file changed

Lines changed: 55 additions & 44 deletions

File tree

pkg/webhooks/machine_webhook.go

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -831,19 +831,7 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st
831831
}
832832
}
833833

834-
switch providerSpec.Placement.Tenancy {
835-
case "", machinev1beta1.DefaultTenancy, machinev1beta1.DedicatedTenancy, machinev1beta1.HostTenancy:
836-
// Do nothing, valid values
837-
default:
838-
errs = append(
839-
errs,
840-
field.Invalid(
841-
field.NewPath("providerSpec", "tenancy"),
842-
providerSpec.Placement.Tenancy,
843-
fmt.Sprintf("Invalid providerSpec.tenancy, the only allowed options are: %s, %s, %s", machinev1beta1.DefaultTenancy, machinev1beta1.DedicatedTenancy, machinev1beta1.HostTenancy),
844-
),
845-
)
846-
}
834+
errs = append(errs, processAWSPlacementTenancy(providerSpec.Placement)...)
847835

848836
if providerSpec.PlacementGroupPartition != nil {
849837
partition := *providerSpec.PlacementGroupPartition
@@ -933,43 +921,66 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st
933921
}
934922
}
935923

936-
// Dedicated host support.
937-
// Check if host placement is configured. If so, then we need to determine placement affinity and validate configs.
938-
if providerSpec.HostPlacement != nil {
939-
klog.V(4).Infof("Validating AWS Host Placement")
940-
placement := *providerSpec.HostPlacement
941-
if placement.Affinity == nil {
942-
errs = append(errs, field.Required(field.NewPath("spec.hostPlacement.affinity"), "affinity is required and must be set to either AnyAvailable or DedicatedHost"))
943-
} else {
944-
switch *placement.Affinity {
945-
case machinev1beta1.HostAffinityAnyAvailable:
946-
// Cannot have DedicatedHost set
947-
if placement.DedicatedHost != nil {
948-
errs = append(errs, field.Forbidden(field.NewPath("spec.hostPlacement.dedicatedHost"), "dedicatedHost is required when affinity is DedicatedHost, and forbidden otherwise"))
949-
}
950-
case machinev1beta1.HostAffinityDedicatedHost:
951-
// We need to make sure DedicatedHost is set with a HostID
952-
if placement.DedicatedHost == nil {
953-
errs = append(errs, field.Required(field.NewPath("spec.hostPlacement.dedicatedHost"), "dedicatedHost is required when affinity is DedicatedHost, and forbidden otherwise"))
954-
} else {
955-
// If not set, return required error. If it does not match pattern, return pattern failure message.
956-
if placement.DedicatedHost.ID == "" {
957-
errs = append(errs, field.Required(field.NewPath("spec.hostPlacement.dedicatedHost.id"), "id is required and must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)"))
958-
} else if awsDedicatedHostNamePattern.FindStringSubmatch(placement.DedicatedHost.ID) == nil {
959-
errs = append(errs, field.Invalid(field.NewPath("spec.hostPlacement.dedicatedHost.id"), placement.DedicatedHost.ID, "id must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)"))
924+
if len(errs) > 0 {
925+
return false, warnings, errs
926+
}
927+
928+
return true, warnings, nil
929+
}
930+
931+
// processAWSPlacement analyzes the Placement field in relation to Tenancy and host placement. These are analyzed
932+
// together based based on their relations to one another.
933+
func processAWSPlacementTenancy(placement machinev1beta1.Placement) field.ErrorList {
934+
var errs field.ErrorList
935+
936+
switch placement.Tenancy {
937+
case "", machinev1beta1.DefaultTenancy, machinev1beta1.DedicatedTenancy:
938+
// Host is not supported for these cases
939+
if placement.Host != nil {
940+
errs = append(errs, field.Forbidden(field.NewPath("spec.placement.host"), "host may only be specified when tenancy is 'host'"))
941+
}
942+
case machinev1beta1.HostTenancy:
943+
if placement.Host != nil {
944+
klog.V(4).Infof("Validating AWS Host Placement")
945+
946+
if placement.Host.Affinity == nil {
947+
errs = append(errs, field.Required(field.NewPath("spec.placement.host.affinity"), "affinity is required and must be set to either AnyAvailable or DedicatedHost"))
948+
} else {
949+
switch *placement.Host.Affinity {
950+
case machinev1beta1.HostAffinityAnyAvailable:
951+
// DedicatedHost is optional. If it is set, make sure it follows conventions
952+
if placement.Host.DedicatedHost != nil && !awsDedicatedHostNamePattern.MatchString(placement.Host.DedicatedHost.ID) {
953+
errs = append(errs, field.Invalid(field.NewPath("spec.placement.host.dedicatedHost.id"), placement.Host.DedicatedHost.ID, "id must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)"))
960954
}
955+
case machinev1beta1.HostAffinityDedicatedHost:
956+
// We need to make sure DedicatedHost is set with an ID
957+
if placement.Host.DedicatedHost == nil {
958+
errs = append(errs, field.Required(field.NewPath("spec.placement.host.dedicatedHost"), "dedicatedHost is required when hostAffinity is DedicatedHost, and optional otherwise"))
959+
} else {
960+
// If not set, return required error. If it does not match pattern, return pattern failure message.
961+
if placement.Host.DedicatedHost.ID == "" {
962+
errs = append(errs, field.Required(field.NewPath("spec.placement.host.dedicatedHost.id"), "id is required and must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)"))
963+
} else if !awsDedicatedHostNamePattern.MatchString(placement.Host.DedicatedHost.ID) {
964+
errs = append(errs, field.Invalid(field.NewPath("spec.placement.host.dedicatedHost.id"), placement.Host.DedicatedHost.ID, "id must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)"))
965+
}
966+
}
967+
default:
968+
errs = append(errs, field.Invalid(field.NewPath("spec.placement.host.affinity"), placement.Host.Affinity, "hostAffinity must be either AnyAvailable or DedicatedHost"))
961969
}
962-
default:
963-
errs = append(errs, field.Invalid(field.NewPath("spec.hostPlacement.affinity"), placement.Affinity, "affinity must be either AnyAvailable or DedicatedHost"))
964970
}
965971
}
972+
default:
973+
errs = append(
974+
errs,
975+
field.Invalid(
976+
field.NewPath("providerSpec", "tenancy"),
977+
placement.Tenancy,
978+
fmt.Sprintf("Invalid providerSpec.tenancy, the only allowed options are: %s, %s, %s, or omitted", machinev1beta1.DefaultTenancy, machinev1beta1.DedicatedTenancy, machinev1beta1.HostTenancy),
979+
),
980+
)
966981
}
967982

968-
if len(errs) > 0 {
969-
return false, warnings, errs
970-
}
971-
972-
return true, warnings, nil
983+
return errs
973984
}
974985

975986
// getDuplicatedTags iterates through the AWS TagSpecifications

0 commit comments

Comments
 (0)