openstack_instances.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package openstack
  14. import (
  15. "context"
  16. "fmt"
  17. "regexp"
  18. "github.com/gophercloud/gophercloud"
  19. "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
  20. "k8s.io/klog"
  21. "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. cloudprovider "k8s.io/cloud-provider"
  24. )
  25. var _ cloudprovider.Instances = (*Instances)(nil)
  26. // Instances encapsulates an implementation of Instances for OpenStack.
  27. type Instances struct {
  28. compute *gophercloud.ServiceClient
  29. opts MetadataOpts
  30. }
  31. const (
  32. instanceShutoff = "SHUTOFF"
  33. )
  34. // Instances returns an implementation of Instances for OpenStack.
  35. func (os *OpenStack) Instances() (cloudprovider.Instances, bool) {
  36. klog.V(4).Info("openstack.Instances() called")
  37. compute, err := os.NewComputeV2()
  38. if err != nil {
  39. klog.Errorf("unable to access compute v2 API : %v", err)
  40. return nil, false
  41. }
  42. klog.V(4).Info("Claiming to support Instances")
  43. return &Instances{
  44. compute: compute,
  45. opts: os.metadataOpts,
  46. }, true
  47. }
  48. // CurrentNodeName implements Instances.CurrentNodeName
  49. // Note this is *not* necessarily the same as hostname.
  50. func (i *Instances) CurrentNodeName(ctx context.Context, hostname string) (types.NodeName, error) {
  51. md, err := getMetadata(i.opts.SearchOrder)
  52. if err != nil {
  53. return "", err
  54. }
  55. return types.NodeName(md.Name), nil
  56. }
  57. // AddSSHKeyToAllInstances is not implemented for OpenStack
  58. func (i *Instances) AddSSHKeyToAllInstances(ctx context.Context, user string, keyData []byte) error {
  59. return cloudprovider.NotImplemented
  60. }
  61. // NodeAddresses implements Instances.NodeAddresses
  62. func (i *Instances) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.NodeAddress, error) {
  63. klog.V(4).Infof("NodeAddresses(%v) called", name)
  64. addrs, err := getAddressesByName(i.compute, name)
  65. if err != nil {
  66. return nil, err
  67. }
  68. klog.V(4).Infof("NodeAddresses(%v) => %v", name, addrs)
  69. return addrs, nil
  70. }
  71. // NodeAddressesByProviderID returns the node addresses of an instances with the specified unique providerID
  72. // This method will not be called from the node that is requesting this ID. i.e. metadata service
  73. // and other local methods cannot be used here
  74. func (i *Instances) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]v1.NodeAddress, error) {
  75. instanceID, err := instanceIDFromProviderID(providerID)
  76. if err != nil {
  77. return []v1.NodeAddress{}, err
  78. }
  79. server, err := servers.Get(i.compute, instanceID).Extract()
  80. if err != nil {
  81. return []v1.NodeAddress{}, err
  82. }
  83. addresses, err := nodeAddresses(server)
  84. if err != nil {
  85. return []v1.NodeAddress{}, err
  86. }
  87. return addresses, nil
  88. }
  89. // InstanceExistsByProviderID returns true if the instance with the given provider id still exist.
  90. // If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
  91. func (i *Instances) InstanceExistsByProviderID(ctx context.Context, providerID string) (bool, error) {
  92. instanceID, err := instanceIDFromProviderID(providerID)
  93. if err != nil {
  94. return false, err
  95. }
  96. _, err = servers.Get(i.compute, instanceID).Extract()
  97. if err != nil {
  98. if isNotFound(err) {
  99. return false, nil
  100. }
  101. return false, err
  102. }
  103. return true, nil
  104. }
  105. // InstanceShutdownByProviderID returns true if the instances is in safe state to detach volumes
  106. func (i *Instances) InstanceShutdownByProviderID(ctx context.Context, providerID string) (bool, error) {
  107. instanceID, err := instanceIDFromProviderID(providerID)
  108. if err != nil {
  109. return false, err
  110. }
  111. server, err := servers.Get(i.compute, instanceID).Extract()
  112. if err != nil {
  113. return false, err
  114. }
  115. // SHUTOFF is the only state where we can detach volumes immediately
  116. if server.Status == instanceShutoff {
  117. return true, nil
  118. }
  119. return false, nil
  120. }
  121. // InstanceID returns the kubelet's cloud provider ID.
  122. func (os *OpenStack) InstanceID() (string, error) {
  123. if len(os.localInstanceID) == 0 {
  124. id, err := readInstanceID(os.metadataOpts.SearchOrder)
  125. if err != nil {
  126. return "", err
  127. }
  128. os.localInstanceID = id
  129. }
  130. return os.localInstanceID, nil
  131. }
  132. // InstanceID returns the cloud provider ID of the specified instance.
  133. func (i *Instances) InstanceID(ctx context.Context, name types.NodeName) (string, error) {
  134. srv, err := getServerByName(i.compute, name)
  135. if err != nil {
  136. if err == ErrNotFound {
  137. return "", cloudprovider.InstanceNotFound
  138. }
  139. return "", err
  140. }
  141. // In the future it is possible to also return an endpoint as:
  142. // <endpoint>/<instanceid>
  143. return "/" + srv.ID, nil
  144. }
  145. // InstanceTypeByProviderID returns the cloudprovider instance type of the node with the specified unique providerID
  146. // This method will not be called from the node that is requesting this ID. i.e. metadata service
  147. // and other local methods cannot be used here
  148. func (i *Instances) InstanceTypeByProviderID(ctx context.Context, providerID string) (string, error) {
  149. instanceID, err := instanceIDFromProviderID(providerID)
  150. if err != nil {
  151. return "", err
  152. }
  153. server, err := servers.Get(i.compute, instanceID).Extract()
  154. if err != nil {
  155. return "", err
  156. }
  157. return srvInstanceType(server)
  158. }
  159. // InstanceType returns the type of the specified instance.
  160. func (i *Instances) InstanceType(ctx context.Context, name types.NodeName) (string, error) {
  161. srv, err := getServerByName(i.compute, name)
  162. if err != nil {
  163. return "", err
  164. }
  165. return srvInstanceType(srv)
  166. }
  167. func srvInstanceType(srv *servers.Server) (string, error) {
  168. keys := []string{"name", "id", "original_name"}
  169. for _, key := range keys {
  170. val, found := srv.Flavor[key]
  171. if found {
  172. flavor, ok := val.(string)
  173. if ok {
  174. return flavor, nil
  175. }
  176. }
  177. }
  178. return "", fmt.Errorf("flavor name/id not found")
  179. }
  180. // instanceIDFromProviderID splits a provider's id and return instanceID.
  181. // A providerID is build out of '${ProviderName}:///${instance-id}'which contains ':///'.
  182. // See cloudprovider.GetInstanceProviderID and Instances.InstanceID.
  183. func instanceIDFromProviderID(providerID string) (instanceID string, err error) {
  184. // If Instances.InstanceID or cloudprovider.GetInstanceProviderID is changed, the regexp should be changed too.
  185. var providerIDRegexp = regexp.MustCompile(`^` + ProviderName + `:///([^/]+)$`)
  186. matches := providerIDRegexp.FindStringSubmatch(providerID)
  187. if len(matches) != 2 {
  188. return "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"openstack:///InstanceID\"", providerID)
  189. }
  190. return matches[1], nil
  191. }