This repository was archived by the owner on Apr 17, 2025. It is now read-only.
generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 115
Add MWH and update VWH on included-namespace label #39
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
package mutators | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/go-logr/logr" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2" | ||
"sigs.k8s.io/hierarchical-namespaces/internal/config" | ||
) | ||
|
||
// NamespaceMutatorServingPath is where the mutator will run. Must be kept in | ||
// sync with the kubebuilder markers below. | ||
const ( | ||
NamespaceMutatorServingPath = "/mutate-namespace" | ||
) | ||
|
||
// Note: the mutating webhook FAILS OPEN. This means that if the webhook goes | ||
// down, all further changes are allowed. (An empty line has to be kept below | ||
// the kubebuilder marker for the controller-gen to generate manifests.) | ||
// | ||
// +kubebuilder:webhook:admissionReviewVersions=v1,path=/mutate-namespace,mutating=true,failurePolicy=ignore,groups="",resources=namespaces,sideEffects=None,verbs=create;update,versions=v1,name=namespacelabel.hnc.x-k8s.io | ||
|
||
type Namespace struct { | ||
Log logr.Logger | ||
decoder *admission.Decoder | ||
} | ||
|
||
// Handle implements the mutating webhook. | ||
func (m *Namespace) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
log := m.Log.WithValues("namespace", req.Name) | ||
ns := &corev1.Namespace{} | ||
err := m.decoder.Decode(req, ns) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
m.handle(log, ns) | ||
marshaledNS, err := json.Marshal(ns) | ||
if err != nil { | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledNS) | ||
} | ||
|
||
// handle implements the non-boilerplate logic of this mutator, allowing it to | ||
// be more easily unit tested (ie without constructing a full admission.Request). | ||
// Currently, we only add `included-namespace` label to non-excluded namespaces | ||
// if the label is missing. | ||
func (m *Namespace) handle(log logr.Logger, ns *corev1.Namespace) { | ||
// Early exit if the namespace is excluded. | ||
if config.ExcludedNamespaces[ns.Name] { | ||
return | ||
} | ||
|
||
// Add label if the namespace doesn't have it. | ||
if _, hasLabel := ns.Labels[api.LabelIncludedNamespace]; !hasLabel { | ||
if ns.Labels == nil { | ||
ns.Labels = map[string]string{} | ||
} | ||
log.Info("Not an excluded namespace; set included-namespace label.") | ||
ns.Labels[api.LabelIncludedNamespace] = "true" | ||
} | ||
} | ||
|
||
// InjectDecoder injects the decoder. | ||
func (m *Namespace) InjectDecoder(d *admission.Decoder) error { | ||
m.decoder = d | ||
return nil | ||
} |
This file contains hidden or 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,49 @@ | ||
package mutators | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
|
||
api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2" | ||
"sigs.k8s.io/hierarchical-namespaces/internal/config" | ||
) | ||
|
||
func TestMutateNamespaceIncludedLabel(t *testing.T) { | ||
m := &Namespace{} | ||
l := zap.New() | ||
config.ExcludedNamespaces = map[string]bool{"excluded": true} | ||
|
||
tests := []struct { | ||
name string | ||
nsn string | ||
lval string | ||
expectlval string | ||
}{ | ||
{name: "Set label on non-excluded namespace without label", nsn: "a", expectlval: "true"}, | ||
{name: "No operation on non-excluded namespace with label (e.g. from a yaml file)", nsn: "a", lval: "true", expectlval: "true"}, | ||
{name: "No operation on non-excluded namespace with label with wrong value(e.g. from a yaml file)", nsn: "a", lval: "wrong", expectlval: "wrong"}, | ||
{name: "No operation on excluded namespace without label", nsn: "excluded"}, | ||
{name: "No operation on excluded namespace with label (e.g. from a yaml file)", nsn: "excluded", lval: "true", expectlval: "true"}, | ||
{name: "No operation on excluded namespace with label with wrong value (e.g. from a yaml file)", nsn: "excluded", lval: "wrong", expectlval: "wrong"}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
nsInst := &corev1.Namespace{} | ||
nsInst.Name = tc.nsn | ||
if tc.lval != "" { | ||
nsInst.SetLabels(map[string]string{api.LabelIncludedNamespace: tc.lval}) | ||
} | ||
|
||
// Test | ||
m.handle(l, nsInst) | ||
|
||
// Report | ||
g.Expect(nsInst.Labels[api.LabelIncludedNamespace]).Should(Equal(tc.expectlval)) | ||
}) | ||
} | ||
} |
This file contains hidden or 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,14 @@ | ||
package mutators | ||
|
||
import ( | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
) | ||
|
||
// Create creates the mutator. This function is called from main.go. | ||
func Create(mgr ctrl.Manager) { | ||
// Create mutator for namespace `included-namespace` label. | ||
mgr.GetWebhookServer().Register(NamespaceMutatorServingPath, &webhook.Admission{Handler: &Namespace{ | ||
Log: ctrl.Log.WithName("mutators").WithName("Namespace"), | ||
}}) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.