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
Changes from 1 commit
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
Next Next commit
chore: removing image id validation from webhooks
bsoghigian committed Nov 7, 2023
commit fd6c8f3a2ce4029f55a124e076b8a3c91899db21
35 changes: 1 addition & 34 deletions pkg/apis/v1alpha2/aksnodeclass_validation.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 11 additions & 7 deletions pkg/providers/instance/instance.go
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"math"
"regexp"
"sort"
"strconv"
"strings"
@@ -57,8 +58,10 @@ import (
)

var (
NodePoolTagKey = strings.ReplaceAll(corev1beta1.NodePoolLabelKey, "/", "_")
listQuery string
NodePoolTagKey = strings.ReplaceAll(corev1beta1.NodePoolLabelKey, "/", "_")
listQuery string
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.]+`)

CapacityTypeToPriority = map[string]string{
corev1beta1.CapacityTypeSpot: string(compute.Spot),
@@ -298,11 +301,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),
@@ -689,3 +689,7 @@ func ConvertToVirtualMachineIdentity(nodeIdentities []string) *armcompute.Virtua

return identity
}

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