/
clusterrolebindings.go
83 lines (75 loc) · 2.93 KB
/
clusterrolebindings.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package resources
import (
"fmt"
"github.com/samber/lo"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/kong/gateway-operator/pkg/consts"
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"
)
// -----------------------------------------------------------------------------
// ClusterRoleBinding generators
// -----------------------------------------------------------------------------
// GenerateNewClusterRoleBindingForControlPlane is a helper to generate a ClusterRoleBinding
// resource to bind roles to the service account used by the controlplane deployment.
func GenerateNewClusterRoleBindingForControlPlane(namespace, controlplaneName, serviceAccountName, clusterRoleName string) *rbacv1.ClusterRoleBinding {
crb := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
GenerateName: k8sutils.TrimGenerateName(fmt.Sprintf("%s-%s-", consts.ControlPlanePrefix, controlplaneName)),
Labels: map[string]string{
"app": controlplaneName,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: clusterRoleName,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: serviceAccountName,
Namespace: namespace,
},
},
}
LabelObjectAsControlPlaneManaged(crb)
return crb
}
// GenerateNewClusterRoleBindingForCertificateConfig is a helper to generate a ClusterRoleBinding
// to be used by the certificateConfig jobs
func GenerateNewClusterRoleBindingForCertificateConfig(namespace, name, labelValue string) *rbacv1.ClusterRoleBinding {
return &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
"app": labelValue,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: name,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: name,
Namespace: namespace,
},
},
}
}
// CompareClusterRoleName compares RoleRef in ClusterRoleBinding with given cluster role name.
// It returns true if the referenced role is the cluster role with the given name.
func CompareClusterRoleName(existingClusterRoleBinding *rbacv1.ClusterRoleBinding, clusterRoleName string) bool {
return existingClusterRoleBinding.RoleRef.APIGroup == "rbac.authorization.k8s.io" &&
existingClusterRoleBinding.RoleRef.Kind == "ClusterRole" &&
existingClusterRoleBinding.RoleRef.Name == clusterRoleName
}
// ClusterRoleBindingContainsServiceAccount returns true if the subjects of the ClusterRoleBinding contains given service account.
func ClusterRoleBindingContainsServiceAccount(existingClusterRoleBinding *rbacv1.ClusterRoleBinding, namespace string, serviceAccountName string) bool {
return lo.ContainsBy(existingClusterRoleBinding.Subjects, func(s rbacv1.Subject) bool {
return s.Kind == "ServiceAccount" && s.Namespace == namespace && s.Name == serviceAccountName
})
}