You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

267 lines
7.6 KiB

  1. // Copyright 2014 Google LLC
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package container contains a deprecated Google Container Engine client.
  15. //
  16. // Deprecated: Use cloud.google.com/go/container/apiv1 instead.
  17. package container // import "cloud.google.com/go/container"
  18. import (
  19. "errors"
  20. "fmt"
  21. "time"
  22. "golang.org/x/net/context"
  23. raw "google.golang.org/api/container/v1"
  24. "google.golang.org/api/option"
  25. htransport "google.golang.org/api/transport/http"
  26. )
  27. type Type string
  28. const (
  29. TypeCreate = Type("createCluster")
  30. TypeDelete = Type("deleteCluster")
  31. )
  32. type Status string
  33. const (
  34. StatusDone = Status("done")
  35. StatusPending = Status("pending")
  36. StatusRunning = Status("running")
  37. StatusError = Status("error")
  38. StatusProvisioning = Status("provisioning")
  39. StatusStopping = Status("stopping")
  40. )
  41. const prodAddr = "https://container.googleapis.com/"
  42. const userAgent = "gcloud-golang-container/20151008"
  43. // Client is a Google Container Engine client, which may be used to manage
  44. // clusters with a project. It must be constructed via NewClient.
  45. type Client struct {
  46. projectID string
  47. svc *raw.Service
  48. }
  49. // NewClient creates a new Google Container Engine client.
  50. func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error) {
  51. o := []option.ClientOption{
  52. option.WithEndpoint(prodAddr),
  53. option.WithScopes(raw.CloudPlatformScope),
  54. option.WithUserAgent(userAgent),
  55. }
  56. o = append(o, opts...)
  57. httpClient, endpoint, err := htransport.NewClient(ctx, o...)
  58. if err != nil {
  59. return nil, fmt.Errorf("dialing: %v", err)
  60. }
  61. svc, err := raw.New(httpClient)
  62. if err != nil {
  63. return nil, fmt.Errorf("constructing container client: %v", err)
  64. }
  65. svc.BasePath = endpoint
  66. c := &Client{
  67. projectID: projectID,
  68. svc: svc,
  69. }
  70. return c, nil
  71. }
  72. // Resource is a Google Container Engine cluster resource.
  73. type Resource struct {
  74. // Name is the name of this cluster. The name must be unique
  75. // within this project and zone, and can be up to 40 characters.
  76. Name string
  77. // Description is the description of the cluster. Optional.
  78. Description string
  79. // Zone is the Google Compute Engine zone in which the cluster resides.
  80. Zone string
  81. // Status is the current status of the cluster. It could either be
  82. // StatusError, StatusProvisioning, StatusRunning or StatusStopping.
  83. Status Status
  84. // Num is the number of the nodes in this cluster resource.
  85. Num int64
  86. // APIVersion is the version of the Kubernetes master and kubelets running
  87. // in this cluster. Allowed value is 0.4.2, or leave blank to
  88. // pick up the latest stable release.
  89. APIVersion string
  90. // Endpoint is the IP address of this cluster's Kubernetes master.
  91. // The endpoint can be accessed at https://username:password@endpoint/.
  92. // See Username and Password fields for the username and password information.
  93. Endpoint string
  94. // Username is the username to use when accessing the Kubernetes master endpoint.
  95. Username string
  96. // Password is the password to use when accessing the Kubernetes master endpoint.
  97. Password string
  98. // ContainerIPv4CIDR is the IP addresses of the container pods in
  99. // this cluster, in CIDR notation (e.g. 1.2.3.4/29).
  100. ContainerIPv4CIDR string
  101. // ServicesIPv4CIDR is the IP addresses of the Kubernetes services in this
  102. // cluster, in CIDR notation (e.g. 1.2.3.4/29). Service addresses are
  103. // always in the 10.0.0.0/16 range.
  104. ServicesIPv4CIDR string
  105. // MachineType is a Google Compute Engine machine type (e.g. n1-standard-1).
  106. // If none set, the default type is used while creating a new cluster.
  107. MachineType string
  108. // This field is ignored. It was removed from the underlying container API in v1.
  109. SourceImage string
  110. // Created is the creation time of this cluster.
  111. Created time.Time
  112. }
  113. func resourceFromRaw(c *raw.Cluster) *Resource {
  114. if c == nil {
  115. return nil
  116. }
  117. r := &Resource{
  118. Name: c.Name,
  119. Description: c.Description,
  120. Zone: c.Zone,
  121. Status: Status(c.Status),
  122. Num: c.CurrentNodeCount,
  123. APIVersion: c.InitialClusterVersion,
  124. Endpoint: c.Endpoint,
  125. Username: c.MasterAuth.Username,
  126. Password: c.MasterAuth.Password,
  127. ContainerIPv4CIDR: c.ClusterIpv4Cidr,
  128. ServicesIPv4CIDR: c.ServicesIpv4Cidr,
  129. MachineType: c.NodeConfig.MachineType,
  130. }
  131. r.Created, _ = time.Parse(time.RFC3339, c.CreateTime)
  132. return r
  133. }
  134. func resourcesFromRaw(c []*raw.Cluster) []*Resource {
  135. r := make([]*Resource, len(c))
  136. for i, val := range c {
  137. r[i] = resourceFromRaw(val)
  138. }
  139. return r
  140. }
  141. // Op represents a Google Container Engine API operation.
  142. type Op struct {
  143. // Name is the name of the operation.
  144. Name string
  145. // Zone is the Google Compute Engine zone.
  146. Zone string
  147. // This field is ignored. It was removed from the underlying container API in v1.
  148. TargetURL string
  149. // Type is the operation type. It could be either be TypeCreate or TypeDelete.
  150. Type Type
  151. // Status is the current status of this operation. It could be either
  152. // OpDone or OpPending.
  153. Status Status
  154. }
  155. func opFromRaw(o *raw.Operation) *Op {
  156. if o == nil {
  157. return nil
  158. }
  159. return &Op{
  160. Name: o.Name,
  161. Zone: o.Zone,
  162. Type: Type(o.OperationType),
  163. Status: Status(o.Status),
  164. }
  165. }
  166. func opsFromRaw(o []*raw.Operation) []*Op {
  167. ops := make([]*Op, len(o))
  168. for i, val := range o {
  169. ops[i] = opFromRaw(val)
  170. }
  171. return ops
  172. }
  173. // Clusters returns a list of cluster resources from the specified zone.
  174. // If no zone is specified, it returns all clusters under the user project.
  175. func (c *Client) Clusters(ctx context.Context, zone string) ([]*Resource, error) {
  176. if zone == "" {
  177. zone = "-"
  178. }
  179. resp, err := c.svc.Projects.Zones.Clusters.List(c.projectID, zone).Do()
  180. if err != nil {
  181. return nil, err
  182. }
  183. return resourcesFromRaw(resp.Clusters), nil
  184. }
  185. // Cluster returns metadata about the specified cluster.
  186. func (c *Client) Cluster(ctx context.Context, zone, name string) (*Resource, error) {
  187. resp, err := c.svc.Projects.Zones.Clusters.Get(c.projectID, zone, name).Do()
  188. if err != nil {
  189. return nil, err
  190. }
  191. return resourceFromRaw(resp), nil
  192. }
  193. // DeleteCluster deletes a cluster.
  194. func (c *Client) DeleteCluster(ctx context.Context, zone, name string) error {
  195. _, err := c.svc.Projects.Zones.Clusters.Delete(c.projectID, zone, name).Do()
  196. return err
  197. }
  198. // Operations returns a list of operations from the specified zone.
  199. // If no zone is specified, it looks up for all of the operations
  200. // that are running under the user's project.
  201. func (c *Client) Operations(ctx context.Context, zone string) ([]*Op, error) {
  202. if zone == "" {
  203. resp, err := c.svc.Projects.Zones.Operations.List(c.projectID, "-").Do()
  204. if err != nil {
  205. return nil, err
  206. }
  207. return opsFromRaw(resp.Operations), nil
  208. }
  209. resp, err := c.svc.Projects.Zones.Operations.List(c.projectID, zone).Do()
  210. if err != nil {
  211. return nil, err
  212. }
  213. return opsFromRaw(resp.Operations), nil
  214. }
  215. // Operation returns an operation.
  216. func (c *Client) Operation(ctx context.Context, zone, name string) (*Op, error) {
  217. resp, err := c.svc.Projects.Zones.Operations.Get(c.projectID, zone, name).Do()
  218. if err != nil {
  219. return nil, err
  220. }
  221. if resp.StatusMessage != "" {
  222. return nil, errors.New(resp.StatusMessage)
  223. }
  224. return opFromRaw(resp), nil
  225. }