Skip to content

Commit

Permalink
Merge pull request #16 from Nordix/add_run_in_kind_setup
Browse files Browse the repository at this point in the history
Add run-in-kind-kpt setup and readme
  • Loading branch information
nephio-prow[bot] authored Jan 26, 2024
2 parents 8f6f858 + 3fb29c4 commit 267436e
Show file tree
Hide file tree
Showing 13 changed files with 969 additions and 8 deletions.
51 changes: 43 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ KUBECONFIG=$(CURDIR)/deployments/local/kubeconfig
BUILDDIR=$(CURDIR)/.build
CACHEDIR=$(CURDIR)/.cache
DEPLOYCONFIGDIR=$(BUILDDIR)/deploy
DEPLOYKPTCONFIGDIR=$(DEPLOYCONFIGDIR)/kpt_pkgs
DEPLOYCONFIG_NO_SA_DIR=$(BUILDDIR)/deploy-no-sa
KPTDIR=$(abspath $(CURDIR)/..)

Expand Down Expand Up @@ -65,6 +66,7 @@ PORCH_FUNCTION_RUNNER_IMAGE ?= porch-function-runner
PORCH_CONTROLLERS_IMAGE ?= porch-controllers
PORCH_WRAPPER_SERVER_IMAGE ?= porch-wrapper-server
TEST_GIT_SERVER_IMAGE ?= test-git-server
SKIP_IMG_BUILD ?= false

# Only enable a subset of reconcilers in porch controllers by default. Use the RECONCILERS
# env variable to specify a specific list of reconcilers or use
Expand Down Expand Up @@ -283,19 +285,52 @@ push-and-deploy-no-sa: push-images deploy-no-sa
KIND_CONTEXT_NAME ?= kind

.PHONY: run-in-kind
run-in-kind: IMAGE_REPO=porch-kind
run-in-kind:
IMAGE_REPO=porch-kind make build-images
kind load docker-image porch-kind/porch-server:${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
kind load docker-image porch-kind/porch-controllers:${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
kind load docker-image porch-kind/porch-function-runner:${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
kind load docker-image porch-kind/porch-wrapper-server:${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
kind load docker-image porch-kind/test-git-server:${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
IMAGE_REPO=porch-kind make deployment-config
KUBECONFIG=$(KUBECONFIG) kubectl apply --wait --recursive --filename ./.build/deploy
make build-images
kind load docker-image $(IMAGE_REPO)/$(PORCH_SERVER_IMAGE):${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
kind load docker-image $(IMAGE_REPO)/$(PORCH_CONTROLLERS_IMAGE):${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
kind load docker-image $(IMAGE_REPO)/$(PORCH_FUNCTION_RUNNER_IMAGE):${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
kind load docker-image $(IMAGE_REPO)/$(PORCH_WRAPPER_SERVER_IMAGE):${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
kind load docker-image $(IMAGE_REPO)/$(TEST_GIT_SERVER_IMAGE):${IMAGE_TAG} -n ${KIND_CONTEXT_NAME}
make deployment-config
KUBECONFIG=$(KUBECONFIG) kubectl apply --wait --recursive --filename $(DEPLOYCONFIGDIR)
KUBECONFIG=$(KUBECONFIG) kubectl rollout status deployment function-runner --namespace porch-system
KUBECONFIG=$(KUBECONFIG) kubectl rollout status deployment porch-controllers --namespace porch-system
KUBECONFIG=$(KUBECONFIG) kubectl rollout status deployment porch-server --namespace porch-system

.PHONY: deployment-config-kpt
deployment-config-kpt:
rm -rf $(DEPLOYKPTCONFIGDIR) || true
mkdir -p $(DEPLOYKPTCONFIGDIR)
./scripts/create-deployment-kpt.sh \
--destination $(DEPLOYKPTCONFIGDIR) \
--server-image "$(IMAGE_REPO)/$(PORCH_SERVER_IMAGE):$(IMAGE_TAG)" \
--controllers-image "$(IMAGE_REPO)/$(PORCH_CONTROLLERS_IMAGE):$(IMAGE_TAG)" \
--function-image "$(IMAGE_REPO)/$(PORCH_FUNCTION_RUNNER_IMAGE):$(IMAGE_TAG)" \
--wrapper-server-image "$(IMAGE_REPO)/$(PORCH_WRAPPER_SERVER_IMAGE):$(IMAGE_TAG)" \
--enabled-reconcilers "$(ENABLED_RECONCILERS)" \
--kind-context "$(KIND_CONTEXT_NAME)"

.PHONY: run-in-kind-kpt
run-in-kind-kpt: IMAGE_REPO=porch-kind
run-in-kind-kpt:
ifeq ($(SKIP_IMG_BUILD), false)
make build-images;
endif
make deployment-config-kpt

PKG=gitea-dev
.PHONY: deploy-gitea-dev-pkg
deploy-gitea-dev-pkg:
PKG=gitea-dev
rm -rf $(DEPLOYKPTCONFIGDIR)/${PKG} || true
mkdir -p $(DEPLOYKPTCONFIGDIR)/${PKG}
./scripts/install-local-kpt-pkg.sh \
--destination $(DEPLOYKPTCONFIGDIR) \
--pkg ${PKG} \
--kubeconfig $(KUBECONFIG)

.PHONY: vulncheck
vulncheck: build
# Scan the source
Expand Down
266 changes: 266 additions & 0 deletions docs/running-porch-in-kind-cluster.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
# Build and deploy custom porch images on a kind cluster

This section covers the process to build and deploy the porch images to a given kind cluster.

## Prerequisites
The following software should be installed prior to running:
1. [git](https://git-scm.com/)
2. [Docker](https://www.docker.com/get-started/)
Docker image load bug on v25.0.0 Use v24.0.7
3. [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)
4. [The go programming language](https://go.dev/doc/install)
5. [kind](https://kind.sigs.k8s.io/docs/user/quick-start#installation)
6. [kpt-cli](https://kpt.dev/installation/kpt-cli)
7. [yq](https://github.com/mikefarah/yq/#install)

## Create a default kind cluster

If one is not already available, create a kind cluster.

```
kind create cluster -n dev
```

Optionally, save the config for the new cluster and set it as the default config in the current shell:

```
kind get kubeconfig --name=dev > ~/.kube/kind-dev-config
export KUBECONFIG=~/.kube/kind-dev-config
```

## Clone Porch and deploy with custom images
Clone the [porch project](https://github.com/nephio-project/porch.git) from Github onto your environment using whatever cloning process for development your organization recommends.
Here, we assume Porch is cloned into a directory called `porch`.

We will now use the make target `run-in-kind-kpt` to build the images and re deploy the porch deployments to use them.

Here, we pass the following vars to the make target:

| VAR | DEFAULT | EG |
|---|---|---|
| IMAGE_TAG | $(git_tag)-dirty | test |
| KUBECONFIG | $(CURDIR)/deployments/local/kubeconfig | ~/.kube/config |
| KIND_CONTEXT_NAME | kind | dev |

```
cd porch
make run-in-kind-kpt IMAGE_TAG='test' KUBECONFIG='/home/ubuntu/.kube/config' KIND_CONTEXT_NAME='dev'
```
This will build the porch images locally with the given tag, load them in to the kind docker ctr, update the [porch kpt pkg](https://github.com/nephio-project/catalog/tree/main/nephio/core/porch) to use those images and deploy the pkg to the cluster.

> **_NOTE:_** The docker build can take some time to complete on first run. Future refinements to the make targets will reduce build and or deploy times. To skip the building of the porch images, we can pass an optional flag SKIP_IMG_BUILD=true to the above make target.
Check that the new images have been deployed:

```
kubectl get deployments -n porch-system -o wide
```

Sample output:
```
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
function-runner 2/2 2 2 81m function-runner porch-kind/porch-function-runner:test app=function-runner
porch-controllers 1/1 1 1 81m porch-controllers porch-kind/porch-controllers:test k8s-app=porch-controllers
porch-server 1/1 1 1 81m porch-server porch-kind/porch-server:test app=porch-server
```



## Deploy a minimal, in memory gitea

To facilitate testing towards a git repo, we can deploy a minimal, in memory [gitea](https://docs.gitea.com/) installation.

Here, we pass the following vars to the make target:

| VAR | DEFAULT | EG |
|---|---|---|
| KUBECONFIG | $(CURDIR)/deployments/local/kubeconfig | ~/.kube/config |

```
make deploy-gitea-dev-pkg KUBECONFIG='~/.kube/config'
```

Check that gitea have been deployed:

```
kubectl get all -n gitea
```

Sample output:
```
NAME READY STATUS RESTARTS AGE
pod/gitea-5746c4b4fc-sqgs6 1/1 Running 0 3m4s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/gitea-http ClusterIP 10.96.104.116 <none> 3000/TCP 3m4s
service/gitea-ssh ClusterIP 10.96.168.195 <none> 22/TCP 3m4s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/gitea 1/1 1 1 3m4s
NAME DESIRED CURRENT READY AGE
replicaset.apps/gitea-5746c4b4fc 1 1 1 3m4s
```

Now, we can expose the svc by using port forwarding.
In another terminal, run the following:

```
kubectl --namespace gitea port-forward svc/gitea-http 3000:3000
```

The gitea UI can now be reached in a local web browser over:

```
http://localhost:3000/
```

Admin login:

```
user : nephio
password : secret
``````

## Setup a test repo on gitea

To create a new test repo, we can use curl towards the forwarded backend svc port 3000:

```
curl -k -H "content-type: application/json" "http://nephio:secret@localhost:3000/api/v1/user/repos" --data '{"name":"testrepo"}'
```

To initialize the test repo with a "main" branch, we can clone it locally and push a default setup to it.

```
git clone http://localhost:3000/nephio/testrepo.git ~/Downloads/testrepo
cd ~/Downloads/testrepo
touch README.md
git init
git checkout -b main
git add README.md
git commit -m "first commit"
git remote add origin http://localhost:3000/nephio/testrepo.git
git push -u origin main
user : nephio
pass : secret
```

## Connect porch to the newly create repo

We can now connect porch to the test repo using the Porch Repository CR (Custom Resource).

Create a test namespace:

```
kubectl create namespace porch-test
```

Create a secret for the Gitea credentials in the porch-test namespace:

```
kubectl create secret generic gitea-porch-secret \
--namespace=porch-test \
--type=kubernetes.io/basic-auth \
--from-literal=username=nephio \
--from-literal=password=secret
```

Now, we define the test Repository CR in Porch:

```
cat <<EOF | kubectl create -f -
apiVersion: config.porch.kpt.dev/v1alpha1
kind: Repository
metadata:
name: testrepo
namespace: porch-test
spec:
description: testrepo
content: Package
deployment: false
type: git
git:
repo: http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git
directory: /
branch: main
secretRef:
name: gitea-porch-secret
EOF
```

Check that the Repository has been correctly created:

```
kubectl get repositories -n porch-test
```

Sample output:
```
NAME TYPE CONTENT DEPLOYMENT READY ADDRESS
testrepo git Package false True http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git
```
Check the logs of the porch-server

```
kubectl logs deployment/porch-server -n porch-system
```

Sample output:

```
...
I0124 11:23:10.400567 1 background.go:134] Repository added: porch-test:testrepo
I0124 11:23:10.533213 1 repository.go:364] repo git://http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git/: poll started
I0124 11:23:10.541438 1 background.go:137] Repository modified: porch-test:testrepo
I0124 11:23:10.681949 1 git.go:1702] discovered 0 packages @ecf8bed2663745165615f9390e5e3df7c0196d93
I0124 11:23:10.681964 1 repository.go:528] repo git://http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git/: addSent 0, modSent 0, delSent for 0 old and 0 new repo packages
I0124 11:23:10.681968 1 repository.go:397] repo git://http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git/: refresh finished in 0.099783 secs
I0124 11:23:10.681975 1 repository.go:365] repo git://http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git/: poll finished in 0.148768 secs
I0124 11:23:10.681979 1 repository.go:364] repo git://http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git/: poll started
I0124 11:23:10.779443 1 repository.go:397] repo git://http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git/: refresh finished in 0.047841 secs
I0124 11:23:10.779453 1 repository.go:365] repo git://http://gitea-http.gitea.svc.cluster.local:3000/nephio/testrepo.git/: poll finished in 0.097474 secs
...
```


## Teardown the custom deployments

Destroy the gitea kpt pkg resources:

```
kpt live destroy .build/deploy/kpt_pkgs/gitea-dev
```

Alternatively, we can use kubectl:

```
kubectl delete --wait --recursive --filename ./.build/deploy/kpt_pkgs/gitea-dev/
```

Destroy the porch kpt pkg resources:

```
kpt live destroy .build/deploy/kpt_pkgs/porch
```

Again, alternatively, we can use kubectl:

```
kubectl delete --wait --recursive --filename ./.build/deploy/porch/
```

Finally, we can delete the kind cluster if necessary:
```
kind delete cluster -n dev
```
Loading

0 comments on commit 267436e

Please sign in to comment.