generated from kubernetes/kubernetes-template-project
/
uws.go
135 lines (117 loc) · 5.27 KB
/
uws.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
/*
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 service
import (
"context"
"fmt"
pkgerr "github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"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/util"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/syncer/util/featuregate"
)
// StartUWS starts the upward syncer
// and blocks until an empty struct is sent to the stop channel.
func (c *controller) StartUWS(stopCh <-chan struct{}) error {
if !cache.WaitForCacheSync(stopCh, c.serviceSynced) {
return fmt.Errorf("failed to wait for caches to sync")
}
return c.UpwardController.Start(stopCh)
}
func (c *controller) BackPopulate(key string) error {
pNamespace, pName, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
utilruntime.HandleError(fmt.Errorf("invalid resource key %v: %v", key, err))
return nil
}
pService, err := c.serviceLister.Services(pNamespace).Get(pName)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
// Make sure the super cluster IP is added to the annotation so that it can be back populated to the tenant object
if pService.Spec.ClusterIP != "" && pService.Annotations[constants.LabelSuperClusterIP] != pService.Spec.ClusterIP {
if pService.Annotations == nil {
pService.Annotations = make(map[string]string)
}
pService.Annotations[constants.LabelSuperClusterIP] = pService.Spec.ClusterIP
_, err = c.serviceClient.Services(pNamespace).Update(context.TODO(), pService, metav1.UpdateOptions{})
if err != nil {
return err
}
// wait for the next reconcile for the rest of backpopulate work.
return nil
}
clusterName, vNamespace := conversion.GetVirtualOwner(pService)
if clusterName == "" || vNamespace == "" {
klog.Infof("drop service %s/%s which is not belongs to any tenant", pNamespace, pName)
return nil
}
vService := &corev1.Service{}
if err := c.MultiClusterController.Get(clusterName, vNamespace, pName, vService); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return pkgerr.Wrapf(err, "could not find pService %s/%s's vService in controller cache", vNamespace, pName)
}
if pService.Annotations[constants.LabelUID] != string(vService.UID) {
return fmt.Errorf("backPopulated pService %s/%s delegated UID is different from updated object", pService.Namespace, pService.Name)
}
tenantClient, err := c.MultiClusterController.GetClusterClient(clusterName)
if err != nil {
return pkgerr.Wrapf(err, "failed to create client from cluster %s config", clusterName)
}
vc, err := util.GetVirtualClusterObject(c.MultiClusterController, clusterName)
if err != nil {
return pkgerr.Wrapf(err, "failed to get spec of cluster %s", clusterName)
}
var newService *corev1.Service
updatedMeta := conversion.Equality(c.Config, vc).CheckUWObjectMetaEquality(&pService.ObjectMeta, &vService.ObjectMeta)
if updatedMeta != nil {
newService = vService.DeepCopy()
newService.ObjectMeta = *updatedMeta
if featuregate.DefaultFeatureGate.Enabled(featuregate.VServiceExternalIP) &&
updatedMeta.Annotations[constants.LabelSuperClusterIP] != "" &&
len(newService.Spec.ExternalIPs) == 0 {
// Add clusterIP to ExternalIPs if it hasn't been set on purpose
newService.Spec.ExternalIPs = []string{updatedMeta.Annotations[constants.LabelSuperClusterIP]}
}
if _, err = tenantClient.CoreV1().Services(vService.Namespace).Update(context.TODO(), newService, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("failed to back populate service %s/%s meta update for cluster %s: %v", vService.Namespace, vService.Name, clusterName, err)
}
}
if !equality.Semantic.DeepEqual(vService.Status, pService.Status) {
if newService == nil {
newService = vService.DeepCopy()
} else {
// vService has been updated, let us fetch the lastest version.
if newService, err = tenantClient.CoreV1().Services(vService.Namespace).Get(context.TODO(), vService.Name, metav1.GetOptions{}); err != nil {
return fmt.Errorf("failed to retrieve vService %s/%s from cluster %s: %v", vService.Namespace, vService.Name, clusterName, err)
}
}
newService.Status = pService.Status
if _, err = tenantClient.CoreV1().Services(vService.Namespace).UpdateStatus(context.TODO(), newService, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("failed to back populate service %s/%s status update for cluster %s: %v", vService.Namespace, vService.Name, clusterName, err)
}
}
return nil
}