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

Feat: implement addon init command #4162

Merged
merged 10 commits into from
Jun 24, 2022

Conversation

charlie0129
Copy link
Member

@charlie0129 charlie0129 commented Jun 13, 2022

Description of your changes

As described in #4116 (comment), this pr adds a vela addon init command.

  • If the user provides all three Chart-related parameters, an addon scaffold, with a helm component inside, will be created.
  • If the user does't provide all the necessary parameters to create a helm component, a basic addon scaffold is generated instead.

This pr does not close that issue, since more features are needed.

Command Help Text
$ vela addon init -h
Create an addon scaffold for quick starting. A Helm Component is generated if you provide Chart-related parameters.

Usage:
  vela addon init [flags]

Examples:
	vela addon init mongodb --helm-repo-url=https://marketplace.azurecr.io/helm/v1/repo --chart=mongodb --version=12.1.16
will create something like this:
	mongodb/
	├── definitions
	├── metadata.yaml
	├── readme.md
	├── resources
	│   └── mongodb.cue
	├── schemas
	└── template.yaml

If you want to store the scaffold in a different directory, you can use the -p/--path flag:
	vela addon init mongodb -p ./some/repo --helm-repo-url=https://marketplace.azurecr.io/helm/v1/repo --chart=mongodb --version=12.1.16

If you don't want the Helm component, just omit the three Chart-related parameters. We will create an empty scaffold for you.
	vela addon init mongodb

Flags:
      --chart string           Helm Chart name
      --helm-repo-url string   URL that points to a Helm repo
  -h, --help                   help for init
  -p, --path string            path to the addon directory (default is ./<addon-name>)
      --version string         version of the Chart

Global Flags:
  -y, --yes   Assume yes for all user prompts

Examples:

Normal creation

Only when ./<addon-name> (or user-specified path with -p) does not exist, or is an empty directory, will this command succeed. Otherwise, to prevent overwrites, this command will fail.

Screenshot from 2022-06-14 18-14-29

$ vela addon create mongodb --helm-repo-url=https://marketplace.azurecr.io/helm/v1/repo --chart=mongodb --version=12.1.16 -p mongo/addon
Scaffold created in directory mongo/addon. What to do next:
- Review and edit what we have generated in mongo/addon
- Check out our guide on how to build your own addon: https://kubevela.io/docs/platform-engineers/addon/intro
- To enable the addon, run: vela addon enable mongo/addon
File Structure and Contents (Helm Component)
$ vela addon init mongo-helm --helm-repo-url=https://marketplace.azurecr.io/helm/v1/repo --chart=mongodb --version=12.1.16
Scaffold created in directory mongo-helm. What to do next:
- Check out our guide on how to build your own addon: https://kubevela.io/docs/platform-engineers/addon/intro
- Review and edit what we have generated in mongo-helm
- To enable the addon, run: vela addon enable mongo-helm
$ tree mongo-helm
mongo-helm
├── definitions
├── metadata.yaml
├── readme.md
├── resources
│   └── mongo-helm.cue
├── schemas
└── template.yaml

3 directories, 4 files
$ cat mongo-helm/resources/mongo-helm.cue
output: {
	type: "helm"
	properties: {
		url:      "https://marketplace.azurecr.io/helm/v1/repo"
		repoType: "helm"
		chart:    "mongodb"
		version:  "12.1.16"
	}
}
$ cat mongo-helm/metadata.yaml
dependencies:
- name: fluxcd
description: An addon for KubeVela.
icon: ""
invisible: false
name: mongo-helm
tags:
- 12.1.16
version: 12.1.16
$ cat mongo-helm/readme.md
# Example mongo-helm Addon

Also check how to build your own addon: https://kubevela.net/docs/platform-engineers/addon/intro

## Directory Structure

- `template.yaml`: contains the basic app, you can add some component and workflow to meet your requirements. Other files in `resources/` and `definitions/` will be rendered as Components and appended in `spec.components`
- `metadata.yaml`: contains addon metadata information.
- `definitions/`: contains the X-Definition yaml/cue files. These file will be rendered as KubeVela Component in `template.yaml`
- `resources/`:
  - `parameter.cue` to expose parameters. It will be converted to JSON schema and rendered in UI forms.
  - All other files will be rendered as KubeVela Components. It can be one of the two types:
    - YAML file that contains only one resource. This will be rendered as a `raw` component
    - CUE template file that can read user input as `parameter.XXX` as defined `parameter.cue`.
      Basically the CUE template file will be combined with `parameter.cue` to render a resource.
      **You can specify the type and trait in this format**
$ cat mongo-helm/template.yaml
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  creationTimestamp: null
  name: mongo-helm
  namespace: vela-system
spec:
  components: null
status: {}
File Structure and Contents (Basic)
$ vela addon init mongo-sample
Scaffold created in directory mongo-sample. What to do next:
- Check out our guide on how to build your own addon: https://kubevela.io/docs/platform-engineers/addon/intro
- Review and edit what we have generated in mongo-sample
- To enable the addon, run: vela addon enable mongo-sample
$ tree mongo-sample
mongo-sample
├── definitions
├── metadata.yaml
├── readme.md
├── resources
├── schemas
└── template.yaml

3 directories, 3 files
$ cat mongo-sample/metadata.yaml
description: An addon for KubeVela.
icon: ""
invisible: false
name: mongo-sample
version: 1.0.0
$ cat mongo-sample/template.yaml
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  creationTimestamp: null
  name: mongo-sample
  namespace: vela-system
spec:
  components: null
status: {}

Directory already exists in local filesystem

If the directory is empty, it will behave the same as above.

$ vela addon init mongodb --helm-repo-url=https://marketplace.azurecr.io/helm/v1/repo --chart=mongodb --version=12.1.16
Error: directory mongodb is not empty. To avoid any data loss, manually delete it first

File with the same name already exists or other reasons

$ vela addon init main --helm-repo-url=https://marketplace.azurecr.io/helm/v1/repo --chart=mongodb --version=12.1.16
Error: we can't create directory main. Make sure the name has not already been taken and you have the proper rights to write to it

I have:

  • Read and followed KubeVela's contribution process.
  • Related Docs updated properly. In a new feature or configuration option, an update to the documentation is necessary.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

Relevant unit tests have been added. Also manually tested several conditions described above.

Special notes for your reviewer

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
@codecov
Copy link

codecov bot commented Jun 13, 2022

Codecov Report

Merging #4162 (61418c6) into master (f978519) will increase coverage by 0.03%.
The diff coverage is 78.16%.

@@            Coverage Diff             @@
##           master    #4162      +/-   ##
==========================================
+ Coverage   60.36%   60.39%   +0.03%     
==========================================
  Files         330      335       +5     
  Lines       31799    32540     +741     
==========================================
+ Hits        19195    19654     +459     
- Misses      10093    10330     +237     
- Partials     2511     2556      +45     
Flag Coverage Δ
apiserver-unittests 34.59% <0.00%> (-0.84%) ⬇️
core-unittests 55.27% <78.16%> (+0.63%) ⬆️
e2e-multicluster-test 20.15% <0.00%> (+0.04%) ⬆️
e2e-rollout-tests 22.16% <0.00%> (-0.18%) ⬇️
e2etests 29.14% <0.00%> (-0.13%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/addon/create.go 78.04% <78.04%> (ø)
pkg/utils/file.go 62.16% <80.00%> (+6.60%) ⬆️
pkg/velaql/parse.go 65.67% <0.00%> (-26.00%) ⬇️
pkg/apiserver/domain/service/project.go 49.25% <0.00%> (-17.41%) ⬇️
pkg/workflow/hooks/data_passing.go 29.41% <0.00%> (-16.05%) ⬇️
pkg/apiserver/event/sync/cr2ux.go 44.77% <0.00%> (-15.23%) ⬇️
pkg/workflow/tasks/discover.go 68.93% <0.00%> (-13.95%) ⬇️
pkg/appfile/dryrun/dryrun.go 14.28% <0.00%> (-6.17%) ⬇️
pkg/resourcekeeper/resourcekeeper.go 69.69% <0.00%> (-4.50%) ⬇️
pkg/cue/definition/template.go 62.80% <0.00%> (-2.80%) ⬇️
... and 63 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f978519...61418c6. Read the comment docs.

pkg/addon/create.go Outdated Show resolved Hide resolved
pkg/addon/create.go Outdated Show resolved Hide resolved
pkg/addon/create.go Outdated Show resolved Hide resolved
pkg/addon/create.go Outdated Show resolved Hide resolved
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
Copy link
Collaborator

@wangyikewxgm wangyikewxgm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can support a flag -p allow user to define the target path, default is ./

@charlie0129
Copy link
Member Author

charlie0129 commented Jun 14, 2022

We can support a flag -p allow user to define the target path, default is ./

Yes, manually specifying path is supported. Just vela addon create ./some/path/to/addon-name, addon-name will be used as addon name.

return fmt.Errorf("an addon name is required. You can specify a path. It will be used as the base directory for the addon")

@wangyikewxgm
Copy link
Collaborator

We can support a flag -p allow user to define the target path, default is ./

Yes, manually specifying path is supported. Just vela addon create ./some/path/to/addon-name, addon-name will be used as addon name.

return fmt.Errorf("an addon name is required. You can specify a path. It will be used as the base directory for the addon")

IMO this is little bit tricky,

vela addon create name -p ./ 

Is better, and this is helm style.

@charlie0129
Copy link
Member Author

Is better, and this is helm style.

Well, it is good to follow existing user habits. Will develop a -p option.

@charlie0129 charlie0129 marked this pull request as draft June 14, 2022 09:44
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
@charlie0129 charlie0129 marked this pull request as ready for review June 14, 2022 10:48
pkg/addon/create.go Outdated Show resolved Hide resolved
pkg/addon/create.go Outdated Show resolved Hide resolved
pkg/addon/create.go Outdated Show resolved Hide resolved
pkg/addon/create.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@wangyikewxgm wangyikewxgm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM. Good job! Please fix the comments.

references/cli/addon.go Outdated Show resolved Hide resolved
references/cli/addon.go Outdated Show resolved Hide resolved
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
@charlie0129 charlie0129 marked this pull request as draft June 15, 2022 07:24
@wonderflow wonderflow changed the title Feat: implement addon create command Feat: implement addon init command Jun 18, 2022
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
@charlie0129 charlie0129 marked this pull request as ready for review June 23, 2022 19:02
pkg/addon/create.go Outdated Show resolved Hide resolved
vela addon init mongodb -p ./some/repo --helm-repo-url=https://marketplace.azurecr.io/helm/v1/repo --chart=mongodb --version=12.1.16

If you don't want the Helm component, just omit the three Chart-related parameters. We will create an empty scaffold for you.
vela addon init mongodb`,
Copy link
Collaborator

@wonderflow wonderflow Jun 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty scaffold should also create other addon dir for convenience, it's even better if we could put some meanful files as example in it.

https://kubevela.io/docs/platform-engineers/addon/intro#build-an-addon

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there will be a minimal example in it (required files of an addon and other directories).

File Structure and Contents (Basic)
$ vela addon init mongo-sample
Scaffold created in directory mongo-sample. What to do next:
- Check out our guide on how to build your own addon: https://kubevela.io/docs/platform-engineers/addon/intro
- Review and edit what we have generated in mongo-sample
- To enable the addon, run: vela addon enable mongo-sample
$ tree mongo-sample
mongo-sample
├── definitions
├── metadata.yaml
├── readme.md
├── resources
├── schemas
└── template.yaml

3 directories, 3 files
$ cat mongo-sample/metadata.yaml
description: An addon for KubeVela.
icon: ""
invisible: false
name: mongo-sample
version: 1.0.0
$ cat mongo-sample/readme.md
# mongo-sample

Also check how to build your own addon: https://kubevela.net/docs/platform-engineers/addon/intro

## Directory Structure

- `template.yaml`: contains the basic app, you can add some component and workflow to meet your requirements. Other files in `resources/` and `definitions/` will be rendered as Components and appended in `spec.components`
- `metadata.yaml`: contains addon metadata information.
- `definitions/`: contains the X-Definition yaml/cue files. These file will be rendered as KubeVela Component in `template.yaml`
- `resources/`:
  - `parameter.cue` to expose parameters. It will be converted to JSON schema and rendered in UI forms.
  - All other files will be rendered as KubeVela Components. It can be one of the two types:
    - YAML file that contains only one resource. This will be rendered as a `raw` component
    - CUE template file that can read user input as `parameter.XXX` as defined `parameter.cue`.
      Basically the CUE template file will be combined with `parameter.cue` to render a resource.
      **You can specify the type and trait in this format**
$ cat mongo-sample/template.yaml
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  creationTimestamp: null
  name: mongo-sample
  namespace: vela-system
spec:
  components: null
status: {}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
Copy link
Collaborator

@wangyikewxgm wangyikewxgm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great pr!

Copy link
Collaborator

@Somefive Somefive left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants