This is an example Achilles SDK based controller showcasing SDK basics.
It implements the AccessToken
CRD, which allows creating a Kubernetes bearer token with
specified permissions.
-
Clone the
achilles-token-controller
.git clone [email protected]:reddit/achilles-token-controller.git
-
Ensure you have k3d installed.
-
Deploy a local cluster with k3d.
k3d cluster create orch
-
Verify the above command updated your
kubecontext
to the k3d cluster.kubectl config current-context
The output should be:
k3d-orch
-
Build the controller image.
make docker
-
Load the controller image into the k3d cluster
k3d image import achilles-token-controller:latest -c orch
-
Open
manifests/base/manager.yaml
and replaceimage: REPLACE-ME
withimage: achilles-token-controller:latest
. If this file doesn't exist, runmake generate
. -
Create the namespace for the controller
kubectl create namespace achilles-system
-
Deploy the controller.
kubectl apply -f manifests/base/manager.yaml
-
Test the controller with this example AccessToken.
apiVersion: group.example.com/v1alpha1 kind: AccessToken metadata: name: test namespace: default spec: namespacedPermissions: - namespace: default rules: - apiGroups: [""] resources: ["configmaps"] verbs: ["*"] - namespace: kube-system rules: - apiGroups: [""] resources: ["configmaps"] verbs: ["get", "list", "watch"] clusterPermissions: rules: - apiGroups: [""] resources: ["namespaces"] verbs: ["get", "list", "watch"]
-
Check that the AccessToken was processed successfully
kubectl get accesstoken test -n default -oyaml
You should see the following status condition, indicating that the object was instantiated successfully.
status: conditions: - lastTransitionTime: "2024-10-24T17:33:35Z" message: All conditions successful. observedGeneration: 1 reason: ConditionsSuccessful status: "True" type: Ready
You'll also see that it provisioned a deploy token as a secret, whose name is under
status.tokenSecretRef
. -
As a bonus, we can use
kubectl auth can-i
(docs here) check that the deploy token in fact has the permissions that we declared for it. We first need to locate the Service Account that the AccessToken was created for, which can be found understatus.resourceRefs
withkind: ServiceAccount
.kubectl auth can-i --as=system:serviceaccount:default:test create configmaps -n default # should report yes kubectl auth can-i --as=system:serviceaccount:default:test create configmaps -n kube-system # should report no kubectl auth can-i --as=system:serviceaccount:default:test list configmaps -n kube-system # should report yes kubectl auth can-i --as=system:serviceaccount:default:test create namespaces # should report no kubectl auth can-i --as=system:serviceaccount:default:test list namespaces # should report yes