# Kubernetes operator examples

ipsonic-probe provides **black-box checks** for Kubernetes services. It does **not** replace kubelet, kubectl, Prometheus, or the Kubernetes API, and it does not require a Kubernetes Python SDK or kubectl.

## Where to execute checks

- **Inside a pod:** Kubernetes DNS names (`*.svc.cluster.local`), ClusterIP endpoints, and the Kubernetes API Service are normally reachable. Pod network policies and service mesh rules still apply.
- **Outside the cluster:** Ingress and externally published DNS/TLS endpoints can be monitored independently. ClusterIP and in-cluster DNS names generally cannot be resolved or reached without a tunnel or network integration.
- Use Nagios, Icinga 2, Checkmk, AWX, or a scheduled systemd task to run the CLI. The probe does not need its own scheduler or database.

## Examples

| YAML | What it tests | Important limitation |
|---|---|---|
| 186 | API server `/readyz` | A successful HTTP response is not full cluster health |
| 187 | API server `/livez` | Liveness only |
| 188 | API server `/version` | Requires authorization to the non-resource URL; expected major version is illustrative |
| 189 | External Ingress `/healthz` | Application-level status only |
| 190 | Internal ClusterIP `/health` | Requires in-cluster networking |
| 191 | Headless Service DNS A | Record presence, not all EndpointSlices |
| 192 | CoreDNS service DNS A | DNS resolution, not CoreDNS internals |
| 193 | Ingress TLS fingerprint | Fingerprint evidence can change during legitimate renewal |
| 194 | Metrics Server TCP | TCP connectivity only |
| 195 | Node exporter HTTP `/metrics` | HTTP status only, not Prometheus metric evaluation |
| 196 | GitOps controller health | Adapt endpoint to controller/version |
| 197 | External application DNS A | DNS record check, not external-dns controller status |

## Kubernetes API credentials

The Kubernetes API server usually requires a **Bearer ServiceAccount token** and trust in the cluster CA. The examples use `auth.token_env` and `tls.ca_file_env`, never embedded secrets.

A dedicated ServiceAccount can be configured with minimal RBAC for non-resource URLs. Verify the actual authorization requirements in your cluster; avoid `cluster-admin`. Example manifest: `integrations/kubernetes/ipsonic-serviceaccount.yaml`.

For a short-lived interactive test **inside a pod** with an automounted ServiceAccount token:

```sh
export IPSONIC_K8S_TOKEN="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"
export IPSONIC_K8S_CA_FILE=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
python3 ipsonic-probe.py --validate --strict examples/186-k8s-apiserver-readyz.yaml
python3 ipsonic-probe.py examples/186-k8s-apiserver-readyz.yaml --output nagios
unset IPSONIC_K8S_TOKEN
```

**Security:** Environment variables can be visible to privileged processes and inherited by child processes. Do not print tokens, use `set -x`, or pass tokens on the command line. Prefer dedicated short-lived ServiceAccount credentials, restrictive RBAC, secure process isolation, and secret injection by your orchestration system. ServiceAccount tokens mounted in pods may rotate: reread them for each execution. Do not place tokens in Git, YAML examples, logs, or baseline files.

For external AWX jobs, do **not** assume an in-cluster ServiceAccount token is available. Use AWX-managed credentials or Vault to inject the token and CA file into the job environment; verify connectivity and RBAC. Consult [AWX tutorial](awx-tutorial.html).

## Install using distribution packages

Install Python 3, Requests, and PyYAML from native distribution repositories as described in the [User Guide](user-guide.html). No `kubectl`, Helm, Kubernetes SDK, pip, or additional runtime package is required for these checks.

## Validate and run

```sh
python3 ipsonic-probe.py --validate --strict examples/190-k8s-service-clusterip.yaml
python3 ipsonic-probe.py examples/190-k8s-service-clusterip.yaml --output nagios
python3 ipsonic-probe.py --inventory examples/ --format tsv
```

Check the DNS/TCP/HTTP examples against real network conditions before deploying. `--validate` checks syntax, not Kubernetes connectivity or RBAC.
