forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_addon_istio.go
144 lines (127 loc) · 3.98 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
package cmd
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"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/spf13/cobra"
)
const (
defaultIstioNamespace = "istio-system"
defaultIstioReleaseName = "istio"
defaultIstioPassword = "istio"
defaultIstioConfigDir = "/istio_service_dir"
)
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, out io.Writer, errOut io.Writer) *cobra.Command {
options := &CreateAddonIstioOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
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)
cmd.Flags().StringVarP(&options.Version, "version", "v", "", "The version of the Istio chart to use")
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.KubeClient()
if err != nil {
return err
}
devNamespace, _, err := kube.GetDevNamespace(o.kubeClient, 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")
}
err = o.installChartAt(o.Dir, o.ReleaseName, o.Chart, o.Version, o.Namespace, true, values)
if err != nil {
return fmt.Errorf("istio deployment failed: %v", err)
}
return nil
}
func (o *CreateAddonIstioOptions) getIstioChartsFromGitHub() (string, error) {
answer, err := ioutil.TempDir("", "istio")
if err != nil {
return answer, err
}
gitRepo := "https://github.com/istio/istio.git"
log.Infof("Cloning %s to %s\n", util.ColorInfo(gitRepo), util.ColorInfo(answer))
err = o.Git().Clone(gitRepo, answer)
if err != nil {
return answer, err
}
return answer, nil
}