Skip to content

Commit

Permalink
feat(dev-env): Setup local development environment using greenhousectl (
Browse files Browse the repository at this point in the history
#452)

* (chore): init local env cli

* (chore): adds kind cluster creation

KinD cluster creation functionality. Additionally, can get a kind cluster, list kind clusters

* (chore): adds cluster command

cluster command to create kind cluster and optionally create a namespace after cluster creation

* (chore): common variables

* (chore): adds environment variables to disable webhooks and controllers

* (chore): adds cluster list and delete commands

* (chore): adds manifests command

manifests cmd explodes the specified chart by doing a helm template and applies it to the KinD cluster using kubectl apply

* (chore): adds webhook command

webhook command will deploy the operator to run in webhook only mode so that local development of controllers can be done without running webhooks. Validation and mutation of CR(s) will happen in-cluster

* (chore): adds setup command

setup command uses a config file to spin up a full development environment

* (chore): adds CLI markdown docs

auto-generates docs for cobra commands

* (chore): move commands to greenhousectl

in order to reduce complexity and encourage contributions by end users, dev CLI commands are moved from standalone to greenhousectl

* (chore): adds error handling and logging

* (fix): fixes typo

* Automatic application of license header

* (chore): adds review dog lint

* (fix): address lint issues

* (chore): use review dog linting

* (chore): update reviewdog lint version

* (chore): run lint as a separate job

* (chore): adds make setup dev

make command to build greenhousectl locally and setup the dev env

* (chore): adds dev setup configuration

* (chore): adds dev setup docs

* (chore): adds dev setup make commands

* (chore): reverts changes to unit-tests.yml

* (chore): tidy up!

* (chore): move to greenhousectl dev subcommands

* (chore): tidy up

* Automatic application of license header

* (chore): tidy up

* tidy up

* (chore): use method chains

* (chore): regenerate docs

* Automatic application of license header

* (fix): fix linting errors

* (chore): regenerate docs

* (revert): revert greenhousectl worklflow

* (chore): go fmt and tidy up!

* Automatic generation of CRD API Docs

* (chore): remove unused method

* Automatic generation of CRD API Docs

* (chore): re-run manifest generation

* Automatic generation of CRD API Docs

* (chore): refactor helm install

* Automatic generation of CRD API Docs

* Update dev-env/localenv/README.md

Co-authored-by: IvoGoman <[email protected]>

* Automatic generation of CRD API Docs

* Apply suggestions from code review

Co-authored-by: IvoGoman <[email protected]>

* (chore): provides back-off options

* (chore): removes unnecessary var assignment

* (chore): reuse existing build command

* (chore): provide helm client options as single slice

* (chore): generate dev setup markdown with templates

* (chore): allow one of webhook or controller modes or regular

* Automatic generation of CRD API Docs

* (chore): adds docker and kubectl check to persistent pre-run dev cmds

* (chore): tidy up

* Automatic generation of CRD API Docs

* (chore): go fmt

* (chore): go fmt

* Automatic generation of CRD API Docs

* (chore): upd variables

* Automatic generation of CRD API Docs

---------

Co-authored-by: License Bot <[email protected]>
Co-authored-by: CRD API Docs Bot <[email protected]>
Co-authored-by: IvoGoman <[email protected]>
  • Loading branch information
4 people authored Aug 29, 2024
1 parent 9b9c8d8 commit f0c7fec
Show file tree
Hide file tree
Showing 31 changed files with 3,566 additions and 759 deletions.
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

CLI ?= $(LOCALBIN)/greenhousectl

.PHONY: all
all: build

Expand Down Expand Up @@ -122,8 +124,13 @@ lint: golint
.PHONY: check
check: fmt lint test

##@ Build
##@ Build CLI Locally
.PHONY: cli
cli: $(CLI)
$(CLI): $(LOCALBIN)
test -s $(LOCALBIN)/greenhousectl || echo "Building Greenhouse CLI..." && make build-greenhousectl

##@ Build
.PHONY: build
build: generate build-greenhouse build-idproxy build-team-membership build-cors-proxy build-greenhousectl build-service-proxy

Expand Down Expand Up @@ -211,3 +218,10 @@ else
cd website && hugo server
endif

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

.PHONY: dev-docs
dev-docs:
go run -tags="dev" -mod=mod dev-env/localenv/docs.go
19 changes: 2 additions & 17 deletions charts/manager/templates/manager-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,6 @@ rules:
- get
- patch
- update
- apiGroups:
- greenhouse.sap
resources:
- plugin
verbs:
- get
- list
- watch
- apiGroups:
- greenhouse.sap
resources:
- plugin/status
verbs:
- get
- list
- patch
- watch
- apiGroups:
- greenhouse.sap
resources:
Expand Down Expand Up @@ -219,8 +202,10 @@ rules:
- plugins/status
verbs:
- get
- list
- patch
- update
- watch
- apiGroups:
- greenhouse.sap
resources:
Expand Down
80 changes: 71 additions & 9 deletions cmd/greenhouse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package main
import (
"errors"
goflag "flag"
"fmt"
"os"
"strconv"
"strings"
"time"

flag "github.com/spf13/pflag"
Expand All @@ -29,9 +32,22 @@ import (
"github.com/cloudoperators/greenhouse/pkg/version"
)

type managerMode int

const (
// regularMode starts Manager with registered Controllers and all Webhooks
regularMode managerMode = iota
// webhookOnlyMode starts the Manager with all Webhooks and no Controllers
webhookOnlyMode
// controllerOnlyMode starts the Manager with registered Controllers and no Webhooks
controllerOnlyMode
)

const (
defaultRemoteClusterBearerTokenValidity = 24 * time.Hour
defaultRenewRemoteClusterBearerTokenAfter = 20 * time.Hour
disableControllersEnv = "WEBHOOK_ONLY" // used to deploy the operator in webhook only mode no controllers will run in this mode.
disableWebhookEnv = "CONTROLLERS_ONLY" // used to disable webhooks when running locally or in debug mode.
)

var (
Expand Down Expand Up @@ -126,21 +142,30 @@ func main() {
})
handleError(err, "unable to start manager")

mode, err := calculateManagerMode()
if err != nil {
handleError(err, "unable to calculate manager mode")
}

// Register controllers.
for controllerName, hookFunc := range knownControllers {
if !isControllerEnabled(controllerName) {
setupLog.Info("skipping controller", "name", controllerName)
if mode != webhookOnlyMode {
for controllerName, hookFunc := range knownControllers {
if !isControllerEnabled(controllerName) {
setupLog.Info("skipping controller", "name", controllerName)
continue
}
setupLog.Info("registering controller", "name", controllerName)
handleError(hookFunc(controllerName, mgr), "unable to create controller", "name", controllerName)
continue
}
setupLog.Info("registering controller", "name", controllerName)
handleError(hookFunc(controllerName, mgr), "unable to create controller", "name", controllerName)
continue
}

// Register webhooks.
for webhookName, hookFunc := range knownWebhooks {
setupLog.Info("registering webhook", "name", webhookName)
handleError(hookFunc(mgr), "unable to create webhook", "name", webhookName)
if mode != controllerOnlyMode {
for webhookName, hookFunc := range knownWebhooks {
setupLog.Info("registering webhook", "name", webhookName)
handleError(hookFunc(mgr), "unable to create webhook", "name", webhookName)
}
}
//+kubebuilder:scaffold:builder

Expand All @@ -157,3 +182,40 @@ func handleError(err error, msg string, keysAndValues ...interface{}) {
os.Exit(1)
}
}

// calculateManagerMode - calculates in which mode the manager should run.
func calculateManagerMode() (managerMode, error) {
webhookOnlyEnv := os.Getenv(disableControllersEnv)
controllersOnlyEnv := os.Getenv(disableWebhookEnv)

var webhookOnly, controllersOnly bool
var err error

if strings.TrimSpace(webhookOnlyEnv) != "" {
webhookOnly, err = strconv.ParseBool(webhookOnlyEnv)
if err != nil {
return -1, fmt.Errorf("unable to parse %s: %w", disableControllersEnv, err)
}
}

if strings.TrimSpace(controllersOnlyEnv) != "" {
controllersOnly, err = strconv.ParseBool(controllersOnlyEnv)
if err != nil {
return -1, fmt.Errorf("unable to parse %s: %w", disableWebhookEnv, err)
}
}

if webhookOnly && controllersOnly {
return -1, errors.New("you can have only one of WEBHOOK_ONLY or CONTROLLERS_ONLY env be set to true")
}

if webhookOnly {
return webhookOnlyMode, nil
}

if controllersOnly {
return controllerOnlyMode, nil
}

return regularMode, nil
}
Loading

0 comments on commit f0c7fec

Please sign in to comment.