Skip to content

Commit 91b83ab

Browse files
Menekse CeylanMenekse Ceylan
authored andcommitted
added labels upon creation of alb and storing certificate
1 parent c7749a6 commit 91b83ab

5 files changed

Lines changed: 70 additions & 4 deletions

File tree

pkg/alb/ingress/alb_spec.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ import (
1717
certsdk "github.com/stackitcloud/stackit-sdk-go/services/certificates/v2api"
1818
)
1919

20+
const (
21+
// prefixCustomerLabel is the api prefix for all custom labels
22+
prefixCustomerLabel = "lb.customer.label/"
23+
24+
// LabelIngressClassUID is the unique key that identifies resources
25+
// owned by a specific IngressClass.
26+
LabelIngressClassUID = prefixCustomerLabel + "ingress-class-uid"
27+
)
28+
2029
func (r *IngressClassReconciler) getAlbSpecForIngressClass(ctx context.Context, class *networkingv1.IngressClass) (*albsdk.CreateLoadBalancerPayload, []errorEvents, error) {
2130
ingresses, err := r.getIngressesForIngressClass(ctx, class)
2231
if err != nil {
@@ -45,7 +54,7 @@ func (r *IngressClassReconciler) getAlbSpecForIngresses(ctx context.Context, cla
4554
errorList = append(errorList, listenerMergeError...)
4655
}
4756

48-
certNameToId, certificateErrorEvents := r.applyCertificates(ctx, certificates)
57+
certNameToId, certificateErrorEvents := r.applyCertificates(ctx, class, certificates)
4958
errorList = append(errorList, certificateErrorEvents...)
5059

5160
alb, albSpecErrorList, err := r.getAlbSpecForResources(ctx, class, listeners, targetPools, certNameToId)
@@ -83,6 +92,28 @@ func (r *IngressClassReconciler) getAlbSpecForResources(ctx context.Context, cla
8392
alb.PlanId = &plan
8493
}
8594

95+
mergedLabels := make(map[string]string)
96+
97+
// Add user labels, mind the limit
98+
for k, v := range class.Labels {
99+
if len(mergedLabels) < 64 {
100+
mergedLabels[k] = v
101+
}
102+
}
103+
104+
// Merge with existing global config labels
105+
if r.ALBConfig.ApplicationLoadBalancer.ExtraLabels != nil {
106+
for k, v := range r.ALBConfig.ApplicationLoadBalancer.ExtraLabels {
107+
if len(mergedLabels) < 64 {
108+
mergedLabels[k] = v
109+
}
110+
}
111+
}
112+
113+
// Add ownership label
114+
mergedLabels[LabelIngressClassUID] = string(class.UID)
115+
alb.Labels = &mergedLabels
116+
86117
for port, listener := range listeners {
87118
albsdkListener := albsdk.Listener{
88119
Http: nil,
@@ -379,7 +410,7 @@ func (r *IngressClassReconciler) getCertificateForSecretName(ctx context.Context
379410
}, nil
380411
}
381412

382-
func (r *IngressClassReconciler) applyCertificates(ctx context.Context, certificates albCertificates) (map[string]string, []errorEvents) {
413+
func (r *IngressClassReconciler) applyCertificates(ctx context.Context, class *networkingv1.IngressClass, certificates albCertificates) (map[string]string, []errorEvents) {
383414
errorList := []errorEvents{}
384415
nameToID := map[string]string{}
385416
for name, certificate := range certificates {
@@ -388,6 +419,9 @@ func (r *IngressClassReconciler) applyCertificates(ctx context.Context, certific
388419
ProjectId: &r.ALBConfig.Global.ProjectID,
389420
PrivateKey: new(string(certificate.privateKey)),
390421
PublicKey: new(string(certificate.publicKey)),
422+
Labels: &map[string]string{
423+
LabelIngressClassUID: string(class.UID),
424+
},
391425
}
392426
response, err := r.CertificateClient.CreateCertificate(ctx, r.ALBConfig.Global.ProjectID, r.ALBConfig.Global.Region, createCertificatePayload)
393427
if err != nil {

pkg/alb/ingress/alb_spec_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ var _ = Describe("Node Controller", func() {
142142
Expect(*spec).To(BeEquivalentTo(albSpec))
143143
})
144144

145+
It("should work with labels", func() {
146+
147+
reconciler.ALBConfig.ApplicationLoadBalancer.ExtraLabels = map[string]string{"managed-by": "alb-ingressClass"}
148+
spec, errorEventList, err := reconciler.getAlbSpecForIngressClass(context.Background(), &ingressClass)
149+
Expect(err).To(Succeed())
150+
Expect(errorEventList).To(BeEmpty())
151+
152+
albSpec.Labels = new(map[string]string{"managed-by": "alb-ingressClass"})
153+
154+
Expect(spec).ToNot(BeNil())
155+
Expect(*spec).To(BeEquivalentTo(albSpec))
156+
})
157+
145158
It("should work with 2 ingresses different path", func() {
146159
ingress2 := testIngress(&ingressClass, &service)
147160
ingress2.Name = "ingress2"

pkg/alb/ingress/certificate.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ func (r *IngressClassReconciler) deleteAllCertsForClass(ctx context.Context, cla
1919
return nil // No certificates to clean up
2020
}
2121

22+
// using labels for certificates
23+
targetUID := string(class.UID)
24+
2225
for _, cert := range certificatesList.Items {
23-
if strings.HasPrefix(*cert.Name, shortUUID(string(class.UID))) {
26+
if cert.Labels == nil {
27+
// This part will go away when Labels are supported by Cert API
28+
// do I need to check if nil
29+
}
30+
31+
if val, ok := (*cert.Labels)[LabelIngressClassUID]; ok && val == targetUID {
2432
err := r.CertificateClient.DeleteCertificate(ctx, r.ALBConfig.Global.ProjectID, r.ALBConfig.Global.Region, *cert.Id)
2533
if err != nil {
2634
return fmt.Errorf("failed to delete orphaned certificate %s: %v", *cert.Name, err)

pkg/alb/ingress/update.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"reflect"
78

89
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
910
albsdk "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"
@@ -161,5 +162,14 @@ func updateNeeded(alb *albsdk.LoadBalancer, albPayload *albsdk.CreateLoadBalance
161162
}
162163
}
163164

165+
// Label comparison
166+
// normalize pointers to prevent nil vs empty map issue
167+
currentLabels := ptr.Deref(alb.Labels, map[string]string{})
168+
desiredLabels := ptr.Deref(albPayload.Labels, map[string]string{})
169+
170+
if !reflect.DeepEqual(currentLabels, desiredLabels) {
171+
return true
172+
}
173+
164174
return false
165175
}

pkg/stackit/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ type ALBConfig struct {
4949
ApplicationLoadBalancer ApplicationLoadBalancerOpts `yaml:"applicationLoadBalancer"`
5050
}
5151
type ApplicationLoadBalancerOpts struct {
52-
NetworkID string `yaml:"networkId"`
52+
NetworkID string `yaml:"networkId"`
53+
ExtraLabels map[string]string `yaml:"extraLabels,omitempty"`
5354
}
5455

5556
func readFile(path string) ([]byte, error) {

0 commit comments

Comments
 (0)