/
config.go
233 lines (207 loc) · 7.46 KB
/
config.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2018 Istio 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 webhook
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"k8s.io/apimachinery/pkg/fields"
"github.com/ghodss/yaml"
"k8s.io/api/admissionregistration/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
admissionregistration "k8s.io/client-go/kubernetes/typed/admissionregistration/v1beta1"
"k8s.io/client-go/tools/cache"
)
// Run an informer that watches the current webhook configuration
// for changes.
func (wh *Webhook) monitorWebhookChanges(stopC <-chan struct{}) chan struct{} {
webhookChangedCh := make(chan struct{}, 1000)
_, controller := cache.NewInformer(
wh.createInformerWebhookSource(wh.clientset, wh.webhookName),
&v1beta1.MutatingWebhookConfiguration{},
0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(_ interface{}) {
webhookChangedCh <- struct{}{}
},
UpdateFunc: func(prev, curr interface{}) {
prevObj := prev.(*v1beta1.MutatingWebhookConfiguration)
currObj := curr.(*v1beta1.MutatingWebhookConfiguration)
if prevObj.ResourceVersion != currObj.ResourceVersion {
webhookChangedCh <- struct{}{}
}
},
DeleteFunc: func(_ interface{}) {
webhookChangedCh <- struct{}{}
},
},
)
go controller.Run(stopC)
return webhookChangedCh
}
func (wh *Webhook) createOrUpdateWebhookConfig() {
if wh.webhookConfiguration == nil {
logError.Print("mutatingwebhookconfiguration update failed: no configuration loaded")
return
}
client := wh.clientset.AdmissionregistrationV1beta1().MutatingWebhookConfigurations()
updated, err := createOrUpdateWebhookConfigHelper(client, wh.webhookConfiguration)
if err != nil {
logError.Printf("%v mutatingwebhookconfiguration update failed: %v", wh.webhookConfiguration.Name, err)
} else if updated {
logInfo.Printf("%v mutatingwebhookconfiguration updated", wh.webhookConfiguration.Name)
}
}
func (wh *Webhook) createInformerWebhookSource(cl clientset.Interface, name string) cache.ListerWatcher {
return cache.NewListWatchFromClient(
cl.AdmissionregistrationV1beta1().RESTClient(),
"mutatingwebhookconfigurations",
"",
fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name=%s", name)))
}
// Create the specified mutatingwebhookconfiguration resource or, if the resource
// already exists, update it's contents with the desired state.
func createOrUpdateWebhookConfigHelper(
client admissionregistration.MutatingWebhookConfigurationInterface,
webhookConfiguration *v1beta1.MutatingWebhookConfiguration,
) (bool, error) {
current, err := client.Get(webhookConfiguration.Name, metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
if _, createErr := client.Create(webhookConfiguration); createErr != nil {
return false, createErr
}
return true, nil
}
return false, err
}
// Minimize the diff between the actual vs. desired state. Only copy the relevant fields
// that we want reconciled and ignore everything else, e.g. labels, selectors.
updated := current.DeepCopyObject().(*v1beta1.MutatingWebhookConfiguration)
updated.Webhooks = webhookConfiguration.Webhooks
updated.OwnerReferences = webhookConfiguration.OwnerReferences
if !reflect.DeepEqual(updated, current) {
_, err := client.Update(updated)
return true, err
}
return false, nil
}
// Rebuild the mutatingwebhookconfiguratio and save for subsequent calls to createOrUpdateWebhookConfig.
func (wh *Webhook) rebuildWebhookConfig() error {
webhookConfig, err := rebuildWebhookConfigHelper(
wh.caFile,
wh.webhookConfigFile,
wh.webhookName,
wh.ownerRefs)
if wh.webhookConfiguration == nil || !reflect.DeepEqual(webhookConfig, wh.webhookConfiguration) {
if err != nil {
logError.Printf("mutatingwebhookconfiguration (re)load failed: %v", err)
return err
}
wh.webhookConfiguration = webhookConfig
// pretty-print the mutatingwebhookconfiguration as YAML
var webhookYAML string
if b, err := yaml.Marshal(wh.webhookConfiguration); err == nil {
webhookYAML = string(b)
}
logInfo.Printf("%v mutatingwebhookconfiguration (re)loaded: \n%v",
wh.webhookConfiguration.Name, webhookYAML)
}
return nil
}
// Load the CA Cert PEM from the input reader. This also verifies that the certificate is a validate x509 cert.
func loadCaCertPem(in io.Reader) ([]byte, error) {
caCertPemBytes, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
block, _ := pem.Decode(caCertPemBytes)
if block == nil {
return nil, errors.New("could not decode pem")
}
if block.Type != "CERTIFICATE" {
return nil, fmt.Errorf("ca bundle contains wrong pem type: %q", block.Type)
}
if _, err := x509.ParseCertificate(block.Bytes); err != nil {
return nil, fmt.Errorf("ca bundle contains invalid x509 certificate: %v", err)
}
return caCertPemBytes, nil
}
// Rebuild the desired mutatingwebhookconfiguration from the specified CA
// and webhook config files. This also ensures the OwnerReferences is set
// so that the cluster-scoped mutatingwebhookconfiguration is properly
// cleaned up when the deployment is deleted.
func rebuildWebhookConfigHelper(
caFile, webhookConfigFile, webhookName string,
ownerRefs []metav1.OwnerReference,
) (*v1beta1.MutatingWebhookConfiguration, error) {
// load and validate configuration
webhookConfigData, err := ioutil.ReadFile(webhookConfigFile)
if err != nil {
return nil, err
}
var webhookConfig v1beta1.MutatingWebhookConfiguration
if err := yaml.Unmarshal(webhookConfigData, &webhookConfig); err != nil {
return nil, fmt.Errorf("could not decode mutatingwebhookconfiguration from %v: %v",
webhookConfigFile, err)
}
// fill in missing defaults to minimize desired vs. actual diffs later.
for i := 0; i < len(webhookConfig.Webhooks); i++ {
if webhookConfig.Webhooks[i].FailurePolicy == nil {
failurePolicy := v1beta1.Fail
webhookConfig.Webhooks[i].FailurePolicy = &failurePolicy
}
if webhookConfig.Webhooks[i].NamespaceSelector == nil {
webhookConfig.Webhooks[i].NamespaceSelector = &metav1.LabelSelector{}
}
}
// the webhook name is fixed at startup time
webhookConfig.Name = webhookName
// update ownerRefs so configuration is cleaned up when the deployment is deleted.
webhookConfig.OwnerReferences = ownerRefs
in, err := os.Open(caFile)
if err != nil {
return nil, fmt.Errorf("failed to read ca bundle from %v: %v", caFile, err)
}
defer in.Close()
caPem, err := loadCaCertPem(in)
if err != nil {
return nil, err
}
// patch the ca-cert into the user provided configuration
for i := range webhookConfig.Webhooks {
webhookConfig.Webhooks[i].ClientConfig.CABundle = caPem
}
return &webhookConfig, nil
}
// Reload the server's cert/key for TLS from file.
func (wh *Webhook) reloadKeyCert() {
pair, err := tls.LoadX509KeyPair(wh.certFile, wh.keyFile)
if err != nil {
logError.Printf("Cert/Key reload error: %v", err)
return
}
wh.mu.Lock()
wh.cert = &pair
wh.mu.Unlock()
logInfo.Printf("Cert and Key reloaded")
}