/
fake.go
145 lines (130 loc) · 3.97 KB
/
fake.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
// Copyright 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 controller
import (
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/serviceregistry/aggregate"
"istio.io/istio/pilot/pkg/serviceregistry/util/xdsfake"
"istio.io/istio/pkg/cluster"
"istio.io/istio/pkg/config/mesh"
kubelib "istio.io/istio/pkg/kube"
"istio.io/istio/pkg/kube/kclient"
"istio.io/istio/pkg/kube/kclient/clienttest"
"istio.io/istio/pkg/kube/namespace"
"istio.io/istio/pkg/queue"
"istio.io/istio/pkg/test"
"istio.io/istio/pkg/test/util/assert"
)
const (
defaultFakeDomainSuffix = "company.com"
)
type FakeControllerOptions struct {
Client kubelib.Client
CRDs []schema.GroupVersionResource
NetworksWatcher mesh.NetworksWatcher
MeshWatcher mesh.Watcher
ServiceHandler model.ServiceHandler
ClusterID cluster.ID
WatchedNamespaces string
DomainSuffix string
XDSUpdater model.XDSUpdater
Stop chan struct{}
SkipRun bool
ConfigCluster bool
SystemNamespace string
}
type FakeController struct {
*Controller
Endpoints *model.EndpointIndex
}
func NewFakeControllerWithOptions(t test.Failer, opts FakeControllerOptions) (*FakeController, *xdsfake.Updater) {
xdsUpdater := opts.XDSUpdater
var endpoints *model.EndpointIndex
if xdsUpdater == nil {
endpoints = model.NewEndpointIndex(model.DisabledCache{})
delegate := model.NewEndpointIndexUpdater(endpoints)
xdsUpdater = xdsfake.NewWithDelegate(delegate)
}
domainSuffix := defaultFakeDomainSuffix
if opts.DomainSuffix != "" {
domainSuffix = opts.DomainSuffix
}
if opts.Client == nil {
opts.Client = kubelib.NewFakeClient()
}
if opts.MeshWatcher == nil {
opts.MeshWatcher = mesh.NewFixedWatcher(&meshconfig.MeshConfig{})
}
cleanupStop := false
stop := opts.Stop
if stop == nil {
// If we created the stop, clean it up. Otherwise, caller is responsible
cleanupStop = true
stop = make(chan struct{})
}
f := namespace.NewDiscoveryNamespacesFilter(
kclient.New[*corev1.Namespace](opts.Client),
opts.MeshWatcher,
stop,
)
kubelib.SetObjectFilter(opts.Client, f)
meshServiceController := aggregate.NewController(aggregate.Options{MeshHolder: opts.MeshWatcher})
options := Options{
DomainSuffix: domainSuffix,
XDSUpdater: xdsUpdater,
Metrics: &model.Environment{},
MeshNetworksWatcher: opts.NetworksWatcher,
MeshWatcher: opts.MeshWatcher,
ClusterID: opts.ClusterID,
MeshServiceController: meshServiceController,
ConfigCluster: opts.ConfigCluster,
SystemNamespace: opts.SystemNamespace,
}
c := NewController(opts.Client, options)
meshServiceController.AddRegistry(c)
if opts.ServiceHandler != nil {
c.AppendServiceHandler(opts.ServiceHandler)
}
t.Cleanup(func() {
c.client.Shutdown()
})
if !opts.SkipRun {
t.Cleanup(func() {
assert.NoError(t, queue.WaitForClose(c.queue, time.Second*5))
})
}
c.stop = stop
if cleanupStop {
t.Cleanup(func() {
close(stop)
})
}
for _, crd := range opts.CRDs {
clienttest.MakeCRD(t, c.client, crd)
}
opts.Client.RunAndWait(c.stop)
var fx *xdsfake.Updater
if x, ok := xdsUpdater.(*xdsfake.Updater); ok {
fx = x
}
if !opts.SkipRun {
go c.Run(c.stop)
kubelib.WaitForCacheSync("test", c.stop, c.HasSynced)
}
return &FakeController{Controller: c, Endpoints: endpoints}, fx
}