/
native.go
145 lines (121 loc) · 3.7 KB
/
native.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 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 pilot
import (
"io"
"net"
multierror "github.com/hashicorp/go-multierror"
"istio.io/istio/pilot/pkg/bootstrap"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/proxy/envoy"
"istio.io/istio/pkg/test/framework/api/component"
"istio.io/istio/pkg/test/framework/api/components"
"istio.io/istio/pkg/test/framework/api/context"
"istio.io/istio/pkg/test/framework/api/descriptors"
"istio.io/istio/pkg/test/framework/api/lifecycle"
"istio.io/istio/pkg/test/framework/runtime/api"
"istio.io/istio/pkg/test/framework/runtime/components/environment/native"
)
var _ Native = &nativeComponent{}
var _ io.Closer = &nativeComponent{}
// TODO(nmittler): Revisit the need for this interface after galley is the config solution for the framework.
// Native is the interface for an native pilot server.
type Native interface {
components.Pilot
model.ConfigStore
GetDiscoveryAddress() *net.TCPAddr
}
// NewNativeComponent factory function for the component
func NewNativeComponent() (api.Component, error) {
return &nativeComponent{
stopChan: make(chan struct{}),
}, nil
}
type nativeComponent struct {
*client
model.ConfigStoreCache
server *bootstrap.Server
stopChan chan struct{}
scope lifecycle.Scope
}
func (c *nativeComponent) Descriptor() component.Descriptor {
return descriptors.Pilot
}
func (c *nativeComponent) Scope() lifecycle.Scope {
return c.scope
}
func (c *nativeComponent) Start(ctx context.Instance, scope lifecycle.Scope) (err error) {
c.scope = scope
env, err := native.GetEnvironment(ctx)
if err != nil {
return err
}
defer func() {
if err != nil {
_ = c.Close()
}
}()
// Dynamically assign all ports.
options := envoy.DiscoveryServiceOptions{
HTTPAddr: ":0",
MonitoringAddr: ":0",
GrpcAddr: ":0",
SecureGrpcAddr: "",
}
bootstrapArgs := bootstrap.PilotArgs{
Namespace: env.Namespace,
DiscoveryOptions: options,
MeshConfig: env.Mesh,
Config: bootstrap.ConfigArgs{
Controller: env.ServiceManager.ConfigStore,
},
// Use the config store for service entries as well.
Service: bootstrap.ServiceArgs{
// A ServiceEntry registry is added by default, which is what we want. Don't include any other registries.
Registries: []string{},
},
// Include all of the default plugins for integration with Mixer, etc.
Plugins: bootstrap.DefaultPlugins,
ForceStop: true,
}
// Save the config store.
c.ConfigStoreCache = env.ServiceManager.ConfigStore
// Create the server for the discovery service.
c.server, err = bootstrap.NewServer(bootstrapArgs)
if err != nil {
return err
}
// Start the server
go func() {
err = c.server.Start(c.stopChan)
}()
c.client, err = newClient(c.server.GRPCListeningAddr.(*net.TCPAddr))
if err != nil {
return err
}
return
}
func (c *nativeComponent) Close() (err error) {
if c.client != nil {
err = multierror.Append(err, c.client.Close()).ErrorOrNil()
}
if c.stopChan != nil {
c.stopChan <- struct{}{}
}
return
}
// GetDiscoveryAddress gets the discovery address for pilot.
func (c *nativeComponent) GetDiscoveryAddress() *net.TCPAddr {
return c.server.GRPCListeningAddr.(*net.TCPAddr)
}