Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to get instance id from node name by regexp #55

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pkg/cloudprovider/kubevirt/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type cloud struct {
}

type CloudConfig struct {
Kubeconfig string `yaml:"kubeconfig"` // The kubeconfig used to connect to the underkube
Kubeconfig string `yaml:"kubeconfig"` // The kubeconfig used to connect to the infra cluster
LoadBalancer LoadBalancerConfig `yaml:"loadbalancer"`
Instances InstancesConfig `yaml:"instances"`
Zones ZonesConfig `yaml:"zones"`
Expand All @@ -40,8 +40,9 @@ type LoadBalancerConfig struct {
}

type InstancesConfig struct {
Enabled bool `yaml:"enabled"` // Enables the instances interface of the CCM
EnableInstanceTypes bool `yaml:"enableInstanceTypes"` // Enables 'flavor' annotation to detect instance types
Enabled bool `yaml:"enabled"` // Enables the instances interface of the CCM
EnableInstanceTypes bool `yaml:"enableInstanceTypes"` // Enables 'flavor' annotation to detect instance types
MatchInstanceIDRegexp string `yaml:"matchInstanceIDRegexp"` // Regexp to match instance id from node name
}

type ZonesConfig struct {
Expand Down Expand Up @@ -125,7 +126,7 @@ func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
return &loadbalancer{
namespace: c.namespace,
client: c.client,
config: c.config.LoadBalancer,
config: c.config,
}, true
}

Expand All @@ -137,7 +138,7 @@ func (c *cloud) Instances() (cloudprovider.Instances, bool) {
return &instances{
namespace: c.namespace,
client: c.client,
config: c.config.Instances,
config: c.config,
}, true
}

Expand All @@ -153,6 +154,7 @@ func (c *cloud) Zones() (cloudprovider.Zones, bool) {
return &zones{
namespace: c.namespace,
client: c.client,
config: c.config,
}, true
}

Expand Down
44 changes: 32 additions & 12 deletions pkg/cloudprovider/kubevirt/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"regexp"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -23,7 +22,7 @@ const (
type instances struct {
namespace string
client client.Client
config InstancesConfig
config CloudConfig
}

// Must match providerIDs built by cloudprovider.GetInstanceProviderID
Expand All @@ -34,7 +33,10 @@ var providerIDRegexp = regexp.MustCompile(`^` + ProviderName + `://([0-9A-Za-z_-
// returns the address of the calling instance. We should do a rename to
// make this clearer.
func (i *instances) NodeAddresses(ctx context.Context, name types.NodeName) ([]corev1.NodeAddress, error) {
instanceID := instanceIDFromNodeName(string(name))
instanceID, err := instanceIDFromNodeName(string(name), i.config.Instances.MatchInstanceIDRegexp)
if err != nil {
return nil, err
}
return i.nodeAddressesByInstanceID(ctx, instanceID)
}

Expand Down Expand Up @@ -96,7 +98,11 @@ func (i *instances) ExternalID(ctx context.Context, nodeName types.NodeName) (st
// InstanceID returns the cloud provider ID of the node with the specified NodeName.
// Note that if the instance does not exist or is no longer running, we must return ("", cloudprovider.InstanceNotFound)
func (i *instances) InstanceID(ctx context.Context, nodeName types.NodeName) (string, error) {
name := instanceIDFromNodeName(string(nodeName))
name, err := instanceIDFromNodeName(string(nodeName), i.config.Instances.MatchInstanceIDRegexp)
if err != nil {
return "", err
}

var vmi kubevirtv1.VirtualMachineInstance
if err := i.client.Get(ctx, client.ObjectKey{Name: name, Namespace: i.namespace}, &vmi); err != nil {
if errors.IsNotFound(err) {
Expand All @@ -120,7 +126,10 @@ func (i *instances) InstanceID(ctx context.Context, nodeName types.NodeName) (st

// InstanceType returns the type of the specified instance.
func (i *instances) InstanceType(ctx context.Context, name types.NodeName) (string, error) {
instanceID := instanceIDFromNodeName(string(name))
instanceID, err := instanceIDFromNodeName(string(name), i.config.Instances.MatchInstanceIDRegexp)
if err != nil {
return "", err
}
return i.instanceTypeByInstanceID(ctx, instanceID)
}

Expand All @@ -135,7 +144,7 @@ func (i *instances) InstanceTypeByProviderID(ctx context.Context, providerID str
}

func (i *instances) instanceTypeByInstanceID(ctx context.Context, instanceID string) (string, error) {
if !i.config.EnableInstanceTypes {
if !i.config.Instances.EnableInstanceTypes {
// Only try to detect instance type if enabled
return "", nil
}
Expand Down Expand Up @@ -190,7 +199,10 @@ func (i *instances) InstanceExistsByProviderID(ctx context.Context, providerID s
instanceID, err := instanceIDFromProviderID(providerID)
if err != nil {
// Retry getting instanceID with the node name if we do not have a valid providerID
instanceID = instanceIDFromNodeName(providerID)
instanceID, err = instanceIDFromNodeName(providerID, i.config.Instances.MatchInstanceIDRegexp)
if err != nil {
return false, err
}
}
// If we can not get the VMI by its providerID, assume it no longer exists
var vmi kubevirtv1.VirtualMachineInstance
Expand Down Expand Up @@ -230,11 +242,19 @@ func (i *instances) InstanceShutdownByProviderID(ctx context.Context, providerID
return false, nil
}

// instanceIDFromNodeName extracts the instance ID from a given node name. In
// case the node name is a FQDN the hostname will be extracted as instance ID.
func instanceIDFromNodeName(nodeName string) string {
data := strings.SplitN(nodeName, ".", 2)
return data[0]
// instanceIDFromNodeName extracts the instance ID from a given node name based on the provided regexp.
// If the regexp is empty instance id will have the same value as a node name.
func instanceIDFromNodeName(nodeName, instanceRegexp string) (string, error) {
if instanceRegexp == "" {
return nodeName, nil
}

exp, err := regexp.CompilePOSIX(instanceRegexp)
if err != nil {
return "", err
}

return exp.FindString(nodeName), nil
}

func instanceIDsFromNodes(nodes []*corev1.Node) []string {
Expand Down
68 changes: 64 additions & 4 deletions pkg/cloudprovider/kubevirt/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ var (
Namespace: "test",
},
}
vmiNodeMatchRegexp = kubevirtv1.VirtualMachineInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "vmNode1",
Namespace: "test",
},
}
vmiNodeMatchRegexp2 = kubevirtv1.VirtualMachineInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "vmNode2",
Namespace: "test",
},
}

vmiList = kubevirtv1.VirtualMachineInstanceList{
Items: []kubevirtv1.VirtualMachineInstance{
vmiNodeHostname,
Expand All @@ -169,6 +182,8 @@ var (
vmiNodePhaseUnknown,
vmiNodeFlavor,
vmiNodeNoFlavor,
vmiNodeMatchRegexp,
vmiNodeMatchRegexp2,
},
}
)
Expand Down Expand Up @@ -336,6 +351,47 @@ func TestInstanceID(t *testing.T) {
}
}

func TestInstanceIDMatchedByRegex(t *testing.T) {
ctrl, ctx := gomock.WithContext(context.Background(), t)
defer ctrl.Finish()
c := mockclient.NewMockClient(ctrl)

i := &instances{
namespace: "test",
client: c,
config: CloudConfig{
Instances: InstancesConfig{
EnableInstanceTypes: true,
MatchInstanceIDRegexp: "vmNode[0-9]*",
},
},
}

gomock.InOrder(
c.EXPECT().Get(ctx, client.ObjectKey{Name: "vmNode1", Namespace: "test"}, gomock.AssignableToTypeOf(&kubevirtv1.VirtualMachineInstance{})).SetArg(2, vmiNodeMatchRegexp),
c.EXPECT().Get(ctx, client.ObjectKey{Name: "vmNode2", Namespace: "test"}, gomock.AssignableToTypeOf(&kubevirtv1.VirtualMachineInstance{})).SetArg(2, vmiNodeMatchRegexp2),
)

tests := []struct {
nodeName types.NodeName
expectedInstanceID string
expectedError error
}{
{types.NodeName("local.vmNode1"), "vmNode1", nil},
{types.NodeName("local.example.vmNode2"), "vmNode2", nil},
}

for _, test := range tests {
externalID, err := i.InstanceID(ctx, test.nodeName)
if externalID != test.expectedInstanceID {
t.Errorf("Expected: %v, got: %v", test.expectedInstanceID, externalID)
}
if test.expectedError != nil && err != nil && err.Error() != test.expectedError.Error() {
t.Errorf("Expected: '%v', got '%v'", test.expectedError, err)
}
}
}

func TestInstanceType(t *testing.T) {
ctrl, ctx := gomock.WithContext(context.Background(), t)
defer ctrl.Finish()
Expand All @@ -344,8 +400,10 @@ func TestInstanceType(t *testing.T) {
i := &instances{
namespace: "test",
client: c,
config: InstancesConfig{
EnableInstanceTypes: true,
config: CloudConfig{
Instances: InstancesConfig{
EnableInstanceTypes: true,
},
},
}

Expand Down Expand Up @@ -384,8 +442,10 @@ func TestInstanceTypeByProviderID(t *testing.T) {
i := &instances{
namespace: "test",
client: c,
config: InstancesConfig{
EnableInstanceTypes: true,
config: CloudConfig{
Instances: InstancesConfig{
EnableInstanceTypes: true,
},
},
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/cloudprovider/kubevirt/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
type loadbalancer struct {
namespace string
client client.Client
config LoadBalancerConfig
config CloudConfig
}

// GetLoadBalancer returns whether the specified load balancer exists, and
Expand Down Expand Up @@ -314,10 +314,10 @@ func (lb *loadbalancer) ensureServiceLabelsDeleted(ctx context.Context, lbName,
}

func (lb *loadbalancer) getLoadBalancerCreatePollInterval() time.Duration {
if lb.config.CreationPollInterval > 0 {
return time.Duration(lb.config.CreationPollInterval)
if lb.config.LoadBalancer.CreationPollInterval > 0 {
return time.Duration(lb.config.LoadBalancer.CreationPollInterval)
}
klog.Infof("Creation poll interval '%d' must be > 0. Setting to '%d'", lb.config.CreationPollInterval, defaultLoadBalancerCreatePollInterval)
klog.Infof("Creation poll interval '%d' must be > 0. Setting to '%d'", lb.config.LoadBalancer.CreationPollInterval, defaultLoadBalancerCreatePollInterval)
return defaultLoadBalancerCreatePollInterval
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/cloudprovider/kubevirt/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,10 @@ func TestEnsureLoadBalancer(t *testing.T) {
lb := &loadbalancer{
namespace: "test",
client: c,
config: LoadBalancerConfig{
CreationPollInterval: 1,
config: CloudConfig{
LoadBalancer: LoadBalancerConfig{
CreationPollInterval: 1,
},
},
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/cloudprovider/kubevirt/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type zones struct {
namespace string
client client.Client
config CloudConfig
}

// GetZone returns the Zone containing the current failure zone and locality region that the program is running in
Expand All @@ -40,7 +41,10 @@ func (z *zones) GetZoneByProviderID(ctx context.Context, providerID string) (clo
// This method is particularly used in the context of external cloud providers where node initialization must be down
// outside the kubelets.
func (z *zones) GetZoneByNodeName(ctx context.Context, nodeName types.NodeName) (cloudprovider.Zone, error) {
instanceID := instanceIDFromNodeName(string(nodeName))
instanceID, err := instanceIDFromNodeName(string(nodeName), z.config.Instances.MatchInstanceIDRegexp)
if err != nil {
return cloudprovider.Zone{}, err
}
return z.getZoneByInstanceID(ctx, instanceID)
}

Expand Down