forked from CrunchyData/postgres-operator
/
rbac.go
107 lines (91 loc) · 3.7 KB
/
rbac.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright 2021 - 2023 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ivorycluster
import (
"context"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"github.com/ivorysql/ivory-operator/internal/initialize"
"github.com/ivorysql/ivory-operator/internal/naming"
"github.com/ivorysql/ivory-operator/internal/patroni"
"github.com/ivorysql/ivory-operator/pkg/apis/ivory-operator.ivorysql.org/v1beta1"
)
// reconcileRBACResources creates Roles, RoleBindings, and ServiceAccounts for
// cluster. The returned instanceServiceAccount has all the authorization needed
// by an instance Pod.
func (r *Reconciler) reconcileRBACResources(
ctx context.Context, cluster *v1beta1.IvoryCluster,
) (
instanceServiceAccount *corev1.ServiceAccount, err error,
) {
return r.reconcileInstanceRBAC(ctx, cluster)
}
// +kubebuilder:rbac:groups="",resources="serviceaccounts",verbs={create,patch}
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources="roles",verbs={create,patch}
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources="rolebindings",verbs={create,patch}
// reconcileInstanceRBAC writes the Role, RoleBinding, and ServiceAccount for
// all instances of cluster.
func (r *Reconciler) reconcileInstanceRBAC(
ctx context.Context, cluster *v1beta1.IvoryCluster,
) (*corev1.ServiceAccount, error) {
account := &corev1.ServiceAccount{ObjectMeta: naming.ClusterInstanceRBAC(cluster)}
account.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ServiceAccount"))
binding := &rbacv1.RoleBinding{ObjectMeta: naming.ClusterInstanceRBAC(cluster)}
binding.SetGroupVersionKind(rbacv1.SchemeGroupVersion.WithKind("RoleBinding"))
role := &rbacv1.Role{ObjectMeta: naming.ClusterInstanceRBAC(cluster)}
role.SetGroupVersionKind(rbacv1.SchemeGroupVersion.WithKind("Role"))
err := errors.WithStack(r.setControllerReference(cluster, account))
if err == nil {
err = errors.WithStack(r.setControllerReference(cluster, binding))
}
if err == nil {
err = errors.WithStack(r.setControllerReference(cluster, role))
}
account.Annotations = naming.Merge(cluster.Spec.Metadata.GetAnnotationsOrNil())
account.Labels = naming.Merge(cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.LabelCluster: cluster.Name,
})
binding.Annotations = naming.Merge(cluster.Spec.Metadata.GetAnnotationsOrNil())
binding.Labels = naming.Merge(cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.LabelCluster: cluster.Name,
})
role.Annotations = naming.Merge(cluster.Spec.Metadata.GetAnnotationsOrNil())
role.Labels = naming.Merge(cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
naming.LabelCluster: cluster.Name,
})
account.AutomountServiceAccountToken = initialize.Bool(true)
binding.RoleRef = rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: role.Kind,
Name: role.Name,
}
binding.Subjects = []rbacv1.Subject{{
Kind: account.Kind,
Name: account.Name,
}}
role.Rules = patroni.Permissions(cluster)
if err == nil {
err = errors.WithStack(r.apply(ctx, account))
}
if err == nil {
err = errors.WithStack(r.apply(ctx, role))
}
if err == nil {
err = errors.WithStack(r.apply(ctx, binding))
}
return account, err
}