Skip to content

Commit 6dae10f

Browse files
author
scrungus
committed
sanitize availability zone
1 parent 515b4c9 commit 6dae10f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

pkg/openstack/instancesv2.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222
sysos "os"
23+
"regexp"
24+
"strings"
2325

2426
"github.com/gophercloud/gophercloud/v2"
2527
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
@@ -107,6 +109,23 @@ func (i *InstancesV2) InstanceShutdown(ctx context.Context, node *v1.Node) (bool
107109
return false, nil
108110
}
109111

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+
110129
// InstanceMetadata returns the instance's metadata.
111130
func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
112131
srv, err := i.getInstance(ctx, node)
@@ -133,11 +152,16 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo
133152
return nil, err
134153
}
135154

155+
availabilityZone, err := sanitizeLabel(server.AvailabilityZone)
156+
if err != nil {
157+
return nil, err
158+
}
159+
136160
return &cloudprovider.InstanceMetadata{
137161
ProviderID: i.makeInstanceID(&server),
138162
InstanceType: instanceType,
139163
NodeAddresses: addresses,
140-
Zone: server.AvailabilityZone,
164+
Zone: availabilityZone,
141165
Region: i.region,
142166
}, nil
143167
}

0 commit comments

Comments
 (0)