Skip to content

Commit

Permalink
refactor and sanitize metadata az value also
Browse files Browse the repository at this point in the history
  • Loading branch information
scrungus committed Aug 23, 2024
1 parent 6dae10f commit bddc345
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
22 changes: 2 additions & 20 deletions pkg/openstack/instancesv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ import (
"context"
"fmt"
sysos "os"
"regexp"
"strings"

"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
v1 "k8s.io/api/core/v1"
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 @@ -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)
Expand All @@ -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
}
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)
}

func CheckMetadataSearchOrder(order string) error {
Expand Down
18 changes: 18 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,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
}

0 comments on commit bddc345

Please sign in to comment.