/
platform_controller.go
218 lines (187 loc) · 6.65 KB
/
platform_controller.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package platform
import (
"context"
"fmt"
"reflect"
"time"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
v1 "tkestack.io/tke/api/business/v1"
clientset "tkestack.io/tke/api/client/clientset/versioned"
authversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/auth/v1"
businessv1informer "tkestack.io/tke/api/client/informers/externalversions/business/v1"
businessv1lister "tkestack.io/tke/api/client/listers/business/v1"
controllerutil "tkestack.io/tke/pkg/controller"
"tkestack.io/tke/pkg/util/log"
"tkestack.io/tke/pkg/util/metrics"
)
const (
platformEnqueueGracePeriod = 1 * time.Second
)
const (
controllerName = "platform-controller"
)
// Controller is responsible for performing actions dependent upon a platform phase.
type Controller struct {
client clientset.Interface
queue workqueue.RateLimitingInterface
lister businessv1lister.PlatformLister
listerSynced cache.InformerSynced
authClient authversionedclient.AuthV1Interface
}
// NewController creates a new platform object.
func NewController(client clientset.Interface, authClient authversionedclient.AuthV1Interface, platformInformer businessv1informer.PlatformInformer,
resyncPeriod time.Duration) *Controller {
// create the controller so we can inject the enqueue function
controller := &Controller{
client: client,
authClient: authClient,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), controllerName),
}
if client != nil && client.BusinessV1().RESTClient().GetRateLimiter() != nil {
_ = metrics.RegisterMetricAndTrackRateLimiterUsage("platform_controller", client.BusinessV1().RESTClient().GetRateLimiter())
}
platformInformer.Informer().AddEventHandlerWithResyncPeriod(
cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueue,
UpdateFunc: func(oldObj, newObj interface{}) {
old, ok1 := oldObj.(*v1.Platform)
cur, ok2 := newObj.(*v1.Platform)
if ok1 && ok2 && controller.needsUpdate(old, cur) {
controller.enqueue(newObj)
}
},
},
resyncPeriod,
)
controller.lister = platformInformer.Lister()
controller.listerSynced = platformInformer.Informer().HasSynced
return controller
}
func (c *Controller) enqueue(obj interface{}) {
key, err := controllerutil.KeyFunc(obj)
if err != nil {
runtime.HandleError(fmt.Errorf("couldn't get key for object %+v: %v", obj, err))
return
}
c.queue.AddAfter(key, platformEnqueueGracePeriod)
}
func (c *Controller) needsUpdate(old *v1.Platform, new *v1.Platform) bool {
if old.UID != new.UID {
return true
}
if !reflect.DeepEqual(old.Spec, new.Spec) {
return true
}
return false
}
// Run will set up the event handlers for types we are interested in, as well
// as syncing informer caches and starting workers.
func (c *Controller) Run(workers int, stopCh <-chan struct{}) {
defer runtime.HandleCrash()
defer c.queue.ShutDown()
// Start the informer factories to begin populating the informer caches
log.Info("Starting platform controller")
defer log.Info("Shutting down platform controller")
if ok := cache.WaitForCacheSync(stopCh, c.listerSynced); !ok {
log.Error("Failed to wait for platform caches to sync")
}
for i := 0; i < workers; i++ {
go wait.Until(c.worker, time.Second, stopCh)
}
<-stopCh
}
// worker processes the queue of platform objects.
// Each platform can be in the queue at most once.
// The system ensures that no two workers can process
// the same platform at the same time.
func (c *Controller) worker() {
workFunc := func() bool {
key, quit := c.queue.Get()
if quit {
return true
}
defer c.queue.Done(key)
err := c.syncItem(key.(string))
if err == nil {
// no error, forget this entry and return
c.queue.Forget(key)
return false
}
// rather than wait for a full resync, re-add the platform to the queue to be processed
c.queue.AddRateLimited(key)
runtime.HandleError(err)
return false
}
for {
quit := workFunc()
if quit {
return
}
}
}
// syncItem will sync the platform with the given key if it has had
// its expectations fulfilled, meaning it did not expect to see any more of its
// platform created or deleted. This function is not meant to be invoked
// concurrently with the same key.
func (c *Controller) syncItem(key string) error {
startTime := time.Now()
defer func() {
log.Info("Finished syncing platform", log.String("platformName", key), log.Duration("processTime", time.Since(startTime)))
}()
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
platform, err := c.lister.Get(name)
switch {
case errors.IsNotFound(err):
log.Info("Platform has been deleted.", log.String("platformName", key))
case err != nil:
log.Warn("Unable to retrieve platform from store", log.String("platformName", key), log.Err(err))
default:
log.Info("Platform has been updated. Attempting to update identity provider", log.String("platformName", key))
err = c.process(context.Background(), platform)
}
return err
}
func (c *Controller) process(ctx context.Context, platform *v1.Platform) error {
tenantID := platform.Spec.TenantID
if len(tenantID) == 0 {
log.Warn("TenantID is empty", log.String("platform", platform.Name))
return nil
}
idp, err := c.authClient.IdentityProviders().Get(ctx, tenantID, metav1.GetOptions{})
if err != nil {
log.Error("Get identity provider for tenant failed", log.String("tennantID", tenantID), log.Err(err))
return err
}
if !reflect.DeepEqual(platform.Spec.Administrators, idp.Spec.Administrators) {
log.Info("Attempting update identity provider for tenant with new administrators", log.String("tenant", tenantID),
log.Strings("administrators", platform.Spec.Administrators))
idp.Spec.Administrators = platform.Spec.Administrators
_, err = c.authClient.IdentityProviders().Update(ctx, idp, metav1.UpdateOptions{})
}
return err
}