Skip to content

Commit d1e7bb2

Browse files
authored
Merge pull request kubernetes#60771 from jennybuckley/kube-up-broken-webhook
Use semantic equality in client ca post start hook
2 parents ea38af7 + d10ee7c commit d1e7bb2

File tree

4 files changed

+189
-2
lines changed

4 files changed

+189
-2
lines changed

pkg/master/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ go_library(
104104
"//staging/src/k8s.io/api/storage/v1:go_default_library",
105105
"//staging/src/k8s.io/api/storage/v1alpha1:go_default_library",
106106
"//staging/src/k8s.io/api/storage/v1beta1:go_default_library",
107+
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
107108
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
108109
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
109110
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",

pkg/master/client_ca_hook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package master
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"reflect"
2322
"time"
2423

2524
corev1 "k8s.io/api/core/v1"
25+
apiequality "k8s.io/apimachinery/pkg/api/equality"
2626
apierrors "k8s.io/apimachinery/pkg/api/errors"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -132,7 +132,7 @@ func writeConfigMap(client corev1client.ConfigMapsGetter, name string, data map[
132132
return err
133133
}
134134

135-
if !reflect.DeepEqual(existing.Data, data) {
135+
if !apiequality.Semantic.DeepEqual(existing.Data, data) {
136136
existing.Data = data
137137
_, err = client.ConfigMaps(metav1.NamespaceSystem).Update(existing)
138138
}

test/integration/apiserver/admissionwebhook/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go_test(
44
name = "go_default_test",
55
srcs = [
66
"admission_test.go",
7+
"broken_webhook_test.go",
78
"main_test.go",
89
],
910
tags = [
@@ -12,8 +13,10 @@ go_test(
1213
],
1314
deps = [
1415
"//cmd/kube-apiserver/app/options:go_default_library",
16+
"//cmd/kube-apiserver/app/testing:go_default_library",
1517
"//staging/src/k8s.io/api/admission/v1beta1:go_default_library",
1618
"//staging/src/k8s.io/api/admissionregistration/v1beta1:go_default_library",
19+
"//staging/src/k8s.io/api/apps/v1:go_default_library",
1720
"//staging/src/k8s.io/api/apps/v1beta1:go_default_library",
1821
"//staging/src/k8s.io/api/core/v1:go_default_library",
1922
"//staging/src/k8s.io/api/extensions/v1beta1:go_default_library",
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package admissionwebhook
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
"time"
23+
24+
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
25+
appsv1 "k8s.io/api/apps/v1"
26+
corev1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/client-go/kubernetes"
29+
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
30+
"k8s.io/kubernetes/test/integration/framework"
31+
)
32+
33+
var (
34+
brokenWebhookName = "integration-broken-webhook-test-webhook-config"
35+
deploymentNamePrefix = "integration-broken-webhook-test-deployment"
36+
)
37+
38+
func TestBrokenWebhook(t *testing.T) {
39+
var tearDownFn kubeapiservertesting.TearDownFunc
40+
defer func() {
41+
if tearDownFn != nil {
42+
tearDownFn()
43+
}
44+
}()
45+
46+
etcdConfig := framework.SharedEtcd()
47+
server := kubeapiservertesting.StartTestServerOrDie(t, nil, nil, etcdConfig)
48+
tearDownFn = server.TearDownFn
49+
50+
client, err := kubernetes.NewForConfig(server.ClientConfig)
51+
if err != nil {
52+
t.Fatalf("unexpected error: %v", err)
53+
}
54+
55+
t.Logf("Creating Deployment to ensure apiserver is functional")
56+
_, err = client.AppsV1().Deployments("default").Create(exampleDeployment(generateDeploymentName(0)))
57+
if err != nil {
58+
t.Fatalf("Failed to create deployment: %v", err)
59+
}
60+
61+
t.Logf("Creating Broken Webhook that will block all operations on all objects")
62+
_, err = client.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Create(brokenWebhookConfig(brokenWebhookName))
63+
if err != nil {
64+
t.Fatalf("Failed to register broken webhook: %v", err)
65+
}
66+
67+
// There is no guarantee on how long it takes the apiserver to honor the configuration and there is
68+
// no API to determine if the configuration is being honored, so we will just wait 10s, which is long enough
69+
// in most cases.
70+
time.Sleep(10 * time.Second)
71+
72+
// test whether the webhook blocks requests
73+
t.Logf("Attempt to create Deployment which should fail due to the webhook")
74+
_, err = client.AppsV1().Deployments("default").Create(exampleDeployment(generateDeploymentName(1)))
75+
if err == nil {
76+
t.Fatalf("Expected the broken webhook to cause creating a deployment to fail, but it succeeded.")
77+
}
78+
79+
t.Logf("Restarting apiserver")
80+
tearDownFn = nil
81+
server.TearDownFn()
82+
server = kubeapiservertesting.StartTestServerOrDie(t, nil, nil, etcdConfig)
83+
tearDownFn = server.TearDownFn
84+
85+
client, err = kubernetes.NewForConfig(server.ClientConfig)
86+
if err != nil {
87+
t.Fatalf("unexpected error: %v", err)
88+
}
89+
90+
// test whether the webhook still blocks requests after restarting
91+
t.Logf("Attempt again to create Deployment which should fail due to the webhook")
92+
_, err = client.AppsV1().Deployments("default").Create(exampleDeployment(generateDeploymentName(2)))
93+
if err == nil {
94+
t.Fatalf("Expected the broken webhook to cause creating a deployment to fail, but it succeeded.")
95+
}
96+
97+
t.Logf("Deleting the broken webhook to fix the cluster")
98+
err = client.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Delete(brokenWebhookName, nil)
99+
if err != nil {
100+
t.Fatalf("Failed to delete broken webhook: %v", err)
101+
}
102+
103+
// The webhook deletion is honored in 10s.
104+
time.Sleep(10 * time.Second)
105+
106+
// test if the deleted webhook no longer blocks requests
107+
t.Logf("Creating Deployment to ensure webhook is deleted")
108+
_, err = client.AppsV1().Deployments("default").Create(exampleDeployment(generateDeploymentName(3)))
109+
if err != nil {
110+
t.Fatalf("Failed to create deployment: %v", err)
111+
}
112+
}
113+
114+
func generateDeploymentName(suffix int) string {
115+
return fmt.Sprintf("%v-%v", deploymentNamePrefix, suffix)
116+
}
117+
118+
func exampleDeployment(name string) *appsv1.Deployment {
119+
var replicas int32 = 1
120+
return &appsv1.Deployment{
121+
TypeMeta: metav1.TypeMeta{
122+
Kind: "Deployment",
123+
APIVersion: "apps/v1",
124+
},
125+
ObjectMeta: metav1.ObjectMeta{
126+
Namespace: "default",
127+
Name: name,
128+
},
129+
Spec: appsv1.DeploymentSpec{
130+
Replicas: &replicas,
131+
Selector: &metav1.LabelSelector{
132+
MatchLabels: map[string]string{"foo": "bar"},
133+
},
134+
Template: corev1.PodTemplateSpec{
135+
ObjectMeta: metav1.ObjectMeta{
136+
Labels: map[string]string{"foo": "bar"},
137+
},
138+
Spec: corev1.PodSpec{
139+
Containers: []corev1.Container{
140+
{
141+
Name: "foo",
142+
Image: "foo",
143+
},
144+
},
145+
},
146+
},
147+
},
148+
}
149+
}
150+
151+
func brokenWebhookConfig(name string) *admissionregistrationv1beta1.ValidatingWebhookConfiguration {
152+
var path string
153+
var failurePolicy admissionregistrationv1beta1.FailurePolicyType = admissionregistrationv1beta1.Fail
154+
return &admissionregistrationv1beta1.ValidatingWebhookConfiguration{
155+
ObjectMeta: metav1.ObjectMeta{
156+
Name: name,
157+
},
158+
Webhooks: []admissionregistrationv1beta1.Webhook{
159+
{
160+
Name: "broken-webhook.k8s.io",
161+
Rules: []admissionregistrationv1beta1.RuleWithOperations{{
162+
Operations: []admissionregistrationv1beta1.OperationType{admissionregistrationv1beta1.OperationAll},
163+
Rule: admissionregistrationv1beta1.Rule{
164+
APIGroups: []string{"*"},
165+
APIVersions: []string{"*"},
166+
Resources: []string{"*/*"},
167+
},
168+
}},
169+
// This client config references a non existent service
170+
// so it should always fail.
171+
ClientConfig: admissionregistrationv1beta1.WebhookClientConfig{
172+
Service: &admissionregistrationv1beta1.ServiceReference{
173+
Namespace: "default",
174+
Name: "invalid-webhook-service",
175+
Path: &path,
176+
},
177+
CABundle: nil,
178+
},
179+
FailurePolicy: &failurePolicy,
180+
},
181+
},
182+
}
183+
}

0 commit comments

Comments
 (0)