Skip to content

Commit 1ac442f

Browse files
Kamil PrzybylKamil Przybyl
authored andcommitted
fix: certificates not created because loadCerts skips all ingress tls references; adjusted requeuing logic for certificates
1 parent 3c1cc68 commit 1ac442f

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

pkg/alb/ingress/alb_spec.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (r *IngressClassReconciler) albSpecFromIngress( //nolint:funlen,gocyclo //
7171
networkID *string,
7272
nodes []corev1.Node,
7373
services map[string]corev1.Service,
74-
) (*albsdk.CreateLoadBalancerPayload, error) {
74+
) (bool, *albsdk.CreateLoadBalancerPayload, error) {
7575
targetPools := []albsdk.TargetPool{}
7676
targetPoolSeen := map[string]bool{}
7777
allCertificateIDs := []string{}
@@ -111,7 +111,7 @@ func (r *IngressClassReconciler) albSpecFromIngress( //nolint:funlen,gocyclo //
111111
for j, path := range rule.HTTP.Paths {
112112
nodePort, err := getNodePort(services, path)
113113
if err != nil {
114-
return nil, err
114+
return false, nil, err
115115
}
116116

117117
targetPoolName := fmt.Sprintf("pool-%d", nodePort)
@@ -140,12 +140,13 @@ func (r *IngressClassReconciler) albSpecFromIngress( //nolint:funlen,gocyclo //
140140
}
141141

142142
// Apend certificates from the current Ingress to the combined certificates
143-
certificateIDs, err := r.loadCerts(ctx, ingressClass, ingress)
144-
if err != nil {
145-
log.Printf("failed to load tls certificates: %v", err)
146-
//nolint:gocritic // TODO: Rework error handling.
147-
// return nil, fmt.Errorf("failed to load tls certificates: %w", err)
148-
}
143+
requeueNeeded, certificateIDs, err := r.loadCerts(ctx, ingressClass, ingress)
144+
if requeueNeeded {
145+
return true, nil, nil
146+
}
147+
if err != nil {
148+
return false, nil, fmt.Errorf("failed to load tls certificates: %w", err)
149+
}
149150
allCertificateIDs = append(allCertificateIDs, certificateIDs...)
150151
}
151152

@@ -245,45 +246,41 @@ func (r *IngressClassReconciler) albSpecFromIngress( //nolint:funlen,gocyclo //
245246
// Set the IP address of the ALB
246247
err := setIPAddresses(ingressClass, alb)
247248
if err != nil {
248-
return nil, fmt.Errorf("failed to set IP address: %w", err)
249+
return false, nil, fmt.Errorf("failed to set IP address: %w", err)
249250
}
250251

251252
alb.Name = ptr.To(getAlbName(ingressClass))
252253
alb.Listeners = listeners
253254
alb.TargetPools = targetPools
254255

255-
return alb, nil
256+
return false, alb, nil
256257
}
257258

258259
// laodCerts loads the tls certificates from Ingress to the Certificates API
259260
func (r *IngressClassReconciler) loadCerts(
260261
ctx context.Context,
261262
ingressClass *networkingv1.IngressClass,
262263
ingress *networkingv1.Ingress,
263-
) ([]string, error) {
264+
) (bool, []string, error) {
264265
certificateIDs := []string{}
265266

266267
for _, tls := range ingress.Spec.TLS {
267-
if tls.SecretName != "" {
268+
if tls.SecretName == "" {
268269
continue
269270
}
270271

271272
secret := &corev1.Secret{}
272273
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: ingress.Namespace, Name: tls.SecretName}, secret); err != nil {
273-
return nil, fmt.Errorf("failed to get TLS secret: %w", err)
274+
return false, nil, fmt.Errorf("failed to get TLS secret: %w", err)
274275
}
275276

276-
// The tls.crt should contain both the leaf certificate and the intermediate CA certificates.
277-
// If it contains only the leaf certificate, the ACME challenge likely hasn't finished.
278-
// Therefore the incomplete certificate shouldn't be loaded as the updates upon them are impossible.
279277
complete, err := isCertReady(secret)
280278
if err != nil {
281-
return nil, fmt.Errorf("failed to check if certificate is ready: %w", err)
279+
return false, nil, fmt.Errorf("failed to check if certificate is ready: %w", err)
282280
}
283281
if !complete {
284-
// TODO: Requeue, instead of returning error - the ACME challenge hasn't finished yet
285-
// return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
286-
return nil, fmt.Errorf("certificate is not complete: %w", err)
282+
// Requeue: The ACME challenge is still in progress and the certificate is not yet fully issued.
283+
return true, nil, fmt.Errorf("certificate is not complete: %w", err)
287284
}
288285

289286
createCertificatePayload := &certsdk.CreateCertificatePayload{
@@ -294,12 +291,12 @@ func (r *IngressClassReconciler) loadCerts(
294291
}
295292
res, err := r.CertificateClient.CreateCertificate(ctx, r.ProjectID, r.Region, createCertificatePayload)
296293
if err != nil {
297-
return nil, fmt.Errorf("failed to create certificate: %w", err)
294+
return false, nil, fmt.Errorf("failed to create certificate: %w", err)
298295
}
299296

300297
certificateIDs = append(certificateIDs, *res.Id)
301298
}
302-
return certificateIDs, nil
299+
return false, certificateIDs, nil
303300
}
304301

305302
// cleanupCerts deletes the certificates from the Certificates API that are no longer associated with any Ingress in the IngressClass

pkg/alb/ingress/alb_spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ func Test_albSpecFromIngress(t *testing.T) {
408408

409409
for _, tt := range tests {
410410
t.Run(tt.name, func(t *testing.T) {
411-
got, err := r.albSpecFromIngress(context.TODO(), tt.ingresses, tt.ingressClass, ptr.To(testNetworkID), nodes, tt.services)
411+
_, got, err := r.albSpecFromIngress(context.TODO(), tt.ingresses, tt.ingressClass, ptr.To(testNetworkID), nodes, tt.services)
412412
if (err != nil) != tt.wantErr {
413413
t.Errorf("got error = %v, wantErr %v", err, tt.wantErr)
414414
return

pkg/alb/ingress/ingressclass_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ func (r *IngressClassReconciler) handleIngressClassWithIngresses(
184184
}
185185

186186
// Create ALB payload from Ingresses
187-
albPayload, err := r.albSpecFromIngress(ctx, ingresses, ingressClass, &r.NetworkID, nodes.Items, services)
187+
requeueNeeded, albPayload, err := r.albSpecFromIngress(ctx, ingresses, ingressClass, &r.NetworkID, nodes.Items, services)
188+
if requeueNeeded {
189+
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
190+
}
188191
if err != nil {
189192
return ctrl.Result{}, fmt.Errorf("failed to create alb payload: %w", err)
190193
}

0 commit comments

Comments
 (0)