-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
init.go
183 lines (155 loc) · 4.88 KB
/
init.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
// Copyright 2020 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 ansible
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/validation"
"sigs.k8s.io/kubebuilder/pkg/model/config"
"sigs.k8s.io/kubebuilder/pkg/plugin"
"sigs.k8s.io/kubebuilder/pkg/plugin/scaffold"
"github.com/operator-framework/operator-sdk/internal/kubebuilder/cmdutil"
"github.com/operator-framework/operator-sdk/internal/plugins/ansible/v1/scaffolds"
"github.com/operator-framework/operator-sdk/internal/plugins/manifests"
"github.com/operator-framework/operator-sdk/internal/plugins/scorecard"
)
type initPlugin struct {
config *config.Config
apiPlugin createAPIPlugin
// If true, run the `create api` plugin.
doCreateAPI bool
// For help text.
commandName string
}
var (
_ plugin.Init = &initPlugin{}
_ cmdutil.RunOptions = &initPlugin{}
)
// UpdateContext injects documentation for the command
func (p *initPlugin) UpdateContext(ctx *plugin.Context) {
ctx.Description = `
Initialize a new Ansible-based operator project.
Writes the following files
- a kubebuilder PROJECT file with the domain and project layout configuration
- a Makefile that provides an interface for building and managing the operator
- Kubernetes manifests and kustomize configuration
- a watches.yaml file that defines the mapping between APIs and Roles/Playbooks
Optionally creates a new API, using the same flags as "create api"
`
ctx.Examples = fmt.Sprintf(`
# Scaffold a project with no API
$ %s init --plugins=%s --domain=my.domain \
# Invokes "create api"
$ %s init --plugins=%s \
--domain=my.domain \
--group=apps --version=v1alpha1 --kind=AppService
$ %s init --plugins=%s \
--domain=my.domain \
--group=apps --version=v1alpha1 --kind=AppService \
--generate-role
$ %s init --plugins=%s \
--domain=my.domain \
--group=apps --version=v1alpha1 --kind=AppService \
--generate-playbook
$ %s init --plugins=%s \
--domain=my.domain \
--group=apps --version=v1alpha1 --kind=AppService \
--generate-playbook \
--generate-role
`,
ctx.CommandName, pluginKey,
ctx.CommandName, pluginKey,
ctx.CommandName, pluginKey,
ctx.CommandName, pluginKey,
ctx.CommandName, pluginKey,
)
p.commandName = ctx.CommandName
}
func (p *initPlugin) BindFlags(fs *pflag.FlagSet) {
fs.SortFlags = false
fs.StringVar(&p.config.Domain, "domain", "my.domain", "domain for groups")
fs.StringVar(&p.config.ProjectName, "project-name", "", "name of this project, the default being directory name")
p.apiPlugin.BindFlags(fs)
}
func (p *initPlugin) InjectConfig(c *config.Config) {
c.Layout = pluginKey
p.config = c
p.apiPlugin.config = p.config
}
func (p *initPlugin) Run() error {
if err := cmdutil.Run(p); err != nil {
return err
}
// Run SDK phase 2 plugins.
if err := p.runPhase2(); err != nil {
return err
}
return nil
}
// SDK phase 2 plugins.
func (p *initPlugin) runPhase2() error {
if err := manifests.RunInit(p.config); err != nil {
return err
}
if err := scorecard.RunInit(p.config); err != nil {
return err
}
if p.doCreateAPI {
if err := p.apiPlugin.runPhase2(); err != nil {
return err
}
}
return nil
}
func (p *initPlugin) Validate() error {
// Check if the project name is a valid k8s namespace (DNS 1123 label).
if p.config.ProjectName == "" {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current directory: %v", err)
}
p.config.ProjectName = strings.ToLower(filepath.Base(dir))
}
if err := validation.IsDNS1123Label(p.config.ProjectName); err != nil {
return fmt.Errorf("project name (%s) is invalid: %v", p.config.ProjectName, err)
}
defaultOpts := scaffolds.CreateOptions{CRDVersion: "v1"}
if !p.apiPlugin.createOptions.GVK.Empty() || p.apiPlugin.createOptions != defaultOpts {
p.doCreateAPI = true
return p.apiPlugin.Validate()
}
return nil
}
func (p *initPlugin) GetScaffolder() (scaffold.Scaffolder, error) {
var (
apiScaffolder scaffold.Scaffolder
err error
)
if p.doCreateAPI {
apiScaffolder, err = p.apiPlugin.GetScaffolder()
if err != nil {
return nil, err
}
}
return scaffolds.NewInitScaffolder(p.config, apiScaffolder), nil
}
func (p *initPlugin) PostScaffold() error {
if !p.doCreateAPI {
fmt.Printf("Next: define a resource with:\n$ %s create api\n", p.commandName)
}
return nil
}