generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 65
/
checker.go
132 lines (114 loc) · 4.8 KB
/
checker.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
/*
Copyright 2021 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 configmap
import (
"context"
"fmt"
"sync/atomic"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/syncer/constants"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/syncer/conversion"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/syncer/metrics"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/syncer/patrol/differ"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/syncer/util"
)
var numMissMatchedConfigMaps uint64
func (c *controller) StartPatrol(stopCh <-chan struct{}) error {
defer utilruntime.HandleCrash()
if !cache.WaitForCacheSync(stopCh, c.configMapSynced) {
return fmt.Errorf("failed to wait for caches to sync before starting ConfigMap checker")
}
c.Patroller.Start(stopCh)
return nil
}
// PatrollerDo checks to see if configmaps in super control plane informer cache and tenant control plane
// keep consistency.
func (c *controller) PatrollerDo() {
clusterNames := c.MultiClusterController.GetClusterNames()
if len(clusterNames) == 0 {
klog.V(5).Infof("super cluster has no tenant control planes, giving up periodic checker: %s", "configmap")
return
}
pConfigMaps, err := c.configMapLister.List(util.GetSuperClusterListerLabelsSelector())
if err != nil {
klog.Errorf("error listing configmaps from super control plane informer cache: %v", err)
return
}
pSet := differ.NewDiffSet()
for _, pCM := range pConfigMaps {
pSet.Insert(differ.ClusterObject{Object: pCM, Key: differ.DefaultClusterObjectKey(pCM, "")})
}
knownClusterSet := sets.NewString(clusterNames...)
vSet := differ.NewDiffSet()
for _, cluster := range clusterNames {
cmList := &corev1.ConfigMapList{}
if err := c.MultiClusterController.List(cluster, cmList); err != nil {
klog.Errorf("error listing configmaps from cluster %s informer cache: %v", cluster, err)
knownClusterSet.Delete(cluster)
continue
}
for i := range cmList.Items {
vSet.Insert(differ.ClusterObject{
Object: &cmList.Items[i],
OwnerCluster: cluster,
Key: differ.DefaultClusterObjectKey(&cmList.Items[i], cluster),
})
}
}
configMapDiffer := differ.HandlerFuncs{}
configMapDiffer.AddFunc = func(vObj differ.ClusterObject) {
if err := c.MultiClusterController.RequeueObject(vObj.OwnerCluster, vObj.Object); err != nil {
klog.Errorf("error requeue vConfigMap %v/%v in cluster %s: %v", vObj.GetNamespace(), vObj.GetName(), vObj.GetOwnerCluster(), err)
} else {
metrics.CheckerRemedyStats.WithLabelValues("RequeuedTenantConfigMaps").Inc()
}
}
configMapDiffer.UpdateFunc = func(vObj, pObj differ.ClusterObject) {
vCM := vObj.Object.(*corev1.ConfigMap)
pCM := pObj.Object.(*corev1.ConfigMap)
if pCM.Annotations[constants.LabelUID] != string(vCM.UID) {
klog.Errorf("Found pConfigMap %s delegated UID is different from tenant object.", pObj.Key)
configMapDiffer.OnDelete(pObj)
return
}
vc, err := util.GetVirtualClusterObject(c.MultiClusterController, vObj.GetOwnerCluster())
if err != nil {
klog.Errorf("fail to get cluster spec : %s", vObj.GetOwnerCluster())
return
}
updated := conversion.Equality(c.Config, vc).CheckConfigMapEquality(pCM, vCM)
if updated != nil {
atomic.AddUint64(&numMissMatchedConfigMaps, 1)
klog.Warningf("ConfigMap %s diff in super&tenant control plane", pObj.Key)
}
}
configMapDiffer.DeleteFunc = func(pObj differ.ClusterObject) {
deleteOptions := &metav1.DeleteOptions{}
deleteOptions.Preconditions = metav1.NewUIDPreconditions(string(pObj.GetUID()))
if err = c.configMapClient.ConfigMaps(pObj.GetNamespace()).Delete(context.TODO(), pObj.GetName(), *deleteOptions); err != nil {
klog.Errorf("error deleting pConfigMap %s in super control plane: %v", pObj.Key, err)
} else {
metrics.CheckerRemedyStats.WithLabelValues("DeletedOrphanSuperControlPlaneConfigMaps").Inc()
}
}
vSet.Difference(pSet, differ.FilteringHandler{
Handler: configMapDiffer,
FilterFunc: differ.DefaultDifferFilter(knownClusterSet),
})
metrics.CheckerMissMatchStats.WithLabelValues("MissMatchedConfigMaps").Set(float64(numMissMatchedConfigMaps))
}