From bddc345bdb2d926624e6d663ed76c7143670e096 Mon Sep 17 00:00:00 2001 From: scrungus Date: Fri, 23 Aug 2024 15:36:16 +0100 Subject: [PATCH] refactor and sanitize metadata az value also --- pkg/openstack/instancesv2.go | 22 ++-------------------- pkg/util/metadata/metadata.go | 2 +- pkg/util/util.go | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/openstack/instancesv2.go b/pkg/openstack/instancesv2.go index b0c4e9022f..999bbd7dd4 100644 --- a/pkg/openstack/instancesv2.go +++ b/pkg/openstack/instancesv2.go @@ -20,8 +20,6 @@ import ( "context" "fmt" sysos "os" - "regexp" - "strings" "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers" @@ -29,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" ) @@ -109,23 +108,6 @@ func (i *InstancesV2) InstanceShutdown(ctx context.Context, node *v1.Node) (bool return false, nil } -func sanitizeLabel(input string) (string, error) { - // 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] - } - - // Convert to lowercase - return strings.ToLower(sanitized), nil -} - // InstanceMetadata returns the instance's metadata. func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) { srv, err := i.getInstance(ctx, node) @@ -152,7 +134,7 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo return nil, err } - availabilityZone, err := sanitizeLabel(server.AvailabilityZone) + availabilityZone, err := util.SanitizeLabel(server.AvailabilityZone) if err != nil { return nil, err } diff --git a/pkg/util/metadata/metadata.go b/pkg/util/metadata/metadata.go index f78fbc64b6..e7cba7dddd 100644 --- a/pkg/util/metadata/metadata.go +++ b/pkg/util/metadata/metadata.go @@ -306,7 +306,7 @@ func (m *metadataService) GetAvailabilityZone() (string, error) { if err != nil { return "", err } - return md.AvailabilityZone, nil + return util.SanitizeLabel(md.AvailabilityZone) } func CheckMetadataSearchOrder(order string) error { diff --git a/pkg/util/util.go b/pkg/util/util.go index fdd76dcb5b..0c6cc90cdb 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "regexp" "strings" "time" @@ -167,3 +168,20 @@ func GetAZFromTopology(topologyKey string, requirement *csi.TopologyRequirement) return zone } + +func SanitizeLabel(input string) (string, error) { + // 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] + } + + // Convert to lowercase + return strings.ToLower(sanitized), nil +}