forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
181 lines (144 loc) · 5.14 KB
/
template.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package podsecuritypolicy
import (
"fmt"
"strings"
v1beta12 "github.com/rancher/types/apis/extensions/v1beta1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
// RegisterTemplate propagates updates to pod security policy templates to their associated pod security policies.
// Ignores pod security policy templates not assigned to a cluster or project.
func RegisterTemplate(context *config.UserContext) {
logrus.Infof("registering podsecuritypolicy template handler for cluster %v", context.ClusterName)
m := &templateManager{
policies: context.Extensions.PodSecurityPolicies(""),
policyLister: context.Extensions.PodSecurityPolicies("").Controller().Lister(),
}
context.Management.Management.PodSecurityPolicyTemplates("").AddHandler(
"PodSecurityPolicyTemplateSyncHandler", m.sync)
}
type templateManager struct {
policies v1beta12.PodSecurityPolicyInterface
policyLister v1beta12.PodSecurityPolicyLister
}
func (m *templateManager) sync(key string, obj *v3.PodSecurityPolicyTemplate) error {
policies, err := m.policyLister.List("", labels.Everything())
if err != nil {
return fmt.Errorf("error getting policies: %v", err)
}
var childPolicies []*v1beta1.PodSecurityPolicy
for _, candidate := range policies {
if candidate.Annotations[podSecurityTemplateParentAnnotation] == obj.Name {
childPolicies = append(childPolicies, candidate)
}
}
if len(policies) == 0 {
// this pspt is not used so return immediately
return nil
}
for _, policy := range childPolicies {
if policy.Annotations[podSecurityVersionAnnotation] != obj.ResourceVersion {
_, err := fromTemplateExplicitName(m.policies, m.policyLister, policy.Name, obj)
if err != nil {
return err
}
}
}
return nil
}
func fromTemplate(policies v1beta12.PodSecurityPolicyInterface, policyLister v1beta12.PodSecurityPolicyLister,
key string, originalTemplate *v3.PodSecurityPolicyTemplate) (*v1beta1.PodSecurityPolicy, error) {
return fromTemplateExplicitName(policies, policyLister, keyToPolicyName(key), originalTemplate)
}
func fromTemplateExplicitName(policies v1beta12.PodSecurityPolicyInterface,
policyLister v1beta12.PodSecurityPolicyLister, key string,
originalTemplate *v3.PodSecurityPolicyTemplate) (*v1beta1.PodSecurityPolicy, error) {
template := originalTemplate.DeepCopy()
objectMeta := v1.ObjectMeta{}
objectMeta.Name = key
objectMeta.Annotations = make(map[string]string)
objectMeta.Annotations[podSecurityTemplateParentAnnotation] = template.Name
objectMeta.Annotations[podSecurityVersionAnnotation] = template.ResourceVersion
psp := &v1beta1.PodSecurityPolicy{
TypeMeta: v1.TypeMeta{
Kind: podSecurityPolicy,
APIVersion: apiVersion,
},
ObjectMeta: objectMeta,
Spec: template.Spec,
}
var policy *v1beta1.PodSecurityPolicy
var err error
if !doesPolicyExist(policyLister, key) {
policy, err = policies.Create(psp)
} else {
policy, err = policies.Update(psp)
}
if err != nil {
return nil, fmt.Errorf("error creating pod security policy: %v", err)
}
logrus.Debugf("created/updated a pod security policy with name %v", objectMeta.Name)
return policy, nil
}
func doesPolicyExist(policyLister v1beta12.PodSecurityPolicyLister, name string) bool {
_, err := policyLister.Get("", name)
return !errors.IsNotFound(err)
}
func getPodSecurityPolicyTemplateID(projectLister v3.ProjectLister, clusterLister v3.ClusterLister, projectID string,
clusterName string) (string, error) {
projects, err := projectLister.List("", labels.Everything())
if err != nil {
return "", fmt.Errorf("error getting projects: %v", err)
}
var project *v3.Project
for _, candidate := range projects {
if candidate.Name == projectID {
project = candidate
break
}
}
if project == nil {
return "", nil
}
podSecurityPolicyTemplateID := project.Spec.PodSecurityPolicyTemplateName
if podSecurityPolicyTemplateID == "" {
// check cluster
cluster, err := clusterLister.Get("", clusterName)
if err != nil {
return "", fmt.Errorf("error getting clusters: %v", err)
}
podSecurityPolicyTemplateID = cluster.Spec.DefaultPodSecurityPolicyTemplateName
if podSecurityPolicyTemplateID == "" {
logrus.Debugf("No pod security policy templates found for project %v and cluster %v", projectID,
clusterName)
return "", nil
}
}
return podSecurityPolicyTemplateID, nil
}
func keyToPolicyName(key string) string {
return fmt.Sprintf("%v-psp", strings.Replace(key, "/", "-", -1))
}
func updatePolicyIfOutdated(templateLister v3.PodSecurityPolicyTemplateLister,
policies v1beta12.PodSecurityPolicyInterface, policyLister v1beta12.PodSecurityPolicyLister, id string) error {
template, err := templateLister.Get("", id)
if err != nil {
return err
}
policy, err := policyLister.Get("", id)
if err != nil {
return err
}
if policy.Annotations[podSecurityVersionAnnotation] != template.ResourceVersion {
_, err = fromTemplate(policies, policyLister, policy.Name, template)
if err != nil {
return err
}
}
return nil
}