This kubebuilder-based Kubernetes operator copies a Secret
to another namespace and synchronizes it with the custom resource SecretMirror
.
- Get
SecretMirror
from request. - If
SecretMirror
doesn't exist, just finish the reconciliation. If error occurs, retry later. - Get
Secret
(fromSecret) withSecretMirror
's name fromfromNamespace
Namespace
. - If
Secret
(fromSecret) doesn't exist, delete the correspondingSecret
(toSecret) if exists. If error occurs, retry later. - Create
toSecret
if not exists. - Check if
toSecret
is managed by secret-mirror-controller. - Update
toSecret
data if data is changed.
-
Install
secret-mirror-operator
.kubectl apply -k github.com/bebit/secret-mirror-operator/config/default
-
Create
src
anddst
namespace.kubectl apply -f config/samples/namespace-dst.yaml,config/samples/namespace-src.yaml
-
Create
Secret
insrc
namespace.kubectl apply -f config/samples/secret.yaml
-
Create
SecretMirror
indst
namespace.kubectl apply -f config/samples/secret_v1alpha1_secretmirror.yaml
apiVersion: secret.bebit.com/v1alpha1 kind: SecretMirror metadata: name: secret namespace: dst spec: fromNamespace: src
-
Check
Secret
indst
namespace.kubectl get secret secret -n dst -o yaml
apiVersion: v1 data: foo: YmFy kind: Secret metadata: creationTimestamp: "2021-12-30T01:14:26Z" name: secret namespace: dst ownerReferences: - apiVersion: secret.bebit.com/v1alpha1 blockOwnerDeletion: true controller: true kind: SecretMirror name: secret uid: f1709c26-6497-40b5-84a7-da13c38cf05f resourceVersion: "41781" uid: 81452942-2562-4f14-be8b-e4c3c856cae6 type: Opaque
-
Change
Secret
indst
Namespace manually.kubectl patch secret secret -p "{\"data\":{\"manually\": \"$(echo updated | base64 -)\"}}" -n dst
The controller keeps the Secret same as the original
Secret
.kubectl get secret secret -n dst -o jsonpath='{.data}' {"foo":"YmFy"}
-
Change
Secret
insrc
Namespace manually.kubectl patch secret secret -p "{\"data\":{\"srcSecret\": \"$(echo updated | base64 -)\"}}" -n src
The controller keeps the Secret same as the original
Secret
.kubectl get secret secret -n dst -o jsonpath='{.data}' {"foo":"YmFy","srcSecret":"dXBkYXRlZAo="}
-
Delete
Secret
indst
Namespace.kubectl delete sercet dst -n dst
The controller recreates the Secret.
kubectl get secret secret -n dst -o jsonpath='{.data}' {"foo":"YmFy","srcSecret":"dXBkYXRlZAo="}
-
Delete
Secret
insrc
Namespace.kubectl delete secret secret -n src
The controller deletes the Secret in
dst
Namespace.kubectl get secret -n dst NAME TYPE DATA AGE default-token-wwsmc kubernetes.io/service-account-token 3 2m19s
-
Recreate
Secret
insrc
Namespace.kubectl apply -f config/samples/secret.yaml
The controller creates a new Secret in
dst
Namespace.kubectl get secret secret -n dst -o jsonpath='{.data}' {"foo":"YmFy"}