Skip to content

Commit ece3f03

Browse files
embano1alexellis
authored andcommitted
Code refactoring and more events supported
- Many important vCenter events supported now - Split event handling in separate pkg - JSON key in outbound Message is now `ManagedObjectReference` for functions to check - Robust vCenter client connection/error handling - Clean shutdown via `os.Signal` - Fixed a bug where `-insecure` was never populated - Improved error handling and logging (removed panics) - More comments in the code - Dockerfiles updated - `dep ensure` was out of sync - README fixes and updates reflecting new events supported - New `docs` section for examples - Bumped Docker image version for the connector to `0.4` (until official OpenFaaS builds are used) - Updated external examples (gotagfn, pytagfn) to use new JSON key - Tested with: - vCenter 6.5 - Kubernetes: kindest/node:v1.13.3 (kind version 0.2.0-alpha) - OpenFaaS: faas-netes (Commit 07bca00) - Python demo function [pytagfn](https://github.com/embano1/pytagfn) (Commit 99e2eea) - Improvements for further PRs: - Migrate to latest openfaas-sdk version - Add code examples to repo instead personal repo - Switch to official builds in YAML - When govmomi ships govc/vcsim Docker images, use them Signed-off-by: Michael Gasch <[email protected]>
1 parent 62dc3f7 commit ece3f03

File tree

12 files changed

+483
-459
lines changed

12 files changed

+483
-459
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ RUN mkdir -p /go/src/github.com/openfaas-incubator/vcenter-connector
33
WORKDIR /go/src/github.com/openfaas-incubator/vcenter-connector
44

55
COPY vendor vendor
6+
COPY pkg pkg
67
COPY main.go .
78

89
# Run a gofmt and exclude all vendored code.

Dockerfile.armhf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ RUN mkdir -p /go/src/github.com/openfaas-incubator/vcenter-connector
33
WORKDIR /go/src/github.com/openfaas-incubator/vcenter-connector
44

55
COPY vendor vendor
6+
COPY pkg pkg
67
COPY main.go .
78

89
# Run a gofmt and exclude all vendored code.

Gopkg.lock

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 10 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -10,143 +10,21 @@ This project uses the [OpenFaaS Connector SDK](https://github.com/openfaas-incub
1010

1111
The code is under **active development** and only suitable for early adopters. For the initial version the vCenter user credentials need to be stored in plain-text in your YAML files, but this will move to using [OpenFaaS secrets](https://docs.openfaas.com/reference/secrets/) in the next version.
1212

13-
**Note:** Currently only a pre-built Docker image is available (based on [PR#11](https://github.com/openfaas-incubator/vcenter-connector/pull/11)) and only [VM events](https://code.vmware.com/doc/preview?id=4206#/doc/vim.event.VmEvent.html) are reportable for now.
13+
## Supported Events
1414

15-
## Example: vCenter Tagging Function
15+
The following event types (incl. their subtypes) are supported and can be used to trigger functions:
1616

17-
### Pre-reqs:
17+
- Alarm Event, e.g. `AlarmCreatedEvent`
18+
- Datastore Event, e.g. `DatastoreDestroyedEvent`
19+
- Host Event, e.g. `HostDisconnectedEvent`
20+
- Resource Pool Event, e.g. `ResourcePoolCreatedEvent`
21+
- VM Event, e.g. `VmPoweredOnEvent`
1822

19-
* [OpenFaaS](https://docs.openfaas.com/) running on a local or remote Kubernetes cluster (e.g. [kind](https://blog.alexellis.io/get-started-with-openfaas-and-kind/))
20-
* An installation of vCenter (tested against 6.5)
21-
* A vCenter user/service account with sufficient rights to perform the (tagging) action of the example function
22-
* `docker` to run tools like `govc` if not installed on your machine already
23-
* `git` to clone the function example
24-
* `faas-cli` to deploy the function
25-
* `kubectl` to deploy the connector
23+
For further details and naming see the [vSphere Web Services API](https://code.vmware.com/apis/358/vsphere#/doc/vim.event.Event.html) documentation.
2624

27-
**Note:** Make sure that your OpenFaaS environment can reach vCenter as the tagging function performs API calls against vCenter.
25+
## Example
2826

29-
### How it works:
30-
31-
Functions can subscribe to events in vCenter through the "topic" [annotations](https://docs.openfaas.com/reference/yaml/#function-annotations) applied through your `stack.yml` file. Based on these events a function can take action, e.g. tag a VM, run post-processing scripts, audit to an external system, etc.
32-
33-
### Get started with the vCenter Tagging Function example
34-
35-
In the following example we will subscribe to the event "vm.powered.on" by adding an annotation to our function of "vm.powered.on". The function will then add a specific tag to any VM when it is powered on.
36-
37-
**Note:** In a DRS-enabled cluster the event is called `drs...` and the example would not work as it's a different event type. If you run this example in a DRS cluster you can use `vm.powered.off` throughout the example below as a workaround.
38-
39-
1) Create a category/tag to be attached to a VM when it is powered on. Since we need the unique tag ID (i.e. vSphere URN) we will use [govc](https://github.com/vmware/govmomi/tree/master/govc) for this job. You can also use vSphere APIs (REST/SOAP) to retrieve the URN.
40-
41-
```bash
42-
# Run pre-built govc Docker image
43-
docker run --rm -it embano1/govc:0ee42d3 sh
44-
45-
# Test connection to vCenter, ignore TLS warnings
46-
export GOVC_INSECURE=true
47-
export GOVC_URL='https://vcuser:[email protected]'
48-
./govc tags.ls
49-
50-
# If the connection is successful create a demo category/tag to be used by the function
51-
./govc tags.category.create democat1
52-
urn:...
53-
./govc tags.create -c democat1 demotag1
54-
urn:vmomi:InventoryServiceTag:019c0a9e-0672-48f5-ac2a-e394669e2916:GLOBAL
55-
```
56-
2) Take a note of the `urn:...` for `demotag1` as we will need it for the next steps
57-
3) In a separate terminal download the example function
58-
59-
```bash
60-
git clone https://github.com/embano1/pytagfn
61-
cd pytagfn
62-
```
63-
64-
4) Configure the Python tagging function `stack.yaml`.
65-
66-
> **Note:** The example cloned from Github already has the annotation to subscribe to VM power on events. More details in the [README](https://github.com/embano1/pytagfn/blob/master/README.md).
67-
68-
```yaml
69-
environment:
70-
VC: vcenter.ip # FQDN/IP, must be reachable/resolvable from OpenFaaS
71-
VC_USERNAME: VCUSER # WIP: migration to secrets
72-
VC_PASSWORD: VCPASSWORD # WIP: migration to secrets
73-
# Replace TAG_URN example below with the one you created with govc above
74-
TAG_URN: urn:vmomi:InventoryServiceTag:019c0a9e-0672-48f5-ac2a-e394669e2916:GLOBAL
75-
TAG_ACTION: attach # this function also supports detach
76-
```
77-
78-
5) Deploy the function
79-
80-
```bash
81-
faas-cli deploy
82-
Deploying: pytag-fn.
83-
84-
Deployed. 202 Accepted.
85-
URL: http://127.0.0.1:8080/function/pytag-fn
86-
```
87-
88-
6) Download and deploy the OpenFaaS vCenter Connector deployment manifest in a separate
89-
90-
> **Note:** The deployment assumes you have basic authentication configured for OpenFaaS on Kubernetes as per [this guide](https://github.com/openfaas/faas-netes/blob/67f61a468bc73833e53b626fa5243f5d539a9e00/yaml/README.md#L5). Thus, the deployment assumes a secret `gateway-basic-auth` to be available (`volumes` section in the YAML). If you don't use authentication for the gateway, remove the volumes section as the deployment would fail, not being able to mount the secret to the deployment.
91-
92-
```bash
93-
git clone https://github.com/openfaas-incubator/vcenter-connector
94-
cd vcenter-connector
95-
96-
# In yaml/kubernetes/connector-dep.yml modify the container args "-vcenter" (following URL scheme), "-vc-user" and "-vc-pass" accordingly
97-
# Note: If you are not running your vCenter connector in the same cluster as OpenFaaS edit the -gateway flag
98-
99-
# Deploy the connector to Kubernetes
100-
kubectl -n openfaas create -f yaml/kubernetes/connector-dep.yml
101-
102-
# Check the logs of the pod whether it connected successfully to vCenter and OpenFaaS
103-
kubectl -n openfaas logs deploy/vcenter-connector -f
104-
```
105-
106-
7) Generate a "Power On" event
107-
108-
In the next step we will power on a VM to trigger an event in vCenter ("VM powered on..."). This event will be received by the connector and forwarded to the OpenFaaS function subscribed to the corresponding event type (`vm.powered.on`). The function will then add the tag we created above (`demotag1`) to the VM.
109-
110-
```bash
111-
# Note: This can be done in vCenter UI or via govc
112-
# Pick a VM that is powered off and does not have already "demotag1", then in the running govc container power on the VM
113-
./govc vm.power -on /Datacenter-North/vm/Nested-Pod/vesxi67-2
114-
115-
# Verify that the tag was correctly attached
116-
./govc tags.attached.ls demotag1
117-
VirtualMachine:vm-267
118-
```
119-
120-
### Troubleshooting
121-
122-
If your VM did not get the tag attached, verify:
123-
124-
- vCenter IP/username/password
125-
- Permissions of the vCenter user
126-
- Whether the components can talk to each other (connector to vCenter and OpenFaaS, function to vCenter)
127-
- Check the logs:
128-
129-
```bash
130-
kubectl -n openfaas logs vcenter-connector-<...> -f
131-
132-
# Successful log message in the OpenFaaS vCenter connector
133-
2019/01/25 23:39:09 Message on topic: vm.powered.on
134-
2019/01/25 23:39:09 Invoke function: pytag-fn
135-
2019/01/25 23:39:10 Response [200] from pytag-fn
136-
```
137-
- Enable debugging in the function with `write_debug` and `read_debug` env vars in `stack.yaml`
138-
139-
```bash
140-
kubectl -n openfaas-fn logs pytag-fn-<...> -f
141-
142-
# Successful log message in the OpenFaaS tagging function
143-
2019/01/25 23:48:55 Forking fprocess.
144-
2019/01/25 23:48:55 Query
145-
2019/01/25 23:48:55 Path /
146-
147-
{"status": "200", "message": "successfully attached tag on VM: vm-267"}
148-
2019/01/25 23:48:56 Duration: 1.551482 seconds
149-
```
27+
You can find a detailed example using vSphere tags for `VmPoweredOnEvent` [here](docs/example.md).
15028

15129
## License
15230

docs/example.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
## Example: vCenter Tagging Function
2+
3+
### Pre-reqs:
4+
5+
* [OpenFaaS](https://docs.openfaas.com/) running on a local or remote Kubernetes cluster (e.g. [kind](https://blog.alexellis.io/get-started-with-openfaas-and-kind/))
6+
* An installation of vCenter (tested against 6.5) or the vCenter simulator [vcsim](https://github.com/vmware/govmomi/tree/master/vcsim)
7+
* A vCenter user/service account with sufficient rights to perform the (tagging) action of the example function
8+
* `docker` to run tools like `govc` if not installed on your machine already
9+
* `git` to clone the function example
10+
* `faas-cli` to deploy the function
11+
* `kubectl` to deploy the connector
12+
13+
**Note:** Make sure that your OpenFaaS environment can reach vCenter as the tagging function performs API calls against vCenter.
14+
15+
### How it works:
16+
17+
Functions can subscribe to events in vCenter through the "topic" [annotations](https://docs.openfaas.com/reference/yaml/#function-annotations) applied through your `stack.yml` file. Based on these events a function can take action, e.g. tag a VM, run post-processing scripts, audit to an external system, etc.
18+
19+
For example, a `VmPoweredOnEvent` from vCenter would have a function annotation `vm.powered.on`.
20+
21+
### Get started with the vCenter Tagging Function example
22+
23+
In the following example we will subscribe to the `VmPoweredOnEvent` by adding an annotation to our function of `vm.powered.on`. The function will then add a specific tag to any VM when it is powered on.
24+
25+
**Note:** In a DRS-enabled cluster the annotation would be `drs.vm.powered.on`.
26+
27+
1) Create a category/tag to be attached to a VM when it is powered on. Since we need the unique tag ID (i.e. vSphere URN) we will use [govc](https://github.com/vmware/govmomi/tree/master/govc) for this job. You can also use vSphere APIs (REST/SOAP) to retrieve the URN.
28+
29+
```bash
30+
# Run pre-built govc Docker image if you don't have govc already installed on your machine
31+
docker run --rm -it embano1/govc:0ee42d3 sh
32+
33+
# Test connection to vCenter, ignore TLS warnings
34+
export GOVC_INSECURE=true
35+
export GOVC_URL='https://vcuser:[email protected]'
36+
./govc tags.ls
37+
38+
# If the connection is successful create a demo category/tag to be used by the function
39+
./govc tags.category.create democat1
40+
urn:...
41+
./govc tags.create -c democat1 demotag1
42+
urn:vmomi:InventoryServiceTag:019c0a9e-0672-48f5-ac2a-e394669e2916:GLOBAL
43+
```
44+
2) Take a note of the `urn:...` for `demotag1` as we will need it for the next steps
45+
3) In a separate terminal download the example function
46+
47+
```bash
48+
git clone https://github.com/embano1/pytagfn
49+
cd pytagfn
50+
```
51+
52+
4) Configure the Python tagging function `stack.yaml`.
53+
54+
```yaml
55+
environment:
56+
# FQDN/IP<:PORT>, must be reachable/resolvable from OpenFaaS. If port is != 443 please specify (e.g. "vcsim.openfaas:8989")
57+
VC: vcenter.ip
58+
VC_USERNAME: VCUSER # WIP: migration to secrets
59+
VC_PASSWORD: VCPASSWORD # WIP: migration to secrets
60+
61+
# Replace TAG_URN example below with the one you created with govc above
62+
TAG_URN: urn:vmomi:InventoryServiceTag:019c0a9e-0672-48f5-ac2a-e394669e2916:GLOBAL
63+
TAG_ACTION: attach # alternatively, this Python function also supports detach
64+
65+
# Enable debugging in case something goes wrong
66+
write_debug: true
67+
read_debug: true
68+
69+
annotations:
70+
topic: vm.powered.on
71+
```
72+
73+
5) Deploy the function
74+
75+
```bash
76+
faas-cli template pull
77+
faas-cli deploy
78+
Deploying: pytag-fn.
79+
80+
Deployed. 202 Accepted.
81+
URL: http://127.0.0.1:8080/function/pytag-fn
82+
```
83+
84+
6) Download and deploy the OpenFaaS vCenter Connector
85+
86+
> **Note:** The deployment assumes you have basic authentication configured for OpenFaaS on Kubernetes as per [this guide](https://github.com/openfaas/faas-netes/blob/67f61a468bc73833e53b626fa5243f5d539a9e00/yaml/README.md#L5). Thus, the deployment requires a secret `gateway-basic-auth` to be available (`volumes` section in the YAML).
87+
>
88+
> If you **don't** use authentication for the gateway, edit `yaml/kubernetes/connector-dep.yml` as per instructions in the file.
89+
90+
```bash
91+
git clone https://github.com/openfaas-incubator/vcenter-connector
92+
cd vcenter-connector
93+
94+
# In yaml/kubernetes/connector-dep.yml modify the container args "-vcenter" (incl. protocol, i.e. "https://"), "-vc-user" and "-vc-pass" accordingly. If vCenter port != 443, please specify (e.g. "https://vcsim.openfaas:8989").
95+
96+
# Deploy the connector to Kubernetes
97+
kubectl -n openfaas create -f yaml/kubernetes/connector-dep.yml
98+
99+
# Check the logs of the pod whether it connected successfully to vCenter and OpenFaaS
100+
kubectl -n openfaas logs deploy/vcenter-connector -f
101+
```
102+
103+
> Note: If you are not running your vCenter connector in the same cluster as OpenFaaS also edit the `-gateway` flag in the connector deployment manifest accordingly (default: `http://gateway.openfaas:8080`, i.e. OpenFaaS in a Kubernetes namespace `openfaas`).
104+
105+
7) Generate a "Power On" event
106+
107+
In the next step we will power on a VM to trigger an event in vCenter ("VM powered on..."). This event will be received by the connector and forwarded to the OpenFaaS function(s) subscribed to the corresponding event type (`vm.powered.on`). The function will then add the tag we created above (`demotag1`) to the VM.
108+
109+
```bash
110+
# Note: This can be done in vCenter UI or via govc
111+
# Pick a VM that is powered off and does not have already "demotag1", then in the running govc container power on the VM
112+
./govc vm.power -on /Datacenter-North/vm/Nested-Pod/vesxi67-2
113+
114+
# Verify that the tag was correctly attached
115+
./govc tags.attached.ls demotag1
116+
VirtualMachine:vm-267
117+
```
118+
119+
### Troubleshooting
120+
121+
If your VM did not get the tag attached, verify:
122+
123+
- vCenter IP/username/password
124+
- Permissions of the vCenter user
125+
- Whether the components can talk to each other (connector to vCenter and OpenFaaS, function to vCenter)
126+
- Check the logs:
127+
128+
```bash
129+
kubectl -n openfaas logs deploy/vcenter-connector -f
130+
131+
# Successful log message in the OpenFaaS vCenter connector
132+
2019/01/25 23:39:09 Message on topic: vm.powered.on
133+
2019/01/25 23:39:09 Invoke function: pytag-fn
134+
2019/01/25 23:39:10 Response [200] from pytag-fn
135+
```
136+
137+
```bash
138+
kubectl -n openfaas-fn logs deploy/pytag-fn -f
139+
140+
# Successful log message in the OpenFaaS tagging function
141+
2019/01/25 23:48:55 Forking fprocess.
142+
2019/01/25 23:48:55 Query
143+
2019/01/25 23:48:55 Path /
144+
145+
{"status": "200", "message": "successfully attached tag on VM: vm-267"}
146+
2019/01/25 23:48:56 Duration: 1.551482 seconds
147+
```

0 commit comments

Comments
 (0)