forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
229 lines (201 loc) · 7.49 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package clusteradd
import (
"fmt"
"io"
"os"
"path"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/variable"
"github.com/openshift/origin/pkg/oc/clusteradd/componentinstall"
"github.com/openshift/origin/pkg/oc/clusteradd/components/automation-service-broker"
"github.com/openshift/origin/pkg/oc/clusteradd/components/default-imagestreams"
"github.com/openshift/origin/pkg/oc/clusteradd/components/registry"
"github.com/openshift/origin/pkg/oc/clusteradd/components/router"
"github.com/openshift/origin/pkg/oc/clusteradd/components/sample-templates"
"github.com/openshift/origin/pkg/oc/clusteradd/components/service-catalog"
"github.com/openshift/origin/pkg/oc/clusteradd/components/template-service-broker"
"github.com/openshift/origin/pkg/oc/clusteradd/components/web-console-operator"
"github.com/openshift/origin/pkg/oc/clusterup/coreinstall/components/persistent-volumes"
"github.com/openshift/origin/pkg/oc/clusterup/docker/dockerhelper"
"github.com/openshift/origin/pkg/version"
)
const (
CmdAddRecommendedName = "add"
)
var (
cmdAddLong = templates.LongDesc(`
Adds a component to an 'oc cluster up' cluster.
`)
cmdAddExample = templates.Examples(`
# Add service catalog
%[1]s service-catalog
# Add template service broker to a different basedir
%[1]s --base-dir=other/path template-service-broker`)
)
// availableComponents lists the components that are available for installation.
var availableComponents = map[string]func(ctx componentinstall.Context) componentinstall.Component{
"automation-service-broker": func(ctx componentinstall.Context) componentinstall.Component {
return &automation_service_broker.AutomationServiceBrokerComponentOptions{InstallContext: ctx}
},
"centos-imagestreams": func(ctx componentinstall.Context) componentinstall.Component {
return &default_imagestreams.CentosImageStreamsComponentOptions{InstallContext: ctx}
},
"registry": func(ctx componentinstall.Context) componentinstall.Component {
return ®istry.RegistryComponentOptions{InstallContext: ctx}
},
"rhel-imagestreams": func(ctx componentinstall.Context) componentinstall.Component {
return &default_imagestreams.RHELImageStreamsComponentOptions{InstallContext: ctx}
},
"router": func(ctx componentinstall.Context) componentinstall.Component {
return &router.RouterComponentOptions{InstallContext: ctx}
},
"sample-templates": func(ctx componentinstall.Context) componentinstall.Component {
return &sample_templates.SampleTemplatesComponentOptions{InstallContext: ctx}
},
"persistent-volumes": func(ctx componentinstall.Context) componentinstall.Component {
return &persistent_volumes.SetupPersistentVolumesOptions{InstallContext: ctx}
},
"service-catalog": func(ctx componentinstall.Context) componentinstall.Component {
return &service_catalog.ServiceCatalogComponentOptions{InstallContext: ctx}
},
"template-service-broker": func(ctx componentinstall.Context) componentinstall.Component {
return &template_service_broker.TemplateServiceBrokerComponentOptions{InstallContext: ctx}
},
"web-console": func(ctx componentinstall.Context) componentinstall.Component {
return &web_console_operator.WebConsoleOperatorComponentOptions{InstallContext: ctx}
},
}
func NewCmdAdd(name, fullName string, out, errout io.Writer) *cobra.Command {
config := &ClusterAddConfig{
Out: out,
ErrOut: errout,
ImageTemplate: variable.NewDefaultImageTemplate(),
}
cmd := &cobra.Command{
Use: name,
Short: "Add components to an 'oc cluster up' cluster",
Long: cmdAddLong,
Example: fmt.Sprintf(cmdAddExample, fullName),
RunE: func(c *cobra.Command, args []string) error {
if err := config.Complete(c); err != nil {
return err
}
if err := config.Validate(); err != nil {
return err
}
if err := config.Check(); err != nil {
return err
}
if err := config.Run(c); err != nil {
return err
}
return nil
},
}
config.Bind(cmd.Flags())
return cmd
}
// Start runs the start tasks ensuring that they are executed in sequence
func (c *ClusterAddConfig) Run(cmd *cobra.Command) error {
defaultPullPolicy := "Always"
if len(c.ImageTag) > 0 {
defaultPullPolicy = "IfNotPresent"
}
componentsToInstall := []componentinstall.Component{}
installContext, err := componentinstall.NewComponentInstallContext(c.cliImage(), c.imageFormat(), defaultPullPolicy, c.BaseDir, c.ServerLogLevel)
if err != nil {
return err
}
for _, componentName := range c.ComponentsToInstall {
fmt.Fprintf(c.Out, "Adding %s ...\n", componentName)
component := availableComponents[componentName](installContext)
componentsToInstall = append(componentsToInstall, component)
}
return componentinstall.InstallComponents(componentsToInstall, c.dockerClient)
}
type ClusterAddConfig struct {
ImageTemplate variable.ImageTemplate
ImageTag string
BaseDir string
ServerLogLevel int
ComponentsToInstall []string
Out io.Writer
ErrOut io.Writer
dockerClient dockerhelper.Interface
}
func (c *ClusterAddConfig) Complete(cmd *cobra.Command) error {
c.ComponentsToInstall = cmd.Flags().Args()
validComponentNames := sets.StringKeySet(availableComponents)
for _, name := range c.ComponentsToInstall {
if !validComponentNames.Has(name) {
return fmt.Errorf("unknown component %q, valid component names are: %q", name, strings.Join(validComponentNames.List(), ","))
}
}
// do some defaulting
c.ImageTemplate.Format = variable.Expand(c.ImageTemplate.Format, func(s string) (string, bool) {
if s == "version" {
if len(c.ImageTag) == 0 {
return strings.TrimRight("v"+version.Get().Major+"."+version.Get().Minor, "+"), true
}
return c.ImageTag, true
}
return "", false
}, variable.Identity)
if len(c.BaseDir) == 0 {
c.BaseDir = "openshift.local.clusterup"
}
if !path.IsAbs(c.BaseDir) {
cwd, err := os.Getwd()
if err != nil {
return err
}
absHostDir, err := cmdutil.MakeAbs(c.BaseDir, cwd)
if err != nil {
return err
}
c.BaseDir = absHostDir
}
client, err := dockerhelper.GetDockerClient()
if err != nil {
return err
}
c.dockerClient = client
return nil
}
// Validate validates that required fields in StartConfig have been populated
func (c *ClusterAddConfig) Validate() error {
if c.dockerClient == nil {
return fmt.Errorf("missing dockerClient")
}
return nil
}
// Check is a spot to do NON-MUTATING, preflight checks. Over time, we should try to move our non-mutating checks out of
// Complete and into Check.
// TODO check for basedir correctness
func (c *ClusterAddConfig) Check() error {
return nil
}
func (c *ClusterAddConfig) Bind(flags *pflag.FlagSet) {
flags.StringVar(&c.ImageTag, "tag", "", "Specify the tag for OpenShift images")
flags.MarkHidden("tag")
flags.StringVar(&c.ImageTemplate.Format, "image", c.ImageTemplate.Format, "Specify the images to use for OpenShift")
flags.StringVar(&c.BaseDir, "base-dir", c.BaseDir, "Directory on Docker host for cluster up configuration")
flags.IntVar(&c.ServerLogLevel, "server-loglevel", 0, "Log level for OpenShift server")
}
func (c *ClusterAddConfig) openshiftImage() string {
return c.ImageTemplate.ExpandOrDie("control-plane")
}
func (c *ClusterAddConfig) cliImage() string {
return c.ImageTemplate.ExpandOrDie("cli")
}
func (c *ClusterAddConfig) GetLogDir() string {
return path.Join(c.BaseDir, "logs")
}
func (c *ClusterAddConfig) imageFormat() string {
return c.ImageTemplate.Format
}