Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands/.../new,pkg/scaffold/helm: source chart flag for new projects #949

Merged
merged 21 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7ff7dc2
commands/operator-sdk/cmd/new,pkg/scaffold/helm: allow source helm ch…
joelanford Jan 17, 2019
9ac3181
Merge branch 'master' into helm-new-source-chart
joelanford Jan 17, 2019
8d1d6a8
pkg/scaffold/helm/chart.go,commands/.../new.go: remove requirement fo…
joelanford Jan 22, 2019
465faf2
Merge branch 'master' into helm-new-source-chart
joelanford Jan 24, 2019
8f6e09a
Merge branch 'master' into helm-new-source-chart
joelanford Jan 31, 2019
0bdb356
Merge branch 'master' into helm-new-source-chart
joelanford Feb 4, 2019
ad12673
commands/operator-sdk/cmd/new.go: improve helm-chart flags' help texts
joelanford Feb 4, 2019
0e900b9
commands/operator-sdk/cmd/new.go: comment to describe verifyFlags log…
joelanford Feb 4, 2019
0f8cc28
pkg/scaffold/helm/chart.go: revert to using r.LowerKind for scaffolde…
joelanford Feb 4, 2019
3818f27
commands/.../new.go,pkg/scaffold/helm/chart.go: improve function sign…
joelanford Feb 4, 2019
b41d3a8
pkg/scaffold/helm/chart.go: comment to explain decision not to use hy…
joelanford Feb 4, 2019
b7c93b9
commands/operator-sdk/cmd/new.go: fix typo in help text
joelanford Feb 5, 2019
6f6cdec
commands/operator-sdk/cmd/new.go: fix helm verify flags logic
joelanford Feb 6, 2019
5cf1a40
pkg/scaffold/helm/chart.go: log tmpDir remove failure
joelanford Feb 6, 2019
fafb5b8
CHANGELOG.md,doc,pkg/scaffold/helm: update docs
joelanford Feb 6, 2019
9edea92
Merge branch 'master' into helm-new-source-chart
joelanford Feb 6, 2019
6b5657e
vendor: add necessary vendor dirs
joelanford Feb 6, 2019
226bebe
Merge branch 'master' into helm-new-source-chart
joelanford Feb 12, 2019
770a0bb
CHANGELOG.md: moving updates to Unreleased section
joelanford Feb 12, 2019
911f7d6
Merge branch 'master' into helm-new-source-chart
joelanford Feb 15, 2019
8f2e88a
Gopkg.lock,vendor: removing non-go
joelanford Feb 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Added

- 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))

### Changed

### Deprecated
Expand Down
36 changes: 34 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 36 additions & 7 deletions commands/operator-sdk/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ generates a skeletal app-operator application in $GOPATH/src/github.com/example.
newCmd.Flags().BoolVar(&generatePlaybook, "generate-playbook", false, "Generate a playbook skeleton. (Only used for --type ansible)")
newCmd.Flags().BoolVar(&isClusterScoped, "cluster-scoped", false, "Generate cluster-scoped resources instead of namespace-scoped")

newCmd.Flags().StringVar(&helmChartRef, "helm-chart", "", "Initialize helm operator with existing helm chart (<URL>, <repo>/<name>, or local path)")
newCmd.Flags().StringVar(&helmChartVersion, "helm-chart-version", "", "Specific version of the helm chart (default is latest version)")
newCmd.Flags().StringVar(&helmChartRepo, "helm-chart-repo", "", "Chart repository URL for the requested helm chart")

return newCmd
}

Expand All @@ -67,6 +71,10 @@ var (
skipGit bool
generatePlaybook bool
isClusterScoped bool

helmChartRef string
helmChartVersion string
helmChartRepo string
)

const (
Expand Down Expand Up @@ -256,14 +264,17 @@ func doHelmScaffold() error {
ProjectName: projectName,
}

resource, err := scaffold.NewResource(apiVersion, kind)
if err != nil {
return err
createOpts := helm.CreateChartOptions{
ResourceAPIVersion: apiVersion,
ResourceKind: kind,
Chart: helmChartRef,
Version: helmChartVersion,
Repo: helmChartRepo,
}

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

valuesPath := filepath.Join("<project_dir>", helm.HelmChartsDir, chart.GetMetadata().GetName(), "values.yaml")
Expand All @@ -272,7 +283,10 @@ func doHelmScaffold() error {
s := &scaffold.Scaffold{}
err = s.Execute(cfg,
&helm.Dockerfile{},
&helm.WatchesYAML{Resource: resource},
&helm.WatchesYAML{
Resource: resource,
ChartName: chart.GetMetadata().GetName(),
},
&scaffold.ServiceAccount{},
&scaffold.Role{IsClusterScoped: isClusterScoped},
&scaffold.RoleBinding{IsClusterScoped: isClusterScoped},
Expand Down Expand Up @@ -300,11 +314,26 @@ func verifyFlags() error {
if operatorType != projutil.OperatorTypeAnsible && generatePlaybook {
return fmt.Errorf("value of --generate-playbook can only be used with --type `ansible`")
}

if len(helmChartRef) != 0 {
if operatorType != projutil.OperatorTypeHelm {
return fmt.Errorf("value of --helm-chart can only be used with --type=helm")
}
} else if len(helmChartRepo) != 0 {
return fmt.Errorf("value of --helm-chart-repo can only be used with --type=helm and --helm-chart")
} else if len(helmChartVersion) != 0 {
return fmt.Errorf("value of --helm-chart-version can only be used with --type=helm and --helm-chart")
}

if operatorType == projutil.OperatorTypeGo && (len(apiVersion) != 0 || len(kind) != 0) {
return fmt.Errorf("operators of type Go do not use --api-version or --kind")
}

if operatorType != projutil.OperatorTypeGo {
// --api-version and --kind are required with --type=ansible and --type=helm, with one exception.
//
// If --type=helm and --helm-chart is set, --api-version and --kind are optional. If left unset,
// sane defaults are used when the specified helm chart is created.
if operatorType == projutil.OperatorTypeAnsible || operatorType == projutil.OperatorTypeHelm && len(helmChartRef) == 0 {
lilic marked this conversation as resolved.
Show resolved Hide resolved
if len(apiVersion) == 0 {
return fmt.Errorf("value of --api-version must not have empty value")
}
Expand Down
25 changes: 25 additions & 0 deletions doc/helm/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ Nginx resource with APIVersion `example.com/v1apha1` and Kind
To learn more about the project directory structure, see the
[project layout][layout_doc] doc.

### Use an existing chart

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.

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.

If `--helm-chart` is a local chart archive or directory, it will be validated and unpacked or copied into the project.

Otherwise, the SDK will attempt to fetch the specified helm chart from a remote repository.

If a custom repository URL is not specified by `--helm-chart-repo`, the following chart reference formats are supported:

- `<repoName>/<chartName>`: Fetch the helm chart named `chartName` from the helm
chart repository named `repoName`, as specified in the
$HELM_HOME/repositories/repositories.yaml file.

- `<url>`: Fetch the helm chart archive at the specified URL.

If a custom repository URL is specified by `--helm-chart-repo`, the only supported format for `--helm-chart` is:

- `<chartName>`: Fetch the helm chart named `chartName` in the helm chart repository
specified by the `--helm-chart-repo` URL.

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.

### Operator scope

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.
Expand Down
44 changes: 40 additions & 4 deletions doc/sdk-cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,28 +237,62 @@ Scaffolds a new operator project.
* `--kind` string - CRD Kind. (e.g AppService)
* `--generate-playbook` - Generate a playbook skeleton. (Only used for `--type ansible`)
* `--cluster-scoped` - Initialize the operator to be cluster-scoped instead of namespace-scoped
* `--helm-chart` string - Initialize helm operator with existing helm chart (`<URL>`, `<repo>/<name>`, or local path)
* `--helm-chart-repo` string - Chart repository URL for the requested helm chart
* `--helm-chart-version` string - Specific version of the helm chart (default is latest version)
* `-h, --help` - help for new

### Example

Go project:
#### Go project

```console
$ mkdir $GOPATH/src/github.com/example.com/
$ cd $GOPATH/src/github.com/example.com/
$ operator-sdk new app-operator
```

Ansible project:
#### Ansible project

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

Helm project:
#### Helm project

For more details about creating new Helm operator projects, see the [Helm user guide][helm-user-guide-create-project].

```console
$ operator-sdk new app-operator --type=helm --api-version=app.example.com/v1alpha1 --kind=AppService
$ operator-sdk new app-operator --type=helm \
--api-version=app.example.com/v1alpha1 \
--kind=AppService

$ operator-sdk new app-operator --type=helm \
--api-version=app.example.com/v1alpha1 \
--kind=AppService \
--helm-chart=myrepo/app

$ operator-sdk new app-operator --type=helm \
--helm-chart=myrepo/app

$ operator-sdk new app-operator --type=helm \
--helm-chart=myrepo/app \
--helm-chart-version=1.2.3

$ operator-sdk new app-operator --type=helm \
--helm-chart=app \
--helm-chart-repo=https://charts.mycompany.com/

$ operator-sdk new app-operator --type=helm \
--helm-chart=app \
--helm-chart-repo=https://charts.mycompany.com/ \
--helm-chart-version=1.2.3

$ operator-sdk new app-operator --type=helm \
--helm-chart=/path/to/local/chart-directories/app/

$ operator-sdk new app-operator --type=helm \
--helm-chart=/path/to/local/chart-archives/app-1.2.3.tgz
```

## add
Expand Down Expand Up @@ -524,3 +558,5 @@ $ operator-sdk up local --namespace "testing"
[utility_link]: https://github.com/operator-framework/operator-sdk/blob/89bf021063d18b6769bdc551ed08fc37027939d5/pkg/util/k8sutil/k8sutil.go#L140
[k8s-code-generator]: https://github.com/kubernetes/code-generator
[openapi-code-generator]: https://github.com/kubernetes/kube-openapi
[helm-user-guide-create-project]: https://github.com/operator-framework/operator-sdk/blob/master/doc/helm/user-guide.md#create-a-new-project

Loading