-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.go
280 lines (235 loc) · 8.73 KB
/
setup.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package pidalio
import (
"context"
"encoding/json"
"errors"
"reflect"
"strconv"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/k-cloud-labs/pidalio/pkg/lister"
policyv1alpha1 "github.com/k-cloud-labs/pkg/apis/policy/v1alpha1"
"github.com/k-cloud-labs/pkg/client/clientset/versioned"
clientsetscheme "github.com/k-cloud-labs/pkg/client/clientset/versioned/scheme"
"github.com/k-cloud-labs/pkg/client/listers/policy/v1alpha1"
"github.com/k-cloud-labs/pkg/utils/dynamiclister"
"github.com/k-cloud-labs/pkg/utils/informermanager"
"github.com/k-cloud-labs/pkg/utils/interrupter"
"github.com/k-cloud-labs/pkg/utils/metrics"
"github.com/k-cloud-labs/pkg/utils/overridemanager"
"github.com/k-cloud-labs/pkg/utils/templatemanager"
"github.com/k-cloud-labs/pkg/utils/templatemanager/templates"
"github.com/k-cloud-labs/pkg/utils/tokenmanager"
)
// aggregatedScheme aggregates Kubernetes and extended schemes.
var aggregatedScheme = runtime.NewScheme()
func init() {
var _ = scheme.AddToScheme(aggregatedScheme) // add Kubernetes schemes
// add custom crd scheme to aggregatedScheme
var _ = clientsetscheme.AddToScheme(aggregatedScheme)
}
// NewForConfig creates a new client for the given config.
func NewForConfig(config *rest.Config) (client.Client, error) {
return client.New(config, client.Options{
Scheme: aggregatedScheme,
})
}
type setupManager struct {
policyClient versioned.Interface
client client.Client
drLister dynamiclister.DynamicResourceLister
opLister v1alpha1.OverridePolicyLister
copLister v1alpha1.ClusterOverridePolicyLister
informerManager informermanager.SingleClusterInformerManager
overrideManager overridemanager.OverrideManager
policyInterrupterManager interrupter.PolicyInterrupterManager
tokenManager tokenmanager.TokenManager
}
func (s *setupManager) setupAll(cfg *rest.Config, done <-chan struct{}) error {
if err := s.init(cfg, done); err != nil {
return err
}
if err := s.setupOverridePolicyManager(); err != nil {
return err
}
if err := s.setupInterrupter(); err != nil {
return err
}
return nil
}
func (s *setupManager) init(cfg *rest.Config, done <-chan struct{}) error {
cli, err := NewForConfig(cfg)
if err != nil {
return err
}
pc, err := versioned.NewForConfig(cfg)
if err != nil {
return err
}
s.policyClient = pc
s.client = cli
s.informerManager = informermanager.NewSingleClusterInformerManager(dynamic.NewForConfigOrDie(cfg), 0, done)
s.policyInterrupterManager = interrupter.NewPolicyInterrupterManager()
s.tokenManager = tokenmanager.NewTokenManager()
s.drLister, err = dynamiclister.NewDynamicResourceLister(cfg, done)
if err != nil {
klog.ErrorS(err, "failed to init dynamic client.")
return err
}
return nil
}
func (s *setupManager) waitForCacheSync() error {
s.informerManager.Start()
if result := s.informerManager.WaitForCacheSync(); !result[opGVR] || !result[copGVR] {
return errors.New("failed to sync override policy")
}
return nil
}
func (s *setupManager) setupInterrupter() error {
otm, err := templatemanager.NewOverrideTemplateManager(&templatemanager.TemplateSource{
Content: templates.OverrideTemplate,
TemplateName: "BaseTemplate",
})
if err != nil {
klog.ErrorS(err, "failed to setup mutating template manager.")
return err
}
vtm, err := templatemanager.NewValidateTemplateManager(&templatemanager.TemplateSource{
Content: templates.ValidateTemplate,
TemplateName: "BaseTemplate",
})
if err != nil {
klog.ErrorS(err, "failed to setup validate template manager.")
return err
}
// base
baseInterrupter := interrupter.NewBaseInterrupter(otm, vtm, templatemanager.NewCueManager())
// op
overridePolicyInterrupter := interrupter.NewOverridePolicyInterrupter(baseInterrupter, s.tokenManager, s.client, s.opLister)
s.policyInterrupterManager.AddInterrupter(schema.GroupVersionKind{
Group: policyv1alpha1.SchemeGroupVersion.Group,
Version: policyv1alpha1.SchemeGroupVersion.Version,
Kind: "OverridePolicy",
}, overridePolicyInterrupter)
// cop
s.policyInterrupterManager.AddInterrupter(schema.GroupVersionKind{
Group: policyv1alpha1.SchemeGroupVersion.Group,
Version: policyv1alpha1.SchemeGroupVersion.Version,
Kind: "ClusterOverridePolicy",
}, interrupter.NewClusterOverridePolicyInterrupter(overridePolicyInterrupter, s.copLister))
return s.policyInterrupterManager.OnStartUp()
}
var (
opGVR = schema.GroupVersionResource{
Group: policyv1alpha1.SchemeGroupVersion.Group,
Version: policyv1alpha1.SchemeGroupVersion.Version,
Resource: "overridepolicies",
}
copGVR = schema.GroupVersionResource{
Group: policyv1alpha1.SchemeGroupVersion.Group,
Version: policyv1alpha1.SchemeGroupVersion.Version,
Resource: "clusteroverridepolicies",
}
)
func (s *setupManager) setupOverridePolicyManager() (err error) {
opInformer := s.informerManager.Informer(opGVR)
copInformer := s.informerManager.Informer(copGVR)
opInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
metrics.IncrPolicy("OverridePolicy")
_ = s.onAddOverridePolicyPolicy(obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
_ = s.onUpdaterOverridePolicyPolicy(oldObj, newObj)
},
DeleteFunc: func(obj interface{}) {
metrics.DecPolicy("OverridePolicy")
},
})
copInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
metrics.IncrPolicy("ClusterOverridePolicy")
_ = s.onAddClusterOverridePolicy(obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
_ = s.onUpdateClusterOverridePolicy(oldObj, newObj)
},
DeleteFunc: func(obj interface{}) {
metrics.DecPolicy("ClusterOverridePolicy")
},
})
s.opLister = lister.NewUnstructuredOverridePolicyLister(opInformer.GetIndexer())
s.copLister = lister.NewUnstructuredClusterOverridePolicyLister(copInformer.GetIndexer())
s.overrideManager = overridemanager.NewOverrideManager(s.drLister, s.copLister, s.opLister)
return nil
}
const (
lastSyncTimeAnno = "policy.kcloudlabs.io/last-sync-time"
)
func (s *setupManager) onAddOverridePolicyPolicy(obj any) error {
op := new(policyv1alpha1.OverridePolicy)
if err := convertToPolicy(obj.(*unstructured.Unstructured), op); err != nil {
return err
}
op.Annotations[lastSyncTimeAnno] = strconv.FormatInt(time.Now().UnixNano(), 10)
_, err := s.policyClient.PolicyV1alpha1().OverridePolicies(op.GetNamespace()).Update(context.TODO(), op, metav1.UpdateOptions{})
return err
}
func (s *setupManager) onUpdaterOverridePolicyPolicy(oldObj, newObj any) error {
old := new(policyv1alpha1.OverridePolicy)
if err := convertToPolicy(oldObj.(*unstructured.Unstructured), old); err != nil {
return err
}
current := new(policyv1alpha1.OverridePolicy)
if err := convertToPolicy(newObj.(*unstructured.Unstructured), current); err != nil {
return err
}
if reflect.DeepEqual(current.Spec, old.Spec) {
return nil
}
current.Annotations[lastSyncTimeAnno] = strconv.FormatInt(time.Now().UnixNano(), 10)
_, err := s.policyClient.PolicyV1alpha1().OverridePolicies(current.GetNamespace()).Update(context.TODO(), current, metav1.UpdateOptions{})
return err
}
func (s *setupManager) onAddClusterOverridePolicy(obj any) error {
cop := new(policyv1alpha1.ClusterOverridePolicy)
if err := convertToPolicy(obj.(*unstructured.Unstructured), cop); err != nil {
return err
}
cop.Annotations[lastSyncTimeAnno] = strconv.FormatInt(time.Now().UnixNano(), 10)
_, err := s.policyClient.PolicyV1alpha1().ClusterOverridePolicies().Update(context.TODO(), cop, metav1.UpdateOptions{})
return err
}
func (s *setupManager) onUpdateClusterOverridePolicy(oldObj, newObj any) error {
old := new(policyv1alpha1.ClusterOverridePolicy)
if err := convertToPolicy(oldObj.(*unstructured.Unstructured), old); err != nil {
return err
}
current := new(policyv1alpha1.ClusterOverridePolicy)
if err := convertToPolicy(newObj.(*unstructured.Unstructured), current); err != nil {
return err
}
if reflect.DeepEqual(current.Spec, old.Spec) {
return nil
}
current.Annotations[lastSyncTimeAnno] = strconv.FormatInt(time.Now().UnixNano(), 10)
_, err := s.policyClient.PolicyV1alpha1().ClusterOverridePolicies().Update(context.TODO(), current, metav1.UpdateOptions{})
return err
}
func convertToPolicy(u *unstructured.Unstructured, data any) error {
klog.V(4).Infof("convertToPolicy, obj=%v", u)
b, err := u.MarshalJSON()
if err != nil {
return err
}
return json.Unmarshal(b, data)
}