Skip to content

Commit 612f42f

Browse files
committed
Fix linter issues
1 parent 45c973d commit 612f42f

File tree

6 files changed

+74
-68
lines changed

6 files changed

+74
-68
lines changed

cmd/application-load-balancer-controller-manager/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import (
4141

4242
"github.com/stackitcloud/cloud-provider-stackit/pkg/alb/ingress"
4343
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
44-
albclient "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
4544
albsdk "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"
4645
certsdk "github.com/stackitcloud/stackit-sdk-go/services/certificates/v2api"
4746
// +kubebuilder:scaffold:imports
@@ -58,7 +57,7 @@ func init() {
5857
// +kubebuilder:scaffold:scheme
5958
}
6059

61-
// nolint:gocyclo
60+
// nolint:gocyclo,funlen // TODO: Refactor into smaller functions.
6261
func main() {
6362
var metricsAddr string
6463
var metricsCertPath, metricsCertName, metricsCertKey string
@@ -120,7 +119,7 @@ func main() {
120119
// Initial webhook TLS options
121120
webhookTLSOpts := tlsOpts
122121

123-
if len(webhookCertPath) > 0 {
122+
if webhookCertPath != "" {
124123
setupLog.Info("Initializing webhook certificate watcher using provided certificates",
125124
"webhook-cert-path", webhookCertPath, "webhook-cert-name", webhookCertName, "webhook-cert-key", webhookCertKey)
126125

@@ -169,7 +168,7 @@ func main() {
169168
// - [METRICS-WITH-CERTS] at config/default/kustomization.yaml to generate and use certificates
170169
// managed by cert-manager for the metrics server.
171170
// - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification.
172-
if len(metricsCertPath) > 0 {
171+
if metricsCertPath != "" {
173172
setupLog.Info("Initializing metrics certificate watcher using provided certificates",
174173
"metrics-cert-path", metricsCertPath, "metrics-cert-name", metricsCertName, "metrics-cert-key", metricsCertKey)
175174

@@ -252,7 +251,7 @@ func main() {
252251
}
253252
// Create an ALB client
254253
fmt.Printf("Create ALB client\n")
255-
albClient, err := albclient.NewApplicationLoadBalancerClient(sdkClient)
254+
albClient, err := stackit.NewApplicationLoadBalancerClient(sdkClient)
256255
if err != nil {
257256
setupLog.Error(err, "unable to create ALB client", "controller", "IngressClass")
258257
os.Exit(1)

pkg/alb/ingress/alb_spec.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

pkg/alb/ingress/alb_spec_test.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"google.golang.org/protobuf/testing/protocmp"
99

10-
v1 "k8s.io/api/core/v1"
10+
corev1 "k8s.io/api/core/v1"
1111
networkingv1 "k8s.io/api/networking/v1"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313
"k8s.io/utils/ptr"
@@ -79,11 +79,11 @@ func fixtureIngressWithParams(name, namespace string, annotations map[string]str
7979
}
8080
}
8181

82-
func fixtureServiceWithParams(port, nodePort int32) *v1.Service {
83-
return &v1.Service{
82+
func fixtureServiceWithParams(port, nodePort int32) *corev1.Service { //nolint:unparam // We might need it later.
83+
return &corev1.Service{
8484
ObjectMeta: metav1.ObjectMeta{Name: testServiceName},
85-
Spec: v1.ServiceSpec{
86-
Ports: []v1.ServicePort{
85+
Spec: corev1.ServiceSpec{
86+
Ports: []corev1.ServicePort{
8787
{
8888
Port: port,
8989
NodePort: nodePort,
@@ -93,11 +93,11 @@ func fixtureServiceWithParams(port, nodePort int32) *v1.Service {
9393
}
9494
}
9595

96-
func fixtureNode(mods ...func(*v1.Node)) *v1.Node {
97-
node := &v1.Node{
96+
func fixtureNode(mods ...func(*corev1.Node)) *corev1.Node {
97+
node := &corev1.Node{
9898
ObjectMeta: metav1.ObjectMeta{Name: testNodeName},
99-
Status: v1.NodeStatus{
100-
Addresses: []v1.NodeAddress{{Type: v1.NodeInternalIP, Address: testNodeIP}},
99+
Status: corev1.NodeStatus{
100+
Addresses: []corev1.NodeAddress{{Type: corev1.NodeInternalIP, Address: testNodeIP}},
101101
},
102102
}
103103
for _, mod := range mods {
@@ -187,23 +187,24 @@ func fixtureAlbPayload(mods ...func(*albsdk.CreateLoadBalancerPayload)) *albsdk.
187187
return payload
188188
}
189189

190+
//nolint:funlen // Just many test cases.
190191
func Test_albSpecFromIngress(t *testing.T) {
191192
r := &IngressClassReconciler{}
192-
nodes := []v1.Node{*fixtureNode()}
193+
nodes := []corev1.Node{*fixtureNode()}
193194

194195
tests := []struct {
195196
name string
196197
ingresses []*networkingv1.Ingress
197198
ingressClass *networkingv1.IngressClass
198-
services map[string]v1.Service
199+
services map[string]corev1.Service
199200
want *albsdk.CreateLoadBalancerPayload
200201
wantErr bool
201202
}{
202203
{
203204
name: "valid ingress with HTTP listener",
204205
ingresses: []*networkingv1.Ingress{fixtureIngress()},
205206
ingressClass: fixtureIngressClass(),
206-
services: map[string]v1.Service{testServiceName: *fixtureServiceWithParams(testServicePort, testNodePort)},
207+
services: map[string]corev1.Service{testServiceName: *fixtureServiceWithParams(testServicePort, testNodePort)},
207208
want: fixtureAlbPayload(),
208209
},
209210
{
@@ -214,7 +215,7 @@ func Test_albSpecFromIngress(t *testing.T) {
214215
ing.Annotations = map[string]string{externalIPAnnotation: "2.2.2.2"}
215216
},
216217
),
217-
services: map[string]v1.Service{testServiceName: *fixtureServiceWithParams(testServicePort, testNodePort)},
218+
services: map[string]corev1.Service{testServiceName: *fixtureServiceWithParams(testServicePort, testNodePort)},
218219
want: fixtureAlbPayload(func(payload *albsdk.CreateLoadBalancerPayload) {
219220
payload.ExternalAddress = ptr.To("2.2.2.2")
220221
payload.Options = &albsdk.LoadBalancerOptions{EphemeralAddress: nil}
@@ -228,7 +229,7 @@ func Test_albSpecFromIngress(t *testing.T) {
228229
ing.Annotations = map[string]string{internalIPAnnotation: "true"}
229230
},
230231
),
231-
services: map[string]v1.Service{testServiceName: *fixtureServiceWithParams(testServicePort, testNodePort)},
232+
services: map[string]corev1.Service{testServiceName: *fixtureServiceWithParams(testServicePort, testNodePort)},
232233
want: fixtureAlbPayload(func(payload *albsdk.CreateLoadBalancerPayload) {
233234
payload.Options = &albsdk.LoadBalancerOptions{PrivateNetworkOnly: ptr.To(true)}
234235
}),
@@ -242,7 +243,7 @@ func Test_albSpecFromIngress(t *testing.T) {
242243
ingressRule("a-host.com", ingressPrefixPath("/a", "svc2")),
243244
),
244245
},
245-
services: map[string]v1.Service{
246+
services: map[string]corev1.Service{
246247
"svc1": *fixtureServiceWithParams(testServicePort, 30001),
247248
"svc2": *fixtureServiceWithParams(testServicePort, 30002),
248249
},
@@ -278,7 +279,7 @@ func Test_albSpecFromIngress(t *testing.T) {
278279
ingressRule("host.com", ingressPrefixPath("/x", "svc2")),
279280
),
280281
},
281-
services: map[string]v1.Service{
282+
services: map[string]corev1.Service{
282283
"svc1": *fixtureServiceWithParams(testServicePort, 30003),
283284
"svc2": *fixtureServiceWithParams(testServicePort, 30004),
284285
},
@@ -305,7 +306,7 @@ func Test_albSpecFromIngress(t *testing.T) {
305306
),
306307
),
307308
},
308-
services: map[string]v1.Service{
309+
services: map[string]corev1.Service{
309310
"svc1": *fixtureServiceWithParams(testServicePort, 30005),
310311
"svc2": *fixtureServiceWithParams(testServicePort, 30006),
311312
},
@@ -332,7 +333,7 @@ func Test_albSpecFromIngress(t *testing.T) {
332333
),
333334
),
334335
},
335-
services: map[string]v1.Service{
336+
services: map[string]corev1.Service{
336337
"svc-exact": *fixtureServiceWithParams(testServicePort, 30100),
337338
"svc-prefix": *fixtureServiceWithParams(testServicePort, 30101),
338339
},
@@ -359,7 +360,7 @@ func Test_albSpecFromIngress(t *testing.T) {
359360
ingressRule("host.com", ingressPrefixPath("/x", "svc2")),
360361
),
361362
},
362-
services: map[string]v1.Service{
363+
services: map[string]corev1.Service{
363364
"svc1": *fixtureServiceWithParams(testServicePort, 30007),
364365
"svc2": *fixtureServiceWithParams(testServicePort, 30008),
365366
},
@@ -386,7 +387,7 @@ func Test_albSpecFromIngress(t *testing.T) {
386387
ingressRule("host.com", ingressPrefixPath("/x", "svc2")),
387388
),
388389
},
389-
services: map[string]v1.Service{
390+
services: map[string]corev1.Service{
390391
"svc1": *fixtureServiceWithParams(testServicePort, 30009),
391392
"svc2": *fixtureServiceWithParams(testServicePort, 30010),
392393
},

pkg/alb/ingress/controller_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
. "github.com/onsi/gomega"
88
corev1 "k8s.io/api/core/v1"
99
networkingv1 "k8s.io/api/networking/v1"
10-
k8serrors "k8s.io/apimachinery/pkg/api/errors"
10+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212
"k8s.io/client-go/kubernetes/scheme"
1313
"k8s.io/utils/ptr"
@@ -135,7 +135,7 @@ var _ = Describe("IngressClassReconciler", func() {
135135
AfterEach(func() {
136136
var ic networkingv1.IngressClass
137137
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(ingressClass), &ic)
138-
if k8serrors.IsNotFound(err) {
138+
if apierrors.IsNotFound(err) {
139139
// nothing to clean up, it’s already deleted
140140
return
141141
}
@@ -148,12 +148,12 @@ var _ = Describe("IngressClassReconciler", func() {
148148

149149
// delete the patched object (ic), not the old ingressClass pointer
150150
err = k8sClient.Delete(ctx, &ic)
151-
if err != nil && !k8serrors.IsNotFound(err) {
151+
if err != nil && !apierrors.IsNotFound(err) {
152152
Expect(err).NotTo(HaveOccurred())
153153
}
154154

155155
Eventually(func() bool {
156-
return k8serrors.IsNotFound(
156+
return apierrors.IsNotFound(
157157
k8sClient.Get(ctx, client.ObjectKeyFromObject(ingressClass), &networkingv1.IngressClass{}),
158158
)
159159
}).Should(BeTrue(), "IngressClass should be fully deleted")
@@ -183,7 +183,7 @@ var _ = Describe("IngressClassReconciler", func() {
183183
Eventually(func(g Gomega) {
184184
var ic networkingv1.IngressClass
185185
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(ingressClass), &ic)
186-
if k8serrors.IsNotFound(err) {
186+
if apierrors.IsNotFound(err) {
187187
// IngressClass is gone — controller must have removed the finalizer
188188
return
189189
}

0 commit comments

Comments
 (0)