Skip to content

Commit b9cdc6c

Browse files
scrungusstoney-cloud
andcommitted
Sanitize availability zone name
Co-authored-by: stoney-cloud <[email protected]>
1 parent 515b4c9 commit b9cdc6c

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

pkg/openstack/instancesv2.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
cloudprovider "k8s.io/cloud-provider"
2828
"k8s.io/cloud-provider-openstack/pkg/client"
2929
"k8s.io/cloud-provider-openstack/pkg/metrics"
30+
"k8s.io/cloud-provider-openstack/pkg/util"
3031
"k8s.io/cloud-provider-openstack/pkg/util/errors"
3132
"k8s.io/klog/v2"
3233
)
@@ -133,11 +134,13 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo
133134
return nil, err
134135
}
135136

137+
availabilityZone := util.SanitizeLabel(server.AvailabilityZone)
138+
136139
return &cloudprovider.InstanceMetadata{
137140
ProviderID: i.makeInstanceID(&server),
138141
InstanceType: instanceType,
139142
NodeAddresses: addresses,
140-
Zone: server.AvailabilityZone,
143+
Zone: availabilityZone,
141144
Region: i.region,
142145
}, nil
143146
}

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), nil
310310
}
311311

312312
func CheckMetadataSearchOrder(order string) error {

pkg/util/util.go

Lines changed: 17 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,19 @@ func GetAZFromTopology(topologyKey string, requirement *csi.TopologyRequirement)
167168

168169
return zone
169170
}
171+
172+
func SanitizeLabel(input string) string {
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+
return sanitized
186+
}

0 commit comments

Comments
 (0)