| 1 | package Kubernetes::REST::Example; | ||
| 2 | # ABSTRACT: Working examples for Kubernetes::REST with Minikube, K3s, and other clusters | ||
| 3 | our $VERSION = '1.109'; | ||
| 4 | 1; | ||
| 5 | |||
| 6 | __END__ | ||
| 7 | |||
| 8 | =pod | ||
| 9 | |||
| 10 | =encoding UTF-8 | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Kubernetes::REST::Example - Working examples for Kubernetes::REST with Minikube, K3s, and other clusters | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | version 1.109 | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | This document walks you through setting up L<Kubernetes::REST> with a local | ||
| 23 | Kubernetes cluster and shows how to manage real resources from Perl using | ||
| 24 | L<IO::K8s> typed objects. | ||
| 25 | |||
| 26 | Every code snippet is ready to copy-paste into a script once you have a | ||
| 27 | running cluster and the Perl dependencies installed. A comprehensive | ||
| 28 | runnable demo script is included in C<eg/demo.pl>. | ||
| 29 | |||
| 30 | =head1 CLUSTER SETUP | ||
| 31 | |||
| 32 | L<Kubernetes::REST> works with any Kubernetes cluster. Below are setup | ||
| 33 | instructions for common local development environments. All of them write | ||
| 34 | a kubeconfig to C<~/.kube/config> that L<Kubernetes::REST::Kubeconfig> picks | ||
| 35 | up automatically. | ||
| 36 | |||
| 37 | =head2 Minikube | ||
| 38 | |||
| 39 | L<Minikube|https://minikube.sigs.k8s.io/> creates a single-node cluster | ||
| 40 | inside a Docker container or VM. | ||
| 41 | |||
| 42 | # Install (Debian/Ubuntu) | ||
| 43 | curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 | ||
| 44 | sudo install minikube-linux-amd64 /usr/local/bin/minikube | ||
| 45 | |||
| 46 | # Start with Docker driver | ||
| 47 | minikube start --driver=docker | ||
| 48 | |||
| 49 | # Verify | ||
| 50 | minikube status | ||
| 51 | |||
| 52 | Minikube uses self-signed certificates and token authentication. The | ||
| 53 | kubeconfig context is named C<minikube>. | ||
| 54 | |||
| 55 | B<NodePort access:> | ||
| 56 | |||
| 57 | minikube service <service-name> -n <namespace> | ||
| 58 | |||
| 59 | =head2 K3s | ||
| 60 | |||
| 61 | L<K3s|https://k3s.io/> is a lightweight Kubernetes distribution from Rancher. | ||
| 62 | It installs as a single binary and runs directly on the host (no Docker | ||
| 63 | required). | ||
| 64 | |||
| 65 | # Install | ||
| 66 | curl -sfL https://get.k3s.io | sh - | ||
| 67 | |||
| 68 | # K3s writes its own kubeconfig to a different path | ||
| 69 | export KUBECONFIG=/etc/rancher/k3s/k3s.yaml | ||
| 70 | |||
| 71 | # Or copy it so Kubernetes::REST::Kubeconfig finds it automatically | ||
| 72 | mkdir -p ~/.kube | ||
| 73 | sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config | ||
| 74 | sudo chown $USER ~/.kube/config | ||
| 75 | chmod 600 ~/.kube/config | ||
| 76 | |||
| 77 | # Verify | ||
| 78 | kubectl get nodes | ||
| 79 | |||
| 80 | B<Key differences from Minikube:> | ||
| 81 | |||
| 82 | =over 4 | ||
| 83 | |||
| 84 | =item * Kubeconfig lives at C</etc/rancher/k3s/k3s.yaml> (not C<~/.kube/config>) | ||
| 85 | |||
| 86 | =item * The server endpoint is C<https://127.0.0.1:6443> | ||
| 87 | |||
| 88 | =item * K3s includes Traefik as its default ingress controller | ||
| 89 | |||
| 90 | =item * K3s uses C<containerd> instead of Docker | ||
| 91 | |||
| 92 | =item * NodePort services are accessible directly on the host IP | ||
| 93 | |||
| 94 | =back | ||
| 95 | |||
| 96 | B<Connecting from Perl with K3s:> | ||
| 97 | |||
| 98 | use Kubernetes::REST::Kubeconfig; | ||
| 99 | |||
| 100 | # If you copied the kubeconfig to ~/.kube/config: | ||
| 101 | my $api = Kubernetes::REST::Kubeconfig->new->api; | ||
| 102 | |||
| 103 | # If using the K3s path directly: | ||
| 104 | my $api = Kubernetes::REST::Kubeconfig->new( | ||
| 105 | kubeconfig_path => '/etc/rancher/k3s/k3s.yaml', | ||
| 106 | )->api; | ||
| 107 | |||
| 108 | =head2 Kind (Kubernetes in Docker) | ||
| 109 | |||
| 110 | L<Kind|https://kind.sigs.k8s.io/> runs Kubernetes nodes as Docker containers. | ||
| 111 | Good for CI and testing. | ||
| 112 | |||
| 113 | # Install | ||
| 114 | go install sigs.k8s.io/kind@latest | ||
| 115 | |||
| 116 | # Create cluster | ||
| 117 | kind create cluster --name perl-test | ||
| 118 | |||
| 119 | # Context is named: kind-perl-test | ||
| 120 | my $api = Kubernetes::REST::Kubeconfig->new( | ||
| 121 | context_name => 'kind-perl-test', | ||
| 122 | )->api; | ||
| 123 | |||
| 124 | =head2 Any cluster with a kubeconfig | ||
| 125 | |||
| 126 | If you have a kubeconfig (EKS, GKE, AKS, self-managed, etc.): | ||
| 127 | |||
| 128 | use Kubernetes::REST::Kubeconfig; | ||
| 129 | |||
| 130 | # Uses current context from ~/.kube/config | ||
| 131 | my $api = Kubernetes::REST::Kubeconfig->new->api; | ||
| 132 | |||
| 133 | # Or specify a context | ||
| 134 | my $api = Kubernetes::REST::Kubeconfig->new( | ||
| 135 | context_name => 'my-production-cluster', | ||
| 136 | )->api; | ||
| 137 | |||
| 138 | =head1 INSTALL PERL DEPENDENCIES | ||
| 139 | |||
| 140 | cpanm Kubernetes::REST | ||
| 141 | |||
| 142 | =head1 CONNECTING TO THE CLUSTER | ||
| 143 | |||
| 144 | =head2 Using the kubeconfig (recommended) | ||
| 145 | |||
| 146 | L<Kubernetes::REST::Kubeconfig> parses your kubeconfig and sets up | ||
| 147 | authentication, SSL certificates, and the server endpoint automatically: | ||
| 148 | |||
| 149 | use Kubernetes::REST::Kubeconfig; | ||
| 150 | |||
| 151 | my $kc = Kubernetes::REST::Kubeconfig->new; | ||
| 152 | my $api = $kc->api; | ||
| 153 | |||
| 154 | say "Cluster version: " . $api->cluster_version; | ||
| 155 | |||
| 156 | If you have multiple contexts: | ||
| 157 | |||
| 158 | my $contexts = $kc->contexts; | ||
| 159 | say "Available: @$contexts"; | ||
| 160 | |||
| 161 | my $api = $kc->api('minikube'); # or 'default', 'kind-test', etc. | ||
| 162 | |||
| 163 | =head2 Manual configuration | ||
| 164 | |||
| 165 | use Kubernetes::REST; | ||
| 166 | |||
| 167 | my $api = Kubernetes::REST->new( | ||
| 168 | server => { | ||
| 169 | endpoint => 'https://127.0.0.1:6443', | ||
| 170 | ssl_verify_server => 0, | ||
| 171 | }, | ||
| 172 | credentials => { token => $token }, | ||
| 173 | resource_map_from_cluster => 0, | ||
| 174 | ); | ||
| 175 | |||
| 176 | =head1 LISTING RESOURCES | ||
| 177 | |||
| 178 | =head2 Namespaces | ||
| 179 | |||
| 180 | my $list = $api->list('Namespace'); | ||
| 181 | |||
| 182 | for my $ns (@{ $list->items }) { | ||
| 183 | printf "%-20s %s\n", | ||
| 184 | $ns->metadata->name, | ||
| 185 | $ns->status->phase // 'Unknown'; | ||
| 186 | } | ||
| 187 | |||
| 188 | =head2 Pods in a namespace | ||
| 189 | |||
| 190 | my $pods = $api->list('Pod', namespace => 'kube-system'); | ||
| 191 | |||
| 192 | for my $pod (@{ $pods->items }) { | ||
| 193 | my $name = $pod->metadata->name; | ||
| 194 | my $phase = $pod->status->phase // 'Unknown'; | ||
| 195 | my $ip = $pod->status->podIP // 'pending'; | ||
| 196 | printf "%-45s %-10s %s\n", $name, $phase, $ip; | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 Services | ||
| 200 | |||
| 201 | my $services = $api->list('Service', namespace => 'default'); | ||
| 202 | |||
| 203 | for my $svc (@{ $services->items }) { | ||
| 204 | my $name = $svc->metadata->name; | ||
| 205 | my $type = $svc->spec->type // 'ClusterIP'; | ||
| 206 | my $ports = join ', ', map { | ||
| 207 | $_->port . '/' . ($_->protocol // 'TCP') | ||
| 208 | } @{ $svc->spec->ports // [] }; | ||
| 209 | printf "%-20s %-12s %s\n", $name, $type, $ports; | ||
| 210 | } | ||
| 211 | |||
| 212 | =head2 Nodes | ||
| 213 | |||
| 214 | my $nodes = $api->list('Node'); | ||
| 215 | |||
| 216 | for my $node (@{ $nodes->items }) { | ||
| 217 | my $info = $node->status->nodeInfo; | ||
| 218 | printf "%-20s OS=%-8s kubelet=%s\n", | ||
| 219 | $node->metadata->name, | ||
| 220 | $info->operatingSystem // '?', | ||
| 221 | $info->kubeletVersion // '?'; | ||
| 222 | } | ||
| 223 | |||
| 224 | =head1 GETTING A SINGLE RESOURCE | ||
| 225 | |||
| 226 | C<get()> returns a fully typed L<IO::K8s> object: | ||
| 227 | |||
| 228 | my $pod = $api->get('Pod', 'my-pod', namespace => 'default'); | ||
| 229 | |||
| 230 | say "Name: " . $pod->metadata->name; | ||
| 231 | say "Namespace: " . $pod->metadata->namespace; | ||
| 232 | say "Kind: " . $pod->kind; # "Pod" | ||
| 233 | say "API: " . $pod->api_version; # "v1" | ||
| 234 | say "Phase: " . $pod->status->phase; | ||
| 235 | say "Node: " . ($pod->spec->nodeName // 'unscheduled'); | ||
| 236 | |||
| 237 | # Access labels (hashref) | ||
| 238 | my $labels = $pod->metadata->labels // {}; | ||
| 239 | for my $key (sort keys %$labels) { | ||
| 240 | say " $key = $labels->{$key}"; | ||
| 241 | } | ||
| 242 | |||
| 243 | =head1 CREATING RESOURCES | ||
| 244 | |||
| 245 | =head2 Namespace | ||
| 246 | |||
| 247 | my $ns = $api->new_object(Namespace => | ||
| 248 | metadata => { name => 'perl-test' }, | ||
| 249 | ); | ||
| 250 | |||
| 251 | my $created = $api->create($ns); | ||
| 252 | say "Created: " . $created->metadata->name; | ||
| 253 | |||
| 254 | =head2 ConfigMap | ||
| 255 | |||
| 256 | my $cm = $api->create($api->new_object(ConfigMap => | ||
| 257 | metadata => { | ||
| 258 | name => 'app-config', | ||
| 259 | namespace => 'perl-test', | ||
| 260 | }, | ||
| 261 | data => { | ||
| 262 | 'database.host' => 'postgres.perl-test.svc.cluster.local', | ||
| 263 | 'database.port' => '5432', | ||
| 264 | 'app.debug' => 'true', | ||
| 265 | }, | ||
| 266 | )); | ||
| 267 | |||
| 268 | =head2 Secret | ||
| 269 | |||
| 270 | use MIME::Base64 qw(encode_base64); | ||
| 271 | |||
| 272 | my $secret = $api->create($api->new_object(Secret => | ||
| 273 | metadata => { | ||
| 274 | name => 'db-credentials', | ||
| 275 | namespace => 'perl-test', | ||
| 276 | }, | ||
| 277 | type => 'Opaque', | ||
| 278 | data => { | ||
| 279 | username => encode_base64('admin', ''), | ||
| 280 | password => encode_base64('s3cret', ''), | ||
| 281 | }, | ||
| 282 | )); | ||
| 283 | |||
| 284 | =head2 LimitRange | ||
| 285 | |||
| 286 | Set default resource limits for all containers in a namespace: | ||
| 287 | |||
| 288 | my $lr = $api->create($api->new_object(LimitRange => | ||
| 289 | metadata => { | ||
| 290 | name => 'default-limits', | ||
| 291 | namespace => 'perl-test', | ||
| 292 | }, | ||
| 293 | spec => { | ||
| 294 | limits => [{ | ||
| 295 | type => 'Container', | ||
| 296 | default => { cpu => '200m', memory => '128Mi' }, | ||
| 297 | defaultRequest => { cpu => '50m', memory => '64Mi' }, | ||
| 298 | }], | ||
| 299 | }, | ||
| 300 | )); | ||
| 301 | |||
| 302 | =head2 Deployment with volumes, probes, and ServiceAccount | ||
| 303 | |||
| 304 | my $deploy = $api->create($api->new_object(Deployment => | ||
| 305 | metadata => { | ||
| 306 | name => 'my-app', | ||
| 307 | namespace => 'perl-test', | ||
| 308 | }, | ||
| 309 | spec => { | ||
| 310 | replicas => 2, | ||
| 311 | selector => { | ||
| 312 | matchLabels => { app => 'my-app' }, | ||
| 313 | }, | ||
| 314 | template => { | ||
| 315 | metadata => { | ||
| 316 | labels => { app => 'my-app' }, | ||
| 317 | annotations => { 'managed-by' => 'perl' }, | ||
| 318 | }, | ||
| 319 | spec => { | ||
| 320 | serviceAccountName => 'default', | ||
| 321 | containers => [{ | ||
| 322 | name => 'nginx', | ||
| 323 | image => 'nginx:1.27-alpine', | ||
| 324 | ports => [{ containerPort => 80, name => 'http' }], | ||
| 325 | env => [{ | ||
| 326 | name => 'APP_ENV', | ||
| 327 | valueFrom => { | ||
| 328 | configMapKeyRef => { | ||
| 329 | name => 'app-config', | ||
| 330 | key => 'app.debug', | ||
| 331 | }, | ||
| 332 | }, | ||
| 333 | }, { | ||
| 334 | name => 'DB_USER', | ||
| 335 | valueFrom => { | ||
| 336 | secretKeyRef => { | ||
| 337 | name => 'db-credentials', | ||
| 338 | key => 'username', | ||
| 339 | }, | ||
| 340 | }, | ||
| 341 | }], | ||
| 342 | volumeMounts => [{ | ||
| 343 | name => 'config-volume', | ||
| 344 | mountPath => '/etc/nginx/conf.d', | ||
| 345 | }, { | ||
| 346 | name => 'data-volume', | ||
| 347 | mountPath => '/data', | ||
| 348 | }], | ||
| 349 | resources => { | ||
| 350 | requests => { cpu => '50m', memory => '32Mi' }, | ||
| 351 | limits => { cpu => '100m', memory => '64Mi' }, | ||
| 352 | }, | ||
| 353 | livenessProbe => { | ||
| 354 | httpGet => { path => '/', port => 80 }, | ||
| 355 | initialDelaySeconds => 5, | ||
| 356 | periodSeconds => 10, | ||
| 357 | }, | ||
| 358 | readinessProbe => { | ||
| 359 | httpGet => { path => '/', port => 80 }, | ||
| 360 | initialDelaySeconds => 3, | ||
| 361 | periodSeconds => 5, | ||
| 362 | }, | ||
| 363 | }], | ||
| 364 | volumes => [{ | ||
| 365 | name => 'config-volume', | ||
| 366 | configMap => { | ||
| 367 | name => 'app-config', | ||
| 368 | items => [{ | ||
| 369 | key => 'nginx.conf', | ||
| 370 | path => 'default.conf', | ||
| 371 | }], | ||
| 372 | }, | ||
| 373 | }, { | ||
| 374 | name => 'data-volume', | ||
| 375 | persistentVolumeClaim => { | ||
| 376 | claimName => 'my-storage', | ||
| 377 | }, | ||
| 378 | }], | ||
| 379 | }, | ||
| 380 | }, | ||
| 381 | }, | ||
| 382 | )); | ||
| 383 | |||
| 384 | =head2 Service (NodePort) | ||
| 385 | |||
| 386 | my $svc = $api->create($api->new_object(Service => | ||
| 387 | metadata => { | ||
| 388 | name => 'my-app', | ||
| 389 | namespace => 'perl-test', | ||
| 390 | }, | ||
| 391 | spec => { | ||
| 392 | type => 'NodePort', | ||
| 393 | selector => { app => 'my-app' }, | ||
| 394 | ports => [{ | ||
| 395 | port => 80, | ||
| 396 | targetPort => 80, | ||
| 397 | protocol => 'TCP', | ||
| 398 | }], | ||
| 399 | }, | ||
| 400 | )); | ||
| 401 | |||
| 402 | my $node_port = $svc->spec->ports->[0]->nodePort; | ||
| 403 | say "NodePort: $node_port"; | ||
| 404 | |||
| 405 | # Minikube: minikube service my-app -n perl-test | ||
| 406 | # K3s: curl http://localhost:$node_port | ||
| 407 | |||
| 408 | =head2 Job | ||
| 409 | |||
| 410 | my $job = $api->create($api->new_object(Job => | ||
| 411 | metadata => { | ||
| 412 | name => 'batch-job', | ||
| 413 | namespace => 'perl-test', | ||
| 414 | }, | ||
| 415 | spec => { | ||
| 416 | backoffLimit => 2, | ||
| 417 | template => { | ||
| 418 | spec => { | ||
| 419 | restartPolicy => 'Never', | ||
| 420 | containers => [{ | ||
| 421 | name => 'worker', | ||
| 422 | image => 'busybox:latest', | ||
| 423 | command => ['sh', '-c', 'echo "done"; exit 0'], | ||
| 424 | }], | ||
| 425 | }, | ||
| 426 | }, | ||
| 427 | }, | ||
| 428 | )); | ||
| 429 | |||
| 430 | =head2 CronJob | ||
| 431 | |||
| 432 | my $cron = $api->create($api->new_object(CronJob => | ||
| 433 | metadata => { | ||
| 434 | name => 'scheduled-job', | ||
| 435 | namespace => 'perl-test', | ||
| 436 | }, | ||
| 437 | spec => { | ||
| 438 | schedule => '*/5 * * * *', | ||
| 439 | jobTemplate => { | ||
| 440 | spec => { | ||
| 441 | template => { | ||
| 442 | spec => { | ||
| 443 | restartPolicy => 'OnFailure', | ||
| 444 | containers => [{ | ||
| 445 | name => 'worker', | ||
| 446 | image => 'busybox:latest', | ||
| 447 | command => ['sh', '-c', 'date'], | ||
| 448 | }], | ||
| 449 | }, | ||
| 450 | }, | ||
| 451 | }, | ||
| 452 | }, | ||
| 453 | }, | ||
| 454 | )); | ||
| 455 | |||
| 456 | =head2 RBAC (Role + RoleBinding) | ||
| 457 | |||
| 458 | my $role = $api->create($api->new_object(Role => | ||
| 459 | metadata => { | ||
| 460 | name => 'pod-reader', | ||
| 461 | namespace => 'perl-test', | ||
| 462 | }, | ||
| 463 | rules => [{ | ||
| 464 | apiGroups => [''], | ||
| 465 | resources => ['pods'], | ||
| 466 | verbs => ['get', 'list', 'watch'], | ||
| 467 | }], | ||
| 468 | )); | ||
| 469 | |||
| 470 | my $binding = $api->create($api->new_object(RoleBinding => | ||
| 471 | metadata => { | ||
| 472 | name => 'read-pods', | ||
| 473 | namespace => 'perl-test', | ||
| 474 | }, | ||
| 475 | roleRef => { | ||
| 476 | apiGroup => 'rbac.authorization.k8s.io', | ||
| 477 | kind => 'Role', | ||
| 478 | name => 'pod-reader', | ||
| 479 | }, | ||
| 480 | subjects => [{ | ||
| 481 | kind => 'ServiceAccount', | ||
| 482 | name => 'default', | ||
| 483 | namespace => 'perl-test', | ||
| 484 | }], | ||
| 485 | )); | ||
| 486 | |||
| 487 | =head2 PersistentVolumeClaim | ||
| 488 | |||
| 489 | Works out of the box on Minikube (hostpath provisioner) and K3s (local-path | ||
| 490 | provisioner). | ||
| 491 | |||
| 492 | my $pvc = $api->create($api->new_object(PersistentVolumeClaim => | ||
| 493 | metadata => { | ||
| 494 | name => 'my-storage', | ||
| 495 | namespace => 'perl-test', | ||
| 496 | }, | ||
| 497 | spec => { | ||
| 498 | accessModes => ['ReadWriteOnce'], | ||
| 499 | resources => { | ||
| 500 | requests => { storage => '100Mi' }, | ||
| 501 | }, | ||
| 502 | }, | ||
| 503 | )); | ||
| 504 | |||
| 505 | =head2 ResourceQuota | ||
| 506 | |||
| 507 | my $quota = $api->create($api->new_object(ResourceQuota => | ||
| 508 | metadata => { | ||
| 509 | name => 'ns-quota', | ||
| 510 | namespace => 'perl-test', | ||
| 511 | }, | ||
| 512 | spec => { | ||
| 513 | hard => { | ||
| 514 | pods => '20', | ||
| 515 | 'requests.cpu' => '2', | ||
| 516 | 'requests.memory' => '1Gi', | ||
| 517 | }, | ||
| 518 | }, | ||
| 519 | )); | ||
| 520 | |||
| 521 | =head1 UPDATING RESOURCES | ||
| 522 | |||
| 523 | Fetch a resource, modify it, send it back: | ||
| 524 | |||
| 525 | # Scale the deployment | ||
| 526 | my $deploy = $api->get('Deployment', 'my-app', | ||
| 527 | namespace => 'perl-test', | ||
| 528 | ); | ||
| 529 | $deploy->spec->replicas(3); | ||
| 530 | my $updated = $api->update($deploy); | ||
| 531 | say "Scaled to " . $updated->spec->replicas . " replicas"; | ||
| 532 | |||
| 533 | # Rolling update (change image) | ||
| 534 | $deploy = $api->get('Deployment', 'my-app', namespace => 'perl-test'); | ||
| 535 | $deploy->spec->template->spec->containers->[0]->image('nginx:1.27-bookworm'); | ||
| 536 | my $ann = $deploy->spec->template->metadata->annotations // {}; | ||
| 537 | $ann->{'kubernetes.io/change-cause'} = 'Image update via Perl'; | ||
| 538 | $deploy->spec->template->metadata->annotations($ann); | ||
| 539 | $api->update($deploy); | ||
| 540 | |||
| 541 | # Add a label | ||
| 542 | my $pod = $api->get('Pod', 'my-pod', namespace => 'perl-test'); | ||
| 543 | my $labels = $pod->metadata->labels // {}; | ||
| 544 | $labels->{environment} = 'testing'; | ||
| 545 | $pod->metadata->labels($labels); | ||
| 546 | $api->update($pod); | ||
| 547 | |||
| 548 | =head1 PATCHING RESOURCES | ||
| 549 | |||
| 550 | C<patch()> modifies specific fields without fetching and replacing the | ||
| 551 | entire object. This avoids conflicts when multiple clients update the | ||
| 552 | same resource. | ||
| 553 | |||
| 554 | =head2 Strategic Merge Patch (default) | ||
| 555 | |||
| 556 | The Kubernetes-native patch type. Arrays are merged intelligently | ||
| 557 | based on the field's merge strategy. | ||
| 558 | |||
| 559 | # Add a label without touching other labels | ||
| 560 | my $patched = $api->patch('Pod', 'my-pod', | ||
| 561 | namespace => 'perl-test', | ||
| 562 | patch => { | ||
| 563 | metadata => { labels => { environment => 'staging' } }, | ||
| 564 | }, | ||
| 565 | ); | ||
| 566 | |||
| 567 | # Scale a deployment (one field only) | ||
| 568 | $api->patch('Deployment', 'my-app', | ||
| 569 | namespace => 'perl-test', | ||
| 570 | patch => { spec => { replicas => 5 } }, | ||
| 571 | ); | ||
| 572 | |||
| 573 | =head2 JSON Merge Patch | ||
| 574 | |||
| 575 | Simple merge where C<null> deletes a key. Arrays are replaced entirely. | ||
| 576 | |||
| 577 | # Remove an annotation | ||
| 578 | use JSON::PP (); | ||
| 579 | $api->patch('Pod', 'my-pod', | ||
| 580 | namespace => 'perl-test', | ||
| 581 | type => 'merge', | ||
| 582 | patch => { | ||
| 583 | metadata => { annotations => { 'old-key' => JSON::PP::null } }, | ||
| 584 | }, | ||
| 585 | ); | ||
| 586 | |||
| 587 | =head2 JSON Patch (RFC 6902) | ||
| 588 | |||
| 589 | An array of precise operations for surgical edits. | ||
| 590 | |||
| 591 | $api->patch('Deployment', 'my-app', | ||
| 592 | namespace => 'perl-test', | ||
| 593 | type => 'json', | ||
| 594 | patch => [ | ||
| 595 | { op => 'replace', path => '/spec/replicas', value => 3 }, | ||
| 596 | { op => 'add', path => '/metadata/labels/version', value => '2.0' }, | ||
| 597 | ], | ||
| 598 | ); | ||
| 599 | |||
| 600 | =head2 Patch vs Update | ||
| 601 | |||
| 602 | Use C<patch()> when you only need to change a few fields - it's safer | ||
| 603 | because it won't overwrite changes made by other clients between your | ||
| 604 | C<get()> and C<update()>. Use C<update()> when you need to replace the | ||
| 605 | entire spec. | ||
| 606 | |||
| 607 | Neither one ever changes a resource's C<status> once that kind has a status | ||
| 608 | subresource - and most kinds with a meaningful C<status> field do, built-in | ||
| 609 | or custom. Writing status needs C<patch_status()>/C<update_status()> instead; | ||
| 610 | see L</The status Subresource> for why, and for the CRD case specifically. | ||
| 611 | |||
| 612 | =head1 WAITING FOR READINESS | ||
| 613 | |||
| 614 | Kubernetes operations are asynchronous. Poll the API to wait for a | ||
| 615 | desired state: | ||
| 616 | |||
| 617 | # Wait for a deployment's pods to be ready | ||
| 618 | for my $i (1..60) { | ||
| 619 | my $d = $api->get('Deployment', 'my-app', namespace => 'perl-test'); | ||
| 620 | my $ready = $d->status->readyReplicas // 0; | ||
| 621 | my $desired = $d->spec->replicas // 1; | ||
| 622 | if ($ready == $desired) { | ||
| 623 | say "All $ready replicas ready!"; | ||
| 624 | last; | ||
| 625 | } | ||
| 626 | sleep 1; | ||
| 627 | } | ||
| 628 | |||
| 629 | # Wait for a job to complete | ||
| 630 | for my $i (1..60) { | ||
| 631 | my $j = $api->get('Job', 'batch-job', namespace => 'perl-test'); | ||
| 632 | if (($j->status->succeeded // 0) >= 1) { | ||
| 633 | say "Job completed at " . $j->status->completionTime; | ||
| 634 | last; | ||
| 635 | } | ||
| 636 | sleep 1; | ||
| 637 | } | ||
| 638 | |||
| 639 | # Wait for a pod to reach Running phase | ||
| 640 | for my $i (1..30) { | ||
| 641 | my $p = $api->get('Pod', 'my-pod', namespace => 'perl-test'); | ||
| 642 | my $phase = $p->status->phase // 'Unknown'; | ||
| 643 | if ($phase eq 'Running') { | ||
| 644 | say "Pod running on " . $p->spec->nodeName; | ||
| 645 | last; | ||
| 646 | } | ||
| 647 | sleep 2; | ||
| 648 | } | ||
| 649 | |||
| 650 | =head1 INSPECTING POD STATUS | ||
| 651 | |||
| 652 | my $pod = $api->get('Pod', 'my-pod', namespace => 'perl-test'); | ||
| 653 | |||
| 654 | say "Phase: " . ($pod->status->phase // 'Unknown'); | ||
| 655 | say "IP: " . ($pod->status->podIP // 'pending'); | ||
| 656 | say "Node: " . ($pod->spec->nodeName // 'unscheduled'); | ||
| 657 | |||
| 658 | # Container-level status | ||
| 659 | for my $cs (@{ $pod->status->containerStatuses // [] }) { | ||
| 660 | say "Container: " . $cs->name; | ||
| 661 | say " Ready: " . ($cs->ready ? 'yes' : 'no'); | ||
| 662 | say " Restarts: " . ($cs->restartCount // 0); | ||
| 663 | say " Image: " . $cs->image; | ||
| 664 | } | ||
| 665 | |||
| 666 | =head1 ERROR HANDLING | ||
| 667 | |||
| 668 | C<create()> croaks if the resource already exists. Use C<eval> to handle | ||
| 669 | this gracefully: | ||
| 670 | |||
| 671 | my $ns = eval { | ||
| 672 | $api->create($api->new_object(Namespace => | ||
| 673 | metadata => { name => 'perl-test' }, | ||
| 674 | )); | ||
| 675 | }; | ||
| 676 | if ($@) { | ||
| 677 | say "Already exists, fetching instead"; | ||
| 678 | $ns = $api->get('Namespace', 'perl-test'); | ||
| 679 | } | ||
| 680 | |||
| 681 | =head1 SERIALIZATION | ||
| 682 | |||
| 683 | Every L<IO::K8s> object can be serialized to JSON or YAML: | ||
| 684 | |||
| 685 | my $pod = $api->get('Pod', 'my-pod', namespace => 'perl-test'); | ||
| 686 | |||
| 687 | # To JSON | ||
| 688 | my $json = JSON::MaybeXS->new( | ||
| 689 | canonical => 1, pretty => 1, utf8 => 1, | ||
| 690 | )->encode($pod->TO_JSON); | ||
| 691 | |||
| 692 | # To YAML (suitable for kubectl apply -f) | ||
| 693 | say $pod->to_yaml; | ||
| 694 | |||
| 695 | # Save to file | ||
| 696 | $pod->save('/tmp/my-pod.yaml'); | ||
| 697 | |||
| 698 | # Round-trip: hashref -> object | ||
| 699 | my $pod2 = $api->inflate($pod->TO_JSON); | ||
| 700 | say $pod2->metadata->name; | ||
| 701 | |||
| 702 | =head1 WATCHING FOR CHANGES | ||
| 703 | |||
| 704 | The Watch API streams events as resources are added, modified, or deleted. | ||
| 705 | This is the foundation for building Kubernetes controllers. | ||
| 706 | |||
| 707 | =head2 Basic watch | ||
| 708 | |||
| 709 | $api->watch('Pod', | ||
| 710 | namespace => 'default', | ||
| 711 | timeout => 60, | ||
| 712 | on_event => sub { | ||
| 713 | my ($event) = @_; | ||
| 714 | printf "%-10s %s\n", | ||
| 715 | $event->type, | ||
| 716 | $event->object->metadata->name; | ||
| 717 | }, | ||
| 718 | ); | ||
| 719 | |||
| 720 | =head2 Resumable watch loop | ||
| 721 | |||
| 722 | # Watch indefinitely, surviving timeouts and disconnects | ||
| 723 | my $rv; | ||
| 724 | while (1) { | ||
| 725 | $rv = eval { | ||
| 726 | $api->watch('Pod', | ||
| 727 | namespace => 'default', | ||
| 728 | resourceVersion => $rv, | ||
| 729 | on_event => sub { | ||
| 730 | my ($event) = @_; | ||
| 731 | say $event->type . ": " . $event->object->metadata->name; | ||
| 732 | }, | ||
| 733 | ); | ||
| 734 | }; | ||
| 735 | if ($@ && $@ =~ /410 Gone/) { | ||
| 736 | # resourceVersion too old, re-list to get fresh state | ||
| 737 | warn "Watch expired, re-listing...\n"; | ||
| 738 | $api->list('Pod', namespace => 'default'); | ||
| 739 | $rv = undef; | ||
| 740 | } elsif ($@) { | ||
| 741 | warn "Watch error: $@\n"; | ||
| 742 | sleep 5; | ||
| 743 | } | ||
| 744 | # Normal timeout, just restart the watch | ||
| 745 | } | ||
| 746 | |||
| 747 | =head2 Watch with selectors | ||
| 748 | |||
| 749 | $api->watch('Pod', | ||
| 750 | namespace => 'default', | ||
| 751 | labelSelector => 'app=web', | ||
| 752 | fieldSelector => 'status.phase=Running', | ||
| 753 | on_event => sub { | ||
| 754 | my ($event) = @_; | ||
| 755 | say $event->type . ": " . $event->object->metadata->name; | ||
| 756 | }, | ||
| 757 | ); | ||
| 758 | |||
| 759 | =head2 Watch any resource type | ||
| 760 | |||
| 761 | # Watch deployments | ||
| 762 | $api->watch('Deployment', | ||
| 763 | namespace => 'default', | ||
| 764 | on_event => sub { | ||
| 765 | my ($event) = @_; | ||
| 766 | my $deploy = $event->object; | ||
| 767 | printf "%s %s: %d/%d ready\n", | ||
| 768 | $event->type, | ||
| 769 | $deploy->metadata->name, | ||
| 770 | $deploy->status->readyReplicas // 0, | ||
| 771 | $deploy->spec->replicas // 0; | ||
| 772 | }, | ||
| 773 | ); | ||
| 774 | |||
| 775 | # Watch namespaces (cluster-scoped) | ||
| 776 | $api->watch('Namespace', | ||
| 777 | on_event => sub { | ||
| 778 | my ($event) = @_; | ||
| 779 | say $event->type . ": " . $event->object->metadata->name; | ||
| 780 | }, | ||
| 781 | ); | ||
| 782 | |||
| 783 | =head1 DELETING RESOURCES | ||
| 784 | |||
| 785 | # Delete by object | ||
| 786 | $api->delete($pod); | ||
| 787 | |||
| 788 | # Delete by name | ||
| 789 | $api->delete('Pod', 'my-pod', namespace => 'perl-test'); | ||
| 790 | $api->delete('Deployment', 'my-app', namespace => 'perl-test'); | ||
| 791 | $api->delete('Namespace', 'perl-test'); | ||
| 792 | |||
| 793 | When cleaning up a full namespace, delete dependent resources first to | ||
| 794 | avoid errors (e.g. delete the RoleBinding before the Role it references): | ||
| 795 | |||
| 796 | for my $r ( | ||
| 797 | ['CronJob', 'scheduled-job'], | ||
| 798 | ['Job', 'batch-job'], | ||
| 799 | ['Service', 'my-app'], | ||
| 800 | ['Deployment', 'my-app'], | ||
| 801 | ['RoleBinding', 'read-pods'], | ||
| 802 | ['Role', 'pod-reader'], | ||
| 803 | ['PersistentVolumeClaim','my-storage'], | ||
| 804 | ['Secret', 'db-credentials'], | ||
| 805 | ['ConfigMap', 'app-config'], | ||
| 806 | ['ResourceQuota', 'ns-quota'], | ||
| 807 | ['LimitRange', 'default-limits'], | ||
| 808 | ) { | ||
| 809 | my ($kind, $name) = @$r; | ||
| 810 | eval { $api->delete($kind, $name, namespace => 'perl-test') }; | ||
| 811 | } | ||
| 812 | eval { $api->delete('Namespace', 'perl-test') }; | ||
| 813 | |||
| 814 | =head1 RESOURCE QUOTA INSPECTION | ||
| 815 | |||
| 816 | my $q = $api->get('ResourceQuota', 'ns-quota', | ||
| 817 | namespace => 'perl-test', | ||
| 818 | ); | ||
| 819 | my $used = $q->status->used // {}; | ||
| 820 | my $hard = $q->status->hard // {}; | ||
| 821 | for my $key (sort keys %$hard) { | ||
| 822 | printf "%-25s %s / %s\n", $key, | ||
| 823 | $used->{$key} // '0', $hard->{$key}; | ||
| 824 | } | ||
| 825 | |||
| 826 | =head1 CUSTOM RESOURCE DEFINITIONS (CRDs) | ||
| 827 | |||
| 828 | L<Kubernetes::REST> can manage Custom Resources the same way it manages | ||
| 829 | built-in ones. You define a Perl class for your CRD, register it in | ||
| 830 | the resource map, and use the standard CRUD API. | ||
| 831 | |||
| 832 | =head2 Defining the CRD in Kubernetes | ||
| 833 | |||
| 834 | First, apply a CRD to your cluster. Example - a C<StaticWebSite> resource | ||
| 835 | for hosting static websites in your homelab: | ||
| 836 | |||
| 837 | # staticwebsite-crd.yaml | ||
| 838 | apiVersion: apiextensions.k8s.io/v1 | ||
| 839 | kind: CustomResourceDefinition | ||
| 840 | metadata: | ||
| 841 | name: staticwebsites.homelab.example.com | ||
| 842 | spec: | ||
| 843 | group: homelab.example.com | ||
| 844 | versions: | ||
| 845 | - name: v1 | ||
| 846 | served: true | ||
| 847 | storage: true | ||
| 848 | schema: | ||
| 849 | openAPIV3Schema: | ||
| 850 | type: object | ||
| 851 | properties: | ||
| 852 | spec: | ||
| 853 | type: object | ||
| 854 | required: [domain, image] | ||
| 855 | properties: | ||
| 856 | domain: { type: string } | ||
| 857 | image: { type: string } | ||
| 858 | replicas: { type: integer, default: 1 } | ||
| 859 | tls: { type: boolean, default: false } | ||
| 860 | status: | ||
| 861 | type: object | ||
| 862 | properties: | ||
| 863 | readyReplicas: { type: integer } | ||
| 864 | url: { type: string } | ||
| 865 | subresources: | ||
| 866 | status: {} | ||
| 867 | scope: Namespaced | ||
| 868 | names: | ||
| 869 | plural: staticwebsites | ||
| 870 | singular: staticwebsite | ||
| 871 | kind: StaticWebSite | ||
| 872 | shortNames: [sw] | ||
| 873 | |||
| 874 | $ kubectl apply -f staticwebsite-crd.yaml | ||
| 875 | |||
| 876 | =head2 Writing a CRD class | ||
| 877 | |||
| 878 | Create a Perl class using C<IO::K8s::APIObject> with import parameters | ||
| 879 | for C<api_version> and C<resource_plural>: | ||
| 880 | |||
| 881 | package My::StaticWebSite; | ||
| 882 | use IO::K8s::APIObject | ||
| 883 | api_version => 'homelab.example.com/v1', | ||
| 884 | resource_plural => 'staticwebsites'; | ||
| 885 | with 'IO::K8s::Role::Namespaced'; | ||
| 886 | |||
| 887 | k8s spec => { Str => 1 }; | ||
| 888 | k8s status => { Str => 1 }; | ||
| 889 | 1; | ||
| 890 | |||
| 891 | Key points: | ||
| 892 | |||
| 893 | =over 4 | ||
| 894 | |||
| 895 | =item * C<api_version> - the CRD's C<group/version> (e.g. C<homelab.example.com/v1>) | ||
| 896 | |||
| 897 | =item * C<resource_plural> - must match the CRD's C<spec.names.plural> | ||
| 898 | |||
| 899 | =item * C<kind> is derived automatically from the class name (last C<::> segment) | ||
| 900 | |||
| 901 | =item * Apply C<IO::K8s::Role::Namespaced> for namespace-scoped CRDs | ||
| 902 | |||
| 903 | =item * C<spec> and C<status> are opaque hashrefs (C<< { Str => 1 } >>) | ||
| 904 | |||
| 905 | =back | ||
| 906 | |||
| 907 | =head2 Registering the CRD class | ||
| 908 | |||
| 909 | Register your class with the C<+> prefix in the resource map: | ||
| 910 | |||
| 911 | my $api = Kubernetes::REST::Kubeconfig->new->api; | ||
| 912 | $api->resource_map->{StaticWebSite} = '+My::StaticWebSite'; | ||
| 913 | |||
| 914 | Or pass it at construction time: | ||
| 915 | |||
| 916 | my $api = Kubernetes::REST->new( | ||
| 917 | server => { endpoint => '...' }, | ||
| 918 | credentials => { token => '...' }, | ||
| 919 | resource_map => { | ||
| 920 | %{ IO::K8s->default_resource_map }, | ||
| 921 | StaticWebSite => '+My::StaticWebSite', | ||
| 922 | }, | ||
| 923 | ); | ||
| 924 | |||
| 925 | =head2 CRUD on Custom Resources | ||
| 926 | |||
| 927 | Once registered, CRDs work exactly like built-in resources: | ||
| 928 | |||
| 929 | use JSON::PP (); | ||
| 930 | |||
| 931 | # Create | ||
| 932 | my $site = $api->create($api->new_object(StaticWebSite => | ||
| 933 | metadata => { | ||
| 934 | name => 'my-blog', | ||
| 935 | namespace => 'default', | ||
| 936 | }, | ||
| 937 | spec => { | ||
| 938 | domain => 'blog.example.com', | ||
| 939 | image => 'nginx:1.27-alpine', | ||
| 940 | replicas => 2, | ||
| 941 | tls => JSON::PP::true, | ||
| 942 | }, | ||
| 943 | )); | ||
| 944 | |||
| 945 | # Get | ||
| 946 | my $site = $api->get('StaticWebSite', 'my-blog', namespace => 'default'); | ||
| 947 | say $site->spec->{domain}; # blog.example.com | ||
| 948 | |||
| 949 | # List | ||
| 950 | my $list = $api->list('StaticWebSite', namespace => 'default'); | ||
| 951 | for my $s (@{ $list->items }) { | ||
| 952 | say $s->metadata->name . ": " . $s->spec->{domain}; | ||
| 953 | } | ||
| 954 | |||
| 955 | # Update | ||
| 956 | $site->spec->{replicas} = 3; | ||
| 957 | $api->update($site); | ||
| 958 | |||
| 959 | # Delete | ||
| 960 | $api->delete('StaticWebSite', 'my-blog', namespace => 'default'); | ||
| 961 | |||
| 962 | B<Note:> For boolean CRD fields, use C<JSON::PP::true> and C<JSON::PP::false> | ||
| 963 | instead of C<1> and C<0>. The Kubernetes API validates JSON types strictly | ||
| 964 | for custom resources. | ||
| 965 | |||
| 966 | =head2 The status Subresource | ||
| 967 | |||
| 968 | The CRD defined above already carries a status subresource -- the | ||
| 969 | C<subresources: { status: {} }> line under C<spec.versions[0]>. That single | ||
| 970 | line splits C<spec> and C<status> into two separate write paths. | ||
| 971 | |||
| 972 | B<Once it's set, C<create>, C<update>, C<patch> and server-side apply all | ||
| 973 | stop writing C<status>.> The API server silently strips the C<status> stanza | ||
| 974 | from every write to the main endpoint and still answers 2xx, so the call | ||
| 975 | looks like it worked and nothing is stored: | ||
| 976 | |||
| 977 | # Looks fine. Does nothing to status. | ||
| 978 | $site->status({ readyReplicas => 2 }); | ||
| 979 | $api->update($site); # 200 OK | ||
| 980 | |||
| 981 | my $refetched = $api->get('StaticWebSite', 'my-blog', namespace => 'default'); | ||
| 982 | say defined $refetched->status ? 'status was written' : 'status is still undef'; | ||
| 983 | # status is still undef | ||
| 984 | |||
| 985 | Status has to go through the C</status> subresource instead, with | ||
| 986 | L<Kubernetes::REST/patch_status> or L<Kubernetes::REST/update_status>: | ||
| 987 | |||
| 988 | # Partial update. Merge patch by default -- custom resources answer | ||
| 989 | # 415 to a strategic merge patch, so patch_status() doesn't use one. | ||
| 990 | my $updated = $api->patch_status('StaticWebSite', 'my-blog', | ||
| 991 | namespace => 'default', | ||
| 992 | patch => { status => { readyReplicas => 2, url => 'blog.example.com' } }, | ||
| 993 | ); | ||
| 994 | |||
| 995 | # Full replace, like update() but scoped to status. | ||
| 996 | $site->status({ readyReplicas => 2, url => 'blog.example.com' }); | ||
| 997 | $updated = $api->update_status($site); | ||
| 998 | |||
| 999 | The built-in kinds (C<Pod>, C<Node>, C<Deployment>, ...) have always worked | ||
| 1000 | this way -- see L</INSPECTING POD STATUS> for reading one. Any controller | ||
| 1001 | that writes status, for a built-in kind or your own CRD, needs C<patch_status> | ||
| 1002 | or C<update_status>; C<create>/C<update>/C<patch> stay for C<spec> and | ||
| 1003 | C<metadata>. | ||
| 1004 | |||
| 1005 | =head2 Serialization | ||
| 1006 | |||
| 1007 | CRD objects support the same serialization as built-in resources: | ||
| 1008 | |||
| 1009 | say $site->to_yaml; | ||
| 1010 | # --- | ||
| 1011 | # apiVersion: homelab.example.com/v1 | ||
| 1012 | # kind: StaticWebSite | ||
| 1013 | # metadata: | ||
| 1014 | # name: my-blog | ||
| 1015 | # namespace: default | ||
| 1016 | # spec: | ||
| 1017 | # domain: blog.example.com | ||
| 1018 | # ... | ||
| 1019 | |||
| 1020 | $site->save('/tmp/my-blog.yaml'); | ||
| 1021 | |||
| 1022 | =head2 AutoGen (dynamic class generation) | ||
| 1023 | |||
| 1024 | If you don't want to write Perl classes by hand, you can generate them | ||
| 1025 | dynamically using L<IO::K8s::AutoGen>: | ||
| 1026 | |||
| 1027 | use IO::K8s::AutoGen; | ||
| 1028 | |||
| 1029 | my $class = IO::K8s::AutoGen::get_or_generate( | ||
| 1030 | 'com.example.homelab.v1.StaticWebSite', # definition name | ||
| 1031 | $schema, # OpenAPI schema | ||
| 1032 | {}, # all definitions | ||
| 1033 | 'MyApp::K8s', # namespace | ||
| 1034 | api_version => 'homelab.example.com/v1', | ||
| 1035 | kind => 'StaticWebSite', | ||
| 1036 | resource_plural => 'staticwebsites', | ||
| 1037 | is_namespaced => 1, | ||
| 1038 | ); | ||
| 1039 | |||
| 1040 | $api->resource_map->{StaticWebSite} = "+$class"; | ||
| 1041 | |||
| 1042 | # Now use it like any other resource | ||
| 1043 | my $site = $api->k8s->struct_to_object($class, { | ||
| 1044 | metadata => { name => 'my-blog', namespace => 'default' }, | ||
| 1045 | spec => { domain => 'blog.example.com', image => 'nginx' }, | ||
| 1046 | }); | ||
| 1047 | $api->create($site); | ||
| 1048 | |||
| 1049 | =head2 More CRD ideas | ||
| 1050 | |||
| 1051 | Custom resources can simplify complex Kubernetes patterns for your homelab: | ||
| 1052 | |||
| 1053 | =over 4 | ||
| 1054 | |||
| 1055 | =item * B<BackupSchedule> - wraps CronJob for scheduled backups with retention policies | ||
| 1056 | |||
| 1057 | =item * B<DatabaseCluster> - wraps StatefulSet + PVC + Service for database instances | ||
| 1058 | |||
| 1059 | =item * B<HomeService> - simplified Deployment + Service + optional Ingress | ||
| 1060 | |||
| 1061 | =item * B<DNSRecord> - manages external DNS entries alongside cluster resources | ||
| 1062 | |||
| 1063 | =back | ||
| 1064 | |||
| 1065 | =head1 IO::K8s CLASS REFERENCE | ||
| 1066 | |||
| 1067 | The objects returned by L<Kubernetes::REST> are all L<IO::K8s> classes. | ||
| 1068 | Here are the most commonly used ones: | ||
| 1069 | |||
| 1070 | =over 4 | ||
| 1071 | |||
| 1072 | =item L<IO::K8s::Api::Core::V1::Pod> - Pods | ||
| 1073 | |||
| 1074 | =item L<IO::K8s::Api::Core::V1::Service> - Services | ||
| 1075 | |||
| 1076 | =item L<IO::K8s::Api::Core::V1::ConfigMap> - ConfigMaps | ||
| 1077 | |||
| 1078 | =item L<IO::K8s::Api::Core::V1::Secret> - Secrets | ||
| 1079 | |||
| 1080 | =item L<IO::K8s::Api::Core::V1::Namespace> - Namespaces | ||
| 1081 | |||
| 1082 | =item L<IO::K8s::Api::Core::V1::PersistentVolumeClaim> - PersistentVolumeClaims | ||
| 1083 | |||
| 1084 | =item L<IO::K8s::Api::Core::V1::ResourceQuota> - ResourceQuotas | ||
| 1085 | |||
| 1086 | =item L<IO::K8s::Api::Core::V1::LimitRange> - LimitRanges | ||
| 1087 | |||
| 1088 | =item L<IO::K8s::Api::Core::V1::ServiceAccount> - ServiceAccounts | ||
| 1089 | |||
| 1090 | =item L<IO::K8s::Api::Apps::V1::Deployment> - Deployments | ||
| 1091 | |||
| 1092 | =item L<IO::K8s::Api::Batch::V1::Job> - Jobs | ||
| 1093 | |||
| 1094 | =item L<IO::K8s::Api::Batch::V1::CronJob> - CronJobs | ||
| 1095 | |||
| 1096 | =item L<IO::K8s::Api::Rbac::V1::Role> - Roles | ||
| 1097 | |||
| 1098 | =item L<IO::K8s::Api::Rbac::V1::RoleBinding> - RoleBindings | ||
| 1099 | |||
| 1100 | =item L<IO::K8s::Api::Networking::V1::Ingress> - Ingress | ||
| 1101 | |||
| 1102 | =item L<IO::K8s::Apimachinery::Pkg::Apis::Meta::V1::ObjectMeta> - object metadata | ||
| 1103 | |||
| 1104 | =back | ||
| 1105 | |||
| 1106 | All classes share these patterns: | ||
| 1107 | |||
| 1108 | $obj->metadata->name; # resource name | ||
| 1109 | $obj->metadata->namespace; # namespace (if namespaced) | ||
| 1110 | $obj->metadata->labels; # hashref of labels | ||
| 1111 | $obj->metadata->uid; # unique ID assigned by Kubernetes | ||
| 1112 | $obj->kind; # "Pod", "Service", etc. | ||
| 1113 | $obj->api_version; # "v1", "apps/v1", etc. | ||
| 1114 | $obj->TO_JSON; # serialize to hashref | ||
| 1115 | $obj->to_yaml; # serialize to YAML string | ||
| 1116 | |||
| 1117 | See L<IO::K8s> for full documentation on the object system, including | ||
| 1118 | type safety, YAML loading, and custom resource support. | ||
| 1119 | |||
| 1120 | =head1 DEMO SCRIPT | ||
| 1121 | |||
| 1122 | This distribution ships with a comprehensive runnable demo at | ||
| 1123 | C<eg/demo.pl>. It exercises most of the CRUD and watch functionality | ||
| 1124 | documented above in 22 steps against a live cluster (CRDs are not part of | ||
| 1125 | the script; see L</CUSTOM RESOURCE DEFINITIONS (CRDs)> above for those): | ||
| 1126 | |||
| 1127 | perl eg/demo.pl | ||
| 1128 | |||
| 1129 | The script connects via kubeconfig, creates a dedicated namespace, then | ||
| 1130 | walks through: LimitRange, ResourceQuota, ConfigMap, Secret, ServiceAccount, | ||
| 1131 | Role + RoleBinding, PersistentVolumeClaim, Deployment (2 replicas with | ||
| 1132 | volumes, probes, ConfigMap/Secret env vars), Service (NodePort), pod | ||
| 1133 | inspection, scaling to 3 replicas, patching (strategic merge and JSON merge), | ||
| 1134 | rolling image update, Job (run to completion), CronJob, utility Pod (with | ||
| 1135 | volume mounts and env vars from ConfigMap and Secret), full namespace | ||
| 1136 | inventory, YAML/JSON serialization with round-trip inflate, quota usage | ||
| 1137 | inspection, a short pod watch, and ordered cleanup. | ||
| 1138 | |||
| 1139 | It works on Minikube, K3s, Kind, or any cluster with a kubeconfig. | ||
| 1140 | |||
| 1141 | =head1 SEE ALSO | ||
| 1142 | |||
| 1143 | L<Kubernetes::REST>, L<Kubernetes::REST::Kubeconfig>, L<IO::K8s>, | ||
| 1144 | L<https://minikube.sigs.k8s.io/>, L<https://k3s.io/>, | ||
| 1145 | L<https://kind.sigs.k8s.io/>, L<https://kubernetes.io/docs/> | ||
| 1146 | |||
| 1147 | =head1 SUPPORT | ||
| 1148 | |||
| 1149 | =head2 Issues | ||
| 1150 | |||
| 1151 | Please report bugs and feature requests on GitHub at | ||
| 1152 | L<https://github.com/pplu/kubernetes-rest/issues>. | ||
| 1153 | |||
| 1154 | =head2 IRC | ||
| 1155 | |||
| 1156 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||
| 1157 | |||
| 1158 | =head1 CONTRIBUTING | ||
| 1159 | |||
| 1160 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 1161 | |||
| 1162 | =head1 AUTHORS | ||
| 1163 | |||
| 1164 | =over 4 | ||
| 1165 | |||
| 1166 | =item * | ||
| 1167 | |||
| 1168 | Torsten Raudssus <getty@cpan.org> | ||
| 1169 | |||
| 1170 | =item * | ||
| 1171 | |||
| 1172 | Jose Luis Martinez Torres <jlmartin@cpan.org> | ||
| 1173 | |||
| 1174 | =back | ||
| 1175 | |||
| 1176 | =head1 COPYRIGHT AND LICENSE | ||
| 1177 | |||
| 1178 | This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>. | ||
| 1179 | |||
| 1180 | This is free software, licensed under: | ||
| 1181 | |||
| 1182 | The Apache License, Version 2.0, January 2004 | ||
| 1183 | |||
| 1184 | =cut |