This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
config.go
231 lines (205 loc) · 7.41 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
// 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 validation
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"github.com/ghodss/yaml"
"k8s.io/api/admissionregistration/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
admissionregistration "k8s.io/client-go/kubernetes/typed/admissionregistration/v1beta1"
"k8s.io/client-go/tools/cache"
"istio.io/istio/pkg/log"
)
var scope = log.RegisterScope("validation", "CRD validation debugging", 0)
// 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.ValidatingWebhookConfiguration{},
0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(_ interface{}) {
webhookChangedCh <- struct{}{}
},
UpdateFunc: func(prev, curr interface{}) {
prevObj := prev.(*v1beta1.ValidatingWebhookConfiguration)
currObj := curr.(*v1beta1.ValidatingWebhookConfiguration)
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 {
scope.Error("validatingwebhookconfiguration update failed: no configuration loaded")
reportValidationConfigUpdateError(errors.New("no configuration loaded"))
return
}
client := wh.clientset.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations()
updated, err := createOrUpdateWebhookConfigHelper(client, wh.webhookConfiguration)
if err != nil {
scope.Errorf("%v validatingwebhookconfiguration update failed: %v", wh.webhookConfiguration.Name, err)
reportValidationConfigUpdateError(err)
} else if updated {
scope.Infof("%v validatingwebhookconfiguration updated", wh.webhookConfiguration.Name)
reportValidationConfigUpdate()
}
}
// Create the specified validatingwebhookconfiguration resource or, if the resource
// already exists, update it's contents with the desired state.
func createOrUpdateWebhookConfigHelper(
client admissionregistration.ValidatingWebhookConfigurationInterface,
webhookConfiguration *v1beta1.ValidatingWebhookConfiguration,
) (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.ValidatingWebhookConfiguration)
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 validatingwebhookconfiguratio and save for subsequent calls to createOrUpdateWebhookConfig.
func (wh *Webhook) rebuildWebhookConfig() error {
webhookConfig, err := rebuildWebhookConfigHelper(
wh.caFile,
wh.webhookConfigFile,
wh.webhookName,
wh.ownerRefs)
if err != nil {
reportValidationConfigLoadError(err)
scope.Errorf("validatingwebhookconfiguration (re)load failed: %v", err)
return err
}
wh.webhookConfiguration = webhookConfig
// pretty-print the validatingwebhookconfiguration as YAML
var webhookYAML string
if b, err := yaml.Marshal(wh.webhookConfiguration); err == nil {
webhookYAML = string(b)
}
scope.Infof("%v validatingwebhookconfiguration (re)loaded: \n%v",
wh.webhookConfiguration.Name, webhookYAML)
reportValidationConfigLoad()
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 validatingwebhookconfiguration from the specified CA
// and webhook config files. This also ensures the OwnerReferences is set
// so that the cluster-scoped validatingwebhookconfiguration is properly
// cleaned up when istio-galley is deleted.
func rebuildWebhookConfigHelper(
caFile, webhookConfigFile, webhookName string,
ownerRefs []metav1.OwnerReference,
) (*v1beta1.ValidatingWebhookConfiguration, error) {
// load and validate configuration
webhookConfigData, err := ioutil.ReadFile(webhookConfigFile)
if err != nil {
return nil, err
}
var webhookConfig v1beta1.ValidatingWebhookConfiguration
if err := yaml.Unmarshal(webhookConfigData, &webhookConfig); err != nil {
return nil, fmt.Errorf("could not decode validatingwebhookconfiguration 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 validation 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() // nolint: errcheck
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 {
reportValidationCertKeyUpdateError(err)
scope.Errorf("Cert/Key reload error: %v", err)
return
}
wh.mu.Lock()
wh.cert = &pair
wh.mu.Unlock()
reportValidationCertKeyUpdate()
scope.Info("Cert and Key reloaded")
}