|
| 1 | +// Originally copied from https://github.com/carvel-dev/kapp/tree/d7fc2e15439331aa3a379485bb124e91a0829d2e |
| 2 | +// Attribution: |
| 3 | +// Copyright 2024 The Carvel Authors. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package crdupgradesafety |
| 7 | + |
| 8 | +import ( |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "reflect" |
| 12 | + |
| 13 | + "github.com/openshift/crd-schema-checker/pkg/manifestcomparators" |
| 14 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 15 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 16 | +) |
| 17 | + |
| 18 | +// ChangeValidation is a function that accepts a FieldDiff |
| 19 | +// as a parameter and should return: |
| 20 | +// - a boolean representation of whether or not the change |
| 21 | +// - an error if the change would be unsafe |
| 22 | +// has been fully handled (i.e no additional changes exist) |
| 23 | +type ChangeValidation func(diff FieldDiff) (bool, error) |
| 24 | + |
| 25 | +// ChangeValidator is a Validation implementation focused on |
| 26 | +// handling updates to existing fields in a CRD |
| 27 | +type ChangeValidator struct { |
| 28 | + // Validations is a slice of ChangeValidations |
| 29 | + // to run against each changed field |
| 30 | + Validations []ChangeValidation |
| 31 | +} |
| 32 | + |
| 33 | +func (cv *ChangeValidator) Name() string { |
| 34 | + return "ChangeValidator" |
| 35 | +} |
| 36 | + |
| 37 | +// Validate will compare each version in the provided existing and new CRDs. |
| 38 | +// Since the ChangeValidator is tailored to handling updates to existing fields in |
| 39 | +// each version of a CRD. As such the following is assumed: |
| 40 | +// - Validating the removal of versions during an update is handled outside of this |
| 41 | +// validator. If a version in the existing version of the CRD does not exist in the new |
| 42 | +// version that version of the CRD is skipped in this validator. |
| 43 | +// - Removal of existing fields is unsafe. Regardless of whether or not this is handled |
| 44 | +// by a validator outside this one, if a field is present in a version provided by the existing CRD |
| 45 | +// but not present in the same version provided by the new CRD this validation will fail. |
| 46 | +// |
| 47 | +// Additionally, any changes that are not validated and handled by the known ChangeValidations |
| 48 | +// are deemed as unsafe and returns an error. |
| 49 | +func (cv *ChangeValidator) Validate(old, new apiextensionsv1.CustomResourceDefinition) error { |
| 50 | + errs := []error{} |
| 51 | + for _, version := range old.Spec.Versions { |
| 52 | + newVersion := manifestcomparators.GetVersionByName(&new, version.Name) |
| 53 | + if newVersion == nil { |
| 54 | + // if the new version doesn't exist skip this version |
| 55 | + continue |
| 56 | + } |
| 57 | + flatOld := FlattenSchema(version.Schema.OpenAPIV3Schema) |
| 58 | + flatNew := FlattenSchema(newVersion.Schema.OpenAPIV3Schema) |
| 59 | + |
| 60 | + diffs, err := CalculateFlatSchemaDiff(flatOld, flatNew) |
| 61 | + if err != nil { |
| 62 | + errs = append(errs, fmt.Errorf("calculating schema diff for CRD version %q", version.Name)) |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + for field, diff := range diffs { |
| 67 | + handled := false |
| 68 | + for _, validation := range cv.Validations { |
| 69 | + ok, err := validation(diff) |
| 70 | + if err != nil { |
| 71 | + errs = append(errs, fmt.Errorf("version %q, field %q: %w", version.Name, field, err)) |
| 72 | + } |
| 73 | + if ok { |
| 74 | + handled = true |
| 75 | + break |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if !handled { |
| 80 | + errs = append(errs, fmt.Errorf("version %q, field %q has unknown change, refusing to determine that change is safe", version.Name, field)) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if len(errs) > 0 { |
| 86 | + return errors.Join(errs...) |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +type FieldDiff struct { |
| 92 | + Old *apiextensionsv1.JSONSchemaProps |
| 93 | + New *apiextensionsv1.JSONSchemaProps |
| 94 | +} |
| 95 | + |
| 96 | +// FlatSchema is a flat representation of a CRD schema. |
| 97 | +type FlatSchema map[string]*apiextensionsv1.JSONSchemaProps |
| 98 | + |
| 99 | +// FlattenSchema takes in a CRD version OpenAPIV3Schema and returns |
| 100 | +// a flattened representation of it. For example, a CRD with a schema of: |
| 101 | +// ```yaml |
| 102 | +// |
| 103 | +// ... |
| 104 | +// spec: |
| 105 | +// type: object |
| 106 | +// properties: |
| 107 | +// foo: |
| 108 | +// type: string |
| 109 | +// bar: |
| 110 | +// type: string |
| 111 | +// ... |
| 112 | +// |
| 113 | +// ``` |
| 114 | +// would be represented as: |
| 115 | +// |
| 116 | +// map[string]*apiextensionsv1.JSONSchemaProps{ |
| 117 | +// "^": {}, |
| 118 | +// "^.spec": {}, |
| 119 | +// "^.spec.foo": {}, |
| 120 | +// "^.spec.bar": {}, |
| 121 | +// } |
| 122 | +// |
| 123 | +// where "^" represents the "root" schema |
| 124 | +func FlattenSchema(schema *apiextensionsv1.JSONSchemaProps) FlatSchema { |
| 125 | + fieldMap := map[string]*apiextensionsv1.JSONSchemaProps{} |
| 126 | + |
| 127 | + manifestcomparators.SchemaHas(schema, |
| 128 | + field.NewPath("^"), |
| 129 | + field.NewPath("^"), |
| 130 | + nil, |
| 131 | + func(s *apiextensionsv1.JSONSchemaProps, _, simpleLocation *field.Path, _ []*apiextensionsv1.JSONSchemaProps) bool { |
| 132 | + fieldMap[simpleLocation.String()] = s.DeepCopy() |
| 133 | + return false |
| 134 | + }) |
| 135 | + |
| 136 | + return fieldMap |
| 137 | +} |
| 138 | + |
| 139 | +// CalculateFlatSchemaDiff finds fields in a FlatSchema that are different |
| 140 | +// and returns a mapping of field --> old and new field schemas. If a field |
| 141 | +// exists in the old FlatSchema but not the new an empty diff mapping and an error is returned. |
| 142 | +func CalculateFlatSchemaDiff(o, n FlatSchema) (map[string]FieldDiff, error) { |
| 143 | + diffMap := map[string]FieldDiff{} |
| 144 | + for field, schema := range o { |
| 145 | + if _, ok := n[field]; !ok { |
| 146 | + return diffMap, fmt.Errorf("field %q in existing not found in new", field) |
| 147 | + } |
| 148 | + newSchema := n[field] |
| 149 | + |
| 150 | + // Copy the schemas and remove any child properties for comparison. |
| 151 | + // In theory this will focus in on detecting changes for only the |
| 152 | + // field we are looking at and ignore changes in the children fields. |
| 153 | + // Since we are iterating through the map that should have all fields |
| 154 | + // we should still detect changes in the children fields. |
| 155 | + oldCopy := schema.DeepCopy() |
| 156 | + newCopy := newSchema.DeepCopy() |
| 157 | + oldCopy.Properties, oldCopy.Items = nil, nil |
| 158 | + newCopy.Properties, newCopy.Items = nil, nil |
| 159 | + if !reflect.DeepEqual(oldCopy, newCopy) { |
| 160 | + diffMap[field] = FieldDiff{ |
| 161 | + Old: oldCopy, |
| 162 | + New: newCopy, |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + return diffMap, nil |
| 167 | +} |
0 commit comments