forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.go
253 lines (223 loc) · 8.12 KB
/
shared.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// 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 mesh contains types and functions.
package mesh
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"sync"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"istio.io/istio/operator/pkg/apis/istio/v1alpha1"
"istio.io/istio/operator/pkg/cache"
"istio.io/istio/operator/pkg/helmreconciler"
"istio.io/istio/operator/pkg/manifest"
"istio.io/istio/operator/pkg/name"
"istio.io/istio/operator/pkg/object"
"istio.io/istio/operator/pkg/util/clog"
"istio.io/pkg/log"
)
var (
// installerScope is the scope for all commands in the mesh package.
installerScope = log.RegisterScope("installer", "installer", 0)
// testK8Interface is used if it is set. Not possible to inject due to cobra command boundary.
testK8Interface *kubernetes.Clientset
testRestConfig *rest.Config
)
func initLogsOrExit(_ *rootArgs) {
if err := configLogs(log.DefaultOptions()); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Could not configure logs: %s", err)
os.Exit(1)
}
}
var logMutex = sync.Mutex{}
func configLogs(opt *log.Options) error {
logMutex.Lock()
defer logMutex.Unlock()
op := []string{"stderr"}
opt2 := *opt
opt2.OutputPaths = op
opt2.ErrorOutputPaths = op
return log.Configure(&opt2)
}
func refreshGoldenFiles() bool {
ev := os.Getenv("REFRESH_GOLDEN")
return ev == "true" || ev == "1"
}
func kubeBuilderInstalled() bool {
ev := os.Getenv("KUBEBUILDER")
return ev == "true" || ev == "1"
}
// confirm waits for a user to confirm with the supplied message.
func confirm(msg string, writer io.Writer) bool {
fmt.Fprintf(writer, "%s ", msg)
var response string
_, err := fmt.Scanln(&response)
if err != nil {
return false
}
response = strings.ToUpper(response)
if response == "Y" || response == "YES" {
return true
}
return false
}
// K8sConfig creates a rest.Config, Clientset and controller runtime Client from the given kubeconfig path and context.
func K8sConfig(kubeConfigPath string, context string) (*rest.Config, *kubernetes.Clientset, client.Client, error) {
restConfig, clientset, err := InitK8SRestClient(kubeConfigPath, context)
if err != nil {
return nil, nil, nil, err
}
// We are running a one-off command locally, so we don't need to worry too much about rate limitting
// Bumping this up greatly decreases install time
restConfig.QPS = 50
restConfig.Burst = 100
client, err := client.New(restConfig, client.Options{Scheme: scheme.Scheme})
if err != nil {
return nil, nil, nil, err
}
return restConfig, clientset, client, nil
}
// InitK8SRestClient creates a rest.Config qne Clientset from the given kubeconfig path and context.
func InitK8SRestClient(kubeconfig, kubeContext string) (*rest.Config, *kubernetes.Clientset, error) {
if testRestConfig != nil || testK8Interface != nil {
if !(testRestConfig != nil && testK8Interface != nil) {
return nil, nil, fmt.Errorf("testRestConfig and testK8Interface must both be either nil or set")
}
return testRestConfig, testK8Interface, nil
}
restConfig, err := defaultRestConfig(kubeconfig, kubeContext)
if err != nil {
return nil, nil, err
}
clientset, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, nil, err
}
return restConfig, clientset, nil
}
func defaultRestConfig(kubeconfig, kubeContext string) (*rest.Config, error) {
config, err := BuildClientConfig(kubeconfig, kubeContext)
if err != nil {
return nil, err
}
config.APIPath = "/api"
config.GroupVersion = &v1.SchemeGroupVersion
config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
return config, nil
}
// BuildClientConfig is a helper function that builds client config from a kubeconfig filepath.
// It overrides the current context with the one provided (empty to use default).
//
// This is a modified version of k8s.io/client-go/tools/clientcmd/BuildConfigFromFlags with the
// difference that it loads default configs if not running in-cluster.
func BuildClientConfig(kubeconfig, context string) (*rest.Config, error) {
if kubeconfig != "" {
info, err := os.Stat(kubeconfig)
if err != nil || info.Size() == 0 {
// If the specified kubeconfig doesn't exists / empty file / any other error
// from file stat, fall back to default
kubeconfig = ""
}
}
// Config loading rules:
// 1. kubeconfig if it not empty string
// 2. In cluster config if running in-cluster
// 3. Config(s) in KUBECONFIG environment variable
// 4. Use $HOME/.kube/config
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
loadingRules.ExplicitPath = kubeconfig
configOverrides := &clientcmd.ConfigOverrides{
ClusterDefaults: clientcmd.ClusterDefaults,
CurrentContext: context,
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides).ClientConfig()
}
// applyOptions contains the startup options for applying the manifest.
type applyOptions struct {
// Path to the kubeconfig file.
Kubeconfig string
// ComponentName of the kubeconfig context to use.
Context string
// DryRun performs all steps except actually applying the manifests or creating output dirs/files.
DryRun bool
// Maximum amount of time to wait for resources to be ready after install when Wait=true.
WaitTimeout time.Duration
}
func applyManifest(restConfig *rest.Config, client client.Client, manifestStr string,
componentName name.ComponentName, opts *applyOptions, iop *v1alpha1.IstioOperator, l clog.Logger) error {
// Needed in case we are running a test through this path that doesn't start a new process.
cache.FlushObjectCaches()
reconciler, err := helmreconciler.NewHelmReconciler(client, restConfig, iop, &helmreconciler.Options{DryRun: opts.DryRun, Log: l})
if err != nil {
l.LogAndError(err)
return err
}
ms := name.Manifest{
Name: componentName,
Content: manifestStr,
}
_, _, err = reconciler.ApplyManifest(ms, reconciler.CheckSSAEnabled())
return err
}
// --manifests is an alias for --set installPackagePath=
// --revision is an alias for --set revision=
func applyFlagAliases(flags []string, manifestsPath, revision string) []string {
if manifestsPath != "" {
flags = append(flags, fmt.Sprintf("installPackagePath=%s", manifestsPath))
}
if revision != "" {
flags = append(flags, fmt.Sprintf("revision=%s", revision))
}
return flags
}
// getCRAndNamespaceFromFile returns the CR name and istio namespace from a file containing an IstioOperator CR.
func getCRAndNamespaceFromFile(filePath string, l clog.Logger) (customResource string, istioNamespace string, err error) {
if filePath == "" {
return "", "", nil
}
_, mergedIOPS, err := manifest.GenerateConfig([]string{filePath}, nil, false, nil, l)
if err != nil {
return "", "", err
}
b, err := ioutil.ReadFile(filePath)
if err != nil {
return "", "", fmt.Errorf("could not read values from file %s: %s", filePath, err)
}
customResource = string(b)
istioNamespace = mergedIOPS.Namespace
return
}
// createNamespace creates a namespace using the given k8s interface.
func createNamespace(cs kubernetes.Interface, namespace string, network string) error {
return helmreconciler.CreateNamespace(cs, namespace, network)
}
// saveIOPToCluster saves the state in an IOP CR in the cluster.
func saveIOPToCluster(reconciler *helmreconciler.HelmReconciler, iop string) error {
obj, err := object.ParseYAMLToK8sObject([]byte(iop))
if err != nil {
return err
}
return reconciler.ApplyObject(obj.UnstructuredObject(), false)
}