Skip to content

alirezarezvani/claude-skills / engineering/skills/kubernetes-operator

kubernetes-operator

Unverified

Use when building a Kubernetes operator or CRD (controller-runtime, kubebuilder, operator-sdk, KOPF), before writing the controller. Validates CRDs with crd_validator.py, lints reconcile loops with reconcile_lint.py and scores OperatorHub capability level.

FileKindSize
SKILL.mdskill md10.7 KB
assets/crd_template.yamlconfig2.6 KB
assets/reconcile_skeleton.goscript4.0 KB
references/crd_design.mdmarkdown6.8 KB
references/operator_pattern.mdmarkdown7.0 KB
references/reconcile_loop.mdmarkdown7.1 KB
references/tooling_landscape.mdmarkdown7.5 KB
scripts/crd_validator.pyexecutablescript5.8 KB
scripts/operator_capability_audit.pyexecutablescript5.8 KB
scripts/reconcile_lint.pyexecutablescript6.3 KB

assets/reconcile_skeleton.go

View on GitHub
// Reconcile skeleton — passes reconcile_lint.py.
// Replace <PLACEHOLDER> markers; rename receiver + types to match your CR.
package controllers

import (
	"context"
	"errors"
	"time"

	apierrors "k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/api/meta"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
	"sigs.k8s.io/controller-runtime/pkg/log"
	"sigs.k8s.io/controller-runtime/pkg/predicate"

	appsv1alpha1 "<MODULE>/api/v1alpha1"
)

const finalizerName = "<group>/finalizer"

type MyAppReconciler struct {
	client.Client
	Scheme *runtime.Scheme
}

func (r *MyAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	logger := log.FromContext(ctx).WithValues("myapp", req.NamespacedName)

	var cr appsv1alpha1.MyApp
	if err := r.Get(ctx, req.NamespacedName, &cr); err != nil {
		if apierrors.IsNotFound(err) {
			return ctrl.Result{}, nil
		}
		return ctrl.Result{}, err
	}

	if !cr.DeletionTimestamp.IsZero() {
		return r.reconcileDelete(ctx, &cr)
	}

	if !controllerutil.ContainsFinalizer(&cr, finalizerName) {
		controllerutil.AddFinalizer(&cr, finalizerName)
		return ctrl.Result{}, r.Update(ctx, &cr)
	}

	meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
		Type:               "Reconciling",
		Status:             metav1.ConditionTrue,
		Reason:             "InProgress",
		Message:            "Converging to desired state",
		ObservedGeneration: cr.Generation,
	})

	res, recErr := r.reconcileNormal(ctx, &cr)

	if recErr == nil {
		meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
			Type: "Ready", Status: metav1.ConditionTrue,
			Reason: "AllReady", Message: "all components healthy",
			ObservedGeneration: cr.Generation,
		})
	} else {
		meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
			Type: "Ready", Status: metav1.ConditionFalse,
			Reason: "ReconcileError", Message: recErr.Error(),
			ObservedGeneration: cr.Generation,
		})
	}

	cr.Status.ObservedGeneration = cr.Generation

	if statusErr := r.Status().Update(ctx, &cr); statusErr != nil {
		logger.Error(statusErr, "failed to update status")
		return res, errors.Join(recErr, statusErr)
	}
	return res, recErr
}

func (r *MyAppReconciler) reconcileNormal(ctx context.Context, cr *appsv1alpha1.MyApp) (ctrl.Result, error) {
	// Idempotent: read desired, build child, CreateOrUpdate.
	deployment := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: cr.Name, Namespace: cr.Namespace}}
	op, err := controllerutil.CreateOrUpdate(ctx, r.Client, deployment, func() error {
		deployment.Spec.Replicas = &cr.Spec.Replicas
		// Build container spec from cr.Spec — extracted helper for clarity
		// deployment.Spec.Template.Spec.Containers = buildContainers(&cr.Spec)
		return controllerutil.SetControllerReference(cr, deployment, r.Scheme)
	})
	if err != nil {
		return ctrl.Result{}, err
	}
	log.FromContext(ctx).Info("deployment", "operation", op)

	// Periodic resync — keeps status fresh even when nothing changes.
	return ctrl.Result{RequeueAfter: 5 * time.Minute}, nil
}

func (r *MyAppReconciler) reconcileDelete(ctx context.Context, cr *appsv1alpha1.MyApp) (ctrl.Result, error) {
	if !controllerutil.ContainsFinalizer(cr, finalizerName) {
		return ctrl.Result{}, nil
	}
	if err := r.deleteExternalResources(ctx, cr); err != nil {
		return ctrl.Result{RequeueAfter: 30 * time.Second}, err
	}
	controllerutil.RemoveFinalizer(cr, finalizerName)
	return ctrl.Result{}, r.Update(ctx, cr)
}

func (r *MyAppReconciler) deleteExternalResources(ctx context.Context, cr *appsv1alpha1.MyApp) error {
	// Implement teardown of external state (cloud DB, S3 bucket, DNS record, ...)
	return nil
}

func (r *MyAppReconciler) SetupWithManager(mgr ctrl.Manager) error {
	return ctrl.NewControllerManagedBy(mgr).
		For(&appsv1alpha1.MyApp{}).
		Owns(&appsv1.Deployment{}).
		WithEventFilter(predicate.GenerationChangedPredicate{}).
		Complete(r)
}
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 323kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall