service.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 Yunion
  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 k8s
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. )
  20. type ServiceSpecOptions struct {
  21. Port []string `help:"Port for the service that is created, format is <protocol>:<service_port>:<container_port> e.g. tcp:80:3000"`
  22. IsExternal bool `help:"Created service is external loadbalance"`
  23. LbNetwork string `help:"LoadBalancer service network id"`
  24. }
  25. func (o ServiceSpecOptions) Params() (*jsonutils.JSONDict, error) {
  26. if len(o.Port) == 0 {
  27. return nil, nil
  28. }
  29. params := jsonutils.NewDict()
  30. portMappings, err := parsePortMappings(o.Port)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if o.IsExternal {
  35. params.Add(jsonutils.JSONTrue, "isExternal")
  36. if o.LbNetwork != "" {
  37. params.Add(jsonutils.NewString(o.LbNetwork), "loadBalancerNetwork")
  38. }
  39. }
  40. params.Add(portMappings, "portMappings")
  41. return params, nil
  42. }
  43. func (o ServiceSpecOptions) Attach(data *jsonutils.JSONDict) error {
  44. return attachData(o, data, "service")
  45. }
  46. type ServiceCreateOptions struct {
  47. NamespaceWithClusterOptions
  48. ServiceSpecOptions
  49. NAME string `help:"Name of deployment"`
  50. Selector []string `help:"Selectors are backends pods labels, e.g. 'run=app'"`
  51. }
  52. func (o ServiceCreateOptions) Params() (jsonutils.JSONObject, error) {
  53. params := o.NamespaceWithClusterOptions.Params()
  54. svcSpec, err := o.ServiceSpecOptions.Params()
  55. if err != nil {
  56. return nil, err
  57. }
  58. selector := jsonutils.NewDict()
  59. for _, s := range o.Selector {
  60. parts := strings.Split(s, "=")
  61. if len(parts) != 2 {
  62. return nil, fmt.Errorf("Invalid selctor string: %s", s)
  63. }
  64. selector.Add(jsonutils.NewString(parts[1]), parts[0])
  65. }
  66. params.Update(svcSpec)
  67. params.Add(selector, "selector")
  68. params.Add(jsonutils.NewString(o.NAME), "name")
  69. return params, nil
  70. }
  71. type ServiceListOptions struct {
  72. NamespaceResourceListOptions
  73. Type string `help:"Service type" choices:"ClusterIP|LoadBalancer|NodePort|ExternalName"`
  74. }
  75. func (o ServiceListOptions) Params() (jsonutils.JSONObject, error) {
  76. params, err := o.NamespaceResourceListOptions.Params()
  77. if err != nil {
  78. return nil, err
  79. }
  80. if o.Type != "" {
  81. params.(*jsonutils.JSONDict).Add(jsonutils.NewString(o.Type), "type")
  82. }
  83. return params, nil
  84. }