@@ -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.
111130func (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