Skip to content

Commit bddc345

Browse files
author
scrungus
committed
refactor and sanitize metadata az value also
1 parent 6dae10f commit bddc345

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

pkg/openstack/instancesv2.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import (
2020
"context"
2121
"fmt"
2222
sysos "os"
23-
"regexp"
24-
"strings"
2523

2624
"github.com/gophercloud/gophercloud/v2"
2725
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
2826
v1 "k8s.io/api/core/v1"
2927
cloudprovider "k8s.io/cloud-provider"
3028
"k8s.io/cloud-provider-openstack/pkg/client"
3129
"k8s.io/cloud-provider-openstack/pkg/metrics"
30+
"k8s.io/cloud-provider-openstack/pkg/util"
3231
"k8s.io/cloud-provider-openstack/pkg/util/errors"
3332
"k8s.io/klog/v2"
3433
)
@@ -109,23 +108,6 @@ func (i *InstancesV2) InstanceShutdown(ctx context.Context, node *v1.Node) (bool
109108
return false, nil
110109
}
111110

112-
func sanitizeLabel(input string) (string, error) {
113-
// Replace non-alphanumeric characters (except '-', '_', '.') with '-'
114-
reg := regexp.MustCompile(`[^-a-zA-Z0-9_.]+`)
115-
sanitized := reg.ReplaceAllString(input, "-")
116-
117-
// Ensure the label starts and ends with an alphanumeric character
118-
sanitized = strings.Trim(sanitized, "-_.")
119-
120-
// Ensure the label is not longer than 63 characters
121-
if len(sanitized) > 63 {
122-
sanitized = sanitized[:63]
123-
}
124-
125-
// Convert to lowercase
126-
return strings.ToLower(sanitized), nil
127-
}
128-
129111
// InstanceMetadata returns the instance's metadata.
130112
func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
131113
srv, err := i.getInstance(ctx, node)
@@ -152,7 +134,7 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo
152134
return nil, err
153135
}
154136

155-
availabilityZone, err := sanitizeLabel(server.AvailabilityZone)
137+
availabilityZone, err := util.SanitizeLabel(server.AvailabilityZone)
156138
if err != nil {
157139
return nil, err
158140
}

pkg/util/metadata/metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func (m *metadataService) GetAvailabilityZone() (string, error) {
306306
if err != nil {
307307
return "", err
308308
}
309-
return md.AvailabilityZone, nil
309+
return util.SanitizeLabel(md.AvailabilityZone)
310310
}
311311

312312
func CheckMetadataSearchOrder(order string) error {

pkg/util/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"regexp"
78
"strings"
89
"time"
910

@@ -167,3 +168,20 @@ func GetAZFromTopology(topologyKey string, requirement *csi.TopologyRequirement)
167168

168169
return zone
169170
}
171+
172+
func SanitizeLabel(input string) (string, error) {
173+
// Replace non-alphanumeric characters (except '-', '_', '.') with '-'
174+
reg := regexp.MustCompile(`[^-a-zA-Z0-9_.]+`)
175+
sanitized := reg.ReplaceAllString(input, "-")
176+
177+
// Ensure the label starts and ends with an alphanumeric character
178+
sanitized = strings.Trim(sanitized, "-_.")
179+
180+
// Ensure the label is not longer than 63 characters
181+
if len(sanitized) > 63 {
182+
sanitized = sanitized[:63]
183+
}
184+
185+
// Convert to lowercase
186+
return strings.ToLower(sanitized), nil
187+
}

0 commit comments

Comments
 (0)