Skip to content

major: Support for vCluster 0.21.0 and up. Dropping support for previous versions. #5

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 19 commits into from
Mar 11, 2025
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
8 changes: 4 additions & 4 deletions .e2e/chainsaw-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec:
values:
- my-virtual-namespace
matchLabels:
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
app: example-app
---
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
Expand All @@ -47,7 +47,7 @@ spec:
values:
- default
matchLabels:
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
app: example-app
---
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
Expand All @@ -72,7 +72,7 @@ spec:
values:
- my-virtual-namespace
matchLabels:
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
app: example-app
---
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
Expand All @@ -97,4 +97,4 @@ spec:
values:
- default
matchLabels:
vcluster.loft.sh/label-vcluster-x-a172cedcae: example-app
app: example-app
2 changes: 1 addition & 1 deletion v2/.e2e/scripts/init.sh → .e2e/scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
PLUGIN_IMAGE=$1
MYDIR=$(dirname $0)
ROOT_DIR=$MYDIR/../../
RESOURCES_FILE=$MYDIR/../../../.e2e/vcluster-resources.yaml
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
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# syntax=docker.io/docker/dockerfile-upstream:1.14.1-labs

# Build the manager binary
FROM golang:1.23.2 AS builder
WORKDIR /vcluster

# Copy the Go Modules manifests
COPY go.mod go.sum /vcluster/

# Install dependencies
RUN go mod download

# Copy the sources
COPY --parents pkg main.go /vcluster/

# Build plugin
RUN CGO_ENABLED=0 go build -o /plugin main.go

# we use alpine for easier debugging
FROM alpine
WORKDIR /plugin
COPY --from=builder /plugin .
81 changes: 77 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,82 @@ This repository contains a [vCluster plugin](https://www.vcluster.com/docs/v0.19

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.
This plugin is compatibe with vCluster version `0.21.0` and above, tested on `v0.23.0`, which was latest at the time of writing this plugin.

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

`v2` - Compatibe with vCluster version `0.20.0-beta.9` which was the latest version at the time of writing.
For more information how to develop plugins in vcluster, please refer to the [official vcluster docs](https://www.vcluster.com/docs/plugins/overview).

## Using the Plugin in vcluster

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

```
# Deploy Prometheus operator on host cluster with Helm:
For more info see -https://artifacthub.io/packages/helm/prometheus-community/kube-prometheus-stack?modal=install

# Create vcluster with plugin
vcluster create my-vcluster -n my-vcluster -f https://raw.githubusercontent.com/codefresh-contrib/vcluster-prometheus-operator-plugin/main/v2/plugin.yaml
```

This will create a new vcluster with the plugin installed. Then test the plugin with:

```
# Apply example ServicerMonitor
vcluster connect my-vcluster -n my-vcluster -- kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/main/example/user-guides/getting-started/example-app-service-monitor.yaml

# Check if car got correctly synced
kubectl get servicemonitor -n my-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
```

Then exchange the image in the `plugin.yaml`

## Development Process

General vcluster plugin project structure:
```
.
├── go.mod # Go module definition
├── go.sum
├── devspace.yaml # Development environment definition
├── devspace_start.sh # Development entrypoint script
├── Dockerfile # Production Dockerfile
├── main.go # Go Entrypoint
├── plugin.yaml # Plugin Helm Values
├── syncers/ # Plugin Syncers
└── manifests/ # Additional plugin resources
```

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.20.0 or higher
- [DevSpace](https://devspace.sh/cli/docs/quickstart), which is used to spin up a development environment

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 build -mod vendor -o plugin main.go && /vcluster/syncer start
```

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
```
75 changes: 19 additions & 56 deletions codefresh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,95 +43,59 @@ steps:
on:
- success

build_v1:
title: "Build v1"
type: build
stage: "build"
working_directory: '${{clone}}/v1'
build_arguments:
- BUILDKIT_INLINE_CACHE=1
image_name: "${{CF_REPO_NAME}}"
tag: "v1-${{IMAGE_TAG}}"
registry: "ghcr-codefresh-contrib"
platform: 'linux/amd64,linux/arm64'
buildx:
builder:
driver_opts: "image=moby/buildkit:v0.14.1"
cache_from:
- ghcr.io/${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}:v1-${{IMAGE_TAG}}
- ghcr.io/${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}:v1-main
when:
steps:
- name: clone
on:
- success

build_v2:
title: "Build v2"
build:
title: "Build image"
type: build
stage: "build"
build_arguments:
- BUILDKIT_INLINE_CACHE=1
working_directory: '${{clone}}/v2'
working_directory: '${{clone}}'
image_name: "${{CF_REPO_NAME}}"
tag: "v2-${{IMAGE_TAG}}"
tag: "${{IMAGE_TAG}}"
registry: "ghcr-codefresh-contrib"
platform: 'linux/amd64,linux/arm64'
buildx:
builder:
driver_opts: "image=moby/buildkit:v0.14.1"
cache_from:
- ghcr.io/${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}:v2-${{IMAGE_TAG}}
- ghcr.io/${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}:v2-main
- ghcr.io/${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}:${{IMAGE_TAG}}
- ghcr.io/${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}:main
when:
steps:
- name: clone
on:
- success
- name: build_v1
on:
- success

create_clusters:
create_cluster:
title: 'Create k3d clusters'
stage: test
type: freestyle
image: 'ghcr.io/k3d-io/k3d:5.5.2-dind'
commands:
- export NETWORK=$(docker network ls | grep bridge | tail -1 | awk '{print $2}')
- export FULL_CLUSTER_NAME_V1="k3d-test-prom-vcluster-plugin-v1"
- export FULL_CLUSTER_NAME_V2="k3d-test-prom-vcluster-plugin-v2"
- export FULL_CLUSTER_NAME="k3d-test-prom-vcluster-plugin"
- export CLUSTER_IMAGE="rancher/k3s:v1.24.4-k3s1"
- k3d cluster create $FULL_CLUSTER_NAME_V1 --network $NETWORK -i $CLUSTER_IMAGE --no-lb
- k3d cluster create $FULL_CLUSTER_NAME_V2 --network $NETWORK -i $CLUSTER_IMAGE --no-lb
- export CLUSTER_IP_V1=$(docker inspect k3d-$FULL_CLUSTER_NAME_V1-server-0 | jq -r '.[0].NetworkSettings.IPAddress')
- export CLUSTER_IP_V2=$(docker inspect k3d-$FULL_CLUSTER_NAME_V2-server-0 | jq -r '.[0].NetworkSettings.IPAddress')
- yq e -i '(.clusters[] | select(.name=="k3d-"+env(FULL_CLUSTER_NAME_V1)) | .cluster.server) = "https://"+env(CLUSTER_IP_V1)+":6443"' $KUBECONFIG
- yq e -i '(.clusters[] | select(.name=="k3d-"+env(FULL_CLUSTER_NAME_V2)) | .cluster.server) = "https://"+env(CLUSTER_IP_V2)+":6443"' $KUBECONFIG
- export V1_KUBE_CONTEXT=k3d-$FULL_CLUSTER_NAME_V1
- export V2_KUBE_CONTEXT=k3d-$FULL_CLUSTER_NAME_V2
- cf_export FULL_CLUSTER_NAME_V1 V1_KUBE_CONTEXT FULL_CLUSTER_NAME_V2 V2_KUBE_CONTEXT
- k3d cluster create $FULL_CLUSTER_NAME --network $NETWORK -i $CLUSTER_IMAGE --no-lb
- export CLUSTER_IP=$(docker inspect k3d-$FULL_CLUSTER_NAME-server-0 | jq -r '.[0].NetworkSettings.IPAddress')
- yq e -i '(.clusters[] | select(.name=="k3d-"+env(FULL_CLUSTER_NAME)) | .cluster.server) = "https://"+env(CLUSTER_IP)+":6443"' $KUBECONFIG
- export KUBE_CONTEXT=k3d-$FULL_CLUSTER_NAME
- cf_export FULL_CLUSTER_NAME KUBE_CONTEXT

init_test_envs:
stage: test
title: 'Init test environments'
image: "dtzar/helm-kubectl:3.15"
working_directory: '${{clone}}'
commands:
- curl -L -o vcluster "https://github.com/loft-sh/vcluster/releases/download/v0.20.0-beta.11/vcluster-linux-amd64" && chmod +x ./vcluster && mv ./vcluster /usr/local/bin/vcluster
- kubectl config use-context $V1_KUBE_CONTEXT
- v1/.e2e/scripts/init.sh "${{steps.build_v1.imageId}}"
- kubectl config use-context $V2_KUBE_CONTEXT
- v2/.e2e/scripts/init.sh "${{steps.build_v2.imageId}}"
- curl -L -o vcluster "https://github.com/loft-sh/vcluster/releases/download/v0.23.0/vcluster-linux-amd64" && chmod +x ./vcluster && mv ./vcluster /usr/local/bin/vcluster
- kubectl config use-context $KUBE_CONTEXT
- .e2e/scripts/init.sh "${{steps.build.imageId}}"
when:
steps:
- name: create_clusters
on:
- success
- name: build_v1
- name: create_cluster
on:
- success
- name: build_v2
- name: build
on:
- success

Expand All @@ -141,8 +105,7 @@ steps:
image: 'ghcr.io/kyverno/chainsaw:v0.2.5'
working_directory: '${{clone}}'
commands:
- chainsaw test --kube-context $V1_KUBE_CONTEXT --test-file .e2e/chainsaw-tests.yaml --skip-delete
- chainsaw test --kube-context $V2_KUBE_CONTEXT --test-file .e2e/chainsaw-tests.yaml --skip-delete
- chainsaw test --kube-context $KUBE_CONTEXT --test-file .e2e/chainsaw-tests.yaml --skip-delete
when:
steps:
- name: init_test_envs
Expand Down
21 changes: 13 additions & 8 deletions v2/devspace.yaml → devspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ hooks:
fi

deployments:
prometheus-operator:
prometheus-operator-crds:
helm:
chart:
name: kube-prometheus-stack
name: prometheus-operator-crds
repo: https://prometheus-community.github.io/helm-charts
version: 60.2.0
version: 18.0.1
vcluster:
helm:
chart:
name: vcluster
repo: https://charts.loft.sh
version: v0.20.0-beta.5
version: v0.23.0
values:
controlPlane:
advanced:
Expand All @@ -36,6 +36,11 @@ deployments:
enabled: false
readinessProbe:
enabled: false
resources:
limits:
memory: 4Gi
requests:
memory: 4Gi
distro:
k3s:
enabled: true
Expand All @@ -53,13 +58,13 @@ deployments:
verbs: ["create", "delete", "patch", "update", "get", "list", "watch"]
clusterRole:
extraRules:
- apiGroups: [ "apiextensions.k8s.io" ]
resources: [ "customresourcedefinitions" ]
verbs: [ "get", "list", "watch" ]
- apiGroups: ["apiextensions.k8s.io"]
resources: ["customresourcedefinitions"]
verbs: ["get", "list", "watch"]
dev:
vcluster:
imageSelector: "ghcr.io/loft-sh/vcluster-pro"
devImage: golang:1.22.2
devImage: golang:1.23.2
workingDir: /plugins/prometheus-operator-resources
container: syncer
terminal:
Expand Down
4 changes: 2 additions & 2 deletions v2/devspace_start.sh → devspace_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ COLOR_RESET="\033[0m"
if [ ! -f "/vcluster/syncer" ]; then
echo "Downloading vCluster syncer..."
mkdir -p /vcluster
curl -L -o /vcluster/syncer "https://github.com/loft-sh/vcluster/releases/download/v0.20.0-beta.5/syncer-linux-$(go env GOARCH)"
curl -L -o /usr/local/bin/kine "https://github.com/k3s-io/kine/releases/download/v0.11.9/kine-$(go env GOARCH)"
curl -L -o /vcluster/syncer "https://github.com/loft-sh/vcluster/releases/download/v0.23.0/syncer-linux-$(go env GOARCH)"
curl -L -o /usr/local/bin/kine "https://github.com/k3s-io/kine/releases/download/v0.13.8/kine-$(go env GOARCH)"
chmod +x /vcluster/syncer
chmod +x /usr/local/bin/kine
echo "Successfully downloaded syncer"
Expand Down
Loading
Loading