base.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/util/sets"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  20. )
  21. type ResourceManager struct {
  22. *modulebase.ResourceManager
  23. }
  24. func NewResourceManager(keyword, keywordPlural string, columns, adminColumns *Columns) *ResourceManager {
  25. man := &modulebase.ResourceManager{
  26. BaseManager: *modulebase.NewBaseManager("k8s", "", "", columns.Array(), adminColumns.Array()),
  27. Keyword: keyword,
  28. KeywordPlural: keywordPlural,
  29. }
  30. return &ResourceManager{man}
  31. }
  32. func NewJointManager(keyword, keywordPlural string, columns, adminColumns *Columns, master, slave modulebase.Manager) modulebase.JointResourceManager {
  33. return modulebase.JointResourceManager{
  34. ResourceManager: *NewResourceManager(keyword, keywordPlural, columns, adminColumns).ResourceManager,
  35. Master: master,
  36. Slave: slave,
  37. }
  38. }
  39. func (m ResourceManager) GetBaseManager() modulebase.ResourceManager {
  40. return *m.ResourceManager
  41. }
  42. type IClusterResourceManager interface {
  43. modulebase.Manager
  44. GetRaw(s *mcclient.ClientSession, id string, params *jsonutils.JSONDict) (jsonutils.JSONObject, error)
  45. UpdateRaw(s *mcclient.ClientSession, id string, query, body *jsonutils.JSONDict) (jsonutils.JSONObject, error)
  46. }
  47. type clusterResourceManager struct {
  48. *ResourceManager
  49. }
  50. func newClusterResourceManager(keyword, keywordPlural string, columns, adminColumns *Columns) *clusterResourceManager {
  51. newAdminCols := NewClusterCols(adminColumns.Array()...)
  52. man := NewResourceManager(keyword, keywordPlural, columns, newAdminCols)
  53. return &clusterResourceManager{man}
  54. }
  55. func (man clusterResourceManager) Get_Cluster(obj jsonutils.JSONObject) interface{} {
  56. cluster, _ := obj.GetString("cluster")
  57. return cluster
  58. }
  59. func (man clusterResourceManager) GetRaw(s *mcclient.ClientSession, id string, params *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
  60. return man.GetSpecific(s, id, "rawdata", params)
  61. }
  62. func (man clusterResourceManager) UpdateRaw(s *mcclient.ClientSession, id string, query, rawdata *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
  63. return man.PutSpecific(s, id, "rawdata", query, rawdata)
  64. }
  65. type ClusterResourceManager struct {
  66. *clusterResourceManager
  67. nameGetter
  68. ageGetter
  69. labelGetter
  70. }
  71. func NewClusterResourceManager(kw, kwp string, columns, adminColumns *Columns) *ClusterResourceManager {
  72. newCols := NewMetaCols(columns.Array()...)
  73. man := newClusterResourceManager(kw, kwp, newCols, adminColumns)
  74. return &ClusterResourceManager{man, getName, getAge, getLabel}
  75. }
  76. type NamespaceResourceManager struct {
  77. *ClusterResourceManager
  78. namespaceGetter
  79. }
  80. func NewNamespaceResourceManager(kw, kwp string, columns, adminColumns *Columns) *NamespaceResourceManager {
  81. newCols := NewNamespaceCols(columns.Array()...)
  82. man := NewClusterResourceManager(kw, kwp, newCols, adminColumns)
  83. return &NamespaceResourceManager{man, getNamespace}
  84. }
  85. type Columns struct {
  86. cols []string
  87. }
  88. func NewColumns(cols ...string) *Columns {
  89. c := &Columns{cols: make([]string, 0)}
  90. return c.Add(cols...)
  91. }
  92. func (c *Columns) Add(cols ...string) *Columns {
  93. for _, col := range cols {
  94. c.add(col)
  95. }
  96. return c
  97. }
  98. func (c *Columns) add(col string) *Columns {
  99. src := sets.NewString(c.Array()...)
  100. if src.Has(col) {
  101. return c
  102. }
  103. c.cols = append(c.cols, col)
  104. return c
  105. }
  106. func (c Columns) Array() []string {
  107. return c.cols
  108. }
  109. func NewNameCols(col ...string) *Columns {
  110. return NewColumns("Name", "Id", "Status").Add(col...)
  111. }
  112. func NewMetaCols(col ...string) *Columns {
  113. return NewNameCols("creationTimestamp", "Created_At").Add(col...)
  114. }
  115. func NewNamespaceCols(col ...string) *Columns {
  116. return NewMetaCols("Namespace_ID", "Namespace", "Labels").Add(col...)
  117. }
  118. func NewClusterCols(col ...string) *Columns {
  119. return NewColumns("Cluster_ID", "Cluster").Add(col...)
  120. }
  121. func NewResourceCols(col ...string) *Columns {
  122. return NewNameCols().Add(col...)
  123. }
  124. func NewFedJointClusterCols(col ...string) *Columns {
  125. return NewColumns(
  126. "Federatedresource_ID", "Federatedresource",
  127. "Cluster_ID", "Cluster",
  128. "Namespace_ID", "Namespace",
  129. "Resource_ID", "Resource",
  130. )
  131. }
  132. type ListPrinter interface {
  133. GetColumns(*mcclient.ClientSession) []string
  134. }