-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Suderman
committed
Apr 25, 2022
1 parent
3d5f94e
commit 2f57a48
Showing
5 changed files
with
478 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package course | ||
|
||
import ( | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// populateNamespaceManagement populates each release with the default namespace management settings if they are not set | ||
func (f *FileV2) populateNamespaceManagement() { | ||
var emptyNamespaceMgmt NamespaceConfig | ||
if f.NamespaceMgmt == nil { | ||
f.NamespaceMgmt = &NamespaceMgmt{} | ||
} | ||
if f.NamespaceMgmt.Default == nil { | ||
f.NamespaceMgmt.Default = &emptyNamespaceMgmt | ||
f.NamespaceMgmt.Default.Settings.Overwrite = boolPtr(false) | ||
} else if f.NamespaceMgmt.Default.Settings.Overwrite == nil { | ||
f.NamespaceMgmt.Default.Settings.Overwrite = boolPtr(false) | ||
} | ||
|
||
for releaseIndex, release := range f.Releases { | ||
newRelease := *release | ||
if newRelease.NamespaceMgmt == nil { | ||
klog.V(5).Infof("using default namespace management for release: %s", release.Name) | ||
newRelease.NamespaceMgmt = f.NamespaceMgmt.Default | ||
} else { | ||
newRelease.NamespaceMgmt = mergeNamespaceManagement(*f.NamespaceMgmt.Default, *newRelease.NamespaceMgmt) | ||
|
||
} | ||
f.Releases[releaseIndex] = &newRelease | ||
} | ||
} | ||
|
||
// mergeNamespaceManagement merges the default namespace management settings with the release specific settings | ||
func mergeNamespaceManagement(defaults NamespaceConfig, mergeInto NamespaceConfig) *NamespaceConfig { | ||
for k, v := range defaults.Metadata.Annotations { | ||
if mergeInto.Metadata.Annotations == nil { | ||
mergeInto.Metadata.Annotations = map[string]string{} | ||
} | ||
if mergeInto.Metadata.Annotations[k] == "" { | ||
mergeInto.Metadata.Annotations[k] = v | ||
} | ||
} | ||
|
||
for k, v := range defaults.Metadata.Labels { | ||
if mergeInto.Metadata.Labels == nil { | ||
mergeInto.Metadata.Labels = map[string]string{} | ||
} | ||
if mergeInto.Metadata.Labels[k] == "" { | ||
mergeInto.Metadata.Labels[k] = v | ||
} | ||
} | ||
|
||
if mergeInto.Settings.Overwrite == nil { | ||
mergeInto.Settings.Overwrite = defaults.Settings.Overwrite | ||
} | ||
|
||
return &mergeInto | ||
} |
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,252 @@ | ||
package course | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_mergeNamespaceManagement(t *testing.T) { | ||
type args struct { | ||
defaults NamespaceConfig | ||
mergeInto NamespaceConfig | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *NamespaceConfig | ||
}{ | ||
{ | ||
name: "basic merge", | ||
args: args{ | ||
defaults: NamespaceConfig{ | ||
Metadata: NSMetadata{ | ||
Annotations: map[string]string{ | ||
"default-annotation": "default-value", | ||
}, | ||
Labels: map[string]string{ | ||
"default-label": "default-value", | ||
}, | ||
}, | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
}, | ||
mergeInto: NamespaceConfig{ | ||
Metadata: NSMetadata{ | ||
Annotations: map[string]string{ | ||
"merge-annotation": "merge-value", | ||
}, | ||
Labels: map[string]string{ | ||
"merge-label": "merge-value", | ||
}, | ||
}, | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
}, | ||
}, | ||
want: &NamespaceConfig{ | ||
Metadata: NSMetadata{ | ||
Annotations: map[string]string{ | ||
"default-annotation": "default-value", | ||
"merge-annotation": "merge-value", | ||
}, | ||
Labels: map[string]string{ | ||
"default-label": "default-value", | ||
"merge-label": "merge-value", | ||
}, | ||
}, | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := mergeNamespaceManagement(tt.args.defaults, tt.args.mergeInto) | ||
assert.EqualValues(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestFileV2_populateNamespaceManagement(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
file *FileV2 | ||
want *FileV2 | ||
}{ | ||
{ | ||
name: "empty default", | ||
file: &FileV2{ | ||
Releases: []*Release{}, | ||
NamespaceMgmt: &NamespaceMgmt{}, | ||
}, | ||
want: &FileV2{ | ||
NamespaceMgmt: &NamespaceMgmt{ | ||
Default: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
}, | ||
}, | ||
Releases: []*Release{}, | ||
}, | ||
}, | ||
{ | ||
name: "empty default overwrite", | ||
file: &FileV2{ | ||
Releases: []*Release{}, | ||
NamespaceMgmt: &NamespaceMgmt{ | ||
Default: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &FileV2{ | ||
NamespaceMgmt: &NamespaceMgmt{ | ||
Default: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
}, | ||
}, | ||
Releases: []*Release{}, | ||
}, | ||
}, | ||
{ | ||
name: "release default", | ||
file: &FileV2{ | ||
Releases: []*Release{ | ||
{ | ||
Name: "default", | ||
}, | ||
}, | ||
NamespaceMgmt: &NamespaceMgmt{ | ||
Default: nil, | ||
}, | ||
}, | ||
want: &FileV2{ | ||
NamespaceMgmt: &NamespaceMgmt{ | ||
Default: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
}, | ||
}, | ||
Releases: []*Release{ | ||
{ | ||
Name: "default", | ||
NamespaceMgmt: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "release specific", | ||
file: &FileV2{ | ||
Releases: []*Release{ | ||
{ | ||
Name: "default", | ||
NamespaceMgmt: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
Metadata: NSMetadata{ | ||
Annotations: map[string]string{ | ||
"release-annotation": "release-value", | ||
}, | ||
Labels: map[string]string{ | ||
"release-label": "release-value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "release2", | ||
NamespaceMgmt: &NamespaceConfig{}, | ||
}, | ||
}, | ||
NamespaceMgmt: &NamespaceMgmt{ | ||
Default: &NamespaceConfig{ | ||
Settings: NSSettings{}, | ||
Metadata: NSMetadata{ | ||
Annotations: map[string]string{ | ||
"course-annotation": "course-value", | ||
}, | ||
Labels: map[string]string{ | ||
"course-label": "course-value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &FileV2{ | ||
NamespaceMgmt: &NamespaceMgmt{ | ||
Default: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
Metadata: NSMetadata{ | ||
Annotations: map[string]string{ | ||
"course-annotation": "course-value", | ||
}, | ||
Labels: map[string]string{ | ||
"course-label": "course-value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Releases: []*Release{ | ||
{ | ||
Name: "default", | ||
NamespaceMgmt: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
Metadata: NSMetadata{ | ||
Annotations: map[string]string{ | ||
"course-annotation": "course-value", | ||
"release-annotation": "release-value", | ||
}, | ||
Labels: map[string]string{ | ||
"course-label": "course-value", | ||
"release-label": "release-value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "release2", | ||
NamespaceMgmt: &NamespaceConfig{ | ||
Settings: NSSettings{ | ||
Overwrite: boolPtr(false), | ||
}, | ||
Metadata: NSMetadata{ | ||
Annotations: map[string]string{ | ||
"course-annotation": "course-value", | ||
}, | ||
Labels: map[string]string{ | ||
"course-label": "course-value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.file.populateNamespaceManagement() | ||
assert.EqualValues(t, tt.want, tt.file) | ||
}) | ||
} | ||
} |
Oops, something went wrong.