Skip to content

Commit c02b48b

Browse files
joelanfordlilic
authored andcommitted
commands/.../new,pkg/scaffold/helm: source chart flag for new projects (operator-framework#949)
* commands/operator-sdk/cmd/new,pkg/scaffold/helm: allow source helm chart when creating new helm projects * pkg/scaffold/helm/chart.go,commands/.../new.go: remove requirement for helm CLI, add helm-chart-version flag, general cleanup * commands/operator-sdk/cmd/new.go: improve helm-chart flags' help texts * commands/operator-sdk/cmd/new.go: comment to describe verifyFlags logic with --helm-chart * pkg/scaffold/helm/chart.go: revert to using r.LowerKind for scaffolded chart name * commands/.../new.go,pkg/scaffold/helm/chart.go: improve function signatures and add CreateChart godoc comment * pkg/scaffold/helm/chart.go: comment to explain decision not to use hyphens in scaffolded chart name * commands/operator-sdk/cmd/new.go: fix typo in help text * commands/operator-sdk/cmd/new.go: fix helm verify flags logic * pkg/scaffold/helm/chart.go: log tmpDir remove failure * CHANGELOG.md,doc,pkg/scaffold/helm: update docs * vendor: add necessary vendor dirs * CHANGELOG.md: moving updates to Unreleased section * Gopkg.lock,vendor: removing non-go
1 parent c83827d commit c02b48b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+12204
-25
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Added
44

5+
- New flags for [`operator-sdk new --type=helm`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#new), which can be used to populate the project with an existing chart. ([#949](https://github.com/operator-framework/operator-sdk/pull/949))
6+
57
### Changed
68

79
### Deprecated

Gopkg.lock

+34-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

commands/operator-sdk/cmd/new.go

+36-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ generates a skeletal app-operator application in $GOPATH/src/github.com/example.
5656
newCmd.Flags().BoolVar(&generatePlaybook, "generate-playbook", false, "Generate a playbook skeleton. (Only used for --type ansible)")
5757
newCmd.Flags().BoolVar(&isClusterScoped, "cluster-scoped", false, "Generate cluster-scoped resources instead of namespace-scoped")
5858

59+
newCmd.Flags().StringVar(&helmChartRef, "helm-chart", "", "Initialize helm operator with existing helm chart (<URL>, <repo>/<name>, or local path)")
60+
newCmd.Flags().StringVar(&helmChartVersion, "helm-chart-version", "", "Specific version of the helm chart (default is latest version)")
61+
newCmd.Flags().StringVar(&helmChartRepo, "helm-chart-repo", "", "Chart repository URL for the requested helm chart")
62+
5963
return newCmd
6064
}
6165

@@ -67,6 +71,10 @@ var (
6771
skipGit bool
6872
generatePlaybook bool
6973
isClusterScoped bool
74+
75+
helmChartRef string
76+
helmChartVersion string
77+
helmChartRepo string
7078
)
7179

7280
const (
@@ -256,14 +264,17 @@ func doHelmScaffold() error {
256264
ProjectName: projectName,
257265
}
258266

259-
resource, err := scaffold.NewResource(apiVersion, kind)
260-
if err != nil {
261-
return err
267+
createOpts := helm.CreateChartOptions{
268+
ResourceAPIVersion: apiVersion,
269+
ResourceKind: kind,
270+
Chart: helmChartRef,
271+
Version: helmChartVersion,
272+
Repo: helmChartRepo,
262273
}
263274

264-
chart, err := helm.CreateChartForResource(resource, cfg.AbsProjectPath)
275+
resource, chart, err := helm.CreateChart(cfg.AbsProjectPath, createOpts)
265276
if err != nil {
266-
log.Fatalf("Failed to create initial helm chart for resource (%v, %v): (%v)", resource.APIVersion, resource.Kind, err)
277+
return fmt.Errorf("failed to create helm chart: %s", err)
267278
}
268279

269280
valuesPath := filepath.Join("<project_dir>", helm.HelmChartsDir, chart.GetMetadata().GetName(), "values.yaml")
@@ -272,7 +283,10 @@ func doHelmScaffold() error {
272283
s := &scaffold.Scaffold{}
273284
err = s.Execute(cfg,
274285
&helm.Dockerfile{},
275-
&helm.WatchesYAML{Resource: resource},
286+
&helm.WatchesYAML{
287+
Resource: resource,
288+
ChartName: chart.GetMetadata().GetName(),
289+
},
276290
&scaffold.ServiceAccount{},
277291
&scaffold.Role{IsClusterScoped: isClusterScoped},
278292
&scaffold.RoleBinding{IsClusterScoped: isClusterScoped},
@@ -300,11 +314,26 @@ func verifyFlags() error {
300314
if operatorType != projutil.OperatorTypeAnsible && generatePlaybook {
301315
return fmt.Errorf("value of --generate-playbook can only be used with --type `ansible`")
302316
}
317+
318+
if len(helmChartRef) != 0 {
319+
if operatorType != projutil.OperatorTypeHelm {
320+
return fmt.Errorf("value of --helm-chart can only be used with --type=helm")
321+
}
322+
} else if len(helmChartRepo) != 0 {
323+
return fmt.Errorf("value of --helm-chart-repo can only be used with --type=helm and --helm-chart")
324+
} else if len(helmChartVersion) != 0 {
325+
return fmt.Errorf("value of --helm-chart-version can only be used with --type=helm and --helm-chart")
326+
}
327+
303328
if operatorType == projutil.OperatorTypeGo && (len(apiVersion) != 0 || len(kind) != 0) {
304329
return fmt.Errorf("operators of type Go do not use --api-version or --kind")
305330
}
306331

307-
if operatorType != projutil.OperatorTypeGo {
332+
// --api-version and --kind are required with --type=ansible and --type=helm, with one exception.
333+
//
334+
// If --type=helm and --helm-chart is set, --api-version and --kind are optional. If left unset,
335+
// sane defaults are used when the specified helm chart is created.
336+
if operatorType == projutil.OperatorTypeAnsible || operatorType == projutil.OperatorTypeHelm && len(helmChartRef) == 0 {
308337
if len(apiVersion) == 0 {
309338
return fmt.Errorf("value of --api-version must not have empty value")
310339
}

doc/helm/user-guide.md

+25
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ Nginx resource with APIVersion `example.com/v1apha1` and Kind
5050
To learn more about the project directory structure, see the
5151
[project layout][layout_doc] doc.
5252

53+
### Use an existing chart
54+
55+
Instead of creating your project with a boilerplate Helm chart, you can also use `--helm-chart`, `--helm-chart-repo`, and `--helm-chart-version` to use an existing chart, either from your local filesystem or a remote chart repository.
56+
57+
If `--helm-chart` is specified, `--api-version` and `--kind` become optional. If left unset, the SDK will default `--api-version` to `charts.helm.k8s.io/v1alpha1` and will deduce `--kind` from the specified chart.
58+
59+
If `--helm-chart` is a local chart archive or directory, it will be validated and unpacked or copied into the project.
60+
61+
Otherwise, the SDK will attempt to fetch the specified helm chart from a remote repository.
62+
63+
If a custom repository URL is not specified by `--helm-chart-repo`, the following chart reference formats are supported:
64+
65+
- `<repoName>/<chartName>`: Fetch the helm chart named `chartName` from the helm
66+
chart repository named `repoName`, as specified in the
67+
$HELM_HOME/repositories/repositories.yaml file.
68+
69+
- `<url>`: Fetch the helm chart archive at the specified URL.
70+
71+
If a custom repository URL is specified by `--helm-chart-repo`, the only supported format for `--helm-chart` is:
72+
73+
- `<chartName>`: Fetch the helm chart named `chartName` in the helm chart repository
74+
specified by the `--helm-chart-repo` URL.
75+
76+
If `--helm-chart-version` is not set, the SDK will fetch the latest available version of the helm chart. Otherwise, it will fetch the specified version. `--helm-chart-version` is not used when `--helm-chart` itself refers to a specific version, for example when it is a local path or a URL.
77+
5378
### Operator scope
5479

5580
A namespace-scoped operator (the default) watches and manages resources in a single namespace, whereas a cluster-scoped operator watches and manages resources cluster-wide. Namespace-scoped operators are preferred because of their flexibility. They enable decoupled upgrades, namespace isolation for failures and monitoring, and differing API definitions. However, there are use cases where a cluster-scoped operator may make sense. For example, the [cert-manager](https://github.com/jetstack/cert-manager) operator is often deployed with cluster-scoped permissions and watches so that it can manage issuing certificates for an entire cluster.

doc/sdk-cli-reference.md

+40-4
Original file line numberDiff line numberDiff line change
@@ -238,28 +238,62 @@ Scaffolds a new operator project.
238238
* `--kind` string - CRD Kind. (e.g AppService)
239239
* `--generate-playbook` - Generate a playbook skeleton. (Only used for `--type ansible`)
240240
* `--cluster-scoped` - Initialize the operator to be cluster-scoped instead of namespace-scoped
241+
* `--helm-chart` string - Initialize helm operator with existing helm chart (`<URL>`, `<repo>/<name>`, or local path)
242+
* `--helm-chart-repo` string - Chart repository URL for the requested helm chart
243+
* `--helm-chart-version` string - Specific version of the helm chart (default is latest version)
241244
* `-h, --help` - help for new
242245

243246
### Example
244247

245-
Go project:
248+
#### Go project
246249

247250
```console
248251
$ mkdir $GOPATH/src/github.com/example.com/
249252
$ cd $GOPATH/src/github.com/example.com/
250253
$ operator-sdk new app-operator
251254
```
252255

253-
Ansible project:
256+
#### Ansible project
254257

255258
```console
256259
$ operator-sdk new app-operator --type=ansible --api-version=app.example.com/v1alpha1 --kind=AppService
257260
```
258261

259-
Helm project:
262+
#### Helm project
263+
264+
For more details about creating new Helm operator projects, see the [Helm user guide][helm-user-guide-create-project].
260265

261266
```console
262-
$ operator-sdk new app-operator --type=helm --api-version=app.example.com/v1alpha1 --kind=AppService
267+
$ operator-sdk new app-operator --type=helm \
268+
--api-version=app.example.com/v1alpha1 \
269+
--kind=AppService
270+
271+
$ operator-sdk new app-operator --type=helm \
272+
--api-version=app.example.com/v1alpha1 \
273+
--kind=AppService \
274+
--helm-chart=myrepo/app
275+
276+
$ operator-sdk new app-operator --type=helm \
277+
--helm-chart=myrepo/app
278+
279+
$ operator-sdk new app-operator --type=helm \
280+
--helm-chart=myrepo/app \
281+
--helm-chart-version=1.2.3
282+
283+
$ operator-sdk new app-operator --type=helm \
284+
--helm-chart=app \
285+
--helm-chart-repo=https://charts.mycompany.com/
286+
287+
$ operator-sdk new app-operator --type=helm \
288+
--helm-chart=app \
289+
--helm-chart-repo=https://charts.mycompany.com/ \
290+
--helm-chart-version=1.2.3
291+
292+
$ operator-sdk new app-operator --type=helm \
293+
--helm-chart=/path/to/local/chart-directories/app/
294+
295+
$ operator-sdk new app-operator --type=helm \
296+
--helm-chart=/path/to/local/chart-archives/app-1.2.3.tgz
263297
```
264298

265299
## add
@@ -525,3 +559,5 @@ $ operator-sdk up local --namespace "testing"
525559
[utility_link]: https://github.com/operator-framework/operator-sdk/blob/89bf021063d18b6769bdc551ed08fc37027939d5/pkg/util/k8sutil/k8sutil.go#L140
526560
[k8s-code-generator]: https://github.com/kubernetes/code-generator
527561
[openapi-code-generator]: https://github.com/kubernetes/kube-openapi
562+
[helm-user-guide-create-project]: https://github.com/operator-framework/operator-sdk/blob/master/doc/helm/user-guide.md#create-a-new-project
563+

0 commit comments

Comments
 (0)