Skip to content

Commit

Permalink
Add validation to CRDUpgradeSafety preflight check to prevent removal…
Browse files Browse the repository at this point in the history
… of existing fields (#922)

Signed-off-by: Rashmi Gottipati <[email protected]>
  • Loading branch information
rashmigottipati authored Apr 14, 2024
1 parent c444768 commit 3fc60e9
Show file tree
Hide file tree
Showing 23 changed files with 1,848 additions and 36 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/k14s/difflib v0.0.0-20240118055029-596a7a5585c3
github.com/k14s/ytt v0.36.0
github.com/mitchellh/go-wordwrap v1.0.1
github.com/openshift/crd-schema-checker v0.0.0-20240404194209-35a9033b1d11
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/openshift/crd-schema-checker v0.0.0-20240404194209-35a9033b1d11 h1:eTNDkNRNV5lZvUbVM9Nop0lBcljSnA8rZX6yQPZ0ZnU=
github.com/openshift/crd-schema-checker v0.0.0-20240404194209-35a9033b1d11/go.mod h1:EmVJt97N+pfWFsli/ipXTBZqSG5F5KGQhm3c3IsGq1o=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
1 change: 1 addition & 0 deletions pkg/kapp/crdupgradesafety/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewPreflight(df cmdcore.DepsFactory, enabled bool) *Preflight {
Validations: []Validation{
NewValidationFunc("NoScopeChange", NoScopeChange),
NewValidationFunc("NoStoredVersionRemoved", NoStoredVersionRemoved),
NewValidationFunc("NoExistingFieldRemoved", NoExistingFieldRemoved),
},
},
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/kapp/crdupgradesafety/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package crdupgradesafety
import (
"errors"
"fmt"
"strings"

"github.com/openshift/crd-schema-checker/pkg/manifestcomparators"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
Expand Down Expand Up @@ -91,3 +93,29 @@ func NoStoredVersionRemoved(old, new v1.CustomResourceDefinition) error {

return nil
}

func NoExistingFieldRemoved(old, new v1.CustomResourceDefinition) error {
reg := manifestcomparators.NewRegistry()
err := reg.AddComparator(manifestcomparators.NoFieldRemoval())
if err != nil {
return err
}

results, errs := reg.Compare(&old, &new)
if len(errs) > 0 {
return errors.Join(errs...)
}

errSet := []error{}

for _, result := range results {
if len(result.Errors) > 0 {
errSet = append(errSet, errors.New(strings.Join(result.Errors, "\n")))
}
}
if len(errSet) > 0 {
return errors.Join(errSet...)
}

return nil
}
Loading

0 comments on commit 3fc60e9

Please sign in to comment.