...
1
16
17 package clusterrole
18
19 import (
20 "context"
21
22 metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
23 "k8s.io/apimachinery/pkg/runtime"
24 "k8s.io/apimachinery/pkg/util/validation/field"
25 "k8s.io/apiserver/pkg/registry/rest"
26 "k8s.io/apiserver/pkg/storage/names"
27 "k8s.io/kubernetes/pkg/api/legacyscheme"
28 "k8s.io/kubernetes/pkg/apis/rbac"
29 "k8s.io/kubernetes/pkg/apis/rbac/validation"
30 )
31
32
33 type strategy struct {
34 runtime.ObjectTyper
35 names.NameGenerator
36 }
37
38
39
40 var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator}
41
42
43 var _ rest.RESTCreateStrategy = Strategy
44
45
46 var _ rest.RESTUpdateStrategy = Strategy
47
48
49 func (strategy) NamespaceScoped() bool {
50 return false
51 }
52
53
54 func (strategy) AllowCreateOnUpdate() bool {
55 return true
56 }
57
58
59
60 func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
61 _ = obj.(*rbac.ClusterRole)
62 }
63
64
65 func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
66 newClusterRole := obj.(*rbac.ClusterRole)
67 oldClusterRole := old.(*rbac.ClusterRole)
68
69 _, _ = newClusterRole, oldClusterRole
70 }
71
72
73 func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
74 clusterRole := obj.(*rbac.ClusterRole)
75 opts := validation.ClusterRoleValidationOptions{
76 AllowInvalidLabelValueInSelector: false,
77 }
78 return validation.ValidateClusterRole(clusterRole, opts)
79 }
80
81
82 func (strategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { return nil }
83
84
85 func (strategy) Canonicalize(obj runtime.Object) {
86 _ = obj.(*rbac.ClusterRole)
87 }
88
89
90 func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
91 newObj := obj.(*rbac.ClusterRole)
92 oldObj := old.(*rbac.ClusterRole)
93 opts := validation.ClusterRoleValidationOptions{
94 AllowInvalidLabelValueInSelector: hasInvalidLabelValueInLabelSelector(oldObj),
95 }
96 errorList := validation.ValidateClusterRole(newObj, opts)
97 return append(errorList, validation.ValidateClusterRoleUpdate(newObj, old.(*rbac.ClusterRole), opts)...)
98 }
99
100
101 func (strategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
102 return nil
103 }
104
105
106
107
108
109
110 func (strategy) AllowUnconditionalUpdate() bool {
111 return true
112 }
113
114 func hasInvalidLabelValueInLabelSelector(role *rbac.ClusterRole) bool {
115 if role.AggregationRule != nil {
116 labelSelectorValidationOptions := metav1validation.LabelSelectorValidationOptions{AllowInvalidLabelValueInSelector: false}
117 for _, selector := range role.AggregationRule.ClusterRoleSelectors {
118 if len(metav1validation.ValidateLabelSelector(&selector, labelSelectorValidationOptions, nil)) > 0 {
119 return true
120 }
121 }
122 }
123 return false
124 }
125
View as plain text