-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_addon_istio.go
225 lines (197 loc) · 5.79 KB
/
create_addon_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
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
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/binaries"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
const (
defaultIstioNamespace = "istio-system"
defaultIstioReleaseName = "istio"
defaultIstioPassword = "istio"
defaultIstioConfigDir = "/istio_service_dir"
defaultIstioVersion = ""
)
var (
createAddonIstioLong = templates.LongDesc(`
Creates the istio addon for service mesh on Kubernetes
`)
createAddonIstioExample = templates.Examples(`
# Create the istio addon
jx create addon istio
# Create the istio addon in a custom namespace
jx create addon istio -n mynamespace
`)
)
// CreateAddonIstioOptions the options for the create spring command
type CreateAddonIstioOptions struct {
CreateAddonOptions
Chart string
Password string
ConfigDir string
NoInjectorWebhook bool
Dir string
}
// NewCmdCreateAddonIstio creates a command object for the "create" command
func NewCmdCreateAddonIstio(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &CreateAddonIstioOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "istio",
Short: "Create the Istio addon for service mesh",
Aliases: []string{"env"},
Long: createAddonIstioLong,
Example: createAddonIstioExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
options.addFlags(cmd, defaultIstioNamespace, defaultIstioReleaseName, defaultIstioVersion)
cmd.Flags().StringVarP(&options.Password, "password", "p", defaultIstioPassword, "The default password to use for Istio")
cmd.Flags().StringVarP(&options.ConfigDir, "config-dir", "d", defaultIstioConfigDir, "The config directory to use")
cmd.Flags().StringVarP(&options.Chart, optionChart, "c", "", "The name of the chart to use")
cmd.Flags().BoolVarP(&options.NoInjectorWebhook, "no-injector-webhook", "", false, "Disables the injector webhook")
return cmd
}
// Run implements the command
func (o *CreateAddonIstioOptions) Run() error {
if o.Chart == "" {
// lets git clone the source to find the istio charts as they are not published anywhere yet
dir, err := o.getIstioChartsFromGitHub()
if err != nil {
return err
}
chartDir := filepath.Join(dir, kube.ChartIstio)
exists, err := util.FileExists(chartDir)
if !exists {
return fmt.Errorf("Could not find folder %s inside istio clone at %s", kube.ChartIstio, dir)
}
o.Dir = dir
o.Chart = kube.ChartIstio
}
if o.ReleaseName == "" {
return util.MissingOption(optionRelease)
}
if o.Chart == "" {
return util.MissingOption(optionChart)
}
err := o.ensureHelm()
if err != nil {
return errors.Wrap(err, "failed to ensure that Helm is present")
}
_, _, err = o.KubeClient()
if err != nil {
return err
}
err = o.generateSecrets()
if err != nil {
return err
}
devNamespace, _, err := kube.GetDevNamespace(o.KubeClientCached, o.currentNamespace)
if err != nil {
return fmt.Errorf("cannot find a dev team namespace to get existing exposecontroller config from. %v", err)
}
log.Infof("found dev namespace %s\n", devNamespace)
values := []string{}
if o.NoInjectorWebhook {
values = append(values, "sidecarInjectorWebhook.enabled=false")
}
setValues := strings.Split(o.SetValues, ",")
values = append(values, setValues...)
err = o.installChartAt(o.Dir, o.ReleaseName, o.Chart, o.Version, o.Namespace, true, values, nil)
if err != nil {
return fmt.Errorf("istio deployment failed: %v", err)
}
return nil
}
// getIstioChartsFromGitHub lets download the latest release of istio
func (o *CreateAddonIstioOptions) getIstioChartsFromGitHub() (string, error) {
answer := ""
latestVersion, err := util.GetLatestVersionFromGitHub("istio", "istio")
if err != nil {
return answer, fmt.Errorf("unable to get latest version for github.com/%s/%s %v", "istio", "istio", err)
}
binDir, err := util.JXBinLocation()
if err != nil {
return answer, err
}
binaryFile := "istioctl"
extension := ""
switch runtime.GOOS {
case "windows":
extension = "win.zip"
binaryFile += ".exe"
case "darwin":
extension = "osx.tar.gz"
default:
extension = "linux.tar.gz"
}
clientURL := fmt.Sprintf("https://github.com/istio/istio/releases/download/%s/istio-%s-%s", latestVersion, latestVersion, extension)
outputDir := filepath.Join(binDir, "istio-"+latestVersion.String())
os.RemoveAll(outputDir)
answer = outputDir
err = os.MkdirAll(outputDir, util.DefaultWritePermissions)
if err != nil {
return answer, err
}
tarPath := filepath.Join(binDir, "istio-"+extension)
os.Remove(tarPath)
err = binaries.DownloadFile(clientURL, tarPath)
if err != nil {
return answer, err
}
defer os.Remove(tarPath)
if strings.HasSuffix(extension, ".zip") {
err = util.Unzip(tarPath, outputDir)
if err != nil {
return answer, err
}
} else {
err = util.UnTargzAll(tarPath, binDir)
if err != nil {
return answer, err
}
}
f := filepath.Join(outputDir, "bin", binaryFile)
exists, err := util.FileExists(f)
if err != nil {
return answer, err
}
if exists {
binaryDest := filepath.Join(binDir, binaryFile)
os.Remove(binaryDest)
err = os.Rename(f, binaryDest)
if err != nil {
return answer, err
}
}
return answer, nil
}
func (o *CreateAddonIstioOptions) generateSecrets() error {
// generate secret for kiali && grafana
return nil
}