From 41e20162dd1cbe5a6541474e59074cc99477fd24 Mon Sep 17 00:00:00 2001 From: Sean Schneeweiss Date: Wed, 10 Feb 2021 08:44:59 +0100 Subject: [PATCH] [occm] validate flavor name, fallback on flavor id (#1364) 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 --- .../openstack/openstack_instances.go | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/pkg/cloudprovider/providers/openstack/openstack_instances.go b/pkg/cloudprovider/providers/openstack/openstack_instances.go index 64e810c290..ceed217d92 100644 --- a/pkg/cloudprovider/providers/openstack/openstack_instances.go +++ b/pkg/cloudprovider/providers/openstack/openstack_instances.go @@ -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" @@ -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.