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

Validate that image cache size does not change when the storage class is not expandable #813

Merged
merged 3 commits into from
Aug 26, 2021
Merged
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
3 changes: 3 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func withCheckDefaultStorageClass(ctx context.Context, storageClassLister lister

if val, ok := sc.Annotations["storageclass.kubernetes.io/is-default-class"]; ok && val == "true" {
ctx = context.WithValue(ctx, buildapi.HasDefaultStorageClass, true)
if *sc.AllowVolumeExpansion {
ctx = context.WithValue(ctx, buildapi.IsExpandable, true)
}
break
}
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/apis/build/v1alpha1/image_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ImageContextKey string

const (
HasDefaultStorageClass ImageContextKey = "hasDefaultStorageClass"
IsExpandable ImageContextKey = "isExpandable"
)

var (
Expand Down Expand Up @@ -96,11 +97,19 @@ func (is *ImageSpec) validateCacheSize(ctx context.Context) *apis.FieldError {

if apis.IsInUpdate(ctx) {
original := apis.GetBaseline(ctx).(*Image)
if original.Spec.CacheSize != nil && is.CacheSize.Cmp(*original.Spec.CacheSize) < 0 {
return &apis.FieldError{
Message: "Field cannot be decreased",
Paths: []string{"cacheSize"},
Details: fmt.Sprintf("current: %v, requested: %v", original.Spec.CacheSize, is.CacheSize),
if original.Spec.CacheSize != nil {
if ctx.Value(IsExpandable) == false && is.CacheSize.Cmp(*original.Spec.CacheSize) != 0 {
return &apis.FieldError{
Message: "Field cannot be changed",
Paths: []string{"cacheSize"},
Details: fmt.Sprintf("current: %v, requested: %v", original.Spec.CacheSize, is.CacheSize),
}
} else if is.CacheSize.Cmp(*original.Spec.CacheSize) < 0 {
return &apis.FieldError{
Message: "Field cannot be decreased",
Paths: []string{"cacheSize"},
Details: fmt.Sprintf("current: %v, requested: %v", original.Spec.CacheSize, is.CacheSize),
}
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/apis/build/v1alpha1/image_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestImageValidation(t *testing.T) {
func testImageValidation(t *testing.T, when spec.G, it spec.S) {
var limit int64 = 90
cacheSize := resource.MustParse("5G")
ctx := context.WithValue(context.TODO(), HasDefaultStorageClass, true)
ctx := context.WithValue(context.WithValue(context.TODO(), HasDefaultStorageClass, true), IsExpandable, true)
image := &Image{
ObjectMeta: metav1.ObjectMeta{
Name: "image-name",
Expand Down Expand Up @@ -267,6 +267,22 @@ func testImageValidation(t *testing.T, when spec.G, it spec.S) {
assert.EqualError(t, err, "Field cannot be decreased: spec.cacheSize\ncurrent: 5G, requested: 4G")
})

it("image.cacheSize has not changed when storageclass is not expandable", func() {
original := image.DeepCopy()
cacheSize := resource.MustParse("6G")
image.Spec.CacheSize = &cacheSize
err := image.Validate(apis.WithinUpdate(context.WithValue(ctx, IsExpandable, false), original))
assert.EqualError(t, err, "Field cannot be changed: spec.cacheSize\ncurrent: 5G, requested: 6G")
})

it("image.cacheSize has changed when storageclass is expandable", func() {
original := image.DeepCopy()
cacheSize := resource.MustParse("6G")
image.Spec.CacheSize = &cacheSize
err := image.Validate(apis.WithinUpdate(ctx, original))
assert.Nil(t, err)
})

when("validating the notary config", func() {
it("handles a valid notary config", func() {
image.Spec.Notary = &NotaryConfig{
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/build/v1alpha2/image_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ImageContextKey string

const (
HasDefaultStorageClass ImageContextKey = "hasDefaultStorageClass"
IsExpandable ImageContextKey = "isExpandable"
)

var (
Expand Down Expand Up @@ -102,7 +103,13 @@ func (is *ImageSpec) validateVolumeCache(ctx context.Context) *apis.FieldError {
if apis.IsInUpdate(ctx) {
original := apis.GetBaseline(ctx).(*Image)
if original.Spec.NeedVolumeCache() && is.NeedVolumeCache() {
if is.Cache.Volume.Size.Cmp(*original.Spec.Cache.Volume.Size) < 0 {
if ctx.Value(IsExpandable) == false && is.Cache.Volume.Size.Cmp(*original.Spec.Cache.Volume.Size) != 0 {
return &apis.FieldError{
Message: "Field cannot be changed, default storage class is not expandable",
Paths: []string{"cache.volume.size"},
Details: fmt.Sprintf("current: %v, requested: %v", original.Spec.Cache.Volume.Size, is.Cache.Volume.Size),
}
} else if is.Cache.Volume.Size.Cmp(*original.Spec.Cache.Volume.Size) < 0 {
return &apis.FieldError{
Message: "Field cannot be decreased",
Paths: []string{"cache.volume.size"},
Expand Down
18 changes: 17 additions & 1 deletion pkg/apis/build/v1alpha2/image_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestImageValidation(t *testing.T) {
func testImageValidation(t *testing.T, when spec.G, it spec.S) {
var limit int64 = 90
cacheSize := resource.MustParse("5G")
ctx := context.WithValue(context.TODO(), HasDefaultStorageClass, true)
ctx := context.WithValue(context.WithValue(context.TODO(), HasDefaultStorageClass, true), IsExpandable, true)
image := &Image{
ObjectMeta: metav1.ObjectMeta{
Name: "image-name",
Expand Down Expand Up @@ -284,6 +284,22 @@ func testImageValidation(t *testing.T, when spec.G, it spec.S) {
assert.EqualError(t, err, "Field cannot be decreased: spec.cache.volume.size\ncurrent: 5G, requested: 4G")
})

it("image.cacheSize has not changed when storageclass is not expandable", func() {
original := image.DeepCopy()
cacheSize := resource.MustParse("6G")
image.Spec.Cache.Volume.Size = &cacheSize
err := image.Validate(apis.WithinUpdate(context.WithValue(ctx, IsExpandable, false), original))
assert.EqualError(t, err, "Field cannot be changed, default storage class is not expandable: spec.cache.volume.size\ncurrent: 5G, requested: 6G")
})

it("image.cacheSize has changed when storageclass is expandable", func() {
original := image.DeepCopy()
cacheSize := resource.MustParse("6G")
image.Spec.Cache.Volume.Size = &cacheSize
err := image.Validate(apis.WithinUpdate(ctx, original))
assert.Nil(t, err)
})

when("validating the notary config", func() {
it("handles a valid notary config", func() {
image.Spec.Notary = &NotaryConfig{
Expand Down