istio
145 строк · 4.0 Кб
1// Copyright Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package controller
16
17import (
18"time"
19
20corev1 "k8s.io/api/core/v1"
21"k8s.io/apimachinery/pkg/runtime/schema"
22
23meshconfig "istio.io/api/mesh/v1alpha1"
24"istio.io/istio/pilot/pkg/model"
25"istio.io/istio/pilot/pkg/serviceregistry/aggregate"
26"istio.io/istio/pilot/pkg/serviceregistry/util/xdsfake"
27"istio.io/istio/pkg/cluster"
28"istio.io/istio/pkg/config/mesh"
29kubelib "istio.io/istio/pkg/kube"
30"istio.io/istio/pkg/kube/kclient"
31"istio.io/istio/pkg/kube/kclient/clienttest"
32"istio.io/istio/pkg/kube/namespace"
33"istio.io/istio/pkg/queue"
34"istio.io/istio/pkg/test"
35"istio.io/istio/pkg/test/util/assert"
36)
37
38const (
39defaultFakeDomainSuffix = "company.com"
40)
41
42type FakeControllerOptions struct {
43Client kubelib.Client
44CRDs []schema.GroupVersionResource
45NetworksWatcher mesh.NetworksWatcher
46MeshWatcher mesh.Watcher
47ServiceHandler model.ServiceHandler
48ClusterID cluster.ID
49WatchedNamespaces string
50DomainSuffix string
51XDSUpdater model.XDSUpdater
52Stop chan struct{}
53SkipRun bool
54ConfigCluster bool
55SystemNamespace string
56}
57
58type FakeController struct {
59*Controller
60Endpoints *model.EndpointIndex
61}
62
63func NewFakeControllerWithOptions(t test.Failer, opts FakeControllerOptions) (*FakeController, *xdsfake.Updater) {
64xdsUpdater := opts.XDSUpdater
65var endpoints *model.EndpointIndex
66if xdsUpdater == nil {
67endpoints = model.NewEndpointIndex(model.DisabledCache{})
68delegate := model.NewEndpointIndexUpdater(endpoints)
69xdsUpdater = xdsfake.NewWithDelegate(delegate)
70}
71
72domainSuffix := defaultFakeDomainSuffix
73if opts.DomainSuffix != "" {
74domainSuffix = opts.DomainSuffix
75}
76if opts.Client == nil {
77opts.Client = kubelib.NewFakeClient()
78}
79if opts.MeshWatcher == nil {
80opts.MeshWatcher = mesh.NewFixedWatcher(&meshconfig.MeshConfig{})
81}
82cleanupStop := false
83stop := opts.Stop
84if stop == nil {
85// If we created the stop, clean it up. Otherwise, caller is responsible
86cleanupStop = true
87stop = make(chan struct{})
88}
89f := namespace.NewDiscoveryNamespacesFilter(
90kclient.New[*corev1.Namespace](opts.Client),
91opts.MeshWatcher,
92stop,
93)
94kubelib.SetObjectFilter(opts.Client, f)
95
96meshServiceController := aggregate.NewController(aggregate.Options{MeshHolder: opts.MeshWatcher})
97
98options := Options{
99DomainSuffix: domainSuffix,
100XDSUpdater: xdsUpdater,
101Metrics: &model.Environment{},
102MeshNetworksWatcher: opts.NetworksWatcher,
103MeshWatcher: opts.MeshWatcher,
104ClusterID: opts.ClusterID,
105MeshServiceController: meshServiceController,
106ConfigCluster: opts.ConfigCluster,
107SystemNamespace: opts.SystemNamespace,
108}
109c := NewController(opts.Client, options)
110meshServiceController.AddRegistry(c)
111
112if opts.ServiceHandler != nil {
113c.AppendServiceHandler(opts.ServiceHandler)
114}
115
116t.Cleanup(func() {
117c.client.Shutdown()
118})
119if !opts.SkipRun {
120t.Cleanup(func() {
121assert.NoError(t, queue.WaitForClose(c.queue, time.Second*5))
122})
123}
124c.stop = stop
125if cleanupStop {
126t.Cleanup(func() {
127close(stop)
128})
129}
130for _, crd := range opts.CRDs {
131clienttest.MakeCRD(t, c.client, crd)
132}
133opts.Client.RunAndWait(c.stop)
134var fx *xdsfake.Updater
135if x, ok := xdsUpdater.(*xdsfake.Updater); ok {
136fx = x
137}
138
139if !opts.SkipRun {
140go c.Run(c.stop)
141kubelib.WaitForCacheSync("test", c.stop, c.HasSynced)
142}
143
144return &FakeController{Controller: c, Endpoints: endpoints}, fx
145}
146