forked from syndesisio/syndesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
283 lines (250 loc) · 8.52 KB
/
install.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 install
import (
"encoding/json"
"fmt"
"strings"
synapi "github.com/syndesisio/syndesis/install/operator/pkg/apis/syndesis/v1beta3"
"github.com/syndesisio/syndesis/install/operator/pkg/syndesis/capabilities"
"github.com/syndesisio/syndesis/install/operator/pkg/syndesis/configuration"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/syndesisio/syndesis/install/operator/pkg"
"github.com/syndesisio/syndesis/install/operator/pkg/cmd/internal"
"github.com/syndesisio/syndesis/install/operator/pkg/generator"
"github.com/syndesisio/syndesis/install/operator/pkg/util"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/yaml"
)
const RoleName = "syndesis-operator"
type Install struct {
// cli parsed config
*internal.Options
wait bool
eject string
image string
tag string
version string
addons string
customResource string
devSupport bool
logLevel int
apiServer capabilities.ApiServerSpec
databaseImage string
templateName string
productName string
// processing state
ejectedResources []unstructured.Unstructured
}
func New(parent *internal.Options) *cobra.Command {
o := Install{Options: parent}
cmd := cobra.Command{
Use: "install",
Short: "installs the all resources",
PersistentPreRunE: o.before,
PersistentPostRun: o.after,
Run: func(cmd *cobra.Command, args []string) {
err := o.installClusterResources()
util.ExitOnError(err)
err = o.installOperatorResources()
util.ExitOnError(err)
err = o.installApplication()
util.ExitOnError(err)
},
}
cmd.AddCommand(&cobra.Command{
Use: "cluster",
Short: "install the cluster level resources (requires cluster admin privileges)",
Run: func(cmd *cobra.Command, args []string) {
err := o.installClusterResources()
util.ExitOnError(err)
},
})
cmd.AddCommand(&cobra.Command{
Use: "operator",
Short: "install the operator resources (requires namespace admin privileges)",
Run: func(cmd *cobra.Command, args []string) {
err := o.installOperatorResources()
util.ExitOnError(err)
},
})
app := &cobra.Command{
Use: "app",
Short: "install the syndesis application (requires namespace admin privileges)",
Run: func(cmd *cobra.Command, args []string) {
err := o.installApplication()
util.ExitOnError(err)
},
}
app.PersistentFlags().StringVarP(&o.addons, "addons", "", "", "a comma separated list of addons that should be enabled")
app.PersistentFlags().StringVarP(&o.customResource, "custom-resource", "", "", "path to a custom resource file to use when deploying")
cmd.AddCommand(app)
forge := &cobra.Command{
Use: "forge",
Short: "forge the resource configuration into an openshift template <deprecated>",
Run: func(cmd *cobra.Command, args []string) {
err := o.installForge()
util.ExitOnError(err)
},
}
forge.PersistentFlags().StringVarP(&o.addons, "addons", "", "", "a coma separated list of addons that should be enabled")
forge.PersistentFlags().StringVarP(&o.templateName, "template-name", "", "", "the name of the template")
cmd.AddCommand(forge)
cmd.PersistentFlags().StringVarP(&o.eject, "eject", "e", "", "eject configuration that would be applied to the cluster in the specified format instead of installing the configuration. One of: json|yaml")
cmd.PersistentFlags().StringVarP(&o.image, "image", "", pkg.DefaultOperatorImage, "sets operator image that gets installed")
cmd.PersistentFlags().StringVarP(&o.tag, "tag", "", pkg.DefaultOperatorTag, "sets operator tag that gets installed")
cmd.PersistentFlags().BoolVarP(&o.wait, "wait", "w", false, "waits for the application to be running")
cmd.PersistentFlags().BoolVarP(&o.devSupport, "dev", "", false, "enable development mode by loading images from image stream tags.")
cmd.PersistentFlags().IntVarP(&o.logLevel, "log-level", "", 0, "specify the level of logging to display, ie. 0 (default) = info, 1 = debug ...")
cmd.PersistentFlags().StringVarP(&configuration.TemplateConfig, "operator-config", "", "/conf/config.yaml", "Path to the operator configuration file.")
cmd.PersistentFlags().AddFlagSet(util.FlagSet)
return &cmd
}
//go:generate go run generator/generator.go
func (o *Install) before(_ *cobra.Command, args []string) (err error) {
switch o.eject {
case "":
case "yaml":
case "json":
default:
return fmt.Errorf("invalid output format: %s", o.eject)
}
if len(args) > 0 {
return fmt.Errorf("unexpected argument: %s", args[0])
}
if o.eject != "" {
o.ejectedResources = []unstructured.Unstructured{}
}
apiSpec, err := capabilities.ApiCapabilities(o.ClientTools())
if err != nil {
return err
}
o.apiServer = *apiSpec
// The default operator image is not valid /w dev mode since it can't have a repository in the image name.
// Not applicable on other platforms so check for Openshift
if o.devSupport && o.image == pkg.DefaultOperatorImage && apiSpec.ImageStreams {
o.image = "syndesis-operator"
}
o.databaseImage = defaultDatabaseImage
config, err := configuration.GetProperties(o.Context, configuration.TemplateConfig, o.ClientTools(), &synapi.Syndesis{})
if err == nil {
o.databaseImage = config.Syndesis.Components.Database.Image
o.version = config.Version
o.productName = config.ProductName
}
return nil
}
func (o *Install) after(cmd *cobra.Command, args []string) {
if o.ejectedResources == nil {
return
}
value := util.UnstructuredsToRuntimeObject(o.ejectedResources)
if value == nil {
return
}
switch o.eject {
case "yaml":
data, err := yaml.Marshal(value)
util.ExitOnError(err)
fmt.Print(string(data))
case "json":
data, err := json.Marshal(value)
util.ExitOnError(err)
fmt.Print(string(data))
default:
panic(o.eject)
}
}
func (o *Install) Println(a ...interface{}) (int, error) {
if o.ejectedResources != nil {
return 0, nil
}
return fmt.Println(a...)
}
type RenderScope struct {
Image string
Tag string
Namespace string
ApiServer capabilities.ApiServerSpec
DevSupport bool
LogLevel int
Role string
Kind string
EnabledAddons []string
Version string
DatabaseImage string
ProductName string
}
func (o *Install) install(action string, resources []unstructured.Unstructured) error {
updateCounter := 0
createCounter := 0
client, err := o.ClientTools().RuntimeClient()
if err != nil {
return err
}
for _, res := range resources {
if o.ejectedResources != nil {
o.ejectedResources = append(o.ejectedResources, res)
} else {
_, result, err := util.CreateOrUpdate(o.Context, client, &res)
if err != nil {
return errors.Wrap(err, util.Dump(res))
}
switch result {
case controllerutil.OperationResultUpdated:
createCounter += 1
case controllerutil.OperationResultCreated:
createCounter += 1
}
}
}
if createCounter == 0 && updateCounter == 0 {
if _, err := o.Println(action + " previously installed"); err != nil {
return err
}
} else if updateCounter != 0 {
if _, err := o.Println(action + " updated successfully"); err != nil {
return err
}
} else {
if _, err := o.Println(action + " installed successfully"); err != nil {
return err
}
}
return nil
}
func (o *Install) render(fromFile string) ([]unstructured.Unstructured, error) {
addons := make([]string, 0)
if o.addons != "" {
addons = strings.Split(o.addons, ",")
}
resources, err := generator.Render(fromFile, RenderScope{
Namespace: o.Namespace,
Image: o.image,
Tag: o.tag,
DevSupport: o.devSupport,
LogLevel: o.logLevel,
ApiServer: o.apiServer,
Role: RoleName,
Kind: "Role",
EnabledAddons: addons,
DatabaseImage: o.databaseImage,
Version: o.version,
ProductName: o.productName,
})
return resources, err
}