Cloud Native at, runs command at schedule by creating a Pod.
apiVersion: example.kazk.dev/v1alpha1
kind: At
metadata:
name: example
spec:
schedule: "2020-12-17T19:21:00Z"
command: [echo, "will be executed"]Example Kubernetes controller programming-kubernetes/cnat in Rust using kube.
- Changed
commandto an array - Removed
Pendingfromstatus.phase.None->Running->Done - Changed to use
apiextensions.k8s.io/v1and added the requiredschema- The schema is generated by
kube(clux/kube-rs#348) scheduleis validated asdate-time
- The schema is generated by
Generated CRD
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: ats.example.kazk.dev
spec:
group: example.kazk.dev
names:
kind: At
plural: ats
shortNames: []
singular: at
scope: Namespaced
versions:
- additionalPrinterColumns: []
name: v1alpha1
schema:
openAPIV3Schema:
description: "Auto-generated derived type for AtSpec via `CustomResource`"
properties:
spec:
description: Spec for custom resource At.
properties:
command:
items:
type: string
type: array
schedule:
format: date-time
type: string
required:
- command
- schedule
type: object
status:
description: Status for custom resource At.
nullable: true
properties:
phase:
description: Describes the status of the scheduled command.
enum:
- Running
- Done
type: string
required:
- phase
type: object
required:
- spec
title: At
type: object
served: true
storage: true
subresources:
status: {}.
├── k8s/ CRD and example
└── src/
├── bin/ Binaries
│ ├── cnat.rs - Run controller (default-run)
│ └── crd.rs - Output CRD YAML
├── controller.rs Implements the Controller
├── lib.rs
└── resource.rs Defines the Custom Resource
cargo run: Run controllercargo run --bin crd: Output CRD
# Create CRD
kubectl apply -f k8s/ats.yml
kubectl wait --for=condition=NamesAccepted crd/ats.example.kazk.dev
# Start controller
cargo run
# Create At resource example
kubectl apply -f k8s/example.yml# Set up service account
kubectl apply -f k8s/rbac.yml
# Create CRD
kubectl apply -f k8s/ats.yml
kubectl wait --for=condition=NamesAccepted crd/ats.example.kazk.dev
# Deploy controller
kubectl apply -f k8s/deployment.yml
# Create At resource example
kubectl apply -f k8s/example.ymlBuilding on Arch Linux:
client-go: 31MB (23MB with-ldflags "-s -w")kubebuilder: TBD requires unknown version of KubeBuilderoperator-sdk: TBD requires unknown version of Operator SDKkube: 8.3MB (5.8MB withRUSTFLAGS='-C link-arg=-s')
| Library | Files | Lines | Code | Comments | Blanks |
|---|---|---|---|---|---|
| client-go | 29 | 2161 | 1212 | 658 | 291 |
| kubebuilder | 16 | 1013 | 529 | 357 | 127 |
| operator-sdk | 13 | 714 | 488 | 134 | 92 |
| kube | 5 | 257 | 217 | 15 | 25 |
client-go:tokei -e cnat-client-go/vendor -e cnat-client-go/hack -t Go cnat-client-gokubebuilder:tokei -e cnat-kubebuilder/vendor -e cnat-kubebuilder/hack -t Go cnat-kubebuilderoperator-sdk:tokei -e cnat-operator/vendor -t Go cnat-operatorkube:tokei -t Rust
client-go: Go modules works fine (3 core modules + 1 code geerator + 1 logger). Versioning has improved since programming-kubernetes/cnat was written. A lot of generated code.kubebuilder: Requires KubeBuilder tool. Build failed withGOPATHnot defined. Uses dep.operator-sdk: Requires Operator SDK tool. Haven't tried. Uses dep.kube: Standard Rust project. No separate codegen and very straightforward. CRD v1 schema is generated from the spec struct clux/kube-rs#348 :)