app.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "io/ioutil"
  18. "strconv"
  19. "strings"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/util/regutils"
  22. "yunion.io/x/pkg/util/sets"
  23. )
  24. type K8sAppBaseCreateOptions struct {
  25. NamespaceWithClusterOptions
  26. ServiceSpecOptions
  27. }
  28. func (o K8sAppBaseCreateOptions) Params() (*jsonutils.JSONDict, error) {
  29. params := o.NamespaceWithClusterOptions.Params()
  30. svcSpec, err := o.ServiceSpecOptions.Params()
  31. if err != nil {
  32. return nil, err
  33. }
  34. params.Update(svcSpec)
  35. return params, nil
  36. }
  37. type portMapping struct {
  38. Port int32 `json:"port"`
  39. TargetPort int32 `json:"targetPort"`
  40. Protocol string `json:"protocol"`
  41. }
  42. func parsePortMapping(port string) (*portMapping, error) {
  43. if len(port) == 0 {
  44. return nil, fmt.Errorf("empty port mapping desc string")
  45. }
  46. parts := strings.Split(port, ":")
  47. mapping := &portMapping{}
  48. for _, part := range parts {
  49. if sets.NewString("tcp", "udp").Has(strings.ToLower(part)) {
  50. mapping.Protocol = strings.ToUpper(part)
  51. }
  52. if port, err := strconv.Atoi(part); err != nil {
  53. continue
  54. } else {
  55. if mapping.Port == 0 {
  56. mapping.Port = int32(port)
  57. } else {
  58. mapping.TargetPort = int32(port)
  59. }
  60. }
  61. }
  62. if mapping.Protocol == "" {
  63. mapping.Protocol = "TCP"
  64. }
  65. if mapping.Port <= 0 {
  66. return nil, fmt.Errorf("Service port not provided")
  67. }
  68. if mapping.TargetPort < 0 {
  69. return nil, fmt.Errorf("Container invalid targetPort %d", mapping.TargetPort)
  70. }
  71. if mapping.TargetPort == 0 {
  72. mapping.TargetPort = mapping.Port
  73. }
  74. return mapping, nil
  75. }
  76. func parsePortMappings(ports []string) (*jsonutils.JSONArray, error) {
  77. ret := jsonutils.NewArray()
  78. for _, port := range ports {
  79. mapping, err := parsePortMapping(port)
  80. if err != nil {
  81. return nil, fmt.Errorf("Port %q error: %v", port, err)
  82. }
  83. ret.Add(jsonutils.Marshal(mapping))
  84. }
  85. return ret, nil
  86. }
  87. func parseNetConfig(net string) (*jsonutils.JSONDict, error) {
  88. ret := jsonutils.NewDict()
  89. for _, p := range strings.Split(net, ":") {
  90. if regutils.MatchIP4Addr(p) {
  91. ret.Add(jsonutils.NewString(p), "address")
  92. } else {
  93. ret.Add(jsonutils.NewString(p), "network")
  94. }
  95. }
  96. return ret, nil
  97. }
  98. type K8sAppCreateFromFileOptions struct {
  99. NamespaceResourceGetOptions
  100. FILE string `help:"K8s resource YAML or JSON file"`
  101. }
  102. func (o K8sAppCreateFromFileOptions) Params() (*jsonutils.JSONDict, error) {
  103. ret, err := o.NamespaceResourceGetOptions.Params()
  104. if err != nil {
  105. return nil, err
  106. }
  107. params := ret.(*jsonutils.JSONDict)
  108. params.Add(jsonutils.NewString(o.NAME), "name")
  109. content, err := ioutil.ReadFile(o.FILE)
  110. if err != nil {
  111. return nil, err
  112. }
  113. params.Add(jsonutils.NewString(string(content)), "content")
  114. return params, nil
  115. }
  116. func parseImage(str string) (jsonutils.JSONObject, error) {
  117. parts := strings.Split(str, "=")
  118. if len(parts) != 2 {
  119. return nil, fmt.Errorf("Invalid image string: %s", str)
  120. }
  121. ci := jsonutils.NewDict()
  122. ci.Add(jsonutils.NewString(parts[0]), "name")
  123. ci.Add(jsonutils.NewString(parts[1]), "image")
  124. return ci, nil
  125. }