/
cmd.go
212 lines (182 loc) · 5.79 KB
/
cmd.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
package install
import (
"fmt"
"os"
"strings"
"github.com/maistra/istio-workspace/pkg/assets"
"github.com/maistra/istio-workspace/pkg/client/dynclient"
"github.com/maistra/istio-workspace/pkg/log"
"github.com/maistra/istio-workspace/pkg/openshift/parser"
"github.com/maistra/istio-workspace/version"
"github.com/go-logr/logr"
openshiftApi "github.com/openshift/api/template/v1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
)
var logger = func() logr.Logger {
return log.Log.WithValues("type", "install")
}
// NewCmd takes care of deploying server-side components of istio-workspace.
func NewCmd() *cobra.Command {
installCmd := &cobra.Command{
Use: "install",
Short: "Takes care of deploying server-side components of istio-workspace",
SilenceUsage: true,
RunE: installOperator,
}
installCmd.Flags().StringP("namespace", "n", "istio-workspace-operator", "Target namespace to which istio-workspace operator is deployed to.")
installCmd.Flags().BoolP("local", "l", false, "Install as local to the namespace only")
helpTpl := installCmd.HelpTemplate() + `
Environment variables you can override:{{range tplParams}}
{{.Name}} - {{.Description}} (default "{{.Value}}"){{end}}
`
installCmd.SetHelpTemplate(helpTpl)
return installCmd
}
func installOperator(cmd *cobra.Command, args []string) error { //nolint:gocyclo //reason cyclo can be skipped for the sake of readability
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}
local, err := cmd.Flags().GetBool("local")
if err != nil {
return err
}
if local && !cmd.Flags().Changed("namespace") {
kubeCfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)
if namespace, _, err = kubeCfg.Namespace(); err != nil {
logger().Error(err, fmt.Sprintf("failed to read k8s config file [%s]. "+
"use --namespace to define existing target namespace", kubeCfg.ConfigAccess().GetExplicitFile()))
return err
}
}
// Propagates NAMESPACE env var which is used by templates
if _, found := os.LookupEnv("NAMESPACE"); !found {
if envErr := os.Setenv("NAMESPACE", namespace); envErr != nil {
return envErr
}
}
// Propagates IKE_IMAGE_TAG env var which is used by templates to be aligned with the version of the actual built binary
if _, found := os.LookupEnv("IKE_IMAGE_TAG"); !found {
if envErr := os.Setenv("IKE_IMAGE_TAG", version.Version); envErr != nil {
return envErr
}
}
// Propagates IKE_VERSION env var which is used by templates to be aligned with the version of the actual built binary
if _, found := os.LookupEnv("IKE_VERSION"); !found {
if envErr := os.Setenv("IKE_VERSION", version.Version); envErr != nil {
return envErr
}
}
app, err := newApplier(namespace)
if err != nil {
return err
}
resources := []string{"crds/maistra.io_sessions.yaml", "service_account.yaml", "cluster_role.yaml"}
templates := []string{"cluster_role_binding.yaml", "operator.tpl.yaml"}
if local {
resources = []string{"crds/maistra.io_sessions.yaml", "service_account.yaml", "role.yaml"}
templates = []string{"role_binding.yaml", "operator.tpl.yaml"}
if envErr := os.Setenv("WATCH_NAMESPACE", namespace); envErr != nil {
return envErr
}
}
if err := apply(app.applyResource, resources...); err != nil {
return err
}
if err := apply(app.applyTemplate, templates...); err != nil {
return err
}
logger().Info("Installing operator", "namespace", os.Getenv("NAMESPACE"),
"image", fmt.Sprintf("%s/%s/%s:%s", os.Getenv("IKE_DOCKER_REGISTRY"),
os.Getenv("IKE_DOCKER_REPOSITORY"),
os.Getenv("IKE_IMAGE_NAME"),
os.Getenv("IKE_IMAGE_TAG")),
"version", version.Version)
return nil
}
type applier struct {
c *dynclient.Client
d parser.DecodeFunc
}
func newApplier(namespace string) (*applier, error) {
client, err := dynclient.NewDefaultDynamicClient(namespace)
if err != nil {
return nil, err
}
decode, err := parser.Decoder()
if err != nil {
return nil, err
}
return &applier{c: client, d: decode}, nil
}
func apply(a func(path string) error, paths ...string) error {
for _, p := range paths {
if err := a(p); err != nil {
if !errors.IsAlreadyExists(err) {
logger().Error(err, "failed creating "+strings.TrimSuffix(p, ".yaml"))
return err
}
logger().Info(strings.TrimSuffix(p, ".yaml") + " already exists")
}
}
return nil
}
func (app *applier) applyResource(resourcePath string) error {
rawCrd, err := assets.Load("deploy/" + resourcePath)
if err != nil {
return err
}
crd, err := parser.Parse(rawCrd)
if err != nil {
return err
}
kind := crd.GetObjectKind()
logger().Info(fmt.Sprintf("Applying '%s' in namespace '%s' [Name: %s; Kind: %s]",
strings.TrimSuffix(resourcePath, ".yaml"),
app.c.Namespace,
name(crd, resourcePath),
kind.GroupVersionKind().Kind))
err = app.c.Create(crd)
return err
}
func name(object runtime.Object, fallback string) (name string) {
accessor := meta.NewAccessor()
name, err := accessor.Name(object)
if err != nil {
name = fallback
}
return
}
func (app *applier) applyTemplate(templatePath string) error {
yaml, err := parser.ProcessTemplateUsingEnvVars("deploy/" + templatePath)
if err != nil {
return err
}
tpl, err := parser.Parse(yaml)
if err != nil {
return err
}
r := tpl.(*openshiftApi.Template)
for _, obj := range r.Objects {
object, gav, err := app.d(obj.Raw, nil, nil)
if err != nil {
return err
}
logger().Info(fmt.Sprintf("Applying '%s' in namespace '%s' [Name: %s; Kind: %s]", strings.TrimSuffix(templatePath, ".yaml"),
app.c.Namespace,
name(object, templatePath),
gav.Kind))
err = app.c.Create(object)
if err != nil {
return err
}
}
return nil
}