From 6139f9ab89d718fc2433d20fd4bc3d8759d3b9a4 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 20 Jul 2017 08:10:17 -0400 Subject: [PATCH] Avoid looking up instance id until we need it currently kube-controller-manager cannot run outside of a vm started by openstack (with --cloud-provider=openstack params). We try to read the instance id from the metadata provider or the config drive or the file location only when we really need it. In the normal scenario, the controller-manager uses the node name to get the instance id. https://github.com/kubernetes/kubernetes/blob/41541910e1699975a8f9202a89b6865e45921194/pkg/volume/cinder/attacher.go#L149 The localInstanceID is currently used only in the test case, so let us not read it until it is really needed. --- .../providers/openstack/openstack.go | 16 +++++----------- .../providers/openstack/openstack_instances.go | 7 +++++++ .../providers/openstack/openstack_test.go | 9 +++++++-- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pkg/cloudprovider/providers/openstack/openstack.go b/pkg/cloudprovider/providers/openstack/openstack.go index 7f9ebc78049..1f194f39b61 100644 --- a/pkg/cloudprovider/providers/openstack/openstack.go +++ b/pkg/cloudprovider/providers/openstack/openstack.go @@ -280,18 +280,12 @@ func newOpenStack(cfg Config) (*OpenStack, error) { return nil, err } - id, err := readInstanceID() - if err != nil { - return nil, err - } - os := OpenStack{ - provider: provider, - region: cfg.Global.Region, - lbOpts: cfg.LoadBalancer, - bsOpts: cfg.BlockStorage, - routeOpts: cfg.Route, - localInstanceID: id, + provider: provider, + region: cfg.Global.Region, + lbOpts: cfg.LoadBalancer, + bsOpts: cfg.BlockStorage, + routeOpts: cfg.Route, } err = checkOpenStackOpts(&os) diff --git a/pkg/cloudprovider/providers/openstack/openstack_instances.go b/pkg/cloudprovider/providers/openstack/openstack_instances.go index 810858acc75..70f29ff7bd0 100644 --- a/pkg/cloudprovider/providers/openstack/openstack_instances.go +++ b/pkg/cloudprovider/providers/openstack/openstack_instances.go @@ -143,6 +143,13 @@ func (i *Instances) ExternalID(name types.NodeName) (string, error) { // InstanceID returns the kubelet's cloud provider ID. func (os *OpenStack) InstanceID() (string, error) { + if len(os.localInstanceID) == 0 { + id, err := readInstanceID() + if err != nil { + return "", err + } + os.localInstanceID = id + } return os.localInstanceID, nil } diff --git a/pkg/cloudprovider/providers/openstack/openstack_test.go b/pkg/cloudprovider/providers/openstack/openstack_test.go index 1471cb262e9..0e6bac592f1 100644 --- a/pkg/cloudprovider/providers/openstack/openstack_test.go +++ b/pkg/cloudprovider/providers/openstack/openstack_test.go @@ -473,7 +473,12 @@ func TestVolumes(t *testing.T) { WaitForVolumeStatus(t, os, vol, volumeAvailableStatus) - diskId, err := os.AttachDisk(os.localInstanceID, vol) + id, err := os.InstanceID() + if err != nil { + t.Fatalf("Cannot find instance id: %v", err) + } + + diskId, err := os.AttachDisk(id, vol) if err != nil { t.Fatalf("Cannot AttachDisk Cinder volume %s: %v", vol, err) } @@ -487,7 +492,7 @@ func TestVolumes(t *testing.T) { } t.Logf("Volume (%s) found at path: %s\n", vol, devicePath) - err = os.DetachDisk(os.localInstanceID, vol) + err = os.DetachDisk(id, vol) if err != nil { t.Fatalf("Cannot DetachDisk Cinder volume %s: %v", vol, err) }