Skip to content

Commit

Permalink
feat(greenhousectl): setup local dev environment for plugin developers (
Browse files Browse the repository at this point in the history
#775)

* (chore): adds dynamic kind configuration creation

* (chore): additional param to pass kind config file for cluster creation

* (chore): changes config from json to yaml

* (chore): dynamic kind config file generation

adds hostPath mounts for plugin development

* (chore): setup config from json to yaml

* (chore): make plugin dev setup with onboarding

* Automatic application of license header

* (chore): remove plugin path for core dev

* (lint): fix lint errors

* (lint): remove whitespace

* (chore): re-generate dev cli docs

* (chore): lose the else block

* (chore): adds local samples to ignore

---------

Co-authored-by: License Bot <[email protected]>
Co-authored-by: Akshay Iyyadurai Balasundaram <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2024
1 parent d3cb10a commit 40fad61
Show file tree
Hide file tree
Showing 24 changed files with 282 additions and 141 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
vendor/**
node_modules

# TestData
dev-env/localenv/samples/**/local_*.yaml

# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
30 changes: 28 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,44 @@ ADMIN_RELEASE ?= greenhouse
ADMIN_CHART_PATH ?= charts/manager
WEBHOOK_DEV ?= false
E2E_REPORT_PATH="$(shell pwd)/bin/$(SCENARIO)-e2e-report.json"
PLUGIN_DIR ?=
GREENHOUSE_ORG ?= demo

.PHONY: setup-demo
setup-demo: prepare-e2e samples
kubectl create secret generic kind-$(REMOTE_CLUSTER) \
--from-literal=kubeconfig="$$(cat ${PWD}/bin/$(REMOTE_CLUSTER)-int.kubeconfig)" \
--namespace=$(GREENHOUSE_ORG) \
--type="greenhouse.sap/kubeconfig" \
--dry-run=client -o yaml | kubectl apply -f -

.PHONY: samples
samples: kustomize
$(KUSTOMIZE) build dev-env/localenv/samples | kubectl apply -n $(GREENHOUSE_ORG) --kubeconfig=$(shell pwd)/bin/$(ADMIN_CLUSTER).kubeconfig -f -
while true; do \
if kubectl get organizations $(GREENHOUSE_ORG) --kubeconfig=$(shell pwd)/bin/$(ADMIN_CLUSTER).kubeconfig -o json | \
jq -e '.status.statusConditions.conditions[] | select(.type == "Ready") | select(.status == "True")' > /dev/null; then \
echo "Organization is ready"; \
exit 0; \
fi; \
sleep 5; \
done

.PHONY: setup-plugin-dev
setup-plugin-dev: cli
PLUGIN_PATH=$(PLUGIN_DIR) $(CLI) dev setup -f dev-env/localenv/plugin.config.yaml && make setup-demo

.PHONY: setup-dev
setup-dev: cli
$(CLI) dev setup -f dev-env/localenv/sample.config.json
$(CLI) dev setup -f dev-env/localenv/dev.config.yaml

.PHONY: setup-webhook
setup-webhook: cli
$(CLI) dev setup webhook --name $(ADMIN_CLUSTER) --namespace $(ADMIN_NAMESPACE) --release $(ADMIN_RELEASE) --chart-path $(ADMIN_CHART_PATH) --dockerfile ./ --dev-mode=$(WEBHOOK_DEV)

.PHONY: setup-e2e
setup-e2e: cli
$(CLI) dev setup -f e2e/config.json
$(CLI) dev setup -f e2e/config.yaml
make prepare-e2e

.PHONY: clean-e2e
Expand Down
11 changes: 8 additions & 3 deletions dev-env/localenv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ greenhousectl dev cluster create [flags]
### Examples

```
greenhousectl dev cluster create --name <my-cluster-name> --namespace <my-namespace>
greenhousectl dev cluster create --name <my-cluster-name> --namespace <my-namespace> --version <v1.30.3>
```

### Options

```
--config string create the cluster with a specific kind configuration file - e.g. --config <path>/<to>/<config>
-h, --help help for create
-c, --name string create a kind cluster with a name - e.g. -c <my-cluster>
-n, --namespace string create a namespace in the cluster - e.g. -c <my-cluster> -n <my-namespace>
--version string create the cluster with a specific version - e.g. -v <v1.30.3>
```

## greenhousectl dev cluster delete
Expand Down Expand Up @@ -136,6 +138,7 @@ greenhousectl dev setup manifest --name greenhouse-admin --namespace greenhouse
-n, --namespace string namespace to install the resources
-r, --release string Helm release name, Default value: greenhouse - e.g. your-release-name (default "greenhouse")
-v, --values-path string local absolute values file path - e.g. <path>/<to>/my-values.yaml
--version string create the cluster with a specific version - e.g. -v <v1.30.3>
```

## greenhousectl dev setup webhook
Expand Down Expand Up @@ -170,13 +173,15 @@ greenhousectl dev setup webhook --name greenhouse-admin --namespace greenhouse -

```
-p, --chart-path string local chart path where manifests are located - e.g. <path>/<to>/charts/manager
--config string create the cluster with a specific kind configuration file - e.g. --config <path>/<to>/<config>
-m, --dev-mode Enable dev mode for webhook setup - Note: Admission Webhooks will be modified for local development
-f, --dockerfile string local path to the Dockerfile of greenhouse manager
-h, --help help for webhook
-c, --name string Name of the kind cluster - e.g. my-cluster (without the kind prefix)
-n, --namespace string namespace to install the resources
-r, --release string Helm release name, Default value: greenhouse - e.g. your-release-name (default "greenhouse")
-v, --values-path string local absolute values file path - e.g. <path>/<to>/my-values.yaml
--version string create the cluster with a specific version - e.g. -v <v1.30.3>
```

## greenhousectl dev setup
Expand All @@ -196,7 +201,7 @@ greenhousectl dev setup [flags]
```
# Setup Greenhouse dev environment with a configuration file
greenhousectl dev setup -f dev-env/localenv/sample.config.json
greenhousectl dev setup -f dev-env/localenv/dev.config.yaml
- This will create an admin and a remote cluster
- Install CRDs, Webhook definitions, RBACs, Certs, etc... for Greenhouse into the target cluster
Expand All @@ -207,7 +212,7 @@ greenhousectl dev setup -f dev-env/localenv/sample.config.json
### Options

```
-f, --config string configuration file path - e.g. -f hack/localenv/sample.config.json
-f, --config string configuration file path - e.g. -f dev-env/localenv/dev.config.yaml
-h, --help help for setup
```

Expand Down
25 changes: 25 additions & 0 deletions dev-env/localenv/dev.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
# SPDX-License-Identifier: Apache-2.0

config:
- cluster:
name: greenhouse-remote
- cluster:
name: greenhouse-admin
namespace: greenhouse
dependencies:
- manifest:
release: greenhouse
chartPath: charts/idproxy
crdOnly: true
- manifest:
release: greenhouse
chartPath: charts/manager
valuesPath: dev-env/localenv/dev.values.yaml
crdOnly: false
webhook:
devMode: false
dockerFile: "./"
envs:
- name: WEBHOOK_ONLY
value: 'true'
File renamed without changes.
25 changes: 25 additions & 0 deletions dev-env/localenv/plugin.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
# SPDX-License-Identifier: Apache-2.0

config:
- cluster:
name: greenhouse-remote
- cluster:
name: greenhouse-admin
namespace: greenhouse
dependencies:
- manifest:
release: greenhouse
chartPath: charts/idproxy
crdOnly: true
- manifest:
release: greenhouse
chartPath: charts/manager
valuesPath: dev-env/localenv/dev.values.yaml
crdOnly: false
webhook:
devMode: false
dockerFile: "./"
envs:
- name: WEBHOOK_ONLY
value: 'false'
42 changes: 0 additions & 42 deletions dev-env/localenv/sample.config.json

This file was deleted.

6 changes: 6 additions & 0 deletions dev-env/localenv/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
# SPDX-License-Identifier: Apache-2.0

# Add more resources in the future to setup a complete environment with plugins
resources:
- organization.yaml
10 changes: 10 additions & 0 deletions dev-env/localenv/samples/organization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
# SPDX-License-Identifier: Apache-2.0

apiVersion: greenhouse.sap/v1alpha1
kind: Organization
metadata:
name: demo
spec:
description: Greenhouse Demo Organization
mappedOrgAdminIdPGroup: DEMO_ORG_ADMIN
41 changes: 0 additions & 41 deletions e2e/config.json

This file was deleted.

25 changes: 25 additions & 0 deletions e2e/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
# SPDX-License-Identifier: Apache-2.0

config:
- cluster:
name: greenhouse-remote
- cluster:
name: greenhouse-admin
namespace: greenhouse
dependencies:
- manifest:
release: greenhouse
chartPath: charts/idproxy
crdOnly: true
- manifest:
release: greenhouse
chartPath: charts/manager
valuesPath: dev-env/localenv/dev.values.yaml
crdOnly: false
webhook:
devMode: false
dockerFile: "./"
envs:
- name: WEBHOOK_ONLY
value: 'false'
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078
sigs.k8s.io/controller-runtime v0.19.1
sigs.k8s.io/e2e-framework v0.5.0
sigs.k8s.io/kind v0.25.0
sigs.k8s.io/yaml v1.4.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ sigs.k8s.io/e2e-framework v0.5.0 h1:YLhk8R7EHuTFQAe6Fxy5eBzn5Vb+yamR5u8MH1Rq3cE=
sigs.k8s.io/e2e-framework v0.5.0/go.mod h1:jJSH8u2RNmruekUZgHAtmRjb5Wj67GErli9UjLSY7Zc=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/kind v0.25.0 h1:ugUvgesHKKA0yKmD6QtYTiEev+kPUpGxdTPbMGf8VTU=
sigs.k8s.io/kind v0.25.0/go.mod h1:t7ueEpzPYJvHA8aeLtI52rtFftNgUYUaCwvxjk7phfw=
sigs.k8s.io/kustomize/api v0.17.3 h1:6GCuHSsxq7fN5yhF2XrC+AAr8gxQwhexgHflOAD/JJU=
sigs.k8s.io/kustomize/api v0.17.3/go.mod h1:TuDH4mdx7jTfK61SQ/j1QZM/QWR+5rmEiNjvYlhzFhc=
sigs.k8s.io/kustomize/kyaml v0.17.2 h1:+AzvoJUY0kq4QAhH/ydPHHMRLijtUKiyVyh7fOSshr0=
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/local/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func processDeleteLocalCluster(_ *cobra.Command, _ []string) error {
}

func processCreateLocalCluster(_ *cobra.Command, _ []string) error {
err := setup.NewExecutionEnv().WithClusterSetup(clusterName, namespaceName, clusterVersion).Run()
err := setup.NewExecutionEnv().WithClusterSetup(clusterName, namespaceName, clusterVersion, kindConfigPath).Run()
if err != nil {
return err
}
Expand All @@ -61,6 +61,7 @@ func init() {
createClusterCmd.Flags().StringVarP(&clusterName, "name", "c", "", "create a kind cluster with a name - e.g. -c <my-cluster>")
createClusterCmd.Flags().StringVarP(&namespaceName, "namespace", "n", "", "create a namespace in the cluster - e.g. -c <my-cluster> -n <my-namespace>")
createClusterCmd.Flags().StringVar(&clusterVersion, "version", "", "create the cluster with a specific version - e.g. -v <v1.30.3>")
createClusterCmd.Flags().StringVar(&kindConfigPath, "config", "", "create the cluster with a specific kind configuration file - e.g. --config <path>/<to>/<config>")
deleteClusterCmd.Flags().StringVarP(&clusterName, "name", "c", "", "delete the kind cluster - e.g. -c <my-cluster>")
cobra.CheckErr(createClusterCmd.MarkFlagRequired("name"))
cobra.CheckErr(deleteClusterCmd.MarkFlagRequired("name"))
Expand Down
39 changes: 39 additions & 0 deletions pkg/internal/local/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ package commands

import (
"fmt"
"os"
"strings"

"gopkg.in/yaml.v3"
kv1alpha4 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"

"github.com/cloudoperators/greenhouse/pkg/internal/local/utils"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand All @@ -15,6 +21,7 @@ var (
clusterName string
namespaceName string
clusterVersion string
kindConfigPath string
dockerFile string
releaseName string
chartPath string
Expand Down Expand Up @@ -77,3 +84,35 @@ func validateFlagInputs(flags *pflag.FlagSet) error {
}
return nil
}

func createHostPathConfig() (string, error) {
pluginDir, ok := os.LookupEnv(utils.PluginDirectoryPath)
if !ok {
return "", nil
}
if strings.TrimSpace(pluginDir) == "" {
return "", nil
}
kindConfig := kv1alpha4.Cluster{
TypeMeta: kv1alpha4.TypeMeta{
Kind: "Cluster",
APIVersion: "kind.x-k8s.io/v1alpha4",
},
Nodes: []kv1alpha4.Node{
{
Role: kv1alpha4.ControlPlaneRole,
ExtraMounts: []kv1alpha4.Mount{
{
HostPath: pluginDir,
ContainerPath: utils.PluginHostPath,
},
},
},
},
}
kindConfigBytes, err := yaml.Marshal(kindConfig)
if err != nil {
return "", err
}
return utils.RandomWriteToTmpFolder("plugin-config.yaml", string(kindConfigBytes))
}
Loading

0 comments on commit 40fad61

Please sign in to comment.