-
Notifications
You must be signed in to change notification settings - Fork 383
Open
Description
π§ 1. Advanced Reconciliation Patterns
β Leader Election
- Ensures only one instance of your Operator is actively reconciling at a time in an HA setup.
- Prevents multiple controllers from conflicting over the same resources.
π How It Works
- Uses
ConfigMaporLeaseAPI to track the leader. - Only one Operator instance holds the lock at a time.
π Example: Enabling Leader Election in Kubebuilder
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
LeaderElection: true,
LeaderElectionID: "laravel-operator-leader",
})β Rate Limiting and Backoff
- Why? Avoids spamming the Kubernetes API when failures occur.
- Uses
ctrl.Result{RequeueAfter: time.Second * X}to retry with delay instead of continuous retries.
π Example:
return ctrl.Result{RequeueAfter: time.Minute}, nilβ Managing Dependencies Between Resources
- What if your LaravelApp depends on a Database?
- Use OwnerReferences to ensure related objects get deleted together.
π Example: Link LaravelApp to a Database
controllerutil.SetControllerReference(laravelApp, databaseInstance, r.Scheme)π‘οΈ 2. Security & RBAC (Role-Based Access Control)
Operators interact with Kubernetes APIs, so RBAC permissions must be carefully restricted.
π Example: Restrict Access to Only LaravelApp Resources
Modify config/rbac/role.yaml:
rules:
- apiGroups: ["laravel.example.com"]
resources: ["laravelapps"]
verbs: ["get", "list", "watch", "create", "update", "delete"]π 3. Multi-Tenancy & Multi-Cluster Operators
β Multi-Tenancy
- Some Operators should manage separate tenants (e.g., per namespace).
- Avoid cross-namespace resource conflicts.
π How?
- Use NamespaceSelectors to filter resources.
namespaceSelector:
matchLabels:
tenant: "customer-a"β Multi-Cluster Operators
- Standard Operators work within a single cluster, but you can also build cross-cluster operators.
- Uses API Aggregation or external controllers to sync resources across clusters.
π Example: Multi-Cluster Operator Using kubeconfig
cfg, err := clientcmd.BuildConfigFromFlags("", "/path/to/kubeconfig")
multiClusterClient, err := client.New(cfg, client.Options{})π‘ 4. Event Recording & Logging Best Practices
Your Operator should provide clear logs and events for debugging.
β Emit Kubernetes Events for Better Observability
Instead of just logging, use Kubernetes events to surface information.
π Example: Emitting Events
r.Recorder.Event(laravelApp, corev1.EventTypeNormal, "Created", "Created Laravel Deployment")π Viewing Events
kubectl describe laravelapp my-laravel-appπ§© 5. CRD Versioning & Upgrades
- Why? CRDs evolve over time (
v1alpha1 β v1beta1 β v1). - Use Case: If you need to change API fields without breaking old resources.
β Managing CRD Upgrades
- Implement conversion webhooks to translate old CR versions.
- Define multiple API versions in
crd.yaml.
π Example: CRD Versioning
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
spec:
versions:
- name: v1alpha1
served: true
storage: false
- name: v1beta1
served: true
storage: trueπ΅οΈ 6. Debugging & Performance Optimization
β Common Debugging Tools
kubectl logsβ Check logs for errors.kubectl describeβ View CRD status.kubectl get eventsβ Look for Kubernetes errors.- Enable Debug Mode in Manager
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))π₯ 7. Operator Best Practices for Production
| Best Practice | Why It Matters |
|---|---|
| Always use Finalizers | Prevents orphaned resources when CR is deleted |
| Use RBAC Minimally | Limits security risks |
| Emit Events & Logs | Helps debugging issues |
Use LeaderElection |
Ensures high availability |
| Optimize Reconciliation | Avoid excessive API calls |
| Implement CRD Versioning | Ensures smooth upgrades |
π― Final Notes
You now have everything you need for any Operator-related interview question!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels