@@ -20,6 +20,8 @@ import (
20
20
"context"
21
21
"fmt"
22
22
sysos "os"
23
+ "regexp"
24
+ "strings"
23
25
24
26
"github.com/gophercloud/gophercloud/v2"
25
27
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
@@ -107,6 +109,23 @@ func (i *InstancesV2) InstanceShutdown(ctx context.Context, node *v1.Node) (bool
107
109
return false , nil
108
110
}
109
111
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
+
110
129
// InstanceMetadata returns the instance's metadata.
111
130
func (i * InstancesV2 ) InstanceMetadata (ctx context.Context , node * v1.Node ) (* cloudprovider.InstanceMetadata , error ) {
112
131
srv , err := i .getInstance (ctx , node )
@@ -133,11 +152,16 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo
133
152
return nil , err
134
153
}
135
154
155
+ availabilityZone , err := sanitizeLabel (server .AvailabilityZone )
156
+ if err != nil {
157
+ return nil , err
158
+ }
159
+
136
160
return & cloudprovider.InstanceMetadata {
137
161
ProviderID : i .makeInstanceID (& server ),
138
162
InstanceType : instanceType ,
139
163
NodeAddresses : addresses ,
140
- Zone : server . AvailabilityZone ,
164
+ Zone : availabilityZone ,
141
165
Region : i .region ,
142
166
}, nil
143
167
}
0 commit comments