|
| 1 | +--- |
| 2 | +description: > |
| 3 | + How to setup a development environment for contributing to kube-bind. |
| 4 | +title: kcp |
| 5 | +--- |
| 6 | + |
| 7 | +# Development Environment using kcp |
| 8 | + |
| 9 | +All the instructions assume you have already cloned the kube-bind repository and have Go installed. |
| 10 | + |
| 11 | +kcp requires initial setup to be run before it can be used. This includes setting up workspace/provider |
| 12 | +and setting up all the `APIResourceSchemas` and `APIExports`. |
| 13 | + |
| 14 | +This is not required if you are doing deeper integration, and controlling the setup with your own scripts. |
| 15 | + |
| 16 | +It's good to have the kcp CLI installed to help with workspace management: |
| 17 | + |
| 18 | +```bash |
| 19 | +kubectl krew index add kcp-dev https://github.com/kcp-dev/krew-index.git |
| 20 | +kubectl krew install kcp-dev/kcp |
| 21 | +kubectl krew install kcp-dev/ws |
| 22 | +kubectl krew install kcp-dev/create-workspace |
| 23 | +``` |
| 24 | + |
| 25 | +## Preparation |
| 26 | + |
| 27 | +Start kcp: |
| 28 | + |
| 29 | +```bash |
| 30 | +make run-kcp |
| 31 | +``` |
| 32 | + |
| 33 | +## Backend |
| 34 | + |
| 35 | +### Bootstrap kcp |
| 36 | + |
| 37 | +This is a dedicated step to set up kcp with required workspaces and `APIExports` for kube-bind: |
| 38 | + |
| 39 | +```bash |
| 40 | +cp .kcp/admin.kubeconfig .kcp/backend.kubeconfig |
| 41 | +export KUBECONFIG=.kcp/backend.kubeconfig |
| 42 | + |
| 43 | +./bin/kcp-init --kcp-kubeconfig $KUBECONFIG |
| 44 | +``` |
| 45 | + |
| 46 | +### Run the Backend |
| 47 | + |
| 48 | +```bash |
| 49 | +kubectl ws use :root:kube-bind |
| 50 | + |
| 51 | +go run ./cmd/backend \ |
| 52 | + --multicluster-runtime-provider kcp \ |
| 53 | + --oidc-issuer-url=http://127.0.0.1:8080/oidc \ |
| 54 | + --oidc-callback-url=http://127.0.0.1:8080/api/callback \ |
| 55 | + --oidc-type=embedded \ |
| 56 | + --pretty-name="BigCorp.com" \ |
| 57 | + --namespace-prefix="kube-bind-" \ |
| 58 | + --schema-source apiresourceschemas \ |
| 59 | + --consumer-scope=cluster |
| 60 | +``` |
| 61 | + |
| 62 | +Optionally add `--fronend=http://localhost:3000` to point to local frontend (must be running |
| 63 | +separately). |
| 64 | + |
| 65 | +This process will keep running, so open a new terminal. |
| 66 | + |
| 67 | +## Provider |
| 68 | + |
| 69 | +### Kubeconfig Setup |
| 70 | + |
| 71 | +Copy the kubeconfig to the provider and create provider workspace: |
| 72 | + |
| 73 | +```bash |
| 74 | +cp .kcp/admin.kubeconfig .kcp/provider.kubeconfig |
| 75 | +export KUBECONFIG=.kcp/provider.kubeconfig |
| 76 | +kubectl ws use :root |
| 77 | +kubectl create-workspace provider --enter |
| 78 | +``` |
| 79 | + |
| 80 | +### APIExport |
| 81 | + |
| 82 | +Bind the APIExport to the provider workspace: |
| 83 | + |
| 84 | +```bash |
| 85 | +kubectl kcp bind apiexport root:kube-bind:kube-bind.io \ |
| 86 | + --accept-permission-claim clusterrolebindings.rbac.authorization.k8s.io \ |
| 87 | + --accept-permission-claim clusterroles.rbac.authorization.k8s.io \ |
| 88 | + --accept-permission-claim customresourcedefinitions.apiextensions.k8s.io \ |
| 89 | + --accept-permission-claim serviceaccounts.core \ |
| 90 | + --accept-permission-claim configmaps.core \ |
| 91 | + --accept-permission-claim secrets.core \ |
| 92 | + --accept-permission-claim subjectaccessreviews.authorization.k8s.io \ |
| 93 | + --accept-permission-claim namespaces.core \ |
| 94 | + --accept-permission-claim roles.rbac.authorization.k8s.io \ |
| 95 | + --accept-permission-claim rolebindings.rbac.authorization.k8s.io \ |
| 96 | + --accept-permission-claim apiresourceschemas.apis.kcp.io |
| 97 | +``` |
| 98 | + |
| 99 | +### Create CRD in provider |
| 100 | + |
| 101 | +```bash |
| 102 | +kubectl apply -f contrib/kcp/deploy/examples/apiexport.yaml |
| 103 | +kubectl apply -f contrib/kcp/deploy/examples/apiresourceschema-cowboys.yaml |
| 104 | +kubectl apply -f contrib/kcp/deploy/examples/apiresourceschema-sheriffs.yaml |
| 105 | +kubectl kcp bind apiexport root:provider:cowboys-stable |
| 106 | + |
| 107 | +kubectl apply -f deploy/examples/template-cowboys.yaml |
| 108 | +kubectl apply -f deploy/examples/template-sheriffs.yaml |
| 109 | +kubectl apply -f deploy/examples/collection.yaml |
| 110 | +``` |
| 111 | + |
| 112 | +### Retrieve the LogicalCluster |
| 113 | + |
| 114 | +```bash |
| 115 | +kubectl get logicalcluster |
| 116 | +# NAME PHASE URL |
| 117 | +# cluster Ready https://192.168.2.166:6443/clusters/2myqz7lt9i0u5kzb |
| 118 | +``` |
| 119 | + |
| 120 | +## Consumer |
| 121 | + |
| 122 | +### Initialization |
| 123 | + |
| 124 | +Now we gonna initiate consumer: |
| 125 | + |
| 126 | +```bash |
| 127 | +cp .kcp/admin.kubeconfig .kcp/consumer.kubeconfig |
| 128 | +export KUBECONFIG=.kcp/consumer.kubeconfig |
| 129 | + |
| 130 | +kubectl ws use :root |
| 131 | +kubectl ws create consumer --enter |
| 132 | +``` |
| 133 | + |
| 134 | +### Binding |
| 135 | + |
| 136 | +Bind the thing: |
| 137 | + |
| 138 | +```bash |
| 139 | +./bin/kubectl-bind login http://127.0.0.1:8080 --cluster 1nso4d2rvleempdp |
| 140 | +./bin/kubectl-bind --dry-run --cluster-identity-namespace default -o yaml > apiserviceexport.yaml |
| 141 | + |
| 142 | +# Extract secret for binding process. Note that secret name is not the same as output from command above. Check secret |
| 143 | +# name by running `kubectl get secret -n kube-bind` |
| 144 | +kubectl get secrets -n kube-bind -o jsonpath='{.items[0].data.kubeconfig}' | base64 -d > remote.kubeconfig |
| 145 | + |
| 146 | +namespace=$(yq '.contexts[0].context.namespace' remote.kubeconfig) |
| 147 | + |
| 148 | +./bin/kubectl-bind apiservice \ |
| 149 | + --remote-kubeconfig remote.kubeconfig \ |
| 150 | + --remote-namespace "$namespace" \ |
| 151 | + --skip-konnector \ |
| 152 | + -f apiserviceexport.yaml \ |
| 153 | + -v 6 |
| 154 | +``` |
| 155 | + |
| 156 | +This will keep running, so switch to a new terminal. |
| 157 | + |
| 158 | +### Launch Konnector |
| 159 | + |
| 160 | +Start konnector: |
| 161 | + |
| 162 | +```bash |
| 163 | +./bin/konnector --lease-namespace default --kubeconfig .kcp/consumer.kubeconfig --server-address ":9090" |
| 164 | +``` |
| 165 | + |
| 166 | +Create example resources in consumer: |
| 167 | + |
| 168 | +```bash |
| 169 | +kubectl apply -f deploy/examples/cr-cowboy.yaml |
| 170 | +kubectl apply -f deploy/examples/cr-sheriff.yaml |
| 171 | +``` |
0 commit comments