2016-07-14 11:41:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-06-15 10:46:52 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2016-07-14 11:41:45 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2016-11-07 08:35:42 +00:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2016-07-14 11:41:45 +00:00
|
|
|
|
2017-06-22 17:25:57 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2016-07-14 11:41:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/cloudprovider"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Instances struct {
|
2017-04-08 13:50:23 +00:00
|
|
|
compute *gophercloud.ServiceClient
|
2016-07-14 11:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instances returns an implementation of Instances for OpenStack.
|
|
|
|
func (os *OpenStack) Instances() (cloudprovider.Instances, bool) {
|
|
|
|
glog.V(4).Info("openstack.Instances() called")
|
|
|
|
|
2017-05-25 13:41:30 +00:00
|
|
|
compute, err := os.NewComputeV2()
|
2016-07-14 11:41:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Info("Claiming to support Instances")
|
|
|
|
|
2017-04-08 13:50:23 +00:00
|
|
|
return &Instances{compute}, true
|
2016-07-14 11:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of Instances.CurrentNodeName
|
2016-08-30 02:34:47 +00:00
|
|
|
// Note this is *not* necessarily the same as hostname.
|
2016-07-16 06:10:29 +00:00
|
|
|
func (i *Instances) CurrentNodeName(hostname string) (types.NodeName, error) {
|
2016-08-30 02:34:47 +00:00
|
|
|
md, err := getMetadata()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return types.NodeName(md.Name), nil
|
2016-07-14 11:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instances) AddSSHKeyToAllInstances(user string, keyData []byte) error {
|
|
|
|
return errors.New("unimplemented")
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:58:42 +00:00
|
|
|
func (i *Instances) NodeAddresses(name types.NodeName) ([]v1.NodeAddress, error) {
|
2016-07-14 11:41:45 +00:00
|
|
|
glog.V(4).Infof("NodeAddresses(%v) called", name)
|
|
|
|
|
|
|
|
addrs, err := getAddressesByName(i.compute, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(4).Infof("NodeAddresses(%v) => %v", name, addrs)
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2017-03-06 21:33:26 +00:00
|
|
|
// NodeAddressesByProviderID returns the node addresses of an instances with the specified unique providerID
|
|
|
|
// This method will not be called from the node that is requesting this ID. i.e. metadata service
|
|
|
|
// and other local methods cannot be used here
|
|
|
|
func (i *Instances) NodeAddressesByProviderID(providerID string) ([]v1.NodeAddress, error) {
|
2017-06-15 10:46:52 +00:00
|
|
|
instanceID, err := instanceIDFromProviderID(providerID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return []v1.NodeAddress{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := servers.Get(i.compute, instanceID).Extract()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return []v1.NodeAddress{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses, err := nodeAddresses(server)
|
|
|
|
if err != nil {
|
|
|
|
return []v1.NodeAddress{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses, nil
|
2017-03-06 21:33:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 11:41:45 +00:00
|
|
|
// ExternalID returns the cloud provider ID of the specified instance (deprecated).
|
2016-07-16 06:10:29 +00:00
|
|
|
func (i *Instances) ExternalID(name types.NodeName) (string, error) {
|
2016-07-14 11:41:45 +00:00
|
|
|
srv, err := getServerByName(i.compute, name)
|
|
|
|
if err != nil {
|
2016-08-30 02:23:08 +00:00
|
|
|
if err == ErrNotFound {
|
|
|
|
return "", cloudprovider.InstanceNotFound
|
|
|
|
}
|
2016-07-14 11:41:45 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return srv.ID, nil
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:55:43 +00:00
|
|
|
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running.
|
2017-08-25 20:15:55 +00:00
|
|
|
// If false is returned with no error, the instance will be immediately deleted.
|
2017-08-21 18:55:43 +00:00
|
|
|
func (i *Instances) InstanceExistsByProviderID(providerID string) (bool, error) {
|
|
|
|
return false, errors.New("unimplemented")
|
|
|
|
}
|
|
|
|
|
2016-07-14 11:41:45 +00:00
|
|
|
// InstanceID returns the kubelet's cloud provider ID.
|
|
|
|
func (os *OpenStack) InstanceID() (string, error) {
|
2017-07-20 12:10:17 +00:00
|
|
|
if len(os.localInstanceID) == 0 {
|
|
|
|
id, err := readInstanceID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
os.localInstanceID = id
|
|
|
|
}
|
2016-07-14 11:41:45 +00:00
|
|
|
return os.localInstanceID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstanceID returns the cloud provider ID of the specified instance.
|
2016-07-16 06:10:29 +00:00
|
|
|
func (i *Instances) InstanceID(name types.NodeName) (string, error) {
|
2016-07-14 11:41:45 +00:00
|
|
|
srv, err := getServerByName(i.compute, name)
|
|
|
|
if err != nil {
|
2017-08-15 08:42:11 +00:00
|
|
|
if err == ErrNotFound {
|
|
|
|
return "", cloudprovider.InstanceNotFound
|
|
|
|
}
|
2016-07-14 11:41:45 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// In the future it is possible to also return an endpoint as:
|
|
|
|
// <endpoint>/<instanceid>
|
|
|
|
return "/" + srv.ID, nil
|
|
|
|
}
|
|
|
|
|
2017-03-06 21:33:26 +00:00
|
|
|
// InstanceTypeByProviderID returns the cloudprovider instance type of the node with the specified unique providerID
|
|
|
|
// This method will not be called from the node that is requesting this ID. i.e. metadata service
|
|
|
|
// and other local methods cannot be used here
|
|
|
|
func (i *Instances) InstanceTypeByProviderID(providerID string) (string, error) {
|
2017-06-15 10:46:52 +00:00
|
|
|
instanceID, err := instanceIDFromProviderID(providerID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := servers.Get(i.compute, instanceID).Extract()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return srvInstanceType(server)
|
2017-03-06 21:33:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 11:41:45 +00:00
|
|
|
// InstanceType returns the type of the specified instance.
|
2016-07-16 06:10:29 +00:00
|
|
|
func (i *Instances) InstanceType(name types.NodeName) (string, error) {
|
2017-06-15 10:46:52 +00:00
|
|
|
srv, err := getServerByName(i.compute, name)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return srvInstanceType(srv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func srvInstanceType(srv *servers.Server) (string, error) {
|
2017-07-19 20:06:49 +00:00
|
|
|
keys := []string{"name", "id", "original_name"}
|
|
|
|
for _, key := range keys {
|
|
|
|
val, found := srv.Flavor[key]
|
|
|
|
if found {
|
|
|
|
flavor, ok := val.(string)
|
|
|
|
if ok {
|
|
|
|
return flavor, nil
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 10:46:52 +00:00
|
|
|
}
|
2017-07-19 20:06:49 +00:00
|
|
|
return "", fmt.Errorf("flavor name/id not found")
|
2017-06-15 10:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func instanceIDFromProviderID(providerID string) (instanceID string, err error) {
|
|
|
|
parsedID, err := url.Parse(providerID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if parsedID.Scheme != ProviderName {
|
|
|
|
return "", fmt.Errorf("unrecognized provider %q", parsedID.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedID.Host, nil
|
2016-07-14 11:41:45 +00:00
|
|
|
}
|