A Kubernetes operator built with kubebuilder that monitors the pcf-vault-tls secret (updated by cert-manager) and automatically creates keystore and truststore secrets in the PCF namespace.
This operator:
- Watches the
pcf-vault-tlssecret in the source namespace - Automatically triggers when cert-manager updates the secret
- Converts TLS certificates to Java keystore (JKS) format
- Creates/updates
pcf-keystoreandpcf-truststoresecrets in the target namespace
- Go 1.21+
- kubectl configured to access your Kubernetes cluster
- Docker (for building the container image)
- kubebuilder tools (optional, for regeneration -
makewill install them) - Cert-manager installed in your cluster
pcf-vault-tlssecret created by cert-manager
Update the deployment arguments in config/manager/manager.yaml:
args:
- --source-secret-name=pcf-vault-tls # Name of secret to watch
- --source-secret-namespace=default # Namespace of source secret
- --target-namespace=pcf # Where to create keystore secrets
- --keystore-secret-name=pcf-keystore # Name of keystore secret
- --truststore-secret-name=pcf-truststore # Name of truststore secretSet the keystore password via environment variable or secret:
env:
- name: KEYSTORE_PASSWORD
valueFrom:
secretKeyRef:
name: keystore-credentials
key: passwordkubectl create secret generic keystore-credentials \
--from-literal=password=your-secure-password \
-n system# Build the Docker image
make docker-build
# Or with a custom image name
make docker-build IMG=your-registry/keystore-operator:v1.0.0
# Push to registry (if needed)
make docker-push IMG=your-registry/keystore-operator:v1.0.0
# Update the image in kustomize
cd config/manager
kustomize edit set image controller=your-registry/keystore-operator:v1.0.0
# Deploy to cluster
make deploy# Check operator pod
kubectl get pods -n system -l control-plane=controller-manager
# Check logs
kubectl logs -n system -l control-plane=controller-manager
# Verify secrets were created
kubectl get secrets -n pcf | grep -E "pcf-keystore|pcf-truststore"Run the operator locally (requires kubectl access to cluster):
make run# Run tests
make test
# Check formatting
make fmt
# Run linter
make vet# Build binary
make build
# Build Docker image
make docker-build- Reconciliation Loop: The operator uses controller-runtime to watch all Secret resources
- Secret Filtering: The reconciler checks if the secret matches
pcf-vault-tlsin the source namespace - Certificate Extraction: Extracts
tls.crt,tls.key, and optionallyca.crtfrom the secret - Keystore Creation:
- Converts certificate and key to PKCS12 format using OpenSSL
- Converts PKCS12 to JKS format using keytool
- Truststore Creation:
- Uses CA certificate if available
- Falls back to server certificate if no CA is present
- Secret Management: Creates or updates secrets in the target namespace
--source-secret-name: Name of the source TLS secret (default:pcf-vault-tls)--source-secret-namespace: Namespace of the source secret (default:default)--target-namespace: Namespace where keystore secrets are created (default:pcf)--keystore-secret-name: Name of the keystore secret (default:pcf-keystore)--truststore-secret-name: Name of the truststore secret (default:pcf-truststore)--keystore-password: Password for keystores (or useKEYSTORE_PASSWORDenv var)
KEYSTORE_PASSWORD: Password for keystore/truststore files
The operator creates two secrets in the target namespace:
- Contains:
keystore.jks - Format: JKS (Java KeyStore)
- Includes: Server certificate and private key
- Contains:
truststore.jks - Format: JKS (Java KeyStore)
- Includes: CA certificate (or server certificate if no CA)
kubectl logs -n system -l control-plane=controller-manager -fkubectl get secret pcf-vault-tls -n default -o yamlkubectl get clusterrole keystore-operator-manager-role
kubectl get clusterrolebinding keystore-operator-manager-rolebinding- Secret not found: Verify
pcf-vault-tlsexists in the source namespace - Permission denied: Check ClusterRole and ClusterRoleBinding
- Invalid certificates: Verify cert-manager has issued valid certificates
- Keystore creation fails: Check that OpenSSL and keytool are available in the container
make undeployproject_operator/
├── api/ # API definitions
│ └── v1alpha1/ # v1alpha1 API version
├── config/ # Kubernetes manifests
│ ├── crd/ # Custom Resource Definitions
│ ├── manager/ # Manager deployment
│ ├── rbac/ # RBAC configurations
│ └── default/ # Kustomize default config
├── internal/ # Internal packages
│ └── controller/ # Controller implementation
├── hack/ # Helper scripts
├── main.go # Entry point
├── Dockerfile # Container image
├── Makefile # Build automation
└── go.mod # Go dependencies
Copyright 2024. Licensed under the Apache License, Version 2.0.