fed_clusterrole.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "strings"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. type FedClusterRoleCreateOptions struct {
  21. FedResourceCreateOptions
  22. // Spec FedClusterRoleSpec `json:"spec"`
  23. Rule []string `help:"role rule, e.g: 'apps/v1:deployments:get,watch,list'"`
  24. }
  25. type FedClusterRoleCreateInput struct {
  26. FedResourceCreateOptions
  27. Spec FedClusterRoleSpec `json:"spec"`
  28. }
  29. type FedClusterRoleSpec struct {
  30. Template ClusterRoleTemplate `json:"template"`
  31. }
  32. type ClusterRoleTemplate struct {
  33. Rules []PolicyRule `json:"rules"`
  34. }
  35. type PolicyRule struct {
  36. APIGroups []string `json:"apiGroups"`
  37. Resources []string `json:"resources"`
  38. Verbs []string `json:"verbs"`
  39. }
  40. func parsePolicyRules(rules []string) ([]PolicyRule, error) {
  41. ret := make([]PolicyRule, 0)
  42. for _, rule := range rules {
  43. r, err := parsePolicyRule(rule)
  44. if err != nil {
  45. return nil, err
  46. }
  47. ret = append(ret, *r)
  48. }
  49. return ret, nil
  50. }
  51. func parsePolicyRule(rule string) (*PolicyRule, error) {
  52. parts := strings.Split(rule, ":")
  53. if len(parts) != 3 {
  54. return nil, errors.Errorf("Invalid rule format: %q", rule)
  55. }
  56. groupStr := parts[0]
  57. resourceStr := parts[1]
  58. verbStr := parts[2]
  59. ret := &PolicyRule{
  60. Verbs: []string{},
  61. APIGroups: []string{},
  62. Resources: []string{},
  63. }
  64. ret.APIGroups = append(ret.APIGroups, strings.Split(groupStr, ",")...)
  65. ret.Resources = append(ret.Resources, strings.Split(resourceStr, ",")...)
  66. ret.Verbs = append(ret.Verbs, strings.Split(verbStr, ",")...)
  67. return ret, nil
  68. }
  69. func (o *FedClusterRoleCreateOptions) Params() (jsonutils.JSONObject, error) {
  70. rules, err := parsePolicyRules(o.Rule)
  71. if err != nil {
  72. return nil, err
  73. }
  74. input := FedClusterRoleCreateInput{
  75. FedResourceCreateOptions: o.FedResourceCreateOptions,
  76. Spec: FedClusterRoleSpec{
  77. Template: ClusterRoleTemplate{
  78. Rules: rules,
  79. },
  80. },
  81. }
  82. return input.JSON(input), nil
  83. }