Skip to content

Commit

Permalink
Provide HasDefaultStorageClass & IsExpandable on webhook context
Browse files Browse the repository at this point in the history
- The conversion wehhook will call validation & defaulting which will lead to diverging behavior from the validation and defaulting webhooks
- This prevented v1alpha1 image resources from being defaulted with a volume cache because defaulting was taking place in the conversion webhook without the correct context
Resolves: #878 #873
  • Loading branch information
matthewmcnew committed Nov 4, 2021
1 parent 90fe84d commit c7ef3b1
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ func defaultingAdmissionController(ctx context.Context, _ configmap.Watcher) *co
// The resources to default.
types,
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
func(ctx context.Context) context.Context {
return withCheckDefaultStorageClass(ctx, storageClassLister)
},
withCheckDefaultStorageClass(storageClassLister),
// Whether to disallow unknown fields.
false,
)
Expand All @@ -85,15 +83,14 @@ func validatingAdmissionController(ctx context.Context, _ configmap.Watcher) *co
// The resources to validate.
types,
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
func(ctx context.Context) context.Context {
return withCheckDefaultStorageClass(ctx, storageClassLister)
},
withCheckDefaultStorageClass(storageClassLister),
// Whether to disallow unknown fields.
true,
)
}

func conversionController(ctx context.Context, _ configmap.Watcher) *controller.Impl {
storageClassLister := getStorageClassInformer(ctx).Lister()

conversions := map[schema.GroupKind]conversion.GroupKindConversion{
v1alpha2.Kind("Image"): {
Expand Down Expand Up @@ -134,34 +131,34 @@ func conversionController(ctx context.Context, _ configmap.Watcher) *controller.
ctx,
"/convert",
conversions,
func(ctx context.Context) context.Context {
return ctx
},
withCheckDefaultStorageClass(storageClassLister),
)
}

func withCheckDefaultStorageClass(ctx context.Context, storageClassLister listersv1.StorageClassLister) context.Context {
storageClasses, err := storageClassLister.List(labels.NewSelector())
if err != nil {
log.Printf("failed to list storage classes: %s\n", err)
return ctx
}

for _, sc := range storageClasses {
if sc.Annotations == nil {
continue
func withCheckDefaultStorageClass(storageClassLister listersv1.StorageClassLister) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
storageClasses, err := storageClassLister.List(labels.NewSelector())
if err != nil {
log.Printf("failed to list storage classes: %s\n", err)
return ctx
}

if val, ok := sc.Annotations["storageclass.kubernetes.io/is-default-class"]; ok && val == "true" {
ctx = context.WithValue(ctx, v1alpha2.HasDefaultStorageClass, true)
if sc.AllowVolumeExpansion != nil && *sc.AllowVolumeExpansion {
ctx = context.WithValue(ctx, v1alpha2.IsExpandable, true)
for _, sc := range storageClasses {
if sc.Annotations == nil {
continue
}

if val, ok := sc.Annotations["storageclass.kubernetes.io/is-default-class"]; ok && val == "true" {
ctx = context.WithValue(ctx, v1alpha2.HasDefaultStorageClass, true)
if sc.AllowVolumeExpansion != nil && *sc.AllowVolumeExpansion {
ctx = context.WithValue(ctx, v1alpha2.IsExpandable, true)
}
break
}
break
}
}

return ctx
return ctx
}
}

// storageClassInformerKey is used for associating the Informer inside the context.Context.
Expand Down

0 comments on commit c7ef3b1

Please sign in to comment.