Skip to content
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

automated development environment for hive #2551

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
47 changes: 47 additions & 0 deletions Makefile.environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
HIVE_ROOT := $(shell git rev-parse --show-toplevel)
export PATH := $(HIVE_ROOT)/.tmp/_output/bin:$(PATH)
export KUBECONFIG := $(HIVE_ROOT)/.tmp/_output/dev-hive.kubeconfig

all: install_environment_bins create_cluster_registry build_push_image deploy

# Install dependency bins, make hive & run containerd, buildkitd
install_environment_bins:
@echo "Installing go and additional dependencies"
./hack/install_dev_env.sh

# Create Kind Cluster & registry
create_cluster_registry:
@echo "Creating a Kind cluster"
./hack/create_cluster_registry.sh

# Build and push image
build_push_image:
@echo "Building and pushing the image"
./hack/buildkitd_build_push_image.sh

# Deploy & install certs
deploy:
set -x
@echo "Deploying the application"
./hack/deploy_dev_hive.sh
HIVE_NS=dev-hive ./hack/hiveadmission-dev-cert.sh

# Scale down operator
scale-down-operator:
oc scale -n dev-hive deployment.v1.apps/hive-operator --replicas=0

# Scale down controllers
scale-down-controllers:
oc scale -n dev-hive deployment.v1.apps/hive-controllers --replicas=0

# Run operator
run-operator: scale-down-operator
./bin/operator --log-level=debug

# Run controller
run-controller: scale-down-controllers
./bin/manager --log-level=debug

# Clean the environment
prune-dev-env:
./hack/prune_dev_env.sh
75 changes: 74 additions & 1 deletion docs/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- [Running Code Locally](#running-code-locally)
- [hive-operator](#hive-operator)
- [hive-controllers](#hive-controllers)
- [Rootless development environment setup](#rootless-development-environment-setup)
- [Setting up VScode for hive development](#setting-up-vscode-for-hive-development)
- [Developing Hiveutil Install Manager](#developing-hiveutil-install-manager)
- [Enable Debug Logging In Hive Controllers](#enable-debug-logging-in-hive-controllers)
- [Using Serving Certificates](#using-serving-certificates)
Expand Down Expand Up @@ -166,7 +168,7 @@ do the trick.
```shell
export EL8_BUILD_IMAGE=registry.ci.openshift.org/openshift/release:golang-1.23
export EL9_BUILD_IMAGE=registry.ci.openshift.org/openshift/release:golang-1.23
export BASE_IMAGE=registry.ci.openshift.org/origin/4.16:base
export BASE_IMAGE=quay.io/centos/centos:stream9
# NOTE: This produces images which are not FIPS-compliant.
export GO="CGO_ENABLED=0 go"
```
Expand Down Expand Up @@ -208,6 +210,77 @@ HIVE_NS="hive" make run

Kind users should also specify `HIVE_IMAGE="localhost:5000/hive:latest"` as the default image location cannot be authenticated to from Kind clusters, resulting in inability to launch install pods.

# Rootless development environment setup
An automated script can set up your development environment - download go, prerequisite binaries, create a cluster, registry, and deploy hive. The script downloads binaries, stores them in the repository's root, and calls them from there. The setup is OS-independent and runs rootless, however, it currently only works on amd64 machines.

To prepare the environment, run

```bash
make -f Makefile.environment
```
Containerd/nerdctl is used for container management, image is built using buildkitd, cluster management is done with kubectl and oc.

Hive will be deployed into a cluster with a *'dev-hive'* namespace.

After the setup is finished, to manage the cluster and do other actions from your shell, source */hack/dev_env_vars.sh* first in order to get access to the necessary paths and environment variables.

```bash
source hack/dev_env_vars.sh
```

To clean up your environment *(remove containers, build cache, /.tmp, /.kube, /hiveadmission-certs, background processes)*, please run

```bash
make -f Makefile.environment prune-dev-env
```

### Setting up VScode for hive development
Prerequisites
- [VScode](https://code.visualstudio.com/docs/setup/linux)
- VScode Go extension *(Ctrl+Shift+X and search for Go)*
- [Delve](https://github.com/go-delve/delve/tree/master/Documentation/installation)
- *source hack/dev_env_vars.sh*

After opening Hive in VScode, you can seup the IDE for debugging by pressing *ctrl + shift + D* and clicking on *create launch.json*.

The launch.json will govern which aspect to debug, e.g. the controller or the operator. Pressing F5 starts the debug process.

Prior to debugging either the controller, or the operator, they need to be scaled down first. This can be done by running
```bash
make -f Makefile.environment scale-down-operator
# or
make -f Makefile.environment scale-down-controllers
```

Example of launch.json for debugging the controller:

*Note: Metrics are a necessary part of the controller, however, for development purposes they are not needed and we can go around it creating an empty ({}) metrics.json file. The .json file is created automatically during the deploy task, exported here as METRICS_CONFIG_FILE*

```bash
{
"version": "0.2.0",
"configurations": [
{
"name": "HiveController",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/manager/main.go",
"env": {
"HIVE_NS": "dev-hive",
"KUBECONFIG":"${workspaceFolder}/.tmp/_output/dev-hive.kubeconfig",
"HIVE_SKIP_LEADER_ELECTION":"1",
"METRICS_CONFIG_FILE":"${workspaceFolder}/.tmp/_output/metrics.json",
"HIVE_MACHINEPOOL_POD_NAME":"hive-machinepool-0",
"HIVE_OPERATOR_NS":"dev-hive",
"HIVE_CLUSTERSYNC_POD_NAME":"hive-clustersync-0",
},
"args": ["--log-level=debug"]
},
]
}
```

## Developing Hiveutil Install Manager

We use a hiveutil subcommand for the install-manager, in pods and thus in an image to wrap the openshift-install process and upload artifacts to Hive. Developing this is tricky because it requires a published image and ClusterImageSet. Instead, you can hack together an environment as follows:
Expand Down
20 changes: 20 additions & 0 deletions hack/buildkitd_build_push_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
HIVE_ROOT="$(git rev-parse --show-toplevel)"
CNI_PATH="$HIVE_ROOT"/.tmp/_output/cni/bin
export HIVE_ROOT
export CNI_PATH
export PATH=$HIVE_ROOT/.tmp/_output/bin:$PATH

touch "$HIVE_ROOT/.tmp/_output/config.json"

# build and push image to localhost:5000/hive:latest
buildctl --addr unix:///run/user/$UID/buildkit/buildkitd.sock build \
--frontend dockerfile.v0 \
--local context=. \
--local dockerfile=. \
--secret id=docker,src="$HIVE_ROOT/.tmp/_output/config.json" \
--opt build-arg:EL8_BUILD_IMAGE=registry.ci.openshift.org/openshift/release:golang-1.22 \
--opt build-arg:EL9_BUILD_IMAGE=registry.ci.openshift.org/openshift/release:golang-1.22 \
--opt build-arg:BASE_IMAGE=quay.io/centos/centos:stream9 \
--opt build-arg:GO="CGO_ENABLED=0 go" \
--output type=image,name=localhost:5000/hive:latest,push=true
31 changes: 31 additions & 0 deletions hack/create_cluster_registry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

HIVE_ROOT="$(git rev-parse --show-toplevel)"
CNI_PATH="$HIVE_ROOT"/.tmp/_output/cni/bin
export HIVE_ROOT
export CNI_PATH
export PATH=$HIVE_ROOT/.tmp/_output/bin:$PATH

set -o errexit

cluster_name="dev-hive"
reg_name='kind-nerdctl-registry'
reg_port='5000'

sleep 3

# create cluster
cat <<EOF | KIND_EXPERIMENTAL_PROVIDER="nerdctl" kind create cluster --name "${cluster_name}" --kubeconfig "${HIVE_ROOT}"/.tmp/_output/"${cluster_name}".kubeconfig --config=-

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
endpoint = ["http://${reg_name}:${reg_port}"]
EOF

sleep 3

# create registry
nerdctl run -d --restart=always -p "5000:5000" --name "kind-nerdctl-registry" --network "kind" registry:2
44 changes: 44 additions & 0 deletions hack/deploy_dev_hive.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

HIVE_ROOT="$(git rev-parse --show-toplevel)"
export HIVE_ROOT
export PATH=$HIVE_ROOT/.tmp/_output/bin:$PATH
export KUBECONFIG=$HIVE_ROOT/.tmp/_output/dev-hive.kubeconfig

namespace="dev-hive"

IMG="localhost:5000/hive:latest"

echo "Creating namespace ${namespace} if it doesn't exist..."
kubectl create namespace "${namespace}" || true

echo "Creating deploy directory and copying kustomization.yaml..."
mkdir -p overlays/deploy
cp overlays/template/kustomization.yaml overlays/deploy


cd overlays/deploy || exit

echo "Setting image and namespace in kustomization.yaml..."
kustomize-4.1.3 edit set image registry.ci.openshift.org/openshift/hive-v4.0:hive=${IMG}
kustomize-4.1.3 edit set namespace "${namespace}"

cd ../../

echo "Building and applying kustomize configuration..."
kustomize-4.1.3 build overlays/deploy | sed 's/ - info/ - debug/' | oc apply -f -

echo "Cleaning up deploy directory..."
rm -rf overlays/deploy

echo "Applying CRDs..."
kubectl apply -f config/crds

echo "Creating default HiveConfig..."
cd config/templates/ || exit
oc process --local=true -p HIVE_NS="${namespace}" -p LOG_LEVEL=debug -f hiveconfig.yaml | oc apply -f -

echo "Operator deployment completed successfully."

# create an empty metrics.json file to satisfy metrics requirements for running the controller
touch "$HIVE_ROOT"/.tmp/_output/metrics.json && echo "{}" >> "$HIVE_ROOT"/.tmp/_output/metrics.json
16 changes: 16 additions & 0 deletions hack/dev_env_vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

HIVE_ROOT="$(git rev-parse --show-toplevel)"
export HIVE_ROOT
export GOPATH="${HIVE_ROOT}/.tmp/_output/go"
export PATH=$HIVE_ROOT/.tmp/_output/bin:$HIVE_ROOT/.tmp/_output/go/bin:${GOPATH}/bin:$PATH:$PATH
export CNI_PATH=$HIVE_ROOT/.tmp/_output/cni/bin
export IMG=localhost:5000/hive:latest
export KUBECONFIG=$HIVE_ROOT/.tmp/_output/dev-hive.kubeconfig
export HIVE_OPERATOR_NS=dev-hive
export HIVE_SKIP_LEADER_ELECTION=1
export METRICS_CONFIG_FILE=$HIVE_ROOT/.tmp/_output/metrics.json
export HIVE_NS=dev-hive
export HIVE_MACHINEPOOL_POD_NAME="hive-machinepool-0"
export HIVE_CLUSTERSYNC_POD_NAME="hive-clustersync-0"
export GOROOT=$HIVE_ROOT/.tmp/_output/go
Loading