|
| 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