Skip to content

Commit 94e9820

Browse files
feat: Create initial version of the plugin (#1)
1 parent 2259ff9 commit 94e9820

34 files changed

+4241
-1
lines changed

.e2e/chainsaw-tests.yaml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
apiVersion: chainsaw.kyverno.io/v1alpha1
2+
kind: Test
3+
metadata:
4+
name: sm-with-ns-selector
5+
spec:
6+
steps:
7+
- try:
8+
- assert:
9+
resource:
10+
apiVersion: monitoring.coreos.com/v1
11+
kind: ServiceMonitor
12+
metadata:
13+
name: sm-with-ns-selector-x-default-x-vcluster
14+
namespace: default
15+
spec:
16+
namespaceSelector: {}
17+
selector:
18+
matchExpressions:
19+
- key: vcluster.loft.sh/namespace
20+
operator: In
21+
values:
22+
- my-virtual-namespace
23+
matchLabels:
24+
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
25+
---
26+
apiVersion: chainsaw.kyverno.io/v1alpha1
27+
kind: Test
28+
metadata:
29+
name: sm-without-ns-selector
30+
spec:
31+
steps:
32+
- try:
33+
- assert:
34+
resource:
35+
apiVersion: monitoring.coreos.com/v1
36+
kind: ServiceMonitor
37+
metadata:
38+
name: sm-without-ns-selector-x-default-x-vcluster
39+
namespace: default
40+
spec:
41+
namespaceSelector: {}
42+
selector:
43+
# Resources are deployed to default namespace of vcluster
44+
matchExpressions:
45+
- key: vcluster.loft.sh/namespace
46+
operator: In
47+
values:
48+
- default
49+
matchLabels:
50+
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
51+
---
52+
apiVersion: chainsaw.kyverno.io/v1alpha1
53+
kind: Test
54+
metadata:
55+
name: pm-with-ns-selector
56+
spec:
57+
steps:
58+
- try:
59+
- assert:
60+
resource:
61+
apiVersion: monitoring.coreos.com/v1
62+
kind: PodMonitor
63+
metadata:
64+
name: pm-with-ns-selector-x-default-x-vcluster
65+
namespace: default
66+
spec:
67+
namespaceSelector: {}
68+
selector:
69+
matchExpressions:
70+
- key: vcluster.loft.sh/namespace
71+
operator: In
72+
values:
73+
- my-virtual-namespace
74+
matchLabels:
75+
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
76+
---
77+
apiVersion: chainsaw.kyverno.io/v1alpha1
78+
kind: Test
79+
metadata:
80+
name: pm-without-ns-selector
81+
spec:
82+
steps:
83+
- try:
84+
- assert:
85+
resource:
86+
apiVersion: monitoring.coreos.com/v1
87+
kind: PodMonitor
88+
metadata:
89+
name: pm-without-ns-selector-x-default-x-vcluster
90+
namespace: default
91+
spec:
92+
namespaceSelector: {}
93+
selector:
94+
matchExpressions:
95+
- key: vcluster.loft.sh/namespace
96+
operator: In
97+
values:
98+
- default
99+
matchLabels:
100+
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app

.e2e/vcluster-resources.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
apiVersion: monitoring.coreos.com/v1
2+
kind: ServiceMonitor
3+
metadata:
4+
name: sm-with-ns-selector
5+
namespace: default
6+
labels:
7+
team: frontend
8+
spec:
9+
namespaceSelector:
10+
any: false
11+
matchNames:
12+
- my-virtual-namespace
13+
selector:
14+
matchLabels:
15+
app: example-app
16+
endpoints:
17+
- port: web
18+
---
19+
apiVersion: monitoring.coreos.com/v1
20+
kind: ServiceMonitor
21+
metadata:
22+
name: sm-without-ns-selector
23+
namespace: default
24+
labels:
25+
team: frontend
26+
spec:
27+
namespaceSelector: {}
28+
selector:
29+
matchLabels:
30+
app: example-app
31+
endpoints:
32+
- port: web
33+
---
34+
apiVersion: monitoring.coreos.com/v1
35+
kind: PodMonitor
36+
metadata:
37+
name: pm-with-ns-selector
38+
namespace: default
39+
labels:
40+
team: frontend
41+
spec:
42+
namespaceSelector:
43+
any: false
44+
matchNames:
45+
- my-virtual-namespace
46+
selector:
47+
matchLabels:
48+
app: example-app
49+
---
50+
apiVersion: monitoring.coreos.com/v1
51+
kind: PodMonitor
52+
metadata:
53+
name: pm-without-ns-selector
54+
namespace: default
55+
labels:
56+
team: frontend
57+
spec:
58+
namespaceSelector: {}
59+
selector:
60+
matchLabels:
61+
app: example-app

.github/release-drafter.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name-template: "v$RESOLVED_VERSION"
2+
tag-template: "v$RESOLVED_VERSION"
3+
categories:
4+
- title: "🚀 Features"
5+
labels:
6+
- "feature"
7+
- "enhancement"
8+
- title: "🐛 Bug Fixes"
9+
labels:
10+
- "fix"
11+
- "bugfix"
12+
- "bug"
13+
- title: "🧰 Maintenance"
14+
label: "chore"
15+
change-template: "- $TITLE @$AUTHOR (#$NUMBER)"
16+
change-title-escapes: '\<*_&'
17+
exclude-labels:
18+
- "no-release"
19+
version-resolver:
20+
major:
21+
labels:
22+
- "major"
23+
minor:
24+
labels:
25+
- "minor"
26+
- "feature"
27+
- "enhancement"
28+
patch:
29+
labels:
30+
- "patch"
31+
- "fix"
32+
- "bugfix"
33+
- "bug"
34+
- "dependencies"
35+
default: patch
36+
template: |
37+
## Changes
38+
$CHANGES

.github/workflows/draft-release.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Draft Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
types: [opened, reopened, synchronize]
9+
10+
jobs:
11+
update_release_draft:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: release-drafter/release-drafter@v5
15+
env:
16+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17+
with:
18+
publish: false
19+
config-name: release-drafter.yaml

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
.devspace/

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
# vcluster-prometheus-operator-plugin
1+
# vCluster Prometheus operator plugin
2+
3+
[![Codefresh build status]( https://g.codefresh.io/api/badges/pipeline/codefresh-inc/codefresh-contrib%2Fvcluster-prometheus-operator-plugin%2Fci?type=cf-1)]( https://g.codefresh.io/public/accounts/codefresh-inc/pipelines/new/667d09d7db9387f8b1294868)
4+
5+
This repository contains a [vCluster plugin](https://www.vcluster.com/docs/v0.19/advanced-topics/plugins-overview) that syncs [Prometheus operator](https://github.com/prometheus-operator/prometheus-operator) resources from virtual clusters to the host cluster.
6+
7+
Currently only the very basic functionality is implemented so the plugin only supports syncing of `PodMonitor` and `ServiceMonitor` resources. This is to allow scraping of metrics from workloads running on virtual clusters from a signle Prometheus or Open Telemetry collector on the host (with [target allocator](https://github.com/open-telemetry/opentelemetry-operator/blob/main/cmd/otel-allocator/README.md) that supports Prometheus operator CRDs).
8+
9+
The repository contains 2 versions of the plugin, each version is compatible with different versions of vCluster, but both versions provide the same functionality.
10+
This compatibility is required as Syncer arhitecture was overhauled by loft.sh in version 0.20.0 and plugin-sdk changed accordingly.
11+
12+
`v1` - Compatible with older versions of vCluster - latest confirmed & tested version is `0.16.4`
13+
14+
`v2` - Compatibe with vCluster version `0.20.0-beta.9` which was the latest version at the time of writing.

v1/.e2e/scripts/init.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
# Deploy CRDs and vcluster with the plugin, connect to vcluster and apply resources in resources folder for testing
3+
4+
PLUGIN_IMAGE=$1
5+
MYDIR=$(dirname $0)
6+
ROOT_DIR=$MYDIR/../../
7+
RESOURCES_FILE=$MYDIR/../../../.e2e/vcluster-resources.yaml
8+
9+
kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.70.0/example/prometheus-operator-crd-full/monitoring.coreos.com_podmonitors.yaml
10+
kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.70.0/example/prometheus-operator-crd-full/monitoring.coreos.com_servicemonitors.yaml
11+
helm upgrade --install --repo https://charts.loft.sh vcluster vcluster --version 0.16.4 --values $MYDIR/vcluster-values.yaml --values $ROOT_DIR/plugin.yaml --set plugin.prometheus-operator-resources.image=$PLUGIN_IMAGE --wait
12+
13+
vcluster connect vcluster -n default -- kubectl get servicemonitor && vcluster connect vcluster -n default -- kubectl get podmonitor
14+
15+
res=$?
16+
secondsWaited=0
17+
timeout=300
18+
19+
while [ $res -ne 0 ] && [ $secondsWaited -lt $timeout ]; do
20+
echo "Waiting for CRDs to get created, sleep for 30 seconds..."
21+
sleep 30
22+
secondsWaited=$((secondsWaited + 30))
23+
vcluster connect vcluster -n default -- kubectl get servicemonitor && vcluster connect vcluster -n default -- kubectl get podmonitor
24+
res=$?
25+
done
26+
27+
if [ $res -ne 0 ]; then
28+
echo "Timed out waiting for CRDs to get created in vcluster"
29+
exit 1
30+
fi
31+
32+
cat $RESOURCES_FILE | vcluster connect vcluster -n default -- kubectl -n default apply -f -

v1/.e2e/scripts/vcluster-values.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
serviceAccount:
2+
create: false
3+
name: default
4+
vcluster:
5+
image: rancher/k3s:v1.28.2-k3s1
6+
rbac:
7+
clusterRole:
8+
create: true
9+
role:
10+
extended: true
11+
syncer:
12+
readinessProbe:
13+
enabled: false
14+
livenessProbe:
15+
enabled: false

v1/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Build the manager binary
2+
FROM golang:1.18 as builder
3+
4+
# Make sure we use go modules
5+
WORKDIR vcluster
6+
# Copy the Go Modules manifests
7+
COPY . .
8+
# Install dependencies
9+
RUN go mod vendor
10+
# Build cmd
11+
RUN CGO_ENABLED=0 GO111MODULE=on go build -mod vendor -o /plugin main.go
12+
13+
# we use alpine for easier debugging
14+
FROM alpine
15+
16+
# Set root path as working directory
17+
WORKDIR /
18+
19+
COPY --from=builder /plugin .
20+
21+
ENTRYPOINT ["/plugin"]

v1/Dockerfile.dev

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang:1.18 as builder
2+
3+
WORKDIR /plugin
4+
5+
# Install Delve for debugging
6+
RUN go install github.com/go-delve/delve/cmd/dlv@latest
7+
8+
ENV GO111MODULE on
9+
ENV DEBUG true
10+
11+
ENTRYPOINT ["sleep", "999999999999"]

v1/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## Development
2+
3+
Before starting to develop, make sure you have installed the following tools on your computer:
4+
- [docker](https://docs.docker.com/)
5+
- [kubectl](https://kubernetes.io/docs/tasks/tools/) with a valid kube context configured
6+
- [helm](https://helm.sh/docs/intro/install/), which is used to deploy vcluster and the plugin
7+
- [vcluster CLI](https://www.vcluster.com/docs/getting-started/setup) v0.6.0 or higher
8+
- [DevSpace](https://devspace.sh/cli/docs/quickstart), which is used to spin up a development environment
9+
10+
If you want to develop within a remote Kubernetes cluster (as opposed to docker-desktop or minikube), make sure to exchange `PLUGIN_IMAGE` in the `devspace.yaml` with a valid registry path you can push to.
11+
12+
After successfully setting up the tools, start the development environment with:
13+
```
14+
devspace dev -n vcluster
15+
```
16+
17+
After a while a terminal should show up with additional instructions. Enter the following command to start the plugin:
18+
```
19+
go run -mod vendor main.go
20+
```
21+
22+
The output should look something like this:
23+
```
24+
I0124 11:20:14.702799 4185 logr.go:249] plugin: Try creating context...
25+
I0124 11:20:14.730044 4185 logr.go:249] plugin: Waiting for vcluster to become leader...
26+
I0124 11:20:14.731097 4185 logr.go:249] plugin: Starting syncers...
27+
[...]
28+
I0124 11:20:15.957331 4185 logr.go:249] plugin: Successfully started plugin.
29+
```
30+
31+
You can now change a file locally in your IDE and then restart the command in the terminal to apply the changes to the plugin.
32+
33+
Delete the development environment with:
34+
```
35+
devspace purge -n vcluster
36+
```
37+
38+
## Using the Plugin in vcluster
39+
40+
### Building the Plugin
41+
To just build the plugin image and push it to the registry, run:
42+
```
43+
# Build
44+
docker build . -t my-repo/my-plugin:0.0.1
45+
46+
# Push
47+
docker push my-repo/my-plugin:0.0.1
48+
```
49+
50+
### Using the plugin
51+
52+
To use the plugin, create a new vcluster with the `plugin.yaml`:
53+
54+
```
55+
# Use local plugin.yaml
56+
vcluster create my-vcluster -n my-vcluster -f ./plugin.yaml
57+
58+
# Use public plugin.yaml
59+
vcluster create my-vcluster -n my-vcluster -f https://raw.githubusercontent.com/codefresh-contrib/vcluster-prometheus-operator-plugin/main/v1/plugin.yaml
60+
```
61+
62+
This will create a new vcluster with the plugin installed.

0 commit comments

Comments
 (0)