Skip to content

feat: Create initial version of the plugin #1

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

Merged
merged 11 commits into from
Jul 2, 2024
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
100 changes: 100 additions & 0 deletions .e2e/chainsaw-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: sm-with-ns-selector
spec:
steps:
- try:
- assert:
resource:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: sm-with-ns-selector-x-default-x-vcluster
namespace: default
spec:
namespaceSelector: {}
selector:
matchExpressions:
- key: vcluster.loft.sh/namespace
operator: In
values:
- my-virtual-namespace
matchLabels:
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
---
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: sm-without-ns-selector
spec:
steps:
- try:
- assert:
resource:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: sm-without-ns-selector-x-default-x-vcluster
namespace: default
spec:
namespaceSelector: {}
selector:
# Resources are deployed to default namespace of vcluster
matchExpressions:
- key: vcluster.loft.sh/namespace
operator: In
values:
- default
matchLabels:
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
---
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: pm-with-ns-selector
spec:
steps:
- try:
- assert:
resource:
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: pm-with-ns-selector-x-default-x-vcluster
namespace: default
spec:
namespaceSelector: {}
selector:
matchExpressions:
- key: vcluster.loft.sh/namespace
operator: In
values:
- my-virtual-namespace
matchLabels:
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
---
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: pm-without-ns-selector
spec:
steps:
- try:
- assert:
resource:
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: pm-without-ns-selector-x-default-x-vcluster
namespace: default
spec:
namespaceSelector: {}
selector:
matchExpressions:
- key: vcluster.loft.sh/namespace
operator: In
values:
- default
matchLabels:
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
61 changes: 61 additions & 0 deletions .e2e/vcluster-resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: sm-with-ns-selector
namespace: default
labels:
team: frontend
spec:
namespaceSelector:
any: false
matchNames:
- my-virtual-namespace
selector:
matchLabels:
app: example-app
endpoints:
- port: web
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: sm-without-ns-selector
namespace: default
labels:
team: frontend
spec:
namespaceSelector: {}
selector:
matchLabels:
app: example-app
endpoints:
- port: web
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: pm-with-ns-selector
namespace: default
labels:
team: frontend
spec:
namespaceSelector:
any: false
matchNames:
- my-virtual-namespace
selector:
matchLabels:
app: example-app
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: pm-without-ns-selector
namespace: default
labels:
team: frontend
spec:
namespaceSelector: {}
selector:
matchLabels:
app: example-app
38 changes: 38 additions & 0 deletions .github/release-drafter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name-template: "v$RESOLVED_VERSION"
tag-template: "v$RESOLVED_VERSION"
categories:
- title: "🚀 Features"
labels:
- "feature"
- "enhancement"
- title: "🐛 Bug Fixes"
labels:
- "fix"
- "bugfix"
- "bug"
- title: "🧰 Maintenance"
label: "chore"
change-template: "- $TITLE @$AUTHOR (#$NUMBER)"
change-title-escapes: '\<*_&'
exclude-labels:
- "no-release"
version-resolver:
major:
labels:
- "major"
minor:
labels:
- "minor"
- "feature"
- "enhancement"
patch:
labels:
- "patch"
- "fix"
- "bugfix"
- "bug"
- "dependencies"
default: patch
template: |
## Changes
$CHANGES
19 changes: 19 additions & 0 deletions .github/workflows/draft-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Draft Release

on:
push:
branches:
- main
pull_request:
types: [opened, reopened, synchronize]

jobs:
update_release_draft:
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
publish: false
config-name: release-drafter.yaml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
.devspace/
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# vcluster-prometheus-operator-plugin
# vCluster Prometheus operator plugin

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

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.

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

The repository contains 2 versions of the plugin, each version is compatible with different versions of vCluster, but both versions provide the same functionality.
This compatibility is required as Syncer arhitecture was overhauled by loft.sh in version 0.20.0 and plugin-sdk changed accordingly.

`v1` - Compatible with older versions of vCluster - latest confirmed & tested version is `0.16.4`

`v2` - Compatibe with vCluster version `0.20.0-beta.9` which was the latest version at the time of writing.
32 changes: 32 additions & 0 deletions v1/.e2e/scripts/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
# Deploy CRDs and vcluster with the plugin, connect to vcluster and apply resources in resources folder for testing

PLUGIN_IMAGE=$1
MYDIR=$(dirname $0)
ROOT_DIR=$MYDIR/../../
RESOURCES_FILE=$MYDIR/../../../.e2e/vcluster-resources.yaml

kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.70.0/example/prometheus-operator-crd-full/monitoring.coreos.com_podmonitors.yaml
kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.70.0/example/prometheus-operator-crd-full/monitoring.coreos.com_servicemonitors.yaml
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

vcluster connect vcluster -n default -- kubectl get servicemonitor && vcluster connect vcluster -n default -- kubectl get podmonitor

res=$?
secondsWaited=0
timeout=300

while [ $res -ne 0 ] && [ $secondsWaited -lt $timeout ]; do
echo "Waiting for CRDs to get created, sleep for 30 seconds..."
sleep 30
secondsWaited=$((secondsWaited + 30))
vcluster connect vcluster -n default -- kubectl get servicemonitor && vcluster connect vcluster -n default -- kubectl get podmonitor
res=$?
done

if [ $res -ne 0 ]; then
echo "Timed out waiting for CRDs to get created in vcluster"
exit 1
fi

cat $RESOURCES_FILE | vcluster connect vcluster -n default -- kubectl -n default apply -f -
15 changes: 15 additions & 0 deletions v1/.e2e/scripts/vcluster-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
serviceAccount:
create: false
name: default
vcluster:
image: rancher/k3s:v1.28.2-k3s1
rbac:
clusterRole:
create: true
role:
extended: true
syncer:
readinessProbe:
enabled: false
livenessProbe:
enabled: false
21 changes: 21 additions & 0 deletions v1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Build the manager binary
FROM golang:1.18 as builder

# Make sure we use go modules
WORKDIR vcluster
# Copy the Go Modules manifests
COPY . .
# Install dependencies
RUN go mod vendor
# Build cmd
RUN CGO_ENABLED=0 GO111MODULE=on go build -mod vendor -o /plugin main.go

# we use alpine for easier debugging
FROM alpine

# Set root path as working directory
WORKDIR /

COPY --from=builder /plugin .

ENTRYPOINT ["/plugin"]
11 changes: 11 additions & 0 deletions v1/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.18 as builder

WORKDIR /plugin

# Install Delve for debugging
RUN go install github.com/go-delve/delve/cmd/dlv@latest

ENV GO111MODULE on
ENV DEBUG true

ENTRYPOINT ["sleep", "999999999999"]
62 changes: 62 additions & 0 deletions v1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
## Development

Before starting to develop, make sure you have installed the following tools on your computer:
- [docker](https://docs.docker.com/)
- [kubectl](https://kubernetes.io/docs/tasks/tools/) with a valid kube context configured
- [helm](https://helm.sh/docs/intro/install/), which is used to deploy vcluster and the plugin
- [vcluster CLI](https://www.vcluster.com/docs/getting-started/setup) v0.6.0 or higher
- [DevSpace](https://devspace.sh/cli/docs/quickstart), which is used to spin up a development environment

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.

After successfully setting up the tools, start the development environment with:
```
devspace dev -n vcluster
```

After a while a terminal should show up with additional instructions. Enter the following command to start the plugin:
```
go run -mod vendor main.go
```

The output should look something like this:
```
I0124 11:20:14.702799 4185 logr.go:249] plugin: Try creating context...
I0124 11:20:14.730044 4185 logr.go:249] plugin: Waiting for vcluster to become leader...
I0124 11:20:14.731097 4185 logr.go:249] plugin: Starting syncers...
[...]
I0124 11:20:15.957331 4185 logr.go:249] plugin: Successfully started plugin.
```

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.

Delete the development environment with:
```
devspace purge -n vcluster
```

## Using the Plugin in vcluster

### Building the Plugin
To just build the plugin image and push it to the registry, run:
```
# Build
docker build . -t my-repo/my-plugin:0.0.1

# Push
docker push my-repo/my-plugin:0.0.1
```

### Using the plugin

To use the plugin, create a new vcluster with the `plugin.yaml`:

```
# Use local plugin.yaml
vcluster create my-vcluster -n my-vcluster -f ./plugin.yaml

# Use public plugin.yaml
vcluster create my-vcluster -n my-vcluster -f https://raw.githubusercontent.com/codefresh-contrib/vcluster-prometheus-operator-plugin/main/v1/plugin.yaml
```

This will create a new vcluster with the plugin installed.
Loading
Loading