-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
276 lines (231 loc) · 9.41 KB
/
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
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
/*
Copyright 2016 The Kubernetes 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 federatedcluster
import (
"context"
"sync"
"time"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
fedv1a1 "github.com/kubernetes-sigs/federation-v2/pkg/apis/core/v1alpha1"
genericclient "github.com/kubernetes-sigs/federation-v2/pkg/client/generic"
"github.com/kubernetes-sigs/federation-v2/pkg/controller/util"
"github.com/kubernetes-sigs/federation-v2/pkg/features"
)
// ClusterData stores cluster client and previous health check probe results of individual cluster.
type ClusterData struct {
// clusterKubeClient is the kube client for the cluster.
clusterKubeClient *ClusterClient
// clusterStatus is the cluster status as of last sampling.
clusterStatus *fedv1a1.FederatedClusterStatus
// How many times in a row the probe has returned the same result.
resultRun int
}
// ClusterController is responsible for maintaining the health status of each
// FederatedCluster in a particular namespace.
type ClusterController struct {
client genericclient.Client
// clusterHealthCheckConfig is the configurable parameters for cluster health check
clusterHealthCheckConfig util.ClusterHealthCheckConfig
mu sync.RWMutex
// clusterDataMap is a mapping of clusterName and the cluster specific details.
clusterDataMap map[string]*ClusterData
// clusterController is the cache.Controller where callbacks are registered
// for events on FederatedClusters.
clusterController cache.Controller
// fedNamespace is the name of the namespace containing
// FederatedCluster resources and their associated secrets.
fedNamespace string
// clusterNamespace is the namespace containing Cluster resources.
clusterNamespace string
}
// StartClusterController starts a new cluster controller.
func StartClusterController(config *util.ControllerConfig, clusterHealthCheckConfig util.ClusterHealthCheckConfig, stopChan <-chan struct{}) error {
controller, err := newClusterController(config, clusterHealthCheckConfig)
if err != nil {
return err
}
glog.Infof("Starting cluster controller")
controller.Run(stopChan)
return nil
}
// newClusterController returns a new cluster controller
func newClusterController(config *util.ControllerConfig, clusterHealthCheckConfig util.ClusterHealthCheckConfig) (*ClusterController, error) {
kubeConfig := restclient.CopyConfig(config.KubeConfig)
kubeConfig.Timeout = time.Duration(clusterHealthCheckConfig.TimeoutSeconds) * time.Second
client := genericclient.NewForConfigOrDieWithUserAgent(kubeConfig, "cluster-controller")
cc := &ClusterController{
client: client,
clusterHealthCheckConfig: clusterHealthCheckConfig,
clusterDataMap: make(map[string]*ClusterData),
fedNamespace: config.FederationNamespace,
clusterNamespace: config.ClusterNamespace,
}
var err error
_, cc.clusterController, err = util.NewGenericInformerWithEventHandler(
config.KubeConfig,
config.FederationNamespace,
&fedv1a1.FederatedCluster{},
util.NoResyncPeriod,
&cache.ResourceEventHandlerFuncs{
DeleteFunc: cc.delFromClusterSet,
AddFunc: cc.addToClusterSet,
},
)
return cc, err
}
// delFromClusterSet removes a cluster from the cluster data map
func (cc *ClusterController) delFromClusterSet(obj interface{}) {
cc.mu.Lock()
defer cc.mu.Unlock()
cluster := obj.(*fedv1a1.FederatedCluster)
glog.V(1).Infof("ClusterController observed a cluster deletion: %v", cluster.Name)
delete(cc.clusterDataMap, cluster.Name)
}
// addToClusterSet creates a new client for the cluster and stores it in cluster data map.
func (cc *ClusterController) addToClusterSet(obj interface{}) {
cc.mu.Lock()
defer cc.mu.Unlock()
cluster := obj.(*fedv1a1.FederatedCluster)
clusterData := cc.clusterDataMap[cluster.Name]
if clusterData != nil && clusterData.clusterKubeClient != nil {
return
}
glog.V(1).Infof("ClusterController observed a new cluster: %v", cluster.Name)
// create the restclient of cluster
restClient, err := NewClusterClientSet(cluster, cc.client, cc.fedNamespace, cc.clusterNamespace)
if err != nil || restClient == nil {
glog.Errorf("Failed to create corresponding restclient of kubernetes cluster: %v", err)
return
}
cc.clusterDataMap[cluster.Name] = &ClusterData{clusterKubeClient: restClient}
}
// Run begins watching and syncing.
func (cc *ClusterController) Run(stopChan <-chan struct{}) {
defer utilruntime.HandleCrash()
go cc.clusterController.Run(stopChan)
// monitor cluster status periodically, in phase 1 we just get the health state from "/healthz"
go wait.Until(func() {
if err := cc.updateClusterStatus(); err != nil {
glog.Errorf("Error monitoring cluster status: %v", err)
}
}, time.Duration(cc.clusterHealthCheckConfig.PeriodSeconds)*time.Second, stopChan)
}
// updateClusterStatus checks cluster health and updates status of all FederatedClusters
func (cc *ClusterController) updateClusterStatus() error {
clusters := &fedv1a1.FederatedClusterList{}
err := cc.client.List(context.TODO(), clusters, cc.fedNamespace)
if err != nil {
return err
}
var wg sync.WaitGroup
for _, obj := range clusters.Items {
cc.mu.RLock()
cluster := obj.DeepCopy()
clusterData := cc.clusterDataMap[cluster.Name]
cc.mu.RUnlock()
if clusterData == nil {
glog.Warningf("Failed to retrieve stored data for cluster %s", cluster.Name)
continue
}
wg.Add(1)
go cc.updateIndividualClusterStatus(cluster, clusterData, &wg)
}
wg.Wait()
return nil
}
func (cc *ClusterController) updateIndividualClusterStatus(cluster *fedv1a1.FederatedCluster,
storedData *ClusterData, wg *sync.WaitGroup) {
clusterClient := storedData.clusterKubeClient
currentClusterStatus := clusterClient.GetClusterHealthStatus()
currentClusterStatus = thresholdAdjustedClusterStatus(currentClusterStatus, storedData, cc.clusterHealthCheckConfig)
if utilfeature.DefaultFeatureGate.Enabled(features.CrossClusterServiceDiscovery) {
currentClusterStatus = updateClusterZonesAndRegion(currentClusterStatus, cluster, clusterClient)
}
storedData.clusterStatus = currentClusterStatus
cluster.Status = *currentClusterStatus
if err := cc.client.UpdateStatus(context.TODO(), cluster); err != nil {
glog.Warningf("Failed to update the status of cluster %q: %v", cluster.Name, err)
}
wg.Done()
}
func thresholdAdjustedClusterStatus(clusterStatus *fedv1a1.FederatedClusterStatus, storedData *ClusterData,
clusterHealthCheckConfig util.ClusterHealthCheckConfig) *fedv1a1.FederatedClusterStatus {
if storedData.clusterStatus == nil {
storedData.resultRun = 1
return clusterStatus
}
threshold := clusterHealthCheckConfig.FailureThreshold
if util.IsClusterReady(clusterStatus) {
threshold = clusterHealthCheckConfig.SuccessThreshold
}
if storedData.resultRun < threshold {
// Success/Failure is below threshold - leave the probe state unchanged.
probeTime := clusterStatus.Conditions[0].LastProbeTime
clusterStatus = storedData.clusterStatus
setProbeTime(clusterStatus, probeTime)
} else {
if clusterStatusEqual(clusterStatus, storedData.clusterStatus) {
// preserve the last transition time
setTransitionTime(clusterStatus, storedData.clusterStatus.Conditions[0].LastTransitionTime)
}
}
if clusterStatusEqual(clusterStatus, storedData.clusterStatus) {
// Increment the result run has there is no change in cluster condition
storedData.resultRun++
} else {
// Reset the result run
storedData.resultRun = 1
}
return clusterStatus
}
func updateClusterZonesAndRegion(clusterStatus *fedv1a1.FederatedClusterStatus, cluster *fedv1a1.FederatedCluster,
clusterClient *ClusterClient) *fedv1a1.FederatedClusterStatus {
if !util.IsClusterReady(clusterStatus) {
return clusterStatus
}
zones, region, err := clusterClient.GetClusterZones()
if err != nil {
glog.Warningf("Failed to get zones and region for cluster %q: %v", clusterClient.clusterName, err)
return clusterStatus
}
// If new zone & region are empty, preserve the old ones so that user configured zone & region
// labels are effective
if len(zones) == 0 {
zones = cluster.Status.Zones
}
if len(region) == 0 {
region = cluster.Status.Region
}
clusterStatus.Zones = zones
clusterStatus.Region = region
return clusterStatus
}
func clusterStatusEqual(newClusterStatus, oldClusterStatus *fedv1a1.FederatedClusterStatus) bool {
return util.IsClusterReady(newClusterStatus) == util.IsClusterReady(oldClusterStatus)
}
func setProbeTime(clusterStatus *fedv1a1.FederatedClusterStatus, probeTime metav1.Time) {
for i := 0; i < len(clusterStatus.Conditions); i++ {
clusterStatus.Conditions[i].LastProbeTime = probeTime
}
}
func setTransitionTime(clusterStatus *fedv1a1.FederatedClusterStatus, transitionTime metav1.Time) {
for i := 0; i < len(clusterStatus.Conditions); i++ {
clusterStatus.Conditions[i].LastTransitionTime = transitionTime
}
}