Skip to content

Instantly share code, notes, and snippets.

@mamachanko
Last active February 13, 2023 16:05
Show Gist options
  • Save mamachanko/be5240c942225a50bca44d7ae4c7a6df to your computer and use it in GitHub Desktop.
Save mamachanko/be5240c942225a50bca44d7ae4c7a6df to your computer and use it in GitHub Desktop.
Will it sync? 📡
#!/usr/bin/env bash
# -----
#
# 📡 Syncing resources between workspaces and physical clusters with kcp.
#
# Note: This script is not meant to be run, but serves to record the steps taken.
#
# -----
# create kind cluster
kind create cluster
# create a custom API on the kind cluster
cat <<EOF | kubectl apply -f-
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: things.mamachanko.com
spec:
group: mamachanko.com
names:
kind: Thing
listKind: ThingList
plural: things
singular: thing
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
stuff:
type: string
served: true
storage: true
EOF
# get kcp
git clone git@github.com:kcp-dev/kcp.git
cd kcp
# build and install kcp binaries
make build-all install
# build the syncer image and load it into the kind cluster
KO_DOCKER_REPO=kind.local \
KIND_CLUSTER_NAME=kind \
ko build \
--base-import-paths \
--platform=linux/amd64 \
./cmd/syncer
# assert that image is loaded
for cluster in $(kind get clusters); do
printf "#\n# images in cluster: %s\n#\n" "$cluster"
docker exec -it "${cluster}-control-plane" crictl images
done
# start kcp in another terminal with
# $ kcp start
# target kcp
export KUBECONFIG=$PWD/.kcp/admin.kubeconfig
# switch to the root workspace
k kcp ws use root
# create a location workspace
k kcp ws create locations --type root:organization
# switch to the location workspace
k kcp ws use root:locations
# register the kind cluster with our custom API as a sync target
k kcp workload sync kind \
--syncer-image kind.local/syncer \
--output-file syncer.yaml \
--resources things.mamachanko.com
# target kind
export KUBECONFIG=~/.kube/config
# install syncer
k apply -f syncer.yaml
# assert that the syncer is healthy
k get deploy -A
# target kcp
export KUBECONFIG=$PWD/.kcp/admin.kubeconfig
# switch to the location workspace (technically redundant, but just to be sure)
k kcp ws use root:locations
# assert that the SyncTarget and Location are well
k get synctarget,location
# assert that the custom API is available
k api-resources | grep things
# assert that the custom API is available as an APIResourceSchema
k get apiresourceschema | grep things
# assert that the custom API is included in the APIExport "kubernetes"
k get apiexport kubernetes -oyaml | yq .spec.latestResourceSchemas
# use the root workspace
k ws use root
# create a workspace for APIs
k kcp ws create apis --type root:organization
# use the APIs workspace
k ws use root:apis
# bind compute from the locations workspace
k kcp bind compute root:locations
# assert that there's a Placement
k get placement
# assert that the custom API is *not* present/bound
k api-resources | grep things
# bind compute *and* the "kubernetes" APIExport from the locations workspace
k kcp bind compute root:locations --apiexports root:locations:kubernetes
# assert that the custom API *is* present/bound
k api-resources | grep things
# create a custom API resource (in the APIs workspace)
cat <<EOF | k apply -f-
---
apiVersion: mamachanko.com/v1alpha1
kind: Thing
metadata:
name: test
namespace: default
spec: {}
EOF
# assert that is has been created
k get things
# target kind
export KUBECONFIG=~/.kube/config
# assert that *no* custom API resources have been synced down
k get things
# target kcp
export KUBECONFIG=$PWD/.kcp/admin.kubeconfig
# create a deployment
k create deployment kuard --image=gcr.io/kuar-demo/kuard-amd64:blue --port=8080 --replicas=2
# target kind
export KUBECONFIG=~/.kube/config
# assert that the deployment has been synced
k get deploy -A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment