Skip to content

Commit

Permalink
[occm] validate flavor name, fallback on flavor id (kubernetes#1364)
Browse files Browse the repository at this point in the history
The node.kubernetes.io/instance-type label accepts alphanumeric
characters, '-', '_', and '.' only. If the flavor name contains
invalid characters, the flavor id is used instead.

Signed-off-by: Sean Schneeweiss <sean.schneeweiss@daimler.com>
  • Loading branch information
seanschneeweiss committed Feb 10, 2021
1 parent 8d1737f commit 41e2016
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions pkg/cloudprovider/providers/openstack/openstack_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/cloud-provider-openstack/pkg/metrics"
"k8s.io/cloud-provider-openstack/pkg/util/errors"
Expand Down Expand Up @@ -280,21 +281,38 @@ func srvInstanceType(client *gophercloud.ServiceClient, srv *servers.Server) (st
keys := []string{"original_name", "id"}
for _, key := range keys {
val, found := srv.Flavor[key]
if found {
flavor, ok := val.(string)
if ok {
if key == "id" {
mc := metrics.NewMetricContext("flavor", "get")
f, err := flavors.Get(client, flavor).Extract()
if mc.ObserveRequest(err) == nil {
return f.Name, nil
}
}
return flavor, nil
if !found {
continue
}

flavor, ok := val.(string)
if !ok {
continue
}

if key == "original_name" && isValidLabelValue(flavor) {
return flavor, nil
}

// get flavor name by id
mc := metrics.NewMetricContext("flavor", "get")
f, err := flavors.Get(client, flavor).Extract()
if mc.ObserveRequest(err) == nil {
if isValidLabelValue(f.Name) {
return f.Name, nil
}
// fallback on flavor id
return f.ID, nil
}
}
return "", fmt.Errorf("flavor name/id not found")
return "", fmt.Errorf("flavor original_name/id not found")
}

func isValidLabelValue(v string) bool {
if errs := validation.IsValidLabelValue(v); len(errs) != 0 {
return false
}
return true
}

// If Instances.InstanceID or cloudprovider.GetInstanceProviderID is changed, the regexp should be changed too.
Expand Down

0 comments on commit 41e2016

Please sign in to comment.