Skip to content

Prevent deletion of resources by default #241

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ param BAZ=qux

bc,is,dc,svc
```
Please note that boolean flags need to be specified with a value, e.g. `upsert-only true`.
Please note that boolean flags need to be specified with a value, e.g. `allow-deletion true`.

Tailor will automatically pick up any file named `Tailorfile.<namespace>` or `Tailorfile` in the working directory. Alternatively, a specific file can be selected via `tailor -f somefile`.

Expand Down
16 changes: 8 additions & 8 deletions cmd/tailor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ var (
"ignore-unknown-parameters",
"If true, will not stop processing if a provided parameter does not exist in the template.",
).Bool()
diffUpsertOnlyFlag = diffCommand.Flag(
"upsert-only",
"Don't delete resource, only create / update.",
diffAllowDeletionFlag = diffCommand.Flag(
"allow-deletion",
"Allow deletion of resource as well.",
).Short('u').Bool()
diffAllowRecreateFlag = diffCommand.Flag(
"allow-recreate",
Expand Down Expand Up @@ -159,9 +159,9 @@ var (
"ignore-unknown-parameters",
"If true, will not stop processing if a provided parameter does not exist in the template.",
).Bool()
applyUpsertOnlyFlag = applyCommand.Flag(
"upsert-only",
"Don't delete resource, only create / apply.",
applyAllowDeletionFlag = applyCommand.Flag(
"allow-deletion",
"Allow deletion of resource as well.",
).Short('u').Bool()
applyAllowRecreateFlag = applyCommand.Flag(
"allow-recreate",
Expand Down Expand Up @@ -362,7 +362,7 @@ func main() {
preservePathFlag,
*diffPreserveImmutableFieldsFlag,
*diffIgnoreUnknownParametersFlag,
*diffUpsertOnlyFlag,
*diffAllowDeletionFlag,
*diffAllowRecreateFlag,
*diffRevealSecretsFlag,
false, // verification only when changes are applied
Expand Down Expand Up @@ -399,7 +399,7 @@ func main() {
preservePathFlag,
*applyPreserveImmutableFieldsFlag,
*applyIgnoreUnknownParametersFlag,
*applyUpsertOnlyFlag,
*applyAllowDeletionFlag,
*applyAllowRecreateFlag,
*applyRevealSecretsFlag,
*applyVerifyFlag,
Expand Down
12 changes: 6 additions & 6 deletions pkg/cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type CompareOptions struct {
PreservePaths []string
PreserveImmutableFields bool
IgnoreUnknownParameters bool
UpsertOnly bool
AllowDeletion bool
AllowRecreate bool
RevealSecrets bool
Verify bool
Expand Down Expand Up @@ -159,7 +159,7 @@ func NewCompareOptions(
preserveFlag []string,
preserveImmutableFieldsFlag bool,
ignoreUnknownParametersFlag bool,
upsertOnlyFlag bool,
allowDeletionFlag bool,
allowRecreateFlag bool,
revealSecretsFlag bool,
verifyFlag bool,
Expand Down Expand Up @@ -284,10 +284,10 @@ func NewCompareOptions(
o.IgnoreUnknownParameters = true
}

if upsertOnlyFlag {
o.UpsertOnly = true
} else if fileFlags["upsert-only"] == "true" {
o.UpsertOnly = true
if allowDeletionFlag {
o.AllowDeletion = true
} else if fileFlags["allow-deletion"] == "true" {
o.AllowDeletion = true
}

if allowRecreateFlag {
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func calculateChangeset(w io.Writer, compareOptions *cli.CompareOptions, ocClien
w,
platformBasedList,
templateBasedList,
compareOptions.UpsertOnly,
compareOptions.AllowDeletion,
compareOptions.AllowRecreate,
compareOptions.RevealSecrets,
compareOptions.PathsToPreserve(),
Expand All @@ -148,8 +148,8 @@ func calculateChangeset(w io.Writer, compareOptions *cli.CompareOptions, ocClien
return updateRequired, changeset, nil
}

func compare(w io.Writer, remoteResourceList *openshift.ResourceList, localResourceList *openshift.ResourceList, upsertOnly bool, allowRecreate bool, revealSecrets bool, preservePaths []string) (*openshift.Changeset, error) {
changeset, err := openshift.NewChangeset(remoteResourceList, localResourceList, upsertOnly, allowRecreate, preservePaths)
func compare(w io.Writer, remoteResourceList *openshift.ResourceList, localResourceList *openshift.ResourceList, allowDeletion bool, allowRecreate bool, revealSecrets bool, preservePaths []string) (*openshift.Changeset, error) {
changeset, err := openshift.NewChangeset(remoteResourceList, localResourceList, allowDeletion, allowRecreate, preservePaths)
if err != nil {
return changeset, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/openshift/changeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Changeset struct {
Noop []*Change
}

func NewChangeset(platformBasedList, templateBasedList *ResourceList, upsertOnly bool, allowRecreate bool, preservePaths []string) (*Changeset, error) {
func NewChangeset(platformBasedList, templateBasedList *ResourceList, allowDeletion bool, allowRecreate bool, preservePaths []string) (*Changeset, error) {
changeset := &Changeset{
Create: []*Change{},
Delete: []*Change{},
Expand All @@ -49,7 +49,7 @@ func NewChangeset(platformBasedList, templateBasedList *ResourceList, upsertOnly
}

// items to delete
if !upsertOnly {
if allowDeletion {
for _, item := range platformBasedList.Items {
if _, err := templateBasedList.getItem(item.Kind, item.Name); err != nil {
change := &Change{
Expand Down
34 changes: 27 additions & 7 deletions pkg/openshift/changeset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func TestNewChangesetCreationOfResources(t *testing.T) {
if err != nil {
t.Fatal(err)
}
upsertOnly := false
allowDeletion := true
allowRecreate := false
preservePaths := []string{}
cs, err := NewChangeset(
platformBasedList,
templateBasedList,
upsertOnly,
allowDeletion,
allowRecreate,
preservePaths,
)
Expand Down Expand Up @@ -598,9 +598,29 @@ items:
filter := &ResourceFilter{
Kinds: []string{"PersistentVolumeClaim"},
}
changeset := getChangeset(t, filter, platformInput, templateInput, false, true, []string{})
if len(changeset.Delete) != 1 {
t.Errorf("Changeset.Delete is blank but should not be")

tests := map[string]struct {
allowDeletion bool
wantChanges int
}{
"when deletion is not allowed": {
allowDeletion: false, // default
wantChanges: 0,
},
"when deletion is allowed": {
allowDeletion: true,
wantChanges: 1,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
changeset := getChangeset(t, filter, platformInput, templateInput, tc.allowDeletion, true, []string{})
gotChanges := len(changeset.Delete)
if gotChanges != tc.wantChanges {
t.Errorf("Changeset.Delete is %d but should not be %d", gotChanges, tc.wantChanges)
}
})
}
}

Expand Down Expand Up @@ -635,7 +655,7 @@ func TestCalculateChangesImmutableFields(t *testing.T) {
}
}

func getChangeset(t *testing.T, filter *ResourceFilter, platformInput, templateInput []byte, upsertOnly bool, allowRecreate bool, preservePaths []string) *Changeset {
func getChangeset(t *testing.T, filter *ResourceFilter, platformInput, templateInput []byte, allowDeletion bool, allowRecreate bool, preservePaths []string) *Changeset {
platformBasedList, err := NewPlatformBasedResourceList(filter, platformInput)
if err != nil {
t.Error("Could not create platform based list:", err)
Expand All @@ -644,7 +664,7 @@ func getChangeset(t *testing.T, filter *ResourceFilter, platformInput, templateI
if err != nil {
t.Error("Could not create template based list:", err)
}
changeset, err := NewChangeset(platformBasedList, templateBasedList, upsertOnly, allowRecreate, preservePaths)
changeset, err := NewChangeset(platformBasedList, templateBasedList, allowDeletion, allowRecreate, preservePaths)
if err != nil {
t.Error("Could not create changeset:", err)
}
Expand Down