@@ -11,7 +11,7 @@ import (
1111 "strconv"
1212 "strings"
1313
14- v1 "k8s.io/api/core/v1"
14+ corev1 "k8s.io/api/core/v1"
1515 networkingv1 "k8s.io/api/networking/v1"
1616 "k8s.io/apimachinery/pkg/types"
1717 "k8s.io/utils/ptr"
@@ -64,13 +64,13 @@ type ruleMetadata struct {
6464// It merges and sorts all routing rules across the ingresses based on host, priority, path specificity, path type, and ingress origin.
6565// The resulting ALB payload includes targets derived from cluster nodes, target pools per backend service, HTTP(S) listeners,
6666// and optional TLS certificate bindings. This spec is later used to create or update the actual ALB instance.
67- func (r * IngressClassReconciler ) albSpecFromIngress (
67+ func (r * IngressClassReconciler ) albSpecFromIngress ( //nolint:funlen,gocyclo // We go through a lot of fields. Not much complexity.
6868 ctx context.Context ,
6969 ingresses []* networkingv1.Ingress ,
7070 ingressClass * networkingv1.IngressClass ,
7171 networkID * string ,
72- nodes []v1 .Node ,
73- services map [string ]v1 .Service ,
72+ nodes []corev1 .Node ,
73+ services map [string ]corev1 .Service ,
7474) (* albsdk.CreateLoadBalancerPayload , error ) {
7575 targetPools := []albsdk.TargetPool {}
7676 targetPoolSeen := map [string ]bool {}
@@ -89,10 +89,11 @@ func (r *IngressClassReconciler) albSpecFromIngress(
8989
9090 // Create targets for each node in the cluster
9191 targets := []albsdk.Target {}
92- for _ , node := range nodes {
92+ for i := range nodes {
93+ node := nodes [i ]
9394 for j := range node .Status .Addresses {
9495 address := node .Status .Addresses [j ]
95- if address .Type == v1 .NodeInternalIP {
96+ if address .Type == corev1 .NodeInternalIP {
9697 targets = append (targets , albsdk.Target {
9798 DisplayName : & node .Name ,
9899 Ip : & address .Address ,
@@ -142,6 +143,7 @@ func (r *IngressClassReconciler) albSpecFromIngress(
142143 certificateIDs , err := r .loadCerts (ctx , ingressClass , ingress )
143144 if err != nil {
144145 log .Printf ("failed to load tls certificates: %v" , err )
146+ //nolint:gocritic // TODO: Rework error handling.
145147 // return nil, fmt.Errorf("failed to load tls certificates: %w", err)
146148 }
147149 allCertificateIDs = append (allCertificateIDs , certificateIDs ... )
@@ -241,7 +243,7 @@ func (r *IngressClassReconciler) albSpecFromIngress(
241243 }
242244
243245 // Set the IP address of the ALB
244- err := setIpAddresses (ingressClass , alb )
246+ err := setIPAddresses (ingressClass , alb )
245247 if err != nil {
246248 return nil , fmt .Errorf ("failed to set IP address: %w" , err )
247249 }
@@ -262,19 +264,19 @@ func (r *IngressClassReconciler) loadCerts(
262264 certificateIDs := []string {}
263265
264266 for _ , tls := range ingress .Spec .TLS {
265- if len ( tls .SecretName ) == 0 {
267+ if tls .SecretName != "" {
266268 continue
267269 }
268270
269- secret := & v1 .Secret {}
271+ secret := & corev1 .Secret {}
270272 if err := r .Client .Get (ctx , types.NamespacedName {Namespace : ingress .Namespace , Name : tls .SecretName }, secret ); err != nil {
271273 return nil , fmt .Errorf ("failed to get TLS secret: %w" , err )
272274 }
273275
274276 // The tls.crt should contain both the leaf certificate and the intermediate CA certificates.
275277 // If it contains only the leaf certificate, the ACME challenge likely hasn't finished.
276278 // Therefore the incomplete certificate shouldn't be loaded as the updates upon them are impossible.
277- complete , err := isCertValid (* secret )
279+ complete , err := isCertValid (secret )
278280 if err != nil {
279281 return nil , fmt .Errorf ("failed to validate certificate: %w" , err )
280282 }
@@ -306,17 +308,18 @@ func (r *IngressClassReconciler) cleanupCerts(ctx context.Context, ingressClass
306308 usedSecrets := map [string ]bool {}
307309 for _ , ingress := range ingresses {
308310 for _ , tls := range ingress .Spec .TLS {
309- if tls .SecretName != "" {
310- // Retrieve the TLS Secret
311- tlsSecret := & v1.Secret {}
312- err := r .Client .Get (ctx , types.NamespacedName {Namespace : ingress .Namespace , Name : tls .SecretName }, tlsSecret )
313- if err != nil {
314- log .Printf ("failed to get TLS secret %s: %v" , tls .SecretName , err )
315- continue
316- }
317- certName := getCertName (ingressClass , ingress , tlsSecret )
318- usedSecrets [certName ] = true
311+ if tls .SecretName == "" {
312+ continue
313+ }
314+ // Retrieve the TLS Secret
315+ tlsSecret := & corev1.Secret {}
316+ err := r .Client .Get (ctx , types.NamespacedName {Namespace : ingress .Namespace , Name : tls .SecretName }, tlsSecret )
317+ if err != nil {
318+ log .Printf ("failed to get TLS secret %s: %v" , tls .SecretName , err )
319+ continue
319320 }
321+ certName := getCertName (ingressClass , ingress , tlsSecret )
322+ usedSecrets [certName ] = true
320323 }
321324 }
322325
@@ -350,7 +353,7 @@ func (r *IngressClassReconciler) cleanupCerts(ctx context.Context, ingressClass
350353
351354// isCertValid checks if the certificate chain is complete. It is used for checking if
352355// the cert-manager's ACME challenge is completed, or if it's sill ongoing.
353- func isCertValid (secret v1 .Secret ) (bool , error ) {
356+ func isCertValid (secret * corev1 .Secret ) (bool , error ) {
354357 tlsCert := secret .Data ["tls.crt" ]
355358 if tlsCert == nil {
356359 return false , fmt .Errorf ("tls.crt not found in secret" )
@@ -406,13 +409,13 @@ func addTargetPool(
406409 }
407410 * targetPools = append (* targetPools , albsdk.TargetPool {
408411 Name : ptr .To (targetPoolName ),
409- TargetPort : ptr .To (int32 ( nodePort ) ),
412+ TargetPort : ptr .To (nodePort ),
410413 TlsConfig : tlsConfig ,
411414 Targets : targets ,
412415 })
413416}
414417
415- func setIpAddresses (ingressClass * networkingv1.IngressClass , alb * albsdk.CreateLoadBalancerPayload ) error {
418+ func setIPAddresses (ingressClass * networkingv1.IngressClass , alb * albsdk.CreateLoadBalancerPayload ) error {
416419 isInternalIP , found := ingressClass .Annotations [internalIPAnnotation ]
417420 if found && isInternalIP == "true" {
418421 alb .Options = & albsdk.LoadBalancerOptions {
@@ -447,7 +450,7 @@ func validateIPAddress(ipAddr string) error {
447450}
448451
449452// getNodePort gets the NodePort of the Service
450- func getNodePort (services map [string ]v1 .Service , path networkingv1.HTTPIngressPath ) (int32 , error ) {
453+ func getNodePort (services map [string ]corev1 .Service , path networkingv1.HTTPIngressPath ) (int32 , error ) {
451454 service , found := services [path .Backend .Service .Name ]
452455 if ! found {
453456 return 0 , fmt .Errorf ("service not found: %s" , path .Backend .Service .Name )
0 commit comments