Skip to content

Commit

Permalink
Sanitize availability zone name
Browse files Browse the repository at this point in the history
Co-authored-by: stoney-cloud <[email protected]>
  • Loading branch information
scrungus and stoney-cloud committed Sep 4, 2024
1 parent 515b4c9 commit b9cdc6c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/openstack/instancesv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
cloudprovider "k8s.io/cloud-provider"
"k8s.io/cloud-provider-openstack/pkg/client"
"k8s.io/cloud-provider-openstack/pkg/metrics"
"k8s.io/cloud-provider-openstack/pkg/util"
"k8s.io/cloud-provider-openstack/pkg/util/errors"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -133,11 +134,13 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo
return nil, err
}

availabilityZone := util.SanitizeLabel(server.AvailabilityZone)

return &cloudprovider.InstanceMetadata{
ProviderID: i.makeInstanceID(&server),
InstanceType: instanceType,
NodeAddresses: addresses,
Zone: server.AvailabilityZone,
Zone: availabilityZone,
Region: i.region,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (m *metadataService) GetAvailabilityZone() (string, error) {
if err != nil {
return "", err
}
return md.AvailabilityZone, nil
return util.SanitizeLabel(md.AvailabilityZone), nil
}

func CheckMetadataSearchOrder(order string) error {
Expand Down
17 changes: 17 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -167,3 +168,19 @@ func GetAZFromTopology(topologyKey string, requirement *csi.TopologyRequirement)

return zone
}

func SanitizeLabel(input string) string {
// Replace non-alphanumeric characters (except '-', '_', '.') with '-'
reg := regexp.MustCompile(`[^-a-zA-Z0-9_.]+`)
sanitized := reg.ReplaceAllString(input, "-")

// Ensure the label starts and ends with an alphanumeric character
sanitized = strings.Trim(sanitized, "-_.")

// Ensure the label is not longer than 63 characters
if len(sanitized) > 63 {
sanitized = sanitized[:63]
}

return sanitized
}

0 comments on commit b9cdc6c

Please sign in to comment.