@@ -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+
2029func (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 )
@@ -60,83 +69,105 @@ func (r *IngressClassReconciler) getAlbSpecForResources(ctx context.Context, cla
6069 Options : & albsdk.LoadBalancerOptions {},
6170 Networks : []albsdk.Network {
6271 {
63- NetworkId : new (r.ALBConfig .ApplicationLoadBalancer.NetworkID ),
64- Role : new ("ROLE_LISTENERS_AND_TARGETS "),
72+ NetworkId : ptr . To (r .ALBConfig .ApplicationLoadBalancer .NetworkID ),
73+ Role : ptr . To ("ROLE_LISTENERS_AND_TARGETS" ),
6574 },
6675 },
67- Name : new (string (class.UID )),
68- DisableTargetSecurityGroupAssignment : new (true ),
76+ Name : ptr . To (string (class .UID )),
77+ DisableTargetSecurityGroupAssignment : ptr . To (true ),
6978 }
7079
7180 externalAddress := getAnnotation (AnnotationExternalIP , "" , class )
7281 if externalAddress != "" {
7382 alb .ExternalAddress = & externalAddress
7483 } else {
75- alb .Options .EphemeralAddress = new (true )
84+ alb .Options .EphemeralAddress = ptr . To (true )
7685 }
7786
7887 if getAnnotation (AnnotationInternal , false , class ) {
79- alb .Options .PrivateNetworkOnly = new (true )
88+ alb .Options .PrivateNetworkOnly = ptr . To (true )
8089 }
8190
8291 if plan := getAnnotation (AnnotationPlanID , "" , class ); plan != "" {
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 ,
89- Name : new (listener.name ),
90- Port : new (int32 (port )),
91- Protocol : new (listener.protocol ),
120+ Name : ptr . To (listener .name ),
121+ Port : ptr . To (int32 (port )),
122+ Protocol : ptr . To (listener .protocol ),
92123 AdditionalProperties : nil ,
93124 }
94125
95126 if listener .wafConfigName != "" {
96- albsdkListener .WafConfigName = new (listener.wafConfigName )
127+ albsdkListener .WafConfigName = ptr . To (listener .wafConfigName )
97128 }
98129
99130 albsdkHosts := []albsdk.HostConfig {}
100131 for host , hostPaths := range listener .hosts {
101132 albsdkHost := albsdk.HostConfig {
102- Host : new (host ),
133+ Host : ptr . To (host ),
103134 }
104135 for path , rule := range hostPaths .path {
105136 albsdkRule := albsdk.Rule {
106- TargetPool : new (rule.targetPoolName ),
107- WebSocket : new (rule.websocket ),
137+ TargetPool : ptr . To (rule .targetPoolName ),
138+ WebSocket : ptr . To (rule .websocket ),
108139 }
109140
110141 if rule .cookiePersistenceName != "" {
111- albsdkRule .CookiePersistence = new (albsdk.CookiePersistence {
112- Name: new (rule.cookiePersistenceName ),
113- Ttl : new (fmt.Sprintf ("%ds" , rule .cookiePersistenceTtlSeconds )),
142+ albsdkRule .CookiePersistence = ptr . To (albsdk.CookiePersistence {
143+ Name : ptr . To (rule .cookiePersistenceName ),
144+ Ttl : ptr . To (fmt .Sprintf ("%ds" , rule .cookiePersistenceTtlSeconds )),
114145 })
115146 }
116147
117148 switch rule .pathTyp {
118149 case networkingv1 .PathTypeExact :
119- albsdkRule.Path = new (albsdk.Path {
120- ExactMatch : new (path ),
150+ albsdkRule .Path = ptr . To (albsdk.Path {
151+ ExactMatch : ptr . To (path ),
121152 })
122153 default :
123- albsdkRule.Path = new (albsdk.Path {
124- Prefix : new (path ),
154+ albsdkRule .Path = ptr . To (albsdk.Path {
155+ Prefix : ptr . To (path ),
125156 })
126157 }
127158
128159 albsdkHost .Rules = append (albsdkHost .Rules , albsdkRule )
129160 }
130161 albsdkHosts = append (albsdkHosts , albsdkHost )
131162
132- albsdkListener .Http = new (albsdk.ProtocolOptionsHTTP {
163+ albsdkListener .Http = ptr . To (albsdk.ProtocolOptionsHTTP {
133164 Hosts : albsdkHosts ,
134165 })
135166 }
136167
137168 if listener .protocol == "PROTOCOL_HTTPS" {
138- albsdkListener .Https = new (albsdk.ProtocolOptionsHTTPS {
139- CertificateConfig: new (albsdk.CertificateConfig {
169+ albsdkListener .Https = ptr . To (albsdk.ProtocolOptionsHTTPS {
170+ CertificateConfig : ptr . To (albsdk.CertificateConfig {
140171 CertificateIds : []string {},
141172 }),
142173 })
@@ -167,8 +198,8 @@ func (r *IngressClassReconciler) getAlbSpecForResources(ctx context.Context, cla
167198
168199 for name , targetPool := range targetPools {
169200 albsdkTargetPool := albsdk.TargetPool {
170- Name : new (name ),
171- TargetPort : new (targetPool.port ),
201+ Name : ptr . To (name ),
202+ TargetPort : ptr . To (targetPool .port ),
172203 Targets : targets ,
173204 ActiveHealthCheck : nil , // TODO
174205 }
@@ -379,15 +410,18 @@ 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 {
386417 createCertificatePayload := & certsdk.CreateCertificatePayload {
387- Name : new (name ),
418+ Name : ptr . To (name ),
388419 ProjectId : & r .ALBConfig .Global .ProjectID ,
389- PrivateKey : new (string (certificate.privateKey )),
390- PublicKey : new (string (certificate.publicKey )),
420+ PrivateKey : ptr .To (string (certificate .privateKey )),
421+ PublicKey : ptr .To (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 {
0 commit comments