Skip to content

Commit ccf83d6

Browse files
committed
Propagate Cert-Manager Certificate status to the one from the IntegrationSink (knative#8527)
* Propagate Cert-Manager Certificate status to the one from the IntegrationSink Signed-off-by: Matthias Wessendorf <mwessend@redhat.com> * Remove cert from top level condition set Signed-off-by: Matthias Wessendorf <mwessend@redhat.com> --------- Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>
1 parent 112a457 commit ccf83d6

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

pkg/apis/sinks/v1alpha1/integration_sink_lifecycle.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
cmv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
21+
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
2022
appsv1 "k8s.io/api/apps/v1"
2123
corev1 "k8s.io/api/core/v1"
2224
"knative.dev/pkg/apis"
@@ -35,6 +37,12 @@ const (
3537
// IntegrationSinkConditionEventPoliciesReady has status True when all the applying EventPolicies for this
3638
// IntegrationSink are ready.
3739
IntegrationSinkConditionEventPoliciesReady apis.ConditionType = "EventPoliciesReady"
40+
41+
// IntegrationSinkConditionCertificateReady has status True when the IntegrationSink's certificate is ready.
42+
IntegrationSinkConditionCertificateReady apis.ConditionType = "CertificateReady"
43+
44+
// Certificate related condition reasons
45+
IntegrationSinkCertificateNotReady string = "CertificateNotReady"
3846
)
3947

4048
var IntegrationSinkCondSet = apis.NewLivingConditionSet(
@@ -112,6 +120,37 @@ func (s *IntegrationSinkStatus) PropagateDeploymentStatus(d *appsv1.DeploymentSt
112120
}
113121
}
114122

123+
func (s *IntegrationSinkStatus) PropagateCertificateStatus(cs cmv1.CertificateStatus) bool {
124+
var topLevel *cmv1.CertificateCondition
125+
for _, cond := range cs.Conditions {
126+
if cond.Type == cmv1.CertificateConditionReady {
127+
topLevel = &cond
128+
break
129+
}
130+
}
131+
132+
if topLevel == nil {
133+
IntegrationSinkCondSet.Manage(s).MarkUnknown(IntegrationSinkConditionCertificateReady,
134+
IntegrationSinkCertificateNotReady, "Certificate is progressing")
135+
return false
136+
}
137+
138+
if topLevel.Status == cmmeta.ConditionUnknown {
139+
IntegrationSinkCondSet.Manage(s).MarkUnknown(IntegrationSinkConditionCertificateReady,
140+
IntegrationSinkCertificateNotReady, "Certificate is progressing, "+topLevel.Reason+" Message: "+topLevel.Message)
141+
return false
142+
}
143+
144+
if topLevel.Status == cmmeta.ConditionFalse {
145+
IntegrationSinkCondSet.Manage(s).MarkFalse(IntegrationSinkConditionCertificateReady,
146+
IntegrationSinkCertificateNotReady, "Certificate is not ready, "+topLevel.Reason+" Message: "+topLevel.Message)
147+
return false
148+
}
149+
150+
IntegrationSinkCondSet.Manage(s).MarkTrue(IntegrationSinkConditionCertificateReady)
151+
return true
152+
}
153+
115154
func (s *IntegrationSinkStatus) SetAddress(address *duckv1.Addressable) {
116155
s.Address = address
117156
if address == nil || address.URL.IsEmpty() {

pkg/reconciler/integration/sink/integrationsink.go

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const (
6363
deploymentUpdated = "DeploymentUpdated"
6464
serviceCreated = "ServiceCreated"
6565
certificateCreated = "CertificateCreated"
66+
certificateUpdated = "CertificateUpdated"
6667
serviceUpdated = "ServiceUpdated"
6768
)
6869

@@ -176,7 +177,6 @@ func (r *Reconciler) reconcileService(ctx context.Context, sink *sinks.Integrati
176177
}
177178

178179
func (r *Reconciler) reconcileIntegrationSinkCertificate(ctx context.Context, sink *sinks.IntegrationSink) (*cmv1.Certificate, error) {
179-
180180
if f := feature.FromContext(ctx); !f.IsStrictTransportEncryption() && !f.IsPermissiveTransportEncryption() {
181181
return nil, r.deleteIntegrationSinkCertificate(ctx, sink)
182182
}
@@ -188,22 +188,41 @@ func (r *Reconciler) reconcileIntegrationSinkCertificate(ctx context.Context, si
188188
return nil, fmt.Errorf("no cert-manager certificate lister created yet, this should rarely happen and recover")
189189
}
190190

191-
cert, err := (*cmCertificateLister).Certificates(sink.Namespace).Get(expected.Name)
191+
curr, err := (*cmCertificateLister).Certificates(expected.GetNamespace()).Get(expected.GetName())
192192
if apierrors.IsNotFound(err) {
193-
cert, err := r.certManagerClient.CertmanagerV1().Certificates(sink.Namespace).Create(ctx, expected, metav1.CreateOptions{})
193+
created, err := r.createCertificate(ctx, sink, expected)
194194
if err != nil {
195-
return nil, fmt.Errorf("creating new Certificate: %v", err)
195+
return nil, err
196196
}
197-
controller.GetEventRecorder(ctx).Eventf(sink, corev1.EventTypeNormal, certificateCreated, "Certificate created %q", cert.Name)
198-
} else if err != nil {
199-
return nil, fmt.Errorf("getting Certificate: %v", err)
200-
} else if !metav1.IsControlledBy(cert, sink) {
201-
return nil, fmt.Errorf("Certificate %q is not owned by IntegrationSink %q", cert.Name, sink.Name)
202-
} else {
203-
logging.FromContext(ctx).Debugw("Reusing existing Certificate", zap.Any("Certificate", cert))
197+
if !sink.Status.PropagateCertificateStatus(created.Status) {
198+
// Wait for Certificate to become ready before continuing.
199+
return nil, controller.NewSkipKey("")
200+
}
201+
return created, nil
202+
}
203+
if err != nil {
204+
return nil, fmt.Errorf("failed to get certificate %s/%s: %w", expected.GetNamespace(), expected.GetName(), err)
204205
}
205206

206-
return cert, nil
207+
if equality.Semantic.DeepDerivative(expected.Spec, curr.Spec) &&
208+
equality.Semantic.DeepDerivative(expected.Labels, curr.Labels) &&
209+
equality.Semantic.DeepDerivative(expected.Annotations, curr.Annotations) {
210+
if !sink.Status.PropagateCertificateStatus(curr.Status) {
211+
// Wait for Certificate to become ready before continuing.
212+
return nil, controller.NewSkipKey("")
213+
}
214+
return curr, nil
215+
}
216+
expected.ResourceVersion = curr.ResourceVersion
217+
updated, err := r.updateCertificate(ctx, sink, expected)
218+
if err != nil {
219+
return nil, err
220+
}
221+
if !sink.Status.PropagateCertificateStatus(updated.Status) {
222+
// Wait for Certificate to become ready before continuing.
223+
return nil, controller.NewSkipKey("")
224+
}
225+
return updated, nil
207226
}
208227

209228
func (r *Reconciler) deleteIntegrationSinkCertificate(ctx context.Context, sink *sinks.IntegrationSink) error {
@@ -342,3 +361,21 @@ func integrationSinkCertificate(sink *sinks.IntegrationSink) *cmv1.Certificate {
342361
),
343362
)
344363
}
364+
365+
func (r *Reconciler) createCertificate(ctx context.Context, sink *sinks.IntegrationSink, expected *cmv1.Certificate) (*cmv1.Certificate, error) {
366+
created, err := r.certManagerClient.CertmanagerV1().Certificates(expected.GetNamespace()).Create(ctx, expected, metav1.CreateOptions{})
367+
if err != nil {
368+
return nil, fmt.Errorf("creating new Certificate %s/%s: %w", expected.GetNamespace(), expected.GetName(), err)
369+
}
370+
controller.GetEventRecorder(ctx).Eventf(sink, corev1.EventTypeNormal, certificateCreated, "Certificate created %q", expected.GetName())
371+
return created, nil
372+
}
373+
374+
func (r *Reconciler) updateCertificate(ctx context.Context, sink *sinks.IntegrationSink, expected *cmv1.Certificate) (*cmv1.Certificate, error) {
375+
updated, err := r.certManagerClient.CertmanagerV1().Certificates(expected.GetNamespace()).Update(ctx, expected, metav1.UpdateOptions{})
376+
if err != nil {
377+
return nil, fmt.Errorf("failed to update certificate %s/%s: %w", expected.GetNamespace(), expected.GetName(), err)
378+
}
379+
controller.GetEventRecorder(ctx).Event(sink, corev1.EventTypeNormal, certificateUpdated, expected.GetName())
380+
return updated, nil
381+
}

0 commit comments

Comments
 (0)