This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
forked from knative/serving
/
certificate.go
190 lines (164 loc) · 7.51 KB
/
certificate.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
/*
Copyright 2019 The Knative 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 certificate
import (
"context"
"fmt"
"reflect"
cmv1alpha1 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
"knative.dev/serving/pkg/apis/networking/v1alpha1"
certmanagerclientset "knative.dev/serving/pkg/client/certmanager/clientset/versioned"
certmanagerlisters "knative.dev/serving/pkg/client/certmanager/listers/certmanager/v1alpha1"
listers "knative.dev/serving/pkg/client/listers/networking/v1alpha1"
"knative.dev/serving/pkg/reconciler"
"knative.dev/serving/pkg/reconciler/certificate/config"
"knative.dev/serving/pkg/reconciler/certificate/resources"
)
const (
noCMConditionReason = "NoCertManagerCertCondition"
noCMConditionMessage = "The ready condition of Cert Manager Certifiate does not exist."
notReconciledReason = "ReconcileFailed"
notReconciledMessage = "Cert-Manager certificate has not yet been reconciled."
)
// Reconciler implements controller.Reconciler for Certificate resources.
type Reconciler struct {
*reconciler.Base
// listers index properties about resources
knCertificateLister listers.CertificateLister
cmCertificateLister certmanagerlisters.CertificateLister
certManagerClient certmanagerclientset.Interface
configStore reconciler.ConfigStore
}
// Check that our Reconciler implements controller.Reconciler
var _ controller.Reconciler = (*Reconciler)(nil)
// Reconcile compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the Certificate resource
// with the current status of the resource.
func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
logger := logging.FromContext(ctx)
ctx = c.configStore.ToContext(ctx)
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
logger.Errorw("Invalid resource key", zap.Error(err))
return nil
}
original, err := c.knCertificateLister.Certificates(namespace).Get(name)
if apierrs.IsNotFound(err) {
logger.Errorf("Knative Certificate %s in work queue no longer exists", key)
return nil
} else if err != nil {
return err
}
// Don't modify the informers copy
knCert := original.DeepCopy()
// Reconcile this copy of the Certificate and then write back any status
// updates regardless of whether the reconciliation errored out.
err = c.reconcile(ctx, knCert)
if err != nil {
logger.Warnw("Failed to reconcile certificate", zap.Error(err))
c.Recorder.Event(knCert, corev1.EventTypeWarning, "InternalError", err.Error())
knCert.Status.MarkNotReady(notReconciledReason, notReconciledMessage)
}
if equality.Semantic.DeepEqual(original.Status, knCert.Status) {
// If we didn't change anything then don't call updateStatus.
// This is important because the copy we loaded from the informer's
// cache may be stale and we don't want to overwrite a prior update
// to status with this stale state.
} else if _, err := c.updateStatus(knCert); err != nil {
logger.Warnw("Failed to update certificate status", zap.Error(err))
c.Recorder.Eventf(knCert, corev1.EventTypeWarning, "UpdateFailed",
"Failed to update status for Certificate %s: %v", key, err)
return err
}
return err
}
func (c *Reconciler) reconcile(ctx context.Context, knCert *v1alpha1.Certificate) error {
logger := logging.FromContext(ctx)
knCert.SetDefaults(ctx)
knCert.Status.InitializeConditions()
logger.Infof("Reconciling Cert-Manager certificate for Knative cert %s/%s.", knCert.Namespace, knCert.Name)
knCert.Status.ObservedGeneration = knCert.Generation
cmConfig := config.FromContext(ctx).CertManager
cmCert := resources.MakeCertManagerCertificate(cmConfig, knCert)
cmCert, err := c.reconcileCMCertificate(ctx, knCert, cmCert)
if err != nil {
return err
}
knCert.Status.NotAfter = cmCert.Status.NotAfter
// Propagate cert-manager Certificate status to Knative Certificate.
cmCertReadyCondition := resources.GetReadyCondition(cmCert)
switch {
case cmCertReadyCondition == nil:
knCert.Status.MarkNotReady(noCMConditionReason, noCMConditionMessage)
case cmCertReadyCondition.Status == cmv1alpha1.ConditionUnknown:
knCert.Status.MarkNotReady(cmCertReadyCondition.Reason, cmCertReadyCondition.Message)
case cmCertReadyCondition.Status == cmv1alpha1.ConditionTrue:
knCert.Status.MarkReady()
case cmCertReadyCondition.Status == cmv1alpha1.ConditionFalse:
knCert.Status.MarkFailed(cmCertReadyCondition.Reason, cmCertReadyCondition.Message)
}
return nil
}
func (c *Reconciler) reconcileCMCertificate(ctx context.Context, knCert *v1alpha1.Certificate, desired *cmv1alpha1.Certificate) (*cmv1alpha1.Certificate, error) {
cmCert, err := c.cmCertificateLister.Certificates(desired.Namespace).Get(desired.Name)
if apierrs.IsNotFound(err) {
cmCert, err = c.certManagerClient.CertmanagerV1alpha1().Certificates(desired.Namespace).Create(desired)
if err != nil {
c.Recorder.Eventf(knCert, corev1.EventTypeWarning, "CreationFailed",
"Failed to create Cert-Manager Certificate %s/%s: %v", desired.Name, desired.Namespace, err)
return nil, fmt.Errorf("failed to create Cert-Manager Certificate: %w", err)
}
c.Recorder.Eventf(knCert, corev1.EventTypeNormal, "Created",
"Created Cert-Manager Certificate %s/%s", desired.Namespace, desired.Name)
} else if err != nil {
return nil, fmt.Errorf("failed to get Cert-Manager Certificate: %w", err)
} else if !metav1.IsControlledBy(desired, knCert) {
knCert.Status.MarkResourceNotOwned("CertManagerCertificate", desired.Name)
return nil, fmt.Errorf("knative Certificate %s in namespace %s does not own CertManager Certificate: %s", knCert.Name, knCert.Namespace, desired.Name)
} else if !equality.Semantic.DeepEqual(cmCert.Spec, desired.Spec) {
copy := cmCert.DeepCopy()
copy.Spec = desired.Spec
updated, err := c.certManagerClient.CertmanagerV1alpha1().Certificates(copy.Namespace).Update(copy)
if err != nil {
c.Recorder.Eventf(knCert, corev1.EventTypeWarning, "UpdateFailed",
"Failed to create Cert-Manager Certificate %s/%s: %v", desired.Namespace, desired.Name, err)
return nil, fmt.Errorf("failed to update Cert-Manager Certificate: %w", err)
}
c.Recorder.Eventf(knCert, corev1.EventTypeNormal, "Updated",
"Updated Spec for Cert-Manager Certificate %s/%s", desired.Namespace, desired.Name)
return updated, nil
}
return cmCert, nil
}
func (c *Reconciler) updateStatus(desired *v1alpha1.Certificate) (*v1alpha1.Certificate, error) {
cert, err := c.knCertificateLister.Certificates(desired.Namespace).Get(desired.Name)
if err != nil {
return nil, err
}
// If there's nothing to update, just return.
if reflect.DeepEqual(cert.Status, desired.Status) {
return cert, nil
}
// Don't modify the informers copy
existing := cert.DeepCopy()
existing.Status = desired.Status
return c.ServingClientSet.NetworkingV1alpha1().Certificates(existing.Namespace).UpdateStatus(existing)
}