-
Notifications
You must be signed in to change notification settings - Fork 94
/
secret.go
189 lines (167 loc) · 5.85 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
/*
Copyright The Kubed Authors.
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 syncer
import (
"github.com/appscode/go/log"
"github.com/appscode/kubed/pkg/eventer"
"github.com/golang/glog"
"github.com/pkg/errors"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
core_util "kmodules.xyz/client-go/core/v1"
)
func (s *ConfigSyncer) SyncSecret(src *core.Secret) error {
opts := GetSyncOptions(src.Annotations)
if opts.NamespaceSelector != nil { // delete that were in old-ns but not in new-ns and upsert to new-ns
newNs, err := NamespacesForSelector(s.kubeClient, *opts.NamespaceSelector)
if err != nil {
return err
}
glog.Infof("secret %s/%s will be synced into namespaces %v if needed", src.Namespace, src.Name, newNs.List())
if err := s.syncSecretIntoNamespaces(s.kubeClient, src, newNs, true, ""); err != nil {
return err
}
} else { // no sync, delete that were previously added
if err := s.syncSecretIntoNamespaces(s.kubeClient, src, sets.NewString(), true, ""); err != nil {
return err
}
}
return s.syncSecretIntoContexts(src, opts.Contexts)
}
// source deleted, delete that were previously added
func (s *ConfigSyncer) SyncDeletedSecret(src *core.Secret) error {
if err := s.syncSecretIntoNamespaces(s.kubeClient, src, sets.NewString(), true, ""); err != nil {
return err
}
return s.syncSecretIntoContexts(src, sets.NewString())
}
func (s *ConfigSyncer) syncSecretIntoContexts(src *core.Secret, contexts sets.String) error {
// validate contexts specified via annotation
taken := map[string]struct{}{}
for _, ctx := range contexts.List() {
context, found := s.contexts[ctx]
if !found {
return errors.Errorf("context %s not found in kubeconfig file", ctx)
}
if _, found = taken[context.Address]; found {
return errors.Errorf("multiple contexts poniting same cluster")
}
taken[context.Address] = struct{}{}
}
// sync to contexts specified via annotation, do not ignore errors here
for _, ctx := range contexts.List() {
context := s.contexts[ctx]
if context.Namespace == "" { // use source namespace if not specified via context
context.Namespace = src.Namespace
}
err := s.syncSecretIntoNamespaces(context.Client, src, sets.NewString(context.Namespace), false, ctx)
if err != nil {
return err
}
}
// delete from other contexts, ignore errors here
for ctxName, ctx := range s.contexts {
if _, found := taken[ctx.Address]; !found {
err := s.syncSecretIntoNamespaces(ctx.Client, src, sets.NewString(), false, ctxName)
if err != nil {
log.Infoln(err)
}
taken[ctx.Address] = struct{}{} // to avoid deleting form same cluster twice
}
}
return nil
}
// upsert into newNs set, delete from (oldNs-newNs) set
// use skipSrcNs = true for sync in source cluster
func (s *ConfigSyncer) syncSecretIntoNamespaces(kc kubernetes.Interface, src *core.Secret, newNs sets.String, skipSrcNs bool, context string) error {
oldNs, err := namespaceSetForSecretSelector(kc, s.syncerLabelSelector(src.Name, src.Namespace, s.clusterName))
if err != nil {
return err
}
oldNs = oldNs.Difference(newNs)
if skipSrcNs {
oldNs.Delete(src.Namespace)
newNs.Delete(src.Namespace)
}
for _, ns := range oldNs.List() {
if err := kc.CoreV1().Secrets(ns).Delete(src.Name, &metav1.DeleteOptions{}); err != nil {
return err
}
}
for _, ns := range newNs.List() {
if err = s.upsertSecret(kc, src, ns, context); err != nil {
return err
}
}
return nil
}
func (s *ConfigSyncer) syncSecretIntoNewNamespace(src *core.Secret, namespace *core.Namespace) error {
opts := GetSyncOptions(src.Annotations)
if opts.NamespaceSelector == nil {
return nil
}
if selector, err := labels.Parse(*opts.NamespaceSelector); err != nil {
return err
} else if selector.Matches(labels.Set(namespace.Labels)) {
return s.upsertSecret(s.kubeClient, src, namespace.Name, "")
}
return nil
}
func (s *ConfigSyncer) upsertSecret(k8sClient kubernetes.Interface, src *core.Secret, namespace, context string) error {
meta := metav1.ObjectMeta{
Name: src.Name,
Namespace: namespace,
}
_, _, err := core_util.CreateOrPatchSecret(k8sClient, meta, func(obj *core.Secret) *core.Secret {
// check origin cluster, if not match overwrite and create an event
if v, ok := obj.Labels[OriginClusterLabelKey]; ok && v != s.clusterName {
s.recorder.Eventf(
src,
core.EventTypeWarning,
eventer.EventReasonOriginConflict,
"Origin cluster changed from %s in context %s", v, context,
)
}
obj.Type = src.Type
obj.Data = src.Data
obj.Labels = labels.Merge(src.Labels, s.syncerLabels(src.Name, src.Namespace, s.clusterName))
obj.Kind = src.Kind
ref := core.ObjectReference{
APIVersion: src.APIVersion,
Kind: src.Kind,
Name: src.Name,
Namespace: src.Namespace,
UID: src.UID,
ResourceVersion: src.ResourceVersion,
}
obj.Annotations = s.syncerAnnotations(obj.Annotations, src.Annotations, ref)
return obj
}, true)
return err
}
func namespaceSetForSecretSelector(kc kubernetes.Interface, selector string) (sets.String, error) {
secret, err := kc.CoreV1().Secrets(metav1.NamespaceAll).List(metav1.ListOptions{
LabelSelector: selector,
})
if err != nil {
return nil, err
}
ns := sets.NewString()
for _, obj := range secret.Items {
ns.Insert(obj.Namespace)
}
return ns, nil
}