Skip to content

Commit

Permalink
Merge pull request #71 from FabianKramm/hooks
Browse files Browse the repository at this point in the history
refactor: plugin improvements
  • Loading branch information
FabianKramm authored Jan 29, 2024
2 parents 10369a2 + b6f2c20 commit f200e7c
Show file tree
Hide file tree
Showing 22 changed files with 937 additions and 84 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ jobs:
uses: engineerd/[email protected]
with:
version: "v0.20.0"
image: kindest/node:v1.28.0
config: hack/kind-config.yaml
image: kindest/node:v1.28.0@sha256:b7a4cad12c197af3ba43202d3efe03246b3f0793f162afb40a33c923952d5b31

- name: Testing kind cluster set-up
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.idea/
examples/**/vendor/
examples/**/test/
test/
/test
.devspace/
.DS_Store
File renamed without changes.
11 changes: 2 additions & 9 deletions e2e/test_plugin/syncers/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,16 @@ func (s *carSyncer) translateUpdate(ctx context.Context, pObj, vObj *examplev1.C
// check annotations & labels
changed, updatedAnnotations, updatedLabels := s.TranslateMetadataUpdate(ctx, vObj, pObj)
if changed {
updated = newIfNil(updated, pObj)
updated = translator.NewIfNil(updated, pObj)
updated.Labels = updatedLabels
updated.Annotations = updatedAnnotations
}

// check spec
if !equality.Semantic.DeepEqual(vObj.Spec, pObj.Spec) {
updated = newIfNil(updated, pObj)
updated = translator.NewIfNil(updated, pObj)
updated.Spec = vObj.Spec
}

return updated
}

func newIfNil(updated *examplev1.Car, pObj *examplev1.Car) *examplev1.Car {
if updated == nil {
return pObj.DeepCopy()
}
return updated
}
17 changes: 4 additions & 13 deletions examples/bootstrap-with-deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ kubectl get po -n my-vcluster
## Building the Plugin
To just build the plugin image and push it to the registry, run:
```
# Make sure vendor is updated
go mod vendor
# Build
docker build . -t my-repo/my-plugin:0.0.1
Expand All @@ -45,7 +48,6 @@ General vcluster plugin project structure:
├── devspace.yaml # Development environment definition
├── devspace_start.sh # Development entrypoint script
├── Dockerfile # Production Dockerfile
├── Dockerfile.dev # Development Dockerfile
├── main.go # Go Entrypoint
├── plugin.yaml # Plugin Helm Values
├── syncers/ # Plugin Syncers
Expand All @@ -59,25 +61,14 @@ Before starting to develop, make sure you have installed the following tools on
- [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.
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.
Expand Down
14 changes: 1 addition & 13 deletions examples/crd-sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ General vcluster plugin project structure:
├── devspace.yaml # Development environment definition
├── devspace_start.sh # Development entrypoint script
├── Dockerfile # Production Dockerfile
├── Dockerfile.dev # Development Dockerfile
├── main.go # Go Entrypoint
├── plugin.yaml # Plugin Helm Values
├── syncers/ # Plugin Syncers
Expand All @@ -62,25 +61,14 @@ Before starting to develop, make sure you have installed the following tools on
- [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.
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.
Expand Down
14 changes: 1 addition & 13 deletions examples/hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ General vcluster plugin project structure:
├── devspace.yaml # Development environment definition
├── devspace_start.sh # Development entrypoint script
├── Dockerfile # Production Dockerfile
├── Dockerfile.dev # Development Dockerfile
├── main.go # Go Entrypoint
├── plugin.yaml # Plugin Helm Values
├── syncers/ # Plugin Syncers
Expand All @@ -84,25 +83,14 @@ Before starting to develop, make sure you have installed the following tools on
- [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.
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.
Expand Down
14 changes: 1 addition & 13 deletions examples/pull-secret-sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ General vcluster plugin project structure:
├── devspace.yaml # Development environment definition
├── devspace_start.sh # Development entrypoint script
├── Dockerfile # Production Dockerfile
├── Dockerfile.dev # Development Dockerfile
├── main.go # Go Entrypoint
├── plugin.yaml # Plugin Helm Values
├── syncers/ # Plugin Syncers
Expand All @@ -63,25 +62,14 @@ Before starting to develop, make sure you have installed the following tools on
- [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.
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.
Expand Down
5 changes: 5 additions & 0 deletions examples/pull-secret-sync/devspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ deployments:
repo: https://charts.loft.sh
version: v0.19.0-alpha.3
values:
plugin:
pull-secret-sync:
version: v2
config:
destinationNamespace: "test"
serviceAccount:
create: false
name: default
Expand Down
21 changes: 20 additions & 1 deletion examples/pull-secret-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@ package main
import (
"github.com/loft-sh/vcluster-pull-secret-sync/syncers"
"github.com/loft-sh/vcluster-sdk/plugin"
"k8s.io/klog/v2"
)

type PluginConfig struct {
DestinationNamespace string `json:"destinationNamespace,omitempty"`
}

func main() {
// Init plugin
ctx := plugin.MustInit()
plugin.MustRegister(syncers.NewPullSecretSyncer(ctx, "todo"))

// parse plugin config
pluginConfig := &PluginConfig{}
err := plugin.UnmarshalConfig(pluginConfig)
if err != nil {
klog.Fatal("Error parsing plugin config")
} else if pluginConfig.DestinationNamespace == "" {
klog.Fatal("destinationNamespace is empty")
}

// register syncer
plugin.MustRegister(syncers.NewPullSecretSyncer(ctx, pluginConfig.DestinationNamespace))

// start plugin
plugin.MustStart()
}
4 changes: 3 additions & 1 deletion examples/pull-secret-sync/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ plugin:
pull-secret-sync:
version: v2
image: ghcr.io/loft-sh/vcluster-example-pull-sycret-sync:v2
imagePullPolicy: IfNotPresent
imagePullPolicy: IfNotPresent
config:
destinationNamespace: "test"
17 changes: 5 additions & 12 deletions examples/pull-secret-sync/syncers/pull-secret-sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
synctypes "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -65,8 +66,7 @@ func (s *pullSecretSyncer) SyncUp(ctx *synccontext.SyncContext, pObj client.Obje
if pSecret.Type != corev1.SecretTypeDockerConfigJson {
// ignore secrets that are not of "pull secret" type
return ctrl.Result{}, nil
}
if pSecret.GetLabels()[translate.MarkerLabel] != "" {
} else if pSecret.GetLabels()[translate.MarkerLabel] != "" {
// ignore Secrets synced to the host by the vcluster
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func (s *pullSecretSyncer) translateUpdateUp(pObj, vObj *corev1.Secret) *corev1.
// sync annotations
// we sync all of them from the host and remove any added in the vcluster
if !equality.Semantic.DeepEqual(vObj.GetAnnotations(), pObj.GetAnnotations()) {
updated = newIfNil(updated, vObj)
updated = translator.NewIfNil(updated, vObj)
updated.Annotations = pObj.GetAnnotations()
}

Expand All @@ -197,7 +197,7 @@ func (s *pullSecretSyncer) translateUpdateUp(pObj, vObj *corev1.Secret) *corev1.
expectedLabels[k] = v
}
if !equality.Semantic.DeepEqual(vObj.GetLabels(), expectedLabels) {
updated = newIfNil(updated, vObj)
updated = translator.NewIfNil(updated, vObj)
updated.Labels = expectedLabels
}

Expand All @@ -206,16 +206,9 @@ func (s *pullSecretSyncer) translateUpdateUp(pObj, vObj *corev1.Secret) *corev1.

// check data
if !equality.Semantic.DeepEqual(vObj.Data, pObj.Data) {
updated = newIfNil(updated, vObj)
updated = translator.NewIfNil(updated, vObj)
updated.Data = pObj.Data
}

return updated
}

func newIfNil(updated *corev1.Secret, pObj *corev1.Secret) *corev1.Secret {
if updated == nil {
return pObj.DeepCopy()
}
return updated
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/loft-sh/vcluster-sdk
go 1.21.5

require (
github.com/ghodss/yaml v1.0.0
github.com/hashicorp/go-plugin v1.6.0
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a
github.com/loft-sh/vcluster v0.19.0-alpha.3
Expand Down Expand Up @@ -51,7 +52,6 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fvbommel/sortorder v1.1.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down
5 changes: 0 additions & 5 deletions hack/kind-config.yaml

This file was deleted.

10 changes: 10 additions & 0 deletions plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"sync"

"github.com/ghodss/yaml"
"github.com/loft-sh/log/logr"
"github.com/loft-sh/vcluster/pkg/controllers/syncer"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
Expand Down Expand Up @@ -43,6 +44,15 @@ type manager struct {
syncers []syncertypes.Base
}

func (m *manager) UnmarshalConfig(into interface{}) error {
err := yaml.Unmarshal([]byte(os.Getenv(v2.PluginConfigEnv)), into)
if err != nil {
return fmt.Errorf("unmarshal plugin config: %w", err)
}

return nil
}

func (m *manager) Init() (*synccontext.RegisterContext, error) {
return m.InitWithOptions(Options{})
}
Expand Down
4 changes: 4 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ func MustStart() {
func Start() error {
return defaultManager.Start()
}

func UnmarshalConfig(into interface{}) error {
return defaultManager.UnmarshalConfig(into)
}
4 changes: 4 additions & 0 deletions plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Manager interface {
// the functionality if the current vcluster pod is the current leader and
// will stop if the pod will lose leader election.
Start() error

// UnmarshalConfig retrieves the plugin config from environment and parses it into
// the given object.
UnmarshalConfig(into interface{}) error
}

// ClientHook tells the sdk that this action watches on certain vcluster requests and wants
Expand Down
Loading

0 comments on commit f200e7c

Please sign in to comment.