generated from giantswarm/template-operator
/
create.go
95 lines (79 loc) · 2.74 KB
/
create.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
package clusternamespace
import (
"context"
"github.com/giantswarm/k8smetadata/pkg/annotation"
"github.com/giantswarm/k8smetadata/pkg/label"
"github.com/giantswarm/microerror"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgkey "github.com/giantswarm/rbac-operator/pkg/key"
"github.com/giantswarm/rbac-operator/pkg/project"
"github.com/giantswarm/rbac-operator/pkg/rbac"
"github.com/giantswarm/rbac-operator/service/controller/defaultnamespace/key"
)
// EnsureCreated Ensures that ClusterRoles with read and write permissions
// for app resources are created
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
namespace, err := key.ToNamespace(obj)
if err != nil {
return microerror.Mask(err)
}
if !pkgkey.IsDefaultNamespace(namespace.Name) {
return nil
}
err = r.createReadClusterNamespaceAppsRole(ctx)
if err != nil {
return microerror.Mask(err)
}
err = r.createWriteClusterNamespaceAppsRole(ctx)
if err != nil {
return microerror.Mask(err)
}
return nil
}
// Ensures the ClusterRole 'read-cluster-apps'.
//
// Purpose if this role is to enable read permissions (get, list, watch)
// for app resources in the cluster namespace
func (r *Resource) createReadClusterNamespaceAppsRole(ctx context.Context) error {
var err error
role := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: pkgkey.ReadClusterNamespaceAppsRole,
Labels: map[string]string{
label.ManagedBy: project.Name(),
label.DisplayInUserInterface: "true",
},
Annotations: map[string]string{
annotation.Notes: "If referenced within an organization namespace, grants read-only (get, list, watch) permissions to app resources in cluster namespaces belonging to the organization.",
},
},
}
if err = rbac.CreateOrUpdateClusterRole(r, ctx, role); err != nil {
return microerror.Mask(err)
}
return nil
}
// Ensures the ClusterRole 'write-cluster-apps'.
//
// Purpose if this role is to enable write permissions (get, list, watch, create, update, patch, delete)
// for app resources in the cluster namespace
func (r *Resource) createWriteClusterNamespaceAppsRole(ctx context.Context) error {
var err error
role := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: pkgkey.WriteClusterNamespaceAppsRole,
Labels: map[string]string{
label.ManagedBy: project.Name(),
label.DisplayInUserInterface: "true",
},
Annotations: map[string]string{
annotation.Notes: "If referenced within an organization namespace, grants read and write permissions to app resources in cluster namespaces belonging to the organization.",
},
},
}
if err = rbac.CreateOrUpdateClusterRole(r, ctx, role); err != nil {
return microerror.Mask(err)
}
return nil
}