forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
234 lines (206 loc) · 7.04 KB
/
secret.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package secret
import (
"context"
"strings"
"k8s.io/apimachinery/pkg/runtime"
"fmt"
"github.com/rancher/norman/controller"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
// SecretController listens for secret CUD in management API
// and propagates the changes to all corresponding namespaces in cluster API
// NamespaceController listens to cluster namespace events,
// reads secrets from the management namespace of corresponding project,
// and creates the secrets in the cluster namespace
const (
projectIDLabel = "field.cattle.io/projectId"
create = "create"
update = "update"
projectNamespaceAnnotation = "management.cattle.io/system-namespace"
userSecretAnnotation = "secret.user.cattle.io/secret"
)
type Controller struct {
secrets v1.SecretInterface
clusterNamespaceLister v1.NamespaceLister
managementNamespaceLister v1.NamespaceLister
projectLister v3.ProjectLister
clusterName string
}
func Register(ctx context.Context, cluster *config.UserContext) {
clusterSecretsClient := cluster.Core.Secrets("")
s := &Controller{
secrets: clusterSecretsClient,
clusterNamespaceLister: cluster.Core.Namespaces("").Controller().Lister(),
managementNamespaceLister: cluster.Management.Core.Namespaces("").Controller().Lister(),
projectLister: cluster.Management.Management.Projects("").Controller().Lister(),
clusterName: cluster.ClusterName,
}
n := &NamespaceController{
clusterSecretsClient: clusterSecretsClient,
managementSecrets: cluster.Management.Core.Secrets("").Controller().Lister(),
}
cluster.Core.Namespaces("").AddHandler(ctx, "secretsController", n.sync)
sync := v1.NewSecretLifecycleAdapter(fmt.Sprintf("secretsController_%s", cluster.ClusterName), true,
cluster.Management.Core.Secrets(""), s)
cluster.Management.Core.Secrets("").AddHandler(ctx, "secretsController", func(key string, obj *corev1.Secret) (runtime.Object, error) {
if obj == nil {
return sync(key, nil)
}
if !controller.ObjectInCluster(cluster.ClusterName, obj) {
return nil, nil
}
if obj.Labels != nil {
if obj.Labels["cattle.io/creator"] == "norman" {
return sync(key, obj)
}
}
return nil, nil
})
}
type NamespaceController struct {
clusterSecretsClient v1.SecretInterface
managementSecrets v1.SecretLister
}
func (n *NamespaceController) sync(key string, obj *corev1.Namespace) (runtime.Object, error) {
if obj == nil || obj.DeletionTimestamp != nil {
return nil, nil
}
// field.cattle.io/projectId value is <cluster name>:<project name>
if obj.Annotations[projectIDLabel] != "" {
parts := strings.Split(obj.Annotations[projectIDLabel], ":")
if len(parts) == 2 {
// on the management side, secret's namespace name equals to project name
secrets, err := n.managementSecrets.List(parts[1], labels.NewSelector())
if err != nil {
return nil, err
}
for _, secret := range secrets {
namespacedSecret := getNamespacedSecret(secret, obj.Name)
_, err := n.clusterSecretsClient.Create(namespacedSecret)
if err != nil && !errors.IsAlreadyExists(err) {
return nil, err
}
}
}
}
return nil, nil
}
func (s *Controller) Create(obj *corev1.Secret) (runtime.Object, error) {
return nil, s.createOrUpdate(obj, create)
}
func (s *Controller) Updated(obj *corev1.Secret) (runtime.Object, error) {
return nil, s.createOrUpdate(obj, update)
}
func (s *Controller) Remove(obj *corev1.Secret) (runtime.Object, error) {
clusterNamespaces, err := s.getClusterNamespaces(obj)
if err != nil {
return nil, err
}
for _, namespace := range clusterNamespaces {
logrus.Infof("Deleting secret [%s] in namespace [%s]", obj.Name, namespace.Name)
if err := s.secrets.DeleteNamespaced(namespace.Name, obj.Name, &metav1.DeleteOptions{}); err != nil && !errors.IsNotFound(err) {
return nil, err
}
}
return nil, nil
}
func (s *Controller) getClusterNamespaces(obj *corev1.Secret) ([]*corev1.Namespace, error) {
var toReturn []*corev1.Namespace
projectNamespace, err := s.managementNamespaceLister.Get("", obj.Namespace)
if err != nil {
if errors.IsNotFound(err) {
logrus.Warnf("Project namespace [%s] can't be found", obj.Namespace)
return toReturn, nil
}
return toReturn, err
}
if projectNamespace.Annotations == nil {
return toReturn, nil
}
if val, ok := projectNamespace.Annotations[projectNamespaceAnnotation]; !(ok && val == "true") {
return toReturn, nil
}
// Ignore projects from other clusters. Project namespace name = project name, so use it to locate the project
_, err = s.projectLister.Get(s.clusterName, projectNamespace.Name)
if err != nil {
if errors.IsNotFound(err) {
return toReturn, nil
}
return toReturn, err
}
namespaces, err := s.clusterNamespaceLister.List("", labels.NewSelector())
if err != nil {
return toReturn, err
}
// system project namespace name == project.Name
projectID := obj.Namespace
for _, namespace := range namespaces {
parts := strings.Split(namespace.Annotations[projectIDLabel], ":")
if len(parts) == 2 && parts[1] == projectID {
toReturn = append(toReturn, namespace)
}
}
return toReturn, nil
}
func (s *Controller) createOrUpdate(obj *corev1.Secret, action string) error {
if obj.Annotations[projectIDLabel] != "" {
parts := strings.Split(obj.Annotations[projectIDLabel], ":")
if len(parts) == 2 {
if parts[0] != s.clusterName {
return nil
}
}
}
clusterNamespaces, err := s.getClusterNamespaces(obj)
if err != nil {
return err
}
for _, namespace := range clusterNamespaces {
// copy the secret into namespace
namespacedSecret := getNamespacedSecret(obj, namespace.Name)
switch action {
case create:
logrus.Infof("Copying secret [%s] into namespace [%s]", namespacedSecret.Name, namespace.Name)
_, err := s.secrets.Create(namespacedSecret)
if err != nil && !errors.IsAlreadyExists(err) {
return err
}
case update:
_, err := s.secrets.Update(namespacedSecret)
if err != nil && !errors.IsNotFound(err) {
return err
} else if errors.IsNotFound(err) {
_, err := s.secrets.Create(namespacedSecret)
if err != nil {
return err
}
}
}
}
return nil
}
func getNamespacedSecret(obj *corev1.Secret, namespace string) *corev1.Secret {
namespacedSecret := &corev1.Secret{}
namespacedSecret.Name = obj.Name
namespacedSecret.Kind = obj.Kind
namespacedSecret.Data = obj.Data
namespacedSecret.StringData = obj.StringData
namespacedSecret.Namespace = namespace
namespacedSecret.Type = obj.Type
namespacedSecret.Annotations = make(map[string]string)
copyMap(obj.Annotations, namespacedSecret.Annotations)
namespacedSecret.Annotations[userSecretAnnotation] = "true"
return namespacedSecret
}
func copyMap(src map[string]string, dst map[string]string) {
for k, v := range src {
dst[k] = v
}
}