-
Notifications
You must be signed in to change notification settings - Fork 2k
/
sync.go
365 lines (325 loc) · 11.3 KB
/
sync.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
Copyright 2019 The Jetstack cert-manager contributors.
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 controller
import (
"context"
"fmt"
"reflect"
"strconv"
corev1 "k8s.io/api/core/v1"
extv1beta1 "k8s.io/api/extensions/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog"
"github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
"github.com/jetstack/cert-manager/pkg/util"
)
const (
// editInPlaceAnnotation is used to toggle the use of ingressClass instead
// of ingress on the created Certificate resource
editInPlaceAnnotation = "certmanager.k8s.io/acme-http01-edit-in-place"
// issuerNameAnnotation can be used to override the issuer specified on the
// created Certificate resource.
issuerNameAnnotation = "certmanager.k8s.io/issuer"
// clusterIssuerNameAnnotation can be used to override the issuer specified on the
// created Certificate resource. The Certificate will reference the
// specified *ClusterIssuer* instead of normal issuer.
clusterIssuerNameAnnotation = "certmanager.k8s.io/cluster-issuer"
// acmeIssuerChallengeTypeAnnotation can be used to override the default ACME challenge
// type to be used when the specified issuer is an ACME issuer
acmeIssuerChallengeTypeAnnotation = "certmanager.k8s.io/acme-challenge-type"
// acmeIssuerDNS01ProviderNameAnnotation can be used to override the default dns01 provider
// configured on the issuer if the challenge type is set to dns01
acmeIssuerDNS01ProviderNameAnnotation = "certmanager.k8s.io/acme-dns01-provider"
// acmeIssuerHTTP01IngressClassAnnotation can be used to override the http01 ingressClass
// if the challenge type is set to http01
acmeIssuerHTTP01IngressClassAnnotation = "certmanager.k8s.io/acme-http01-ingress-class"
ingressClassAnnotation = util.IngressKey
)
var ingressGVK = extv1beta1.SchemeGroupVersion.WithKind("Ingress")
func (c *Controller) Sync(ctx context.Context, ing *extv1beta1.Ingress) error {
if !shouldSync(ing, c.defaults.autoCertificateAnnotations) {
klog.Infof("Not syncing ingress %s/%s as it does not contain necessary annotations", ing.Namespace, ing.Name)
return nil
}
issuerName, issuerKind := c.issuerForIngress(ing)
if issuerName == "" {
c.Recorder.Eventf(ing, corev1.EventTypeWarning, "BadConfig", "Issuer name annotation is not set and a default issuer has not been configured")
return nil
}
issuer, err := c.helper.GetGenericIssuer(v1alpha1.ObjectReference{
Name: issuerName,
Kind: issuerKind,
}, ing.Namespace)
if apierrors.IsNotFound(err) {
c.Recorder.Eventf(ing, corev1.EventTypeWarning, "BadConfig", "%s resource %q not found", issuerKind, issuerName)
return nil
}
if err != nil {
return err
}
errs := c.validateIngress(ing)
if len(errs) > 0 {
errMsg := errs[0].Error()
if len(errs) > 1 {
errMsg = utilerrors.NewAggregate(errs).Error()
}
c.Recorder.Eventf(ing, corev1.EventTypeWarning, "BadConfig", errMsg)
return nil
}
newCrts, updateCrts, err := c.buildCertificates(ing, issuer, issuerKind)
if err != nil {
return err
}
for _, crt := range newCrts {
_, err := c.CMClient.CertmanagerV1alpha1().Certificates(crt.Namespace).Create(crt)
if err != nil {
return err
}
c.Recorder.Eventf(ing, corev1.EventTypeNormal, "CreateCertificate", "Successfully created Certificate %q", crt.Name)
}
for _, crt := range updateCrts {
_, err := c.CMClient.CertmanagerV1alpha1().Certificates(crt.Namespace).Update(crt)
if err != nil {
return err
}
c.Recorder.Eventf(ing, corev1.EventTypeNormal, "UpdateCertificate", "Successfully updated Certificate %q", crt.Name)
}
return nil
}
func (c *Controller) validateIngress(ing *extv1beta1.Ingress) []error {
var errs []error
if ing.Annotations != nil {
challengeType := ing.Annotations[acmeIssuerChallengeTypeAnnotation]
switch challengeType {
case "", "http01":
case "dns01":
providerName := ing.Annotations[acmeIssuerDNS01ProviderNameAnnotation]
if providerName == "" {
errs = append(errs, fmt.Errorf("No acme dns01 challenge provider specified"))
}
default:
errs = append(errs, fmt.Errorf("Invalid acme challenge type specified %q", challengeType))
}
}
for i, tls := range ing.Spec.TLS {
// validate the ingress TLS block
if len(tls.Hosts) == 0 {
errs = append(errs, fmt.Errorf("Secret %q for ingress TLS has no hosts specified", tls.SecretName))
}
if tls.SecretName == "" {
errs = append(errs, fmt.Errorf("TLS entry %d for hosts %v must specify a secretName", i, tls.Hosts))
}
}
return errs
}
func (c *Controller) buildCertificates(ing *extv1beta1.Ingress, issuer v1alpha1.GenericIssuer, issuerKind string) (new, update []*v1alpha1.Certificate, _ error) {
var newCrts []*v1alpha1.Certificate
var updateCrts []*v1alpha1.Certificate
for _, tls := range ing.Spec.TLS {
existingCrt, err := c.certificateLister.Certificates(ing.Namespace).Get(tls.SecretName)
if !apierrors.IsNotFound(err) && err != nil {
return nil, nil, err
}
crt := &v1alpha1.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: tls.SecretName,
Namespace: ing.Namespace,
Labels: ing.Labels,
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(ing, ingressGVK)},
},
Spec: v1alpha1.CertificateSpec{
DNSNames: tls.Hosts,
SecretName: tls.SecretName,
IssuerRef: v1alpha1.ObjectReference{
Name: issuer.GetObjectMeta().Name,
Kind: issuerKind,
},
},
}
err = c.setIssuerSpecificConfig(crt, issuer, ing, tls)
if err != nil {
return nil, nil, err
}
// check if a Certificate for this TLS entry already exists, and if it
// does then skip this entry
if existingCrt != nil {
klog.Infof("Certificate %q for ingress %q already exists", tls.SecretName, ing.Name)
if !certNeedsUpdate(existingCrt, crt) {
klog.Infof("Certificate %q for ingress %q is up to date", tls.SecretName, ing.Name)
continue
}
updateCrt := existingCrt.DeepCopy()
updateCrt.Spec.DNSNames = tls.Hosts
updateCrt.Spec.SecretName = tls.SecretName
updateCrt.Spec.IssuerRef.Name = issuer.GetObjectMeta().Name
updateCrt.Spec.IssuerRef.Kind = issuerKind
err = c.setIssuerSpecificConfig(updateCrt, issuer, ing, tls)
if err != nil {
return nil, nil, err
}
updateCrts = append(updateCrts, updateCrt)
} else {
newCrts = append(newCrts, crt)
}
}
return newCrts, updateCrts, nil
}
// certNeedsUpdate checks and returns true if two Certificates differ
func certNeedsUpdate(a, b *v1alpha1.Certificate) bool {
if a.Name != b.Name {
return true
}
if len(a.Spec.DNSNames) != len(b.Spec.DNSNames) {
return true
}
for i := range a.Spec.DNSNames {
if a.Spec.DNSNames[i] != b.Spec.DNSNames[i] {
return true
}
}
if a.Spec.SecretName != b.Spec.SecretName {
return true
}
if a.Spec.IssuerRef.Name != b.Spec.IssuerRef.Name {
return true
}
if a.Spec.IssuerRef.Kind != b.Spec.IssuerRef.Kind {
return true
}
var configA, configB []v1alpha1.DomainSolverConfig
if a.Spec.ACME != nil {
configA = a.Spec.ACME.Config
}
if b.Spec.ACME != nil {
configB = b.Spec.ACME.Config
}
if !reflect.DeepEqual(configA, configB) {
return true
}
return false
}
func (c *Controller) setIssuerSpecificConfig(crt *v1alpha1.Certificate, issuer v1alpha1.GenericIssuer, ing *extv1beta1.Ingress, tls extv1beta1.IngressTLS) error {
ingAnnotations := ing.Annotations
if ingAnnotations == nil {
ingAnnotations = map[string]string{}
}
// for ACME issuers
if issuer.GetSpec().ACME != nil {
challengeType, ok := ingAnnotations[acmeIssuerChallengeTypeAnnotation]
if !ok {
challengeType = c.defaults.acmeIssuerChallengeType
}
domainCfg := v1alpha1.DomainSolverConfig{
Domains: tls.Hosts,
}
switch challengeType {
case "http01":
// If the HTTP01 issuer is not enabled, skip setting the ACME field
// on the Certificate resource.
if issuer.GetSpec().ACME.HTTP01 == nil {
crt.Spec.ACME = nil
return nil
}
domainCfg.HTTP01 = &v1alpha1.HTTP01SolverConfig{}
editInPlace, ok := ingAnnotations[editInPlaceAnnotation]
// If annotation isn't present, or it's set to true, edit the existing ingress
if ok && editInPlace == "true" {
domainCfg.HTTP01.Ingress = ing.Name
} else {
ingressClass, ok := ingAnnotations[acmeIssuerHTTP01IngressClassAnnotation]
if ok {
domainCfg.HTTP01.IngressClass = &ingressClass
} else {
ingressClass, ok := ingAnnotations[ingressClassAnnotation]
if ok {
domainCfg.HTTP01.IngressClass = &ingressClass
}
}
}
case "dns01":
// If the DNS01 issuer is not enabled, skip setting the ACME field
// on the Certificate resource.
if issuer.GetSpec().ACME.DNS01 == nil {
crt.Spec.ACME = nil
return nil
}
dnsProvider, ok := ingAnnotations[acmeIssuerDNS01ProviderNameAnnotation]
if !ok {
dnsProvider = c.defaults.acmeIssuerDNS01ProviderName
}
if dnsProvider == "" {
return fmt.Errorf("no acme issuer dns01 challenge provider specified")
}
domainCfg.DNS01 = &v1alpha1.DNS01SolverConfig{Provider: dnsProvider}
// If no challenge type is specified, don't set the ACME field at all
// and instead rely on the 'new API format' to provide solver config.
case "":
crt.Spec.ACME = nil
return nil
default:
return fmt.Errorf("invalid acme issuer challenge type specified %q", challengeType)
}
crt.Spec.ACME = &v1alpha1.ACMECertificateConfig{Config: []v1alpha1.DomainSolverConfig{domainCfg}}
}
return nil
}
// shouldSync returns true if this ingress should have a Certificate resource
// created for it
func shouldSync(ing *extv1beta1.Ingress, autoCertificateAnnotations []string) bool {
annotations := ing.Annotations
if annotations == nil {
annotations = map[string]string{}
}
if _, ok := annotations[issuerNameAnnotation]; ok {
return true
}
if _, ok := annotations[clusterIssuerNameAnnotation]; ok {
return true
}
for _, x := range autoCertificateAnnotations {
if s, ok := annotations[x]; ok {
if b, _ := strconv.ParseBool(s); b {
return true
}
}
}
if _, ok := annotations[acmeIssuerChallengeTypeAnnotation]; ok {
return true
}
if _, ok := annotations[acmeIssuerDNS01ProviderNameAnnotation]; ok {
return true
}
return false
}
// issuerForIngress will determine the issuer that should be specified on a
// Certificate created for the given Ingress resource. If one is not set, the
// default issuer given to the controller will be used.
func (c *Controller) issuerForIngress(ing *extv1beta1.Ingress) (name string, kind string) {
name = c.defaults.issuerName
kind = c.defaults.issuerKind
annotations := ing.Annotations
if annotations == nil {
annotations = map[string]string{}
}
if issuerName, ok := annotations[issuerNameAnnotation]; ok {
name = issuerName
kind = v1alpha1.IssuerKind
}
if issuerName, ok := annotations[clusterIssuerNameAnnotation]; ok {
name = issuerName
kind = v1alpha1.ClusterIssuerKind
}
return name, kind
}