This repository has been archived by the owner on Dec 11, 2021. It is now read-only.
forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
216 lines (189 loc) · 6.21 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
// Copyright 2019 The Operator-SDK Authors
//
// Licensed 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 migrate
import (
"fmt"
"os"
"path/filepath"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/ansible"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/helm"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
depManager string
headerFile string
)
// NewCmd returns a command that will add source code to an existing non-go operator
func NewCmd() *cobra.Command {
newCmd := &cobra.Command{
Use: "migrate",
Short: "Adds source code to an operator",
Long: `operator-sdk migrate adds a main.go source file and any associated source files for an operator that is not of the "go" type.`,
RunE: migrateRun,
}
newCmd.Flags().StringVar(&depManager, "dep-manager", "modules", `Dependency manager the new project will use (choices: "dep", "modules")`)
newCmd.Flags().StringVar(&headerFile, "header-file", "", "Path to file containing headers for generated Go files. Copied to hack/boilerplate.go.txt")
return newCmd
}
// migrateRun determines the current operator type and runs the corresponding
// migrate function.
func migrateRun(cmd *cobra.Command, args []string) error {
projutil.MustInProjectRoot()
_ = projutil.CheckAndGetProjectGoPkg()
opType := projutil.GetOperatorType()
switch opType {
case projutil.OperatorTypeAnsible:
return migrateAnsible()
case projutil.OperatorTypeHelm:
return migrateHelm()
}
return fmt.Errorf("operator of type %s cannot be migrated", opType)
}
// migrateAnsible runs the migration process for an ansible-based operator
func migrateAnsible() error {
wd := projutil.MustGetwd()
cfg := &input.Config{
Repo: projutil.CheckAndGetProjectGoPkg(),
AbsProjectPath: wd,
ProjectName: filepath.Base(wd),
}
dockerfile := ansible.DockerfileHybrid{
Watches: true,
Roles: true,
}
_, err := os.Stat(ansible.PlaybookYamlFile)
switch {
case err == nil:
dockerfile.Playbook = true
case os.IsNotExist(err):
log.Info("No playbook was found, so not including it in the new Dockerfile")
default:
return fmt.Errorf("error trying to stat %s: (%v)", ansible.PlaybookYamlFile, err)
}
if err := renameDockerfile(); err != nil {
return err
}
s := &scaffold.Scaffold{}
if headerFile != "" {
err = s.Execute(cfg, &scaffold.Boilerplate{BoilerplateSrcPath: headerFile})
if err != nil {
return fmt.Errorf("boilerplate scaffold failed: (%v)", err)
}
s.BoilerplatePath = headerFile
}
if err := scaffoldAnsibleDepManager(s, cfg); err != nil {
return errors.Wrap(err, "migrate Ansible dependency manager file scaffold failed")
}
err = s.Execute(cfg,
&ansible.Main{},
&dockerfile,
&ansible.Entrypoint{},
&ansible.UserSetup{},
&ansible.K8sStatus{},
&ansible.AoLogs{},
)
if err != nil {
return fmt.Errorf("migrate ansible scaffold failed: (%v)", err)
}
return nil
}
// migrateHelm runs the migration process for a helm-based operator
func migrateHelm() error {
wd := projutil.MustGetwd()
cfg := &input.Config{
Repo: projutil.CheckAndGetProjectGoPkg(),
AbsProjectPath: wd,
ProjectName: filepath.Base(wd),
}
if err := renameDockerfile(); err != nil {
return err
}
s := &scaffold.Scaffold{}
if headerFile != "" {
err := s.Execute(cfg, &scaffold.Boilerplate{BoilerplateSrcPath: headerFile})
if err != nil {
return fmt.Errorf("boilerplate scaffold failed: (%v)", err)
}
s.BoilerplatePath = headerFile
}
if err := scaffoldHelmDepManager(s, cfg); err != nil {
return errors.Wrap(err, "migrate Helm dependency manager file scaffold failed")
}
err := s.Execute(cfg,
&helm.Main{},
&helm.DockerfileHybrid{
Watches: true,
HelmCharts: true,
},
&helm.Entrypoint{},
&helm.UserSetup{},
)
if err != nil {
return fmt.Errorf("migrate helm scaffold failed: (%v)", err)
}
return nil
}
func renameDockerfile() error {
dockerfilePath := filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile)
newDockerfilePath := dockerfilePath + ".sdkold"
err := os.Rename(dockerfilePath, newDockerfilePath)
if err != nil {
return fmt.Errorf("failed to rename Dockerfile: (%v)", err)
}
log.Infof("Renamed Dockerfile to %s and replaced with newer version. Compare the new Dockerfile to your old one and manually migrate any customizations", newDockerfilePath)
return nil
}
func scaffoldHelmDepManager(s *scaffold.Scaffold, cfg *input.Config) error {
var files []input.File
switch m := projutil.DepManagerType(depManager); m {
case projutil.DepManagerDep:
files = append(files, &helm.GopkgToml{})
case projutil.DepManagerGoMod:
if err := goModCheck(); err != nil {
return err
}
files = append(files, &helm.GoMod{}, &scaffold.Tools{})
default:
return projutil.ErrInvalidDepManager(depManager)
}
return s.Execute(cfg, files...)
}
func scaffoldAnsibleDepManager(s *scaffold.Scaffold, cfg *input.Config) error {
var files []input.File
switch m := projutil.DepManagerType(depManager); m {
case projutil.DepManagerDep:
files = append(files, &ansible.GopkgToml{})
case projutil.DepManagerGoMod:
if err := goModCheck(); err != nil {
return err
}
files = append(files, &ansible.GoMod{}, &scaffold.Tools{})
default:
return projutil.ErrInvalidDepManager(depManager)
}
return s.Execute(cfg, files...)
}
func goModCheck() error {
goModOn, err := projutil.GoModOn()
if err == nil && !goModOn {
log.Fatal(`Dependency manager "modules" has been selected but go modules are not active. ` +
`Activate modules then run "operator-sdk migrate".`)
}
return err
}