generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MWH and update VWH on included-namespace label
Part of 13. Fixes 37. Add mutating webhook to set the correct `included-namespace` label on non-excluded namespaces if it's missing. Leave all other situation as is and let the validating webhook to do the validation. Update namespace validator to allow all other changes if the illegal included-namespace label is unchanged. Since namespace mutator ensures the label exists on included namespaces, update rules to ignore the case when the label is missing on the included namespaces. Add old instance field to the namespace validator. Decode it from request to compare with the new instance. Add more test cases since we now have more combination of cases to test. Update the namespace tree label update logs and function name in the HC reconciler to make it clear that the HC reconciler only updates the tree labels and won't update included-namespace label if the MWH works fine. Tested by `make test` with new test cases. Also tested manually by applying namespace manifests with different labels, viewed the logs and verified the MWH and VWH work fine.
- Loading branch information
1 parent
ba15870
commit 28fec50
Showing
13 changed files
with
363 additions
and
104 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
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" | ||
) | ||
|
||
// NamespaceIncludedLabelServingPath is where the mutator will run. Must be kept | ||
// in sync with the kubebuilder markers below. | ||
const ( | ||
NamespaceIncludedLabelServingPath = "/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 NamespaceIncludedLabel struct { | ||
Log logr.Logger | ||
decoder *admission.Decoder | ||
} | ||
|
||
// Handle implements the mutating webhook. | ||
func (n *NamespaceIncludedLabel) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
log := n.Log.WithValues("namespace", req.Name) | ||
ns := &corev1.Namespace{} | ||
err := n.decoder.Decode(req, ns) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
n.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 (n *NamespaceIncludedLabel) 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 (n *NamespaceIncludedLabel) InjectDecoder(d *admission.Decoder) error { | ||
n.decoder = d | ||
return nil | ||
} |
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,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 := &NamespaceIncludedLabel{} | ||
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 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(NamespaceIncludedLabelServingPath, &webhook.Admission{Handler: &NamespaceIncludedLabel{ | ||
Log: ctrl.Log.WithName("mutators").WithName("NamespaceIncludedLabel"), | ||
}}) | ||
} |
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
Oops, something went wrong.