Skip to content
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

chore: removing webhook validation in favor of kubebuilder CRD validation, and removing old SIG Gallery Logic #10

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/apis/v1alpha2/aksnodeclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type AKSNodeClassSpec struct {
OSDiskSizeGB *int32 `json:"osDiskSizeGB,omitempty"`
// ImageID is the ID of the image that instances use.
// Not exposed in the API yet
// +kubebuilder:validation:Pattern=`(?i)/subscriptions/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/resourceGroups/[\w-]+/providers/Microsoft\.Compute/galleries/[\w-]+/images/[\w-]+/versions/[\d.]+|(?i)/CommunityGalleries/[\w-]+/images/[\w-]+/versions/[\d.]+`
ImageID *string `json:"-"`
// ImageFamily is the image family that instances use.
// +kubebuilder:default=Ubuntu2204
Expand Down
35 changes: 1 addition & 34 deletions pkg/apis/v1alpha2/aksnodeclass_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,19 @@ package v1alpha2

import (
"context"
"fmt"
"regexp"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"

"knative.dev/pkg/apis"
)

var (
SubscriptionShape = regexp.MustCompile(`[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}`)
ComputeGalleryImageVersionRegex = regexp.MustCompile(`(?i)/subscriptions/` + SubscriptionShape.String() + `/resourceGroups/[\w-]+/providers/Microsoft\.Compute/galleries/[\w-]+/images/[\w-]+/versions/[\d.]+`)
CommunityImageVersionRegex = regexp.MustCompile(`(?i)/CommunityGalleries/[\w-]+/images/[\w-]+/versions/[\d.]+`)
)

func (in *AKSNodeClass) SupportedVerbs() []admissionregistrationv1.OperationType {
return []admissionregistrationv1.OperationType{
admissionregistrationv1.Create,
admissionregistrationv1.Update,
}
}

func IsComputeGalleryImageID(imageID string) bool {
return ComputeGalleryImageVersionRegex.MatchString(imageID)
}

func (in *AKSNodeClass) Validate(ctx context.Context) (errs *apis.FieldError) {
//if apis.IsInUpdate(ctx) {
// original := apis.GetBaseline(ctx).(*AKSNodeClass)
// errs = in.validateImmutableFields(original)
//}
return errs.Also(
apis.ValidateObjectMetadata(in).ViaField("metadata"),
in.Spec.validate(ctx).ViaField("spec"),
)
}

func (in *AKSNodeClassSpec) validate(_ context.Context) (errs *apis.FieldError) {
return errs.Also(
in.validateImageID(),
)
}

func (in *AKSNodeClassSpec) validateImageID() (errs *apis.FieldError) {
if in.IsEmptyImageID() || ComputeGalleryImageVersionRegex.MatchString(*in.ImageID) || CommunityImageVersionRegex.MatchString(*in.ImageID) {
return nil
}
return apis.ErrInvalidValue(fmt.Sprintf(
"the provided image ID: '%s' is invalid because it doesn't match the expected format", *in.ImageID), "ImageID")
return nil
}
7 changes: 2 additions & 5 deletions pkg/providers/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,8 @@ func newVMObject(
launchTemplate *launchtemplate.Template,
instanceType *corecloudprovider.InstanceType) armcompute.VirtualMachine {
// Build the image reference from template
imageReference := armcompute.ImageReference{}
if v1alpha2.IsComputeGalleryImageID(launchTemplate.ImageID) {
imageReference.ID = &launchTemplate.ImageID
} else {
imageReference.CommunityGalleryImageID = &launchTemplate.ImageID
imageReference := armcompute.ImageReference{
CommunityGalleryImageID: &launchTemplate.ImageID,
}
vm := armcompute.VirtualMachine{
Location: to.Ptr(location),
Expand Down