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.
 
 
 

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