This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 152
/
multicluster.go
129 lines (113 loc) · 4.38 KB
/
multicluster.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
// Copyright 2018 Istio 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 clusterregistry
import (
"sync"
"time"
"k8s.io/client-go/kubernetes"
meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/serviceregistry"
"istio.io/istio/pilot/pkg/serviceregistry/aggregate"
"istio.io/istio/pilot/pkg/serviceregistry/kube"
"istio.io/istio/pkg/kube/secretcontroller"
"istio.io/istio/pkg/log"
)
type kubeController struct {
rc *kube.Controller
stopCh chan struct{}
}
// Multicluster structure holds the remote kube Controllers and multicluster specific attributes.
type Multicluster struct {
WatchedNamespace string
DomainSuffix string
ResyncPeriod time.Duration
serviceController *aggregate.Controller
XDSUpdater model.XDSUpdater
m sync.Mutex // protects remoteKubeControllers
remoteKubeControllers map[string]*kubeController
meshNetworks *meshconfig.MeshNetworks
}
// NewMulticluster initializes data structure to store multicluster information
// It also starts the secret controller
func NewMulticluster(kc kubernetes.Interface, secretNamespace string,
watchedNamespace string, domainSuffix string, resycnPeriod time.Duration,
serviceController *aggregate.Controller, xds model.XDSUpdater, meshNetworks *meshconfig.MeshNetworks) (*Multicluster, error) {
remoteKubeController := make(map[string]*kubeController)
if resycnPeriod == 0 {
// make sure a resync time of 0 wasn't passed in.
resycnPeriod = 30 * time.Second
log.Info("Resync time was configured to 0, resetting to 30")
}
mc := &Multicluster{
WatchedNamespace: watchedNamespace,
DomainSuffix: domainSuffix,
ResyncPeriod: resycnPeriod,
serviceController: serviceController,
XDSUpdater: xds,
remoteKubeControllers: remoteKubeController,
meshNetworks: meshNetworks,
}
err := secretcontroller.StartSecretController(kc,
mc.AddMemberCluster,
mc.DeleteMemberCluster,
secretNamespace)
return mc, err
}
// AddMemberCluster is passed to the secret controller as a callback to be called
// when a remote cluster is added. This function needs to set up all the handlers
// to watch for resources being added, deleted or changed on remote clusters.
func (m *Multicluster) AddMemberCluster(clientset kubernetes.Interface, clusterID string) error {
// stopCh to stop controller created here when cluster removed.
stopCh := make(chan struct{})
var remoteKubeController kubeController
remoteKubeController.stopCh = stopCh
m.m.Lock()
kubectl := kube.NewController(clientset, kube.ControllerOptions{
WatchedNamespace: m.WatchedNamespace,
ResyncPeriod: m.ResyncPeriod,
DomainSuffix: m.DomainSuffix,
XDSUpdater: m.XDSUpdater,
ClusterID: clusterID,
})
kubectl.InitNetworkLookup(m.meshNetworks)
remoteKubeController.rc = kubectl
m.serviceController.AddRegistry(
aggregate.Registry{
Name: serviceregistry.KubernetesRegistry,
ClusterID: clusterID,
ServiceDiscovery: kubectl,
Controller: kubectl,
})
m.remoteKubeControllers[clusterID] = &remoteKubeController
m.m.Unlock()
_ = kubectl.AppendServiceHandler(func(*model.Service, model.Event) { m.XDSUpdater.ConfigUpdate(true) })
_ = kubectl.AppendInstanceHandler(func(*model.ServiceInstance, model.Event) { m.XDSUpdater.ConfigUpdate(true) })
go kubectl.Run(stopCh)
return nil
}
// DeleteMemberCluster is passed to the secret controller as a callback to be called
// when a remote cluster is deleted. Also must clear the cache so remote resources
// are removed.
func (m *Multicluster) DeleteMemberCluster(clusterID string) error {
m.m.Lock()
defer m.m.Unlock()
m.serviceController.DeleteRegistry(clusterID)
close(m.remoteKubeControllers[clusterID].stopCh)
delete(m.remoteKubeControllers, clusterID)
if m.XDSUpdater != nil {
m.XDSUpdater.ConfigUpdate(true)
}
return nil
}