Unverified Commit 7b83d6eb authored by k8s-ci-robot's avatar k8s-ci-robot Committed by GitHub

Merge pull request #62143 from jennybuckley/mutating-webhooks-on-webhooks

Add e2e test for mutating webhooks affecting webhook-config objects
parents 4b859010 1ba644ef
......@@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"addlabel.go",
"alwaysdeny.go",
"config.go",
"configmap.go",
......
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"github.com/golang/glog"
"k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
addFirstLabelPatch string = `[
{ "op": "add", "path": "/metadata/labels", "value": {"added-label": "yes"}}
]`
addAdditionalLabelPatch string = `[
{ "op": "add", "path": "/metadata/labels/added-label", "value": "yes" }
]`
)
// Add a label {"added-label": "yes"} to the object
func addLabel(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
glog.V(2).Info("calling add-label")
obj := struct {
metav1.ObjectMeta
Data map[string]string
}{}
raw := ar.Request.Object.Raw
err := json.Unmarshal(raw, &obj)
if err != nil {
glog.Error(err)
return toAdmissionResponse(err)
}
reviewResponse := v1beta1.AdmissionResponse{}
reviewResponse.Allowed = true
if len(obj.ObjectMeta.Labels) == 0 {
reviewResponse.Patch = []byte(addFirstLabelPatch)
} else {
reviewResponse.Patch = []byte(addAdditionalLabelPatch)
}
pt := v1beta1.PatchTypeJSONPatch
reviewResponse.PatchType = &pt
return &reviewResponse
}
......@@ -95,6 +95,10 @@ func serveAlwaysDeny(w http.ResponseWriter, r *http.Request) {
serve(w, r, alwaysDeny)
}
func serveAddLabel(w http.ResponseWriter, r *http.Request) {
serve(w, r, addLabel)
}
func servePods(w http.ResponseWriter, r *http.Request) {
serve(w, r, admitPods)
}
......@@ -133,6 +137,7 @@ func main() {
flag.Parse()
http.HandleFunc("/always-deny", serveAlwaysDeny)
http.HandleFunc("/add-label", serveAddLabel)
http.HandleFunc("/pods", servePods)
http.HandleFunc("/pods/attach", serveAttachingPods)
http.HandleFunc("/mutating-pods", serveMutatePods)
......
......@@ -82,7 +82,7 @@ var (
PrivateRegistry = registry.PrivateRegistry
sampleRegistry = registry.SampleRegistry
AdmissionWebhook = ImageConfig{e2eRegistry, "webhook", "1.12v2"}
AdmissionWebhook = ImageConfig{e2eRegistry, "webhook", "1.13v1"}
APIServer = ImageConfig{e2eRegistry, "sample-apiserver", "1.0"}
AppArmorLoader = ImageConfig{e2eRegistry, "apparmor-loader", "1.0"}
BusyBox = ImageConfig{dockerLibraryRegistry, "busybox", "1.29"}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment