/
cert_manager.go
305 lines (254 loc) · 9.14 KB
/
cert_manager.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright 2019 The Kubernetes 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 cluster
import (
"time"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
manifests "sigs.k8s.io/cluster-api/cmd/clusterctl/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
utilresource "sigs.k8s.io/cluster-api/util/resource"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
embeddedCertManagerManifestPath = "cmd/clusterctl/config/assets/cert-manager.yaml"
waitCertManagerInterval = 1 * time.Second
waitCertManagerDefaultTimeout = 10 * time.Minute
certManagerImageComponent = "cert-manager"
timeoutConfigKey = "cert-manager-timeout"
)
// CertManagerClient has methods to work with cert-manager components in the cluster.
type CertManagerClient interface {
// EnsureWebhook makes sure the cert-manager Webhook is Available in a cluster:
// this is a requirement to install a new provider
EnsureWebhook() error
// Images return the list of images required for installing the cert-manager.
Images() ([]string, error)
}
// certManagerClient implements CertManagerClient .
type certManagerClient struct {
configClient config.Client
proxy Proxy
pollImmediateWaiter PollImmediateWaiter
}
// Ensure certManagerClient implements the CertManagerClient interface.
var _ CertManagerClient = &certManagerClient{}
// newCertMangerClient returns a certManagerClient.
func newCertMangerClient(configClient config.Client, proxy Proxy, pollImmediateWaiter PollImmediateWaiter) *certManagerClient {
return &certManagerClient{
configClient: configClient,
proxy: proxy,
pollImmediateWaiter: pollImmediateWaiter,
}
}
// Images return the list of images required for installing the cert-manager.
func (cm *certManagerClient) Images() ([]string, error) {
// Checks if the cert-manager web-hook already exists, if yes, no additional images are required for the web-hook.
// Nb. we are ignoring the error so this operation can support listing images even if there is no an existing management cluster;
// in case there is no an existing management cluster, we assume there is no web-hook installed in the cluster.
hasWebhook, _ := cm.hasWebhook()
if hasWebhook {
return []string{}, nil
}
// Gets the cert-manager objects from the embedded assets.
objs, err := cm.getManifestObjs()
if err != nil {
return []string{}, nil
}
images, err := util.InspectImages(objs)
if err != nil {
return nil, err
}
return images, nil
}
// EnsureWebhook makes sure the cert-manager Web-hook is Available in a cluster:
// this is a requirement to install a new provider
// Nb. In order to provide a simpler out-of-the box experience, the cert-manager manifest
// is embedded in the clusterctl binary.
func (cm *certManagerClient) EnsureWebhook() error {
log := logf.Log
// Checks if the cert-manager web-hook already exists, if yes, exit immediately
hasWebhook, err := cm.hasWebhook()
if err != nil {
return err
}
if hasWebhook {
return nil
}
// Otherwise install cert-manager
log.Info("Installing cert-manager")
// Gets the cert-manager objects from the embedded assets.
objs, err := cm.getManifestObjs()
if err != nil {
return nil
}
// installs the web-hook
createCertManagerBackoff := newWriteBackoff()
objs = utilresource.SortForCreate(objs)
for i := range objs {
o := objs[i]
log.V(5).Info("Creating", logf.UnstructuredToValues(o)...)
// Create the Kubernetes object.
// Nb. The operation is wrapped in a retry loop to make ensureCerts more resilient to unexpected conditions.
if err := retryWithExponentialBackoff(createCertManagerBackoff, func() error {
return cm.createObj(o)
}); err != nil {
return err
}
}
// Waits for for the cert-manager web-hook to be available.
log.Info("Waiting for cert-manager to be available...")
if err := cm.pollImmediateWaiter(waitCertManagerInterval, cm.getWaitTimeout(), func() (bool, error) {
webhook, err := cm.getWebhook()
if err != nil {
//Nb. we are ignoring the error so the pollImmediateWaiter will execute another retry
return false, nil
}
if webhook == nil {
return false, nil
}
isWebhookAvailable, err := cm.isWebhookAvailable(webhook)
if err != nil {
return false, err
}
return isWebhookAvailable, nil
}); err != nil {
return err
}
return nil
}
func (cm *certManagerClient) getWaitTimeout() time.Duration {
log := logf.Log
timeout, err := cm.configClient.Variables().Get(timeoutConfigKey)
if err != nil {
return waitCertManagerDefaultTimeout
}
timeoutDuration, err := time.ParseDuration(timeout)
if err != nil {
log.Info("Invalid value set for ", timeoutConfigKey, timeout)
return waitCertManagerDefaultTimeout
}
return timeoutDuration
}
// getManifestObjs gets the cert-manager manifest, convert to unstructured objects, and fix images
func (cm *certManagerClient) getManifestObjs() ([]unstructured.Unstructured, error) {
yaml, err := manifests.Asset(embeddedCertManagerManifestPath)
if err != nil {
return nil, err
}
objs, err := utilyaml.ToUnstructured(yaml)
if err != nil {
return nil, errors.Wrap(err, "failed to parse yaml for cert-manager manifest")
}
objs, err = util.FixImages(objs, func(image string) (string, error) {
return cm.configClient.ImageMeta().AlterImage(certManagerImageComponent, image)
})
if err != nil {
return nil, errors.Wrap(err, "failed to apply image override to the cert-manager manifest")
}
return objs, nil
}
func (cm *certManagerClient) createObj(o unstructured.Unstructured) error {
c, err := cm.proxy.NewClient()
if err != nil {
return err
}
labels := o.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels[clusterctlv1.ClusterctlCoreLabelName] = "cert-manager"
o.SetLabels(labels)
if err = c.Create(ctx, &o); err != nil {
if apierrors.IsAlreadyExists(err) {
return nil
}
return errors.Wrapf(err, "failed to create cert-manager component: %s, %s/%s", o.GroupVersionKind(), o.GetNamespace(), o.GetName())
}
return nil
}
// getWebhook returns the cert-manager Webhook or nil if it does not exists.
func (cm *certManagerClient) getWebhook() (*unstructured.Unstructured, error) {
c, err := cm.proxy.NewClient()
if err != nil {
return nil, err
}
webhook := &unstructured.Unstructured{}
webhook.SetAPIVersion("apiregistration.k8s.io/v1beta1")
webhook.SetKind("APIService")
webhook.SetName("v1beta1.webhook.cert-manager.io")
key, err := client.ObjectKeyFromObject(webhook)
if err != nil {
return nil, err
}
err = c.Get(ctx, key, webhook)
if err != nil {
if !apierrors.IsNotFound(err) {
return nil, err
}
return nil, nil
}
return webhook, nil
}
// hasWebhook returns true if there is already a web-hook in the cluster
func (cm *certManagerClient) hasWebhook() (bool, error) {
// Checks if the cert-manager web-hook already exists, if yes, no additional images are required
webhook, err := cm.getWebhook()
if err != nil {
return false, errors.Wrap(err, "failed to check if the cert-manager web-hook exists")
}
if webhook != nil {
return true, nil
}
return false, nil
}
// isWebhookAvailable returns true if the cert-manager web-hook has the condition type:Available with status:True.
// This is required to check the web-hook is working and ready to accept requests.
func (cm *certManagerClient) isWebhookAvailable(webhook *unstructured.Unstructured) (bool, error) {
conditions, found, err := unstructured.NestedSlice(webhook.Object, "status", "conditions")
if err != nil {
return false, errors.Wrap(err, "invalid cert-manager web-hook: failed to get conditions")
}
// if status.conditions does not exists, we assume the web-hook is still starting
if !found {
return false, nil
}
// look for the condition with type:Available and status:True or return false
for _, condition := range conditions {
conditionMap, ok := condition.(map[string]interface{})
if !ok {
return false, errors.Wrap(err, "invalid cert-manager web-hook: failed to parse conditions")
}
conditionType, ok := conditionMap["type"]
if !ok {
return false, errors.Wrap(err, "invalid cert-manager web-hook: there are conditions without the type field")
}
if conditionType != "Available" {
continue
}
conditionStatus, ok := conditionMap["status"]
if !ok {
return false, errors.Wrapf(err, "invalid cert-manager web-hook: there %q condition does not have the status field", "Available")
}
if conditionStatus == "True" {
return true, nil
}
return false, nil
}
return false, nil
}