-
Notifications
You must be signed in to change notification settings - Fork 90
/
config.go
37 lines (33 loc) · 1.18 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package k8sutil
import (
"github.com/pkg/errors"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
authorizationv1 "k8s.io/api/authorization/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/client-go/kubernetes"
)
func GetCurrentRules(deployOptions kotsadmtypes.DeployOptions, clientset *kubernetes.Clientset) ([]rbacv1.PolicyRule, error) {
sar := &authorizationv1.SelfSubjectRulesReview{
Spec: authorizationv1.SelfSubjectRulesReviewSpec{
Namespace: deployOptions.Namespace,
},
}
response, err := clientset.AuthorizationV1().SelfSubjectRulesReviews().Create(sar)
if err != nil {
return nil, errors.Wrapf(err, "no SAR in ns %s", deployOptions.Namespace)
}
return convertToPolicyRule(response.Status), nil
}
func convertToPolicyRule(status authorizationv1.SubjectRulesReviewStatus) []rbacv1.PolicyRule {
ret := []rbacv1.PolicyRule{}
// only include resource rules, not NonResourceRules, as those can't be part of a role
for _, resource := range status.ResourceRules {
ret = append(ret, rbacv1.PolicyRule{
Verbs: resource.Verbs,
APIGroups: resource.APIGroups,
Resources: resource.Resources,
ResourceNames: resource.ResourceNames,
})
}
return ret
}