container.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  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 Google Container Engine client.
  15. //
  16. // For more information about the API,
  17. // see https://cloud.google.com/container-engine/docs
  18. package container // import "google.golang.org/cloud/container"
  19. import (
  20. "errors"
  21. "net/http"
  22. "time"
  23. "golang.org/x/net/context"
  24. raw "google.golang.org/api/container/v1"
  25. "google.golang.org/cloud/internal"
  26. )
  27. type Type string
  28. var (
  29. TypeCreate Type = Type("createCluster")
  30. TypeDelete Type = Type("deleteCluster")
  31. )
  32. type Status string
  33. var (
  34. Done = Status("done")
  35. Pending = Status("pending")
  36. Running = Status("running")
  37. Error = Status("error")
  38. Provisioning = Status("provisioning")
  39. Stopping = Status("stopping")
  40. )
  41. // Resource is a Google Container Engine cluster resource.
  42. type Resource struct {
  43. // Name is the name of this cluster. The name must be unique
  44. // within this project and zone, and can be up to 40 characters.
  45. Name string
  46. // Description is the description of the cluster. Optional.
  47. Description string
  48. // Zone is the Google Compute Engine zone in which the cluster resides.
  49. Zone string
  50. // Status is the current status of the cluster. It could either be
  51. // StatusError, StatusProvisioning, StatusRunning or StatusStopping.
  52. Status Status
  53. // Num is the number of the nodes in this cluster resource.
  54. Num int64
  55. // APIVersion is the version of the Kubernetes master and kubelets running
  56. // in this cluster. Allowed value is 0.4.2, or leave blank to
  57. // pick up the latest stable release.
  58. APIVersion string
  59. // Endpoint is the IP address of this cluster's Kubernetes master.
  60. // The endpoint can be accessed at https://username:password@endpoint/.
  61. // See Username and Password fields for the username and password information.
  62. Endpoint string
  63. // Username is the username to use when accessing the Kubernetes master endpoint.
  64. Username string
  65. // Password is the password to use when accessing the Kubernetes master endpoint.
  66. Password string
  67. // ContainerIPv4CIDR is the IP addresses of the container pods in
  68. // this cluster, in CIDR notation (e.g. 1.2.3.4/29).
  69. ContainerIPv4CIDR string
  70. // ServicesIPv4CIDR is the IP addresses of the Kubernetes services in this
  71. // cluster, in CIDR notation (e.g. 1.2.3.4/29). Service addresses are
  72. // always in the 10.0.0.0/16 range.
  73. ServicesIPv4CIDR string
  74. // MachineType is a Google Compute Engine machine type (e.g. n1-standard-1).
  75. // If none set, the default type is used while creating a new cluster.
  76. MachineType string
  77. // This field is ignored. It was removed from the underlying container API in v1.
  78. SourceImage string
  79. // Created is the creation time of this cluster.
  80. Created time.Time
  81. }
  82. func resourceFromRaw(c *raw.Cluster) *Resource {
  83. if c == nil {
  84. return nil
  85. }
  86. r := &Resource{
  87. Name: c.Name,
  88. Description: c.Description,
  89. Zone: c.Zone,
  90. Status: Status(c.Status),
  91. Num: c.InitialNodeCount,
  92. APIVersion: c.InitialClusterVersion,
  93. Endpoint: c.Endpoint,
  94. Username: c.MasterAuth.Username,
  95. Password: c.MasterAuth.Password,
  96. ContainerIPv4CIDR: c.ClusterIpv4Cidr,
  97. ServicesIPv4CIDR: c.ServicesIpv4Cidr,
  98. MachineType: c.NodeConfig.MachineType,
  99. }
  100. r.Created, _ = time.Parse(time.RFC3339, c.CreateTime)
  101. return r
  102. }
  103. func resourcesFromRaw(c []*raw.Cluster) []*Resource {
  104. r := make([]*Resource, len(c))
  105. for i, val := range c {
  106. r[i] = resourceFromRaw(val)
  107. }
  108. return r
  109. }
  110. // Op represents a Google Container Engine API operation.
  111. type Op struct {
  112. // Name is the name of the operation.
  113. Name string
  114. // Zone is the Google Compute Engine zone.
  115. Zone string
  116. // This field is ignored. It was removed from the underlying container API in v1.
  117. TargetURL string
  118. // Type is the operation type. It could be either be TypeCreate or TypeDelete.
  119. Type Type
  120. // Status is the current status of this operation. It could be either
  121. // OpDone or OpPending.
  122. Status Status
  123. }
  124. func opFromRaw(o *raw.Operation) *Op {
  125. if o == nil {
  126. return nil
  127. }
  128. return &Op{
  129. Name: o.Name,
  130. Zone: o.Zone,
  131. Type: Type(o.OperationType),
  132. Status: Status(o.Status),
  133. }
  134. }
  135. func opsFromRaw(o []*raw.Operation) []*Op {
  136. ops := make([]*Op, len(o))
  137. for i, val := range o {
  138. ops[i] = opFromRaw(val)
  139. }
  140. return ops
  141. }
  142. // Clusters returns a list of cluster resources from the specified zone.
  143. // If no zone is specified, it returns all clusters under the user project.
  144. func Clusters(ctx context.Context, zone string) ([]*Resource, error) {
  145. s := rawService(ctx)
  146. if zone == "" {
  147. resp, err := s.Projects.Zones.Clusters.List(internal.ProjID(ctx), "-").Do()
  148. if err != nil {
  149. return nil, err
  150. }
  151. return resourcesFromRaw(resp.Clusters), nil
  152. }
  153. resp, err := s.Projects.Zones.Clusters.List(internal.ProjID(ctx), zone).Do()
  154. if err != nil {
  155. return nil, err
  156. }
  157. return resourcesFromRaw(resp.Clusters), nil
  158. }
  159. // Cluster returns metadata about the specified cluster.
  160. func Cluster(ctx context.Context, zone, name string) (*Resource, error) {
  161. s := rawService(ctx)
  162. resp, err := s.Projects.Zones.Clusters.Get(internal.ProjID(ctx), zone, name).Do()
  163. if err != nil {
  164. return nil, err
  165. }
  166. return resourceFromRaw(resp), nil
  167. }
  168. // CreateCluster creates a new cluster with the provided metadata
  169. // in the specified zone.
  170. func CreateCluster(ctx context.Context, zone string, resource *Resource) (*Resource, error) {
  171. panic("not implemented")
  172. }
  173. // DeleteCluster deletes a cluster.
  174. func DeleteCluster(ctx context.Context, zone, name string) error {
  175. s := rawService(ctx)
  176. _, err := s.Projects.Zones.Clusters.Delete(internal.ProjID(ctx), zone, name).Do()
  177. return err
  178. }
  179. // Operations returns a list of operations from the specified zone.
  180. // If no zone is specified, it looks up for all of the operations
  181. // that are running under the user's project.
  182. func Operations(ctx context.Context, zone string) ([]*Op, error) {
  183. s := rawService(ctx)
  184. if zone == "" {
  185. resp, err := s.Projects.Zones.Operations.List(internal.ProjID(ctx), "-").Do()
  186. if err != nil {
  187. return nil, err
  188. }
  189. return opsFromRaw(resp.Operations), nil
  190. }
  191. resp, err := s.Projects.Zones.Operations.List(internal.ProjID(ctx), zone).Do()
  192. if err != nil {
  193. return nil, err
  194. }
  195. return opsFromRaw(resp.Operations), nil
  196. }
  197. // Operation returns an operation.
  198. func Operation(ctx context.Context, zone, name string) (*Op, error) {
  199. s := rawService(ctx)
  200. resp, err := s.Projects.Zones.Operations.Get(internal.ProjID(ctx), zone, name).Do()
  201. if err != nil {
  202. return nil, err
  203. }
  204. if resp.StatusMessage != "" {
  205. return nil, errors.New(resp.StatusMessage)
  206. }
  207. return opFromRaw(resp), nil
  208. }
  209. func rawService(ctx context.Context) *raw.Service {
  210. return internal.Service(ctx, "container", func(hc *http.Client) interface{} {
  211. svc, _ := raw.New(hc)
  212. return svc
  213. }).(*raw.Service)
  214. }