/
webhook.go
96 lines (86 loc) · 3.02 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2022 Google LLC
//
// 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 nomostest
import (
"fmt"
"time"
"kpt.dev/configsync/e2e/nomostest/testpredicates"
"kpt.dev/configsync/e2e/nomostest/testwatcher"
"kpt.dev/configsync/pkg/api/configmanagement"
"kpt.dev/configsync/pkg/core"
"kpt.dev/configsync/pkg/kinds"
"kpt.dev/configsync/pkg/metadata"
"kpt.dev/configsync/pkg/webhook/configuration"
)
// WaitForWebhookReadiness waits up to 3 minutes for the wehbook becomes ready.
// If the webhook still is not ready after 3 minutes, the test would fail.
func WaitForWebhookReadiness(nt *NT) {
err := nt.Watcher.WatchForCurrentStatus(kinds.Deployment(),
configuration.ShortName, configmanagement.ControllerNamespace,
testwatcher.WatchTimeout(3*time.Minute))
if err != nil {
nt.T.Fatal(err)
}
}
// StopWebhook removes the Config Sync ValidatingWebhookConfiguration object.
func StopWebhook(nt *NT) {
if *nt.WebhookDisabled {
return
}
webhookName := configuration.Name
webhookGK := "validatingwebhookconfigurations.admissionregistration.k8s.io"
out, err := nt.Shell.Kubectl("annotate", webhookGK, webhookName, fmt.Sprintf("%s=%s", metadata.WebhookconfigurationKey, metadata.WebhookConfigurationUpdateDisabled))
if err != nil {
nt.T.Fatalf("got `kubectl annotate %s %s %s=%s` error %v %s, want return nil",
webhookGK, webhookName, metadata.WebhookconfigurationKey, metadata.WebhookConfigurationUpdateDisabled, err, out)
}
err = nt.Watcher.WatchObject(kinds.ValidatingWebhookConfiguration(),
webhookName, "",
[]testpredicates.Predicate{
testpredicates.HasAnnotation(metadata.WebhookconfigurationKey, metadata.WebhookConfigurationUpdateDisabled),
},
testwatcher.WatchTimeout(30*time.Second))
if err != nil {
nt.T.Fatal(err)
}
out, err = nt.Shell.Kubectl("delete", webhookGK, webhookName)
if err != nil {
nt.T.Fatalf("got `kubectl delete %s %s` error %v %s, want return nil", webhookGK, webhookName, err, out)
}
err = nt.Watcher.WatchForNotFound(kinds.ValidatingWebhookConfiguration(),
webhookName, "",
testwatcher.WatchTimeout(30*time.Second))
if err != nil {
nt.T.Fatal(err)
}
*nt.WebhookDisabled = true
}
func installWebhook(nt *NT) error {
objs, err := parseConfigSyncManifests(nt)
if err != nil {
return err
}
for _, o := range objs {
labels := o.GetLabels()
if labels == nil || labels["app"] != "admission-webhook" {
continue
}
nt.T.Logf("installWebhook obj: %v", core.GKNN(o))
if err := nt.KubeClient.Apply(o); err != nil {
return err
}
}
*nt.WebhookDisabled = false
return nil
}