forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_project_catalog.go
148 lines (137 loc) · 4.44 KB
/
template_project_catalog.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package template
import (
"fmt"
"reflect"
"sort"
"k8s.io/api/rbac/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
)
var rolesWithAccessToProjectCatalogTemplates = map[string]bool{
"project-owner": true,
"project-member": true,
"projectcatalogs-manage": true,
"projectcatalogs-view": true,
}
func (tm *RBACTemplateManager) reconcileProjectCatalog(projectID string, clusterID string) error {
if projectID == "" || clusterID == "" {
return nil
}
var projectTemplates, projectTemplateVersions []string
crName := "project-" + clusterID + "-" + projectID + clusterRoleSuffix
crbName := clusterID + "-" + projectID + clusterRoleBindingSuffix
// Get all project catalogs created in this project
projectCatalogs, err := tm.projectCatalogLister.List(projectID, labels.NewSelector())
if err != nil {
return fmt.Errorf("error while listing project catalogs: %v", err)
}
// Get all templates and template versions for each of these project catalogs.
// Templates for cluster catalog are created with label "clusterId-projectId-catalogName=catalogName"
for _, projectCatalog := range projectCatalogs {
c := clusterID + "-" + projectID + "-" + projectCatalog.Name
r, err := labels.NewRequirement(c, selection.Equals, []string{projectCatalog.Name})
if err != nil {
return err
}
//get templates, template versions for this cluster catalog
templates, templateVersions, err := tm.getTemplateAndTemplateVersions(r)
// add it to the cluster's templates templateVersions list
projectTemplates = append(projectTemplates, templates...)
projectTemplateVersions = append(projectTemplateVersions, templateVersions...)
}
// have them sorted for deepEqual calls later
sort.Strings(projectTemplates)
sort.Strings(projectTemplateVersions)
newRules := []v1.PolicyRule{
{
APIGroups: []string{"management.cattle.io"},
Resources: []string{templateRule},
ResourceNames: projectTemplates,
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{"management.cattle.io"},
Resources: []string{templateVersionRule},
ResourceNames: projectTemplateVersions,
Verbs: []string{"get", "list", "watch"},
},
}
// Add these templates/templateversions to the clusterRole for this project's templates/templateVersions
// check if the clusterRole exists
cRole, err := tm.clusterRoleClient.Get(crName, metav1.GetOptions{})
if err != nil {
if !k8serrors.IsNotFound(err) {
return err
}
// error is IsNotFound, so create it
newCR := &v1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: crName,
},
Rules: newRules,
}
if _, err := tm.clusterRoleClient.Create(newCR); err != nil {
return err
}
} else {
// update the role with the latest templates and templateversions
if !reflect.DeepEqual(cRole.Rules, newRules) {
return tm.updateClusterRole(cRole, newRules)
}
}
// Now that the cluster role is created/updated, create/update the cluster role binding with appropriate subjects
// Get all subjects for this CRB, which includes all users in this projects
// get a list of all members/owners of this project using PRTBs
prtbs, err := tm.prtbLister.List(projectID, labels.NewSelector())
if err != nil {
return err
}
subjects := []v1.Subject{}
subjectMap := make(map[v1.Subject]bool)
for _, prtb := range prtbs {
s, valid, err := buildSubjectFromRTB(prtb)
if err != nil {
return err
}
if !valid {
continue
}
if !subjectMap[s] {
subjectMap[s] = true
}
}
for key := range subjectMap {
subjects = append(subjects, key)
}
sort.Slice(subjects, func(i, j int) bool { return subjects[i].Name < subjects[j].Name })
// Now create or update the CRB
// First check if a clusterRoleBinding exists for the templates/templateversions in this cluster
crb, err := tm.crbClient.Get(crbName, metav1.GetOptions{})
if err != nil {
if !k8serrors.IsNotFound(err) {
return err
}
// crb not found, so create it
newCRB := &v1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: crbName,
},
RoleRef: v1.RoleRef{
Name: crName,
Kind: "ClusterRole",
},
Subjects: subjects,
}
if _, err := tm.crbClient.Create(newCRB); err != nil {
return err
}
return nil
}
// crb exists, so update it with the new subjects
if !reflect.DeepEqual(crb.Subjects, subjects) {
return tm.updateCRB(crb, subjects)
}
return nil
}