-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the controllers.namespaceLabels value
- This lets the operator set custom labels on korifi space namespaces - For instance disabling istio sidecar injection or changing the pod-security-admission levels Co-authored-by: Kieron Browne <kbrowne@vmware.com> Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com>
- Loading branch information
1 parent
4e1f92f
commit c49984a
Showing
15 changed files
with
213 additions
and
31 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
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
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,44 @@ | ||
package labels | ||
|
||
// Compiler is a reusable map composer. It persists a set of defaults, which | ||
// can be overridden when calling Compile() to produce the combined map. | ||
type Compiler struct { | ||
defaults map[string]string | ||
} | ||
|
||
func NewCompiler() Compiler { | ||
return Compiler{ | ||
defaults: map[string]string{}, | ||
} | ||
} | ||
|
||
func (o Compiler) Defaults(defaults map[string]string) Compiler { | ||
defaultsCopy := copyMap(o.defaults) | ||
for k, v := range defaults { | ||
defaultsCopy[k] = v | ||
} | ||
return Compiler{ | ||
defaults: defaultsCopy, | ||
} | ||
} | ||
|
||
func (o Compiler) Compile(overrides map[string]string) map[string]string { | ||
res := map[string]string{} | ||
for k, v := range o.defaults { | ||
res[k] = v | ||
} | ||
for k, v := range overrides { | ||
res[k] = v | ||
} | ||
|
||
return res | ||
} | ||
|
||
func copyMap(src map[string]string) map[string]string { | ||
dst := map[string]string{} | ||
for k, v := range src { | ||
dst[k] = v | ||
} | ||
|
||
return dst | ||
} |
13 changes: 13 additions & 0 deletions
13
controllers/controllers/workloads/labels/compiler_suite_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,13 @@ | ||
package labels_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestLabels(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Labels Suite") | ||
} |
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,84 @@ | ||
package labels_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"code.cloudfoundry.org/korifi/controllers/controllers/workloads/labels" | ||
) | ||
|
||
var _ = Describe("Labels", func() { | ||
var ( | ||
compiler labels.Compiler | ||
override map[string]string | ||
output map[string]string | ||
) | ||
|
||
BeforeEach(func() { | ||
override = nil | ||
compiler = labels.NewCompiler() | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
output = compiler.Compile(override) | ||
}) | ||
|
||
It("will return empty if no defaults or override given", func() { | ||
Expect(output).To(BeEmpty()) | ||
}) | ||
|
||
When("default values are provided", func() { | ||
BeforeEach(func() { | ||
compiler = compiler.Defaults(map[string]string{ | ||
"foo": "bar", | ||
}) | ||
}) | ||
|
||
It("puts the default in the output", func() { | ||
Expect(output).To(HaveKeyWithValue("foo", "bar")) | ||
}) | ||
}) | ||
|
||
When("default values are provided twice", func() { | ||
var oldCompiler labels.Compiler | ||
|
||
BeforeEach(func() { | ||
oldCompiler = compiler.Defaults(map[string]string{ | ||
"foo": "bar", | ||
"hello": "there", | ||
}) | ||
compiler = oldCompiler.Defaults(map[string]string{ | ||
"foo": "baz", | ||
}) | ||
}) | ||
|
||
It("puts the latest default in the output", func() { | ||
Expect(output).To(HaveKeyWithValue("foo", "baz")) | ||
Expect(output).To(HaveKeyWithValue("hello", "there")) | ||
}) | ||
|
||
It("is immutable", func() { | ||
Expect(oldCompiler.Compile(nil)).To(HaveKeyWithValue("foo", "bar")) | ||
Expect(oldCompiler.Compile(nil)).To(HaveKeyWithValue("hello", "there")) | ||
}) | ||
}) | ||
|
||
When("a default value is overridden", func() { | ||
BeforeEach(func() { | ||
compiler = compiler.Defaults(map[string]string{ | ||
"foo": "bar", | ||
}) | ||
override = map[string]string{ | ||
"foo": "baz", | ||
} | ||
}) | ||
|
||
It("will use overridden value", func() { | ||
Expect(output).To(HaveKeyWithValue("foo", "baz")) | ||
}) | ||
|
||
It("will not accidently store the override", func() { | ||
Expect(compiler.Compile(nil)).To(HaveKeyWithValue("foo", "bar")) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.