-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handling of updates to default values to the CRD Upgrade Safety p…
…reflight check Signed-off-by: Rashmi Gottipati <chowdary.grashmi@gmail.com>
- Loading branch information
1 parent
73018fd
commit 14eba65
Showing
7 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
test/e2e/preflight_crdupgradesafety_invalid_field_change_default_added_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2024 The Carvel Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package e2e | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Adding a new value as default for a field that did not | ||
// previously have a default value present is invalid | ||
// and this test is covering that case. | ||
func TestPreflightCRDUpgradeSafetyInvalidFieldChangeDefaultAdded(t *testing.T) { | ||
env := BuildEnv(t) | ||
logger := Logger{} | ||
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger} | ||
|
||
testName := "preflightcrdupgradesafetyinvalidfieldchangedefaultadded" | ||
|
||
base := ` | ||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.13.0 | ||
name: memcacheds.__test-name__.example.com | ||
spec: | ||
group: __test-name__.example.com | ||
names: | ||
kind: Memcached | ||
listKind: MemcachedList | ||
plural: memcacheds | ||
singular: memcached | ||
scope: Namespaced | ||
versions: | ||
- name: v1alpha1 | ||
schema: | ||
openAPIV3Schema: | ||
properties: | ||
apiVersion: | ||
type: string | ||
kind: | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
type: string | ||
status: | ||
type: object | ||
type: object | ||
served: true | ||
storage: true | ||
subresources: | ||
status: {} | ||
` | ||
|
||
base = strings.ReplaceAll(base, "__test-name__", testName) | ||
appName := "preflight-crdupgradesafety-app" | ||
|
||
cleanUp := func() { | ||
kapp.Run([]string{"delete", "-a", appName}) | ||
} | ||
cleanUp() | ||
defer cleanUp() | ||
|
||
update := ` | ||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.13.0 | ||
name: memcacheds.__test-name__.example.com | ||
spec: | ||
group: __test-name__.example.com | ||
names: | ||
kind: Memcached | ||
listKind: MemcachedList | ||
plural: memcacheds | ||
singular: memcached | ||
scope: Namespaced | ||
versions: | ||
- name: v1alpha1 | ||
schema: | ||
openAPIV3Schema: | ||
properties: | ||
apiVersion: | ||
type: string | ||
kind: | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
type: string | ||
default: foo | ||
status: | ||
type: object | ||
type: object | ||
served: true | ||
storage: true | ||
subresources: | ||
status: {} | ||
` | ||
|
||
update = strings.ReplaceAll(update, "__test-name__", testName) | ||
logger.Section("deploy app with CRD update that adds a default value that did not exist previously, preflight check enabled, should error", func() { | ||
_, err := kapp.RunWithOpts([]string{"deploy", "-a", appName, "-f", "-"}, RunOpts{StdinReader: strings.NewReader(base)}) | ||
require.NoError(t, err) | ||
_, err = kapp.RunWithOpts([]string{"deploy", "--preflight=CRDUpgradeSafety", "-a", appName, "-f", "-"}, | ||
RunOpts{StdinReader: strings.NewReader(update), AllowError: true}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "new value added as default when previously no default value existed") | ||
}) | ||
} |
118 changes: 118 additions & 0 deletions
118
test/e2e/preflight_crdupgradesafety_invalid_field_change_default_changed_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2024 The Carvel Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package e2e | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Changing an existing field's default value to a new one is not valid | ||
// and this test is covering that case. | ||
func TestPreflightCRDUpgradeSafetyInvalidFieldChangeDefaultChanged(t *testing.T) { | ||
env := BuildEnv(t) | ||
logger := Logger{} | ||
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger} | ||
|
||
testName := "preflightcrdupgradesafetyinvalidfieldchangedefaultchanged" | ||
|
||
base := ` | ||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.13.0 | ||
name: memcacheds.__test-name__.example.com | ||
spec: | ||
group: __test-name__.example.com | ||
names: | ||
kind: Memcached | ||
listKind: MemcachedList | ||
plural: memcacheds | ||
singular: memcached | ||
scope: Namespaced | ||
versions: | ||
- name: v1alpha1 | ||
schema: | ||
openAPIV3Schema: | ||
properties: | ||
apiVersion: | ||
type: string | ||
kind: | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
type: string | ||
default: foo | ||
status: | ||
type: object | ||
type: object | ||
served: true | ||
storage: true | ||
subresources: | ||
status: {} | ||
` | ||
|
||
base = strings.ReplaceAll(base, "__test-name__", testName) | ||
appName := "preflight-crdupgradesafety-app" | ||
|
||
cleanUp := func() { | ||
kapp.Run([]string{"delete", "-a", appName}) | ||
} | ||
cleanUp() | ||
defer cleanUp() | ||
|
||
update := ` | ||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.13.0 | ||
name: memcacheds.__test-name__.example.com | ||
spec: | ||
group: __test-name__.example.com | ||
names: | ||
kind: Memcached | ||
listKind: MemcachedList | ||
plural: memcacheds | ||
singular: memcached | ||
scope: Namespaced | ||
versions: | ||
- name: v1alpha1 | ||
schema: | ||
openAPIV3Schema: | ||
properties: | ||
apiVersion: | ||
type: string | ||
kind: | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
type: string | ||
default: bar | ||
status: | ||
type: object | ||
type: object | ||
served: true | ||
storage: true | ||
subresources: | ||
status: {} | ||
` | ||
|
||
update = strings.ReplaceAll(update, "__test-name__", testName) | ||
logger.Section("deploy app with CRD update that changes an existing field's default value, preflight check enabled, should error", func() { | ||
_, err := kapp.RunWithOpts([]string{"deploy", "-a", appName, "-f", "-"}, RunOpts{StdinReader: strings.NewReader(base)}) | ||
require.NoError(t, err) | ||
_, err = kapp.RunWithOpts([]string{"deploy", "--preflight=CRDUpgradeSafety", "-a", appName, "-f", "-"}, | ||
RunOpts{StdinReader: strings.NewReader(update), AllowError: true}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "default value has been changed") | ||
}) | ||
} |
Oops, something went wrong.