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: add apply terraform example yaml #95

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ Please refer to the following examples:

- [Control the delivery process of multiple resources(e.g. your Applications)](./examples/multiple-apps.md)
- [Request a specified URL and then use the response as a message to notify](./examples/request-and-notify.md)
- [Automatically initialize the environment with terraform](./examples/initialize-env.md)

### Run a WorkflowRun from a Workflow Template

Please refer to the following examples:

- [Run the Workflow Template with different context to control the process](./examples/run-with-template)
- [Run the Workflow Template with different context to control the process](./examples/run-with-template.md)

<h2 align="center">Features</h2>

Expand Down
109 changes: 109 additions & 0 deletions examples/initialize-env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Automatically initialize the environment with terraform

> Note: The example uses the following definitions, please make sure the following definitions are installed. If not, you can use `vela def apply <filename>` to install them first.
> - [Definition `apply-terraform-provider`](https://github.com/kubevela/catalog/blob/master/addons/vela-workflow/definitions/apply-terraform-provider.cue)
> - [Definition `apply-terraform-config`](https://github.com/kubevela/catalog/blob/master/addons/vela-workflow/definitions/apply-terraform-config.cue)
> - [Definition `vela-cli`](https://github.com/kubevela/catalog/blob/master/addons/vela-workflow/definitions/veka-cli.cue)
> - [Definition `clean-jobs`](https://github.com/kubevela/catalog/blob/master/addons/vela-workflow/definitions/clean-jobs.cue)
> - [Definition `apply-object`](https://github.com/kubevela/kubevela/blob/master/vela-templates/definitions/internal/workflowstep/apply-object.cue)

You can use Workflow together with Terraform to initialize your environment automatically.

> Note: please make sure that you have enabled the KubeVela Terraform Addon first:
> ```bash
> vela addon enable terraform
> # supported: terraform-alibaba/terraform-aws/terraform-azure/terraform-baidu/terraform-ec/terraform-gcp/terraform-tencent/terraform-ucloud
> vela addon enable terraform-<cloud name>
> ```

For example, use the cloud provider to create a cluster first, then add this cluster to the management of kubevela workflow. After that, deploy a configmap in the newly created cluster to initialize the environment. Let's take Alibaba Cloud Kubernetes cluster as an example:

Apply the following YAML:

```yaml
apiVersion: core.oam.dev/v1alpha1
kind: WorkflowRun
metadata:
name: apply-terraform-resource
namespace: default
spec:
workflowSpec:
steps:
# initialize the terraform provider with credential first
- name: provider
type: apply-terraform-provider
properties:
type: alibaba
name: my-alibaba-provider
accessKey: <accessKey>
secretKey: <secretKey>
region: cn-hangzhou
# create a ACK cluster with terraform
- name: configuration
type: apply-terraform-config
properties:
source:
# you can choose to use remote tf or specify the hcl directly
# hcl: <your hcl>
path: alibaba/cs/dedicated-kubernetes
remote: https://github.com/FogDong/terraform-modules
providerRef:
name: my-alibaba-provider
writeConnectionSecretToRef:
name: my-terraform-secret
namespace: vela-system
variable:
name: regular-check-ack
new_nat_gateway: true
vpc_name: "tf-k8s-vpc-regular-check"
vpc_cidr: "10.0.0.0/8"
vswitch_name_prefix: "tf-k8s-vsw-regualr-check"
vswitch_cidrs: [ "10.1.0.0/16", "10.2.0.0/16", "10.3.0.0/16" ]
k8s_name_prefix: "tf-k8s-regular-check"
k8s_version: 1.24.6-aliyun.1
k8s_pod_cidr: "192.168.5.0/24"
k8s_service_cidr: "192.168.2.0/24"
k8s_worker_number: 2
cpu_core_count: 4
memory_size: 8
tags:
created_by: "Terraform-of-KubeVela"
created_from: "module-tf-alicloud-ecs-instance"
# add the newly created cluster to the management of kubevela workflow with vela cli
- name: add-cluster
type: vela-cli
properties:
storage:
secret:
- name: secret-mount
secretName: my-terraform-secret
mountPath: /kubeconfig/ack
command:
- vela
- cluster
- join
- /kubeconfig/ack/KUBECONFIG
- --name=ack
# clean the execution job
- name: clean-cli-jobs
type: clean-jobs
if: always
properties:
labelSelector:
"workflowrun.oam.dev/step-name": apply-terraform-resource-add-cluster
# apply the configmap in the created cluster
- name: distribute-config
type: apply-object
properties:
cluster: ack
value:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-cm
namespace: default
data:
test-key: test-value
```

In the workflow, the first step will create a terraform provider with your credentials, after that, it will create the terraform resource. Then, it will throw a job to execute vela command -- add the cluster to the management of vela, and clean the job after it is finished. Finally, it will create a config map in the created cluster.
2 changes: 1 addition & 1 deletion examples/multiple-apps.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Control the delivery process of multiple applications

> Note: The example uses following definitions, please use `vela def apply -f <filename>` to install them first.
> Note: The example uses the following definitions, please make sure the following definitions are installed. If not, you can use `vela def apply <filename>` to install them first.
> - [Definition `read-app`](https://github.com/kubevela/catalog/blob/master/addons/vela-workflow/definitions/read-app.cue)
> - [Definition `apply-app`](https://github.com/kubevela/catalog/blob/master/addons/vela-workflow/definitions/apply-app.cue)

Expand Down
2 changes: 1 addition & 1 deletion examples/request-and-notify.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Use Workflow for request and notify

> Note: The example uses following definitions, please use `vela def apply -f <filename>` to install them first.
> Note: The example uses the following definitions, please make sure the following definitions are installed. If not, you can use `vela def apply <filename>` to install them first.
> - [Definition `request`](https://github.com/kubevela/catalog/blob/master/addons/vela-workflow/definitions/request.cue)
> - [Definition `notification`](https://github.com/kubevela/kubevela/blob/master/vela-templates/definitions/internal/workflowstep/notification.cue)

Expand Down
2 changes: 1 addition & 1 deletion examples/run-with-template.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Run your workflow with template

> Note: The example uses following definitions, please use `vela def apply -f <filename>` to install them first.
> Note: The example uses the following definitions, please make sure the following definitions are installed. If not, you can use `vela def apply <filename>` to install them first.
> - [Definition `apply-deployment`](https://github.com/kubevela/catalog/blob/master/addons/vela-workflow/definitions/apply-deployment.cue)

You can also create a Workflow Template and run it with a WorkflowRun with different context.
Expand Down
76 changes: 76 additions & 0 deletions examples/workflow-run/apply-terraform-resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
apiVersion: core.oam.dev/v1alpha1
kind: WorkflowRun
metadata:
name: apply-terraform-resource
namespace: default
spec:
workflowSpec:
steps:
- name: provider
type: apply-terraform-provider
properties:
type: alibaba
name: my-alibaba-provider
accessKey: <accessKey>
secretKey: <secretKey>
region: cn-hangzhou
- name: configuration
type: apply-terraform-config
properties:
source:
path: alibaba/cs/dedicated-kubernetes
remote: https://github.com/FogDong/terraform-modules
providerRef:
name: my-alibaba-provider
writeConnectionSecretToRef:
name: my-terraform-secret
namespace: vela-system
variable:
name: regular-check-ack
new_nat_gateway: true
vpc_name: "tf-k8s-vpc-regular-check"
vpc_cidr: "10.0.0.0/8"
vswitch_name_prefix: "tf-k8s-vsw-regualr-check"
vswitch_cidrs: [ "10.1.0.0/16", "10.2.0.0/16", "10.3.0.0/16" ]
k8s_name_prefix: "tf-k8s-regular-check"
k8s_version: 1.24.6-aliyun.1
k8s_pod_cidr: "192.168.5.0/24"
k8s_service_cidr: "192.168.2.0/24"
k8s_worker_number: 2
cpu_core_count: 4
memory_size: 8
tags:
created_by: "Terraform-of-KubeVela"
created_from: "module-tf-alicloud-ecs-instance"
- name: add-cluster
type: vela-cli
if: always
properties:
storage:
secret:
- name: secret-mount
secretName: my-terraform-secret
mountPath: /kubeconfig/ack
command:
- vela
- cluster
- join
- /kubeconfig/ack/KUBECONFIG
- --name=ack
- name: clean-cli-jobs
type: clean-jobs
properties:
labelSelector:
"workflowrun.oam.dev/step-name": apply-terraform-resource-add-cluster
- name: distribute-config
type: apply-object
properties:
cluster: ack
value:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-cm
namespace: default
data:
test-key: test-value