service.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "yunion.io/x/onecloud/pkg/mcclient/modules"
  20. )
  21. var Services *ServiceManager
  22. type ServiceManager struct {
  23. *NamespaceResourceManager
  24. }
  25. func init() {
  26. Services = &ServiceManager{NewNamespaceResourceManager(
  27. "k8s_service", "k8s_services",
  28. NewNamespaceCols("Type", "ClusterIP", "Ports", "Selector"),
  29. NewColumns()),
  30. }
  31. modules.Register(Services)
  32. }
  33. func (s ServiceManager) Get_Type(obj jsonutils.JSONObject) interface{} {
  34. typ, _ := obj.GetString("type")
  35. return typ
  36. }
  37. func (s ServiceManager) Get_ClusterIP(obj jsonutils.JSONObject) interface{} {
  38. clusterIp, _ := obj.GetString("clusterIP")
  39. return clusterIp
  40. }
  41. func (s ServiceManager) Get_Selector(obj jsonutils.JSONObject) interface{} {
  42. selectorObj, _ := obj.GetMap("selector")
  43. var selectors []string
  44. for k, obj := range selectorObj {
  45. val, _ := obj.GetString()
  46. selectors = append(selectors, fmt.Sprintf("%s=%s", k, val))
  47. }
  48. selectorStr := strings.Join(selectors, ",")
  49. return selectorStr
  50. }
  51. func (s ServiceManager) Get_Ports(obj jsonutils.JSONObject) interface{} {
  52. var ports []string
  53. var portsStr string
  54. portObjs, _ := obj.GetArray("internalEndpoint", "ports")
  55. if len(portObjs) != 0 {
  56. for _, obj := range portObjs {
  57. port, _ := obj.Int("port")
  58. proto, _ := obj.GetString("protocol")
  59. ports = append(ports, fmt.Sprintf("%d/%s", port, proto))
  60. }
  61. portsStr = strings.Join(ports, ",")
  62. }
  63. return portsStr
  64. }