-
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 required fields to the CRD Upgrade Safety …
…preflight check Signed-off-by: Rashmi Gottipati <chowdary.grashmi@gmail.com>
- Loading branch information
1 parent
9580fa3
commit fcb5fbe
Showing
6 changed files
with
505 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
129 changes: 129 additions & 0 deletions
129
test/e2e/preflight_crdupgradesafety_invalid_field_change_requiredfield_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,129 @@ | ||
// Copyright 2024 The Carvel Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package e2e | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPreflightCRDUpgradeSafetyInvalidFieldChangeRequiredFieldAdded(t *testing.T) { | ||
env := BuildEnv(t) | ||
logger := Logger{} | ||
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger} | ||
|
||
testName := "preflightcrdupgradesafetyinvalidfieldchangerequiredfieldadded" | ||
|
||
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 | ||
properties: | ||
image: | ||
type: string | ||
pollInterval: | ||
type: string | ||
required: | ||
- image | ||
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 | ||
properties: | ||
image: | ||
type: string | ||
pollInterval: | ||
type: string | ||
required: | ||
- image | ||
- pollInterval | ||
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 required field 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(), "new required fields added") | ||
}) | ||
} |
126 changes: 126 additions & 0 deletions
126
...light_crdupgradesafety_invalid_field_change_requiredfield_added_when_none_existed_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,126 @@ | ||
// Copyright 2024 The Carvel Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package e2e | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPreflightCRDUpgradeSafetyInvalidFieldChangeRequiredFieldAddedWhenNoneExisted(t *testing.T) { | ||
env := BuildEnv(t) | ||
logger := Logger{} | ||
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger} | ||
|
||
testName := "preflightcrdupgradesafetyinvalidfieldchangerequiredfieldaddedwhennonexisted" | ||
|
||
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 | ||
properties: | ||
image: | ||
type: string | ||
pollInterval: | ||
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 | ||
properties: | ||
image: | ||
type: string | ||
pollInterval: | ||
type: string | ||
required: | ||
- image | ||
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 new required field value when none existed prior to this, 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 values added as required when previously no required fields existed") | ||
}) | ||
} |
Oops, something went wrong.