/
istio.go
185 lines (157 loc) · 4.83 KB
/
istio.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2022 The Amesh 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 controlplane
import (
"bytes"
"context"
"os/exec"
"github.com/api7/gopkg/pkg/log"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/onsi/ginkgo/v2"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/api7/amesh/e2e/framework/utils"
)
var (
_helm = "helm"
)
type istio struct {
discovery *exec.Cmd
cleanupDiscovery *exec.Cmd
baseStderr *bytes.Buffer
discoveryStderr *bytes.Buffer
cleanupDiscoveryStderr *bytes.Buffer
logger *log.Logger
options *IstioOptions
installCmds [][]string
clusterIP string
}
// IstioOptions contains options to customize Istio control plane.
type IstioOptions struct {
// KubeConfig is the kube config file path.
KubeConfig string
// IstioImage is the image of Istiod (pilot)
IstioImage string
// SidecarInitImage is the sidecar init image
SidecarInitImage string
// SidecarImage is the sidecar image
SidecarImage string
// Namespace is the target namespace to install istio.
Namespace string
// ChartsPath is a directory that contains charts for Amesh.
ChartsPath string
KubectlOpts *k8s.KubectlOptions
}
// NewIstioControlPlane creates an istio control plane.
func NewIstioControlPlane(opts *IstioOptions) ControlPlane {
logger, err := log.NewLogger(
log.WithContext("istio"),
log.WithLogLevel("error"),
)
if err != nil {
utils.AssertNil(err)
return nil
}
return &istio{
logger: logger,
options: opts,
}
}
func (cp *istio) Namespace() string {
return cp.options.Namespace
}
func (cp *istio) Type() string {
return "istio"
}
func (cp *istio) Addr() string {
return "grpc://" + cp.clusterIP + ":15010"
}
func (cp *istio) initCmd() {
opts := cp.options
kc := opts.KubeConfig
image := opts.IstioImage
discovery := exec.Command(_helm, "install", "amesh", "--namespace", opts.Namespace, "--kubeconfig", kc,
"--set", "pilot.image="+image,
"--set", "global.imagePullPolicy=IfNotPresent",
"--set", "global.proxy.privileged=true",
"--set", "global.proxy_init.image="+opts.SidecarInitImage,
"--set", "global.proxy.image="+opts.SidecarImage,
"--set", "global.istioNamespace="+opts.Namespace,
"--set", "global.defaultResources.requests.cpu=1000m",
opts.ChartsPath,
)
deleteDiscovery := exec.Command(_helm, "uninstall", "amesh", "--namespace", opts.Namespace, "--kubeconfig", kc)
baseStderr := bytes.NewBuffer(nil)
discoveryStderr := bytes.NewBuffer(nil)
cleanupDiscoveryStderr := bytes.NewBuffer(nil)
discovery.Stderr = discoveryStderr
deleteDiscovery.Stderr = cleanupDiscoveryStderr
cp.discovery = discovery
cp.cleanupDiscovery = deleteDiscovery
cp.baseStderr = baseStderr
cp.discoveryStderr = discoveryStderr
cp.cleanupDiscoveryStderr = cleanupDiscoveryStderr
}
func (cp *istio) Deploy() error {
cp.initCmd()
log.Infof("Deploying istio, command: %v", cp.discovery.String())
err := cp.discovery.Run()
if err != nil {
log.Errorf("failed to run istio-discovery install command")
log.Errorf("command: %s", cp.discovery.String())
log.Errorf("err: %s", err.Error())
log.Errorf("stderr: %s", cp.discoveryStderr.String())
return err
}
return nil
}
func (cp *istio) WaitForReady() error {
var err error
cp.clusterIP, err = utils.WaitForServiceReady(cp.options.KubectlOpts, cp.options.Namespace, "istiod")
if err != nil {
return err
}
err = utils.WaitForDeploymentPodsReady(cp.options.KubectlOpts, cp.options.Namespace, "istiod")
return err
}
func (cp *istio) Uninstall() error {
log.Infof("Uninstalling istio, command: %v", cp.cleanupDiscovery.String())
err := cp.cleanupDiscovery.Run()
if err != nil {
log.Errorw("failed to uninstall istio-discovery",
zap.Error(err),
zap.String("stderr", cp.cleanupDiscoveryStderr.String()),
)
return err
}
return nil
}
func (cp *istio) InjectNamespace(ns string) error {
client, err := k8s.GetKubernetesClientFromOptionsE(ginkgo.GinkgoT(), cp.options.KubectlOpts)
if err != nil {
return err
}
obj, err := client.CoreV1().Namespaces().Get(context.TODO(), ns, metav1.GetOptions{})
if err != nil {
return err
}
if obj.Labels == nil {
obj.Labels = make(map[string]string)
}
obj.Labels["istio-injection"] = "enabled"
if _, err := client.CoreV1().Namespaces().Update(context.TODO(), obj, metav1.UpdateOptions{}); err != nil {
return err
}
return nil
}