• Stars
    star
    134
  • Rank 261,117 (Top 6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This repo contains binaries that built from libraries in cli-runtime.

cli-utils

cli-utils is a collection of Go libraries designed to facilitate bulk actuation of Kubernetes resource objects by wraping and enahancing kubectl apply with a more user friendly abstraction.

While the name indicates a focus on CLI utilities, the project has evolved to encompass a broader scope, including CLI use and server-side use in GitOps controllers.

Features

  1. Pruning
  2. Status Interpretation
  3. Status Lookup
  4. Diff & Preview
  5. Waiting for Reconciliation
  6. Resource Ordering
  7. Explicit Dependency Ordering
  8. Implicit Dependency Ordering
  9. Apply Time Mutation
  10. CLI Printers

Pruning

The Applier automatically deletes objects that were previously applied and then removed from the input set on a subsequent apply.

The current implementation of kubectl apply --prune uses labels to identify the set of previously applied objects in the prune set calculation. But the use of labels has significant downsides. The current kubectl apply --prune implementation is alpha, and it is improbable that it will graduate to beta. cli-utils attempts to address the current kubectl apply --prune deficiencies by storing the set of previously applied objects in an inventory object which is applied to the cluster. The reference implementation uses a ConfigMap as an inventory object, and references to the applied objects are stored in the data section of the ConfigMap.

The following example illustrates a ConfigMap resource used as an inventory object:

apiVersion: v1
kind: ConfigMap
metadata:
  # DANGER: Do not change the inventory object namespace.
  # Changing the namespace will cause a loss of continuity
  # with previously applied grouped objects. Set deletion
  # and pruning functionality will be impaired.
  namespace: test-namespace
  # NOTE: The name of the inventory object does NOT have
  # any impact on group-related functionality such as
  # deletion or pruning.
  name: inventory-26306433
  labels:
    # DANGER: Do not change the value of this label.
    # Changing this value will cause a loss of continuity
    # with previously applied grouped objects. Set deletion
    # and pruning functionality will be impaired.
    cli-utils.sigs.k8s.io/inventory-id: 46d8946c-c1fa-4e1d-9357-b37fb9bae25f

Status Interpretation

The kstatus library can be used to read an object's current status and interpret whether that object has be reconciled (aka Current) or not, including whether it is expected to never reconcile (aka Failed).

Status Lookup

In addition to performing interpretation of status from an object in-memory, cli-utils can also be used to query status from the server, allowing you to retrieve the status of previously or concurrently applied objects.

Diff & Preview

cli-utils can be used to compare local object manifests with remote objects from the server. These can be compared locally with diff or remotely with preview (aka dry-run). This can be useful for discovering drift or previewing which changes would be made, if the local manifests were applied.

Waiting for Reconciliation

The Applier automatically watches applied and deleted objects and tracks their status, blocking until the objects have reconciled, failed, or been fully deleted.

This functionality is similar to kubectl delete <resource> <name> --wait, in that it waits for all finalizers to complete, except it also works for creates and updates.

While there is a kubectl apply <resource> <name> --wait, it only waits for deletes when combined with --prune. cli-utils provides an alternative that works for all spec changes, waiting for reconciliation, the convergence of status to the desired specification. After reconciliation, it is expected that the object has reached a steady state until the specification is changed again.

Resource Ordering

The Applier and Destroyer use resource type to determine which order to apply and delete objects.

In contrast, when using kubectl apply, the objects are applied in alphanumeric order of their file names, and top to bottom in each file. With cli-utils, this manual sorting is unnecessary for many common use cases.

Explicit Dependency Ordering

While resource ordering provides a smart default user experience, sometimes resource type alone is not enough to determine desired ordering. In these cases, the user can use explicit dependency ordering by adding a config.kubernetes.io/depends-on: <OBJECT_REFERENCE> annotation to an object.

The Applier and Destroyer use these explicit dependency directives to build a dependency tree and flatten it for determining apply ordering. When deleting, the order is reversed, ensuring that dependencies are not deleted before the objects that depend on them (aka dependents).

In addition to ordering the applies and deletes, dependency ordering also waits for dependency reconciliation when applying and deletion finalization when deleting. This ensures that dependencies are not just applied first, but have reconciled before their dependents are applied. Likewise, dependents are not just deleted first, but have completed finalization before their dependencies are deleted.

Also, because dependency ordering is enforced during actuation, a dependency cannot be pruned by the Applier unless all its dependents are also deleted. This prevents accidental premature deletion of objects that are still in active use.

In the following example, the config.kubernetes.io/depends-on annotation identifies that pod-c must be successfully applied prior to pod-a actuation:

apiVersion: v1
kind: Pod
metadata:
  name: pod-a
  annotations:
    config.kubernetes.io/depends-on: /namespaces/default/Pod/pod-c
spec:
  containers:
    - name: kubernetes-pause
      image: registry.k8s.io/pause:2.0

Implicit Dependency Ordering

In addition to being able to specify explicit dependencies, cli-utils automatically detects some implicit dependencies.

Implicit dependencies include:

  1. Namespace-scoped resource objects depend on their Namespace.
  2. Custom resource objects depend on their Custom Resource Definition

Like resource ordering, implicit dependency ordering improves the apply and delete experience to reduce the need to manually specify ordering for many common use cases. This allows more objects to be applied together all at once, with less manual orchestration.

Apply-Time Mutation

The Applier can dynamically modify objects before applying them, performing field value substitution using input(s) from dependency fields.

This allows for applying objects together in a set that you would otherwise need to seperate into multiple sets, with manual modifications between applies.

Apply-Time Mutation is configured using the config.kubernetes.io/apply-time-mutation annotation on the target object to be modified. The annotation may specify one or more substitutions. Each substitution includes a source object, and source field path, and a target field path, with an optional token.

If the token is specified, the token is replaced in the target field value string with the source field value. If the token is not specified, the whole target field value is replaced with the source field value. This alternatively allows either templated interpretation or type preservation.

The source and target field paths are specified using JSONPath, allowing for robust navigation of complex resource field hierarchies using a familiar syntax.

In the following example, pod-a will substitute the IP address and port from the spec and status of the source pod-b into the spec of the target pod-a:

kind: Pod
apiVersion: v1
metadata:
  name: pod-a
  annotations:
    config.kubernetes.io/apply-time-mutation: |
      - sourceRef:
          kind: Pod
          name: pod-b
        sourcePath: $.status.podIP
        targetPath: $.spec.containers[?(@.name=="nginx")].env[?(@.name=="SERVICE_HOST")].value
        token: ${pob-b-ip}
      - sourceRef:
          kind: Pod
          name: pod-b
        sourcePath: $.spec.containers[?(@.name=="nginx")].ports[?(@.name=="tcp")].containerPort
        targetPath: $.spec.containers[?(@.name=="nginx")].env[?(@.name=="SERVICE_HOST")].value
        token: ${pob-b-port}
spec:
  containers:
  - name: nginx
    image: nginx:1.21
    ports:
    - name: tcp
      containerPort: 80
    env:
    - name: SERVICE_HOST
      value: "${pob-b-ip}:${pob-b-port}"

The primary reason to do this with Apply-Time Mutation, instead of client-side manifest templating is that the pod IP is populated by a controller at runtime during reconciliation, and is not known before applying.

That said, this is a toy example using built-in types. For pods, you probably actually want to use DNS for service discovery instead.

Most use cases for Apply-Time Mutation are actually using custom resources, as a temporary alternative to building higher level abstractions, modifying interfaces, or creating dependencies between otherwise independent interfaces.

CLI Printers

Since the original intent of cli-utils was to contain common code for CLIs, and end-to-end testing requires a reference implementation, a few printers are included to translate from the primary event stream into STDOUT text:

  1. Event Printer: The event printer just prints text to STDOT whenever an event is recieved.
  2. JSON Printer: The JSON printer converts events into a JSON string per line, intended for automated interpretation by machine.
  3. Table Printer: The table printer writes and updates in-place a table with one object per line, intended for human consumption.

Packages

β”œβ”€β”€ cmd: the kapply CLI command β”œβ”€β”€ examples: examples that serve as additional end-to-end tests using mdrip β”œβ”€β”€ hack: hacky scripts used by make β”œβ”€β”€ pkg β”‚Β Β  β”œβ”€β”€ apis: API resources that satisfy the kubernetes Object interface β”‚Β Β  β”œβ”€β”€ apply: bulk applier and destroyer β”‚Β Β  β”œβ”€β”€ common: placeholder for common tools that should probably have their own package β”‚Β Β  β”œβ”€β”€ config: inventory config bootstrapping β”‚Β Β  β”œβ”€β”€ errors: error printing β”‚Β Β  β”œβ”€β”€ flowcontrol: flow control enablement discovery β”‚Β Β  β”œβ”€β”€ inventory: inventory resource reference implementation β”‚Β Β  β”œβ”€β”€ jsonpath: utility for using jsonpath to read & write Unstructured object fields β”‚Β Β  β”œβ”€β”€ kstatus: object status event watcher with ability to reduce status to a single enum β”‚Β Β  β”œβ”€β”€ manifestreader: bolk resource object manifest reading and parsing β”‚Β Β  β”œβ”€β”€ multierror: error composition β”‚Β Β  β”œβ”€β”€ object: library for dealing with Unstructured objects β”‚Β Β  β”œβ”€β”€ ordering: sort functionality for objects β”‚Β Β  β”œβ”€β”€ print: CLI output β”‚Β Β  β”œβ”€β”€ printers: CLI output β”‚Β Β  └── testutil: utility for facilitating testing β”œβ”€β”€ release: goreleaser config β”œβ”€β”€ scripts: scripts used by make └── test: end-to-end and stress tests

kapply

To facilitate testing, this repository includes a reference CLI called kapply. The kapply tool is not intended for direct consumer use, but may be useful when trying to determine how to best utilize the cli-utils library packages.

Community, discussion, contribution, and support

Learn how to engage with the Kubernetes community on the community page.

You can reach the maintainers of this project at:

Code of conduct

Participation in the Kubernetes community is governed by the Kubernetes Code of Conduct.

More Repositories

1

kubespray

Deploy a Production Ready Kubernetes Cluster
Jinja
14,679
star
2

kind

Kubernetes IN Docker - local clusters for testing Kubernetes
Go
12,623
star
3

kustomize

Customization of kubernetes YAML configurations
Go
10,363
star
4

kubebuilder

Kubebuilder - SDK for building Kubernetes APIs using CRDs
Go
7,298
star
5

external-dns

Configure external DNS servers (AWS Route53, Google CloudDNS and others) for Kubernetes Ingresses and Services
Go
6,672
star
6

krew

πŸ“¦ Find and install kubectl plugins
Go
6,009
star
7

metrics-server

Scalable and efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines.
Go
4,761
star
8

aws-load-balancer-controller

A Kubernetes controller for Elastic Load Balancers
Go
3,703
star
9

descheduler

Descheduler for Kubernetes
Go
3,444
star
10

cluster-api

Home for Cluster API, a subproject of sig-cluster-lifecycle
Go
2,944
star
11

kui

A hybrid command-line/UI development experience for cloud-native development
TypeScript
2,701
star
12

nfs-subdir-external-provisioner

Dynamic sub-dir volume provisioner on a remote NFS server.
Shell
2,244
star
13

controller-runtime

Repo for the controller-runtime subproject of kubebuilder (sig-apimachinery)
Go
2,240
star
14

kwok

Kubernetes WithOut Kubelet - Simulates thousands of Nodes and Clusters.
Go
2,182
star
15

aws-iam-authenticator

A tool to use AWS IAM credentials to authenticate to a Kubernetes cluster
Go
2,008
star
16

prometheus-adapter

An implementation of the custom.metrics.k8s.io API using Prometheus
Go
1,662
star
17

gateway-api

Repository for the next iteration of composite service (e.g. Ingress) and load balancing APIs.
Go
1,452
star
18

cri-tools

CLI and validation tools for Kubelet Container Runtime Interface (CRI) .
Go
1,333
star
19

secrets-store-csi-driver

Secrets Store CSI driver for Kubernetes secrets - Integrates secrets stores with Kubernetes via a CSI volume.
Go
1,139
star
20

kueue

Kubernetes-native Job Queueing
Go
986
star
21

sig-storage-local-static-provisioner

Static provisioner of local volumes
Go
973
star
22

scheduler-plugins

Repository for out-of-tree scheduler plugins based on scheduler framework.
Go
957
star
23

aws-ebs-csi-driver

CSI driver for Amazon EBS https://aws.amazon.com/ebs/
Go
883
star
24

apiserver-builder-alpha

apiserver-builder-alpha implements libraries and tools to quickly and easily build Kubernetes apiservers/controllers to support custom resource types based on APIServer Aggregation
Go
764
star
25

etcdadm

Go
748
star
26

kube-scheduler-simulator

The simulator for the Kubernetes scheduler
Go
706
star
27

aws-efs-csi-driver

CSI Driver for Amazon EFS https://aws.amazon.com/efs/
Go
668
star
28

controller-tools

Tools to use with the controller-runtime libraries
Go
655
star
29

krew-index

Plugin index for https://github.com/kubernetes-sigs/krew. This repo is for plugin maintainers.
624
star
30

security-profiles-operator

The Kubernetes Security Profiles Operator
C
622
star
31

node-feature-discovery

Node feature discovery for Kubernetes
Go
595
star
32

cluster-api-provider-aws

Kubernetes Cluster API Provider AWS provides consistent deployment and day 2 operations of "self-managed" and EKS Kubernetes clusters on AWS.
Go
592
star
33

hierarchical-namespaces

Home of the Hierarchical Namespace Controller (HNC). Adds hierarchical policies and delegated creation to Kubernetes namespaces for improved in-cluster multitenancy.
Go
532
star
34

cluster-proportional-autoscaler

Kubernetes Cluster Proportional Autoscaler Container
Go
519
star
35

sig-storage-lib-external-provisioner

Go
502
star
36

alibaba-cloud-csi-driver

CSI Plugin for Kubernetes, Support Alibaba Cloud EBS/NAS/OSS/CPFS/LVM.
Go
500
star
37

application

Application metadata descriptor CRD
Go
488
star
38

custom-metrics-apiserver

Framework for implementing custom metrics support for Kubernetes
Go
457
star
39

e2e-framework

A Go framework for end-to-end testing of components running in Kubernetes clusters.
Go
395
star
40

cluster-capacity

Cluster capacity analysis
Go
390
star
41

nfs-ganesha-server-and-external-provisioner

NFS Ganesha Server and Volume Provisioner.
Shell
384
star
42

apiserver-network-proxy

Go
344
star
43

cluster-api-provider-vsphere

Go
339
star
44

image-builder

Tools for building Kubernetes disk images
Shell
325
star
45

kubetest2

Kubetest2 is the framework for launching and running end-to-end tests on Kubernetes.
Go
312
star
46

cluster-api-provider-nested

Cluster API Provider for Nested Clusters
Go
289
star
47

cluster-api-provider-azure

Cluster API implementation for Microsoft Azure
Go
283
star
48

bom

A utility to generate SPDX-compliant Bill of Materials manifests
Go
279
star
49

vsphere-csi-driver

vSphere storage Container Storage Interface (CSI) plugin
Go
278
star
50

cluster-api-provider-openstack

Go
255
star
51

karpenter

Karpenter is a Kubernetes Node Autoscaler built for flexibility, performance, and simplicity.
Go
255
star
52

kubebuilder-declarative-pattern

A toolkit for building declarative operators with kubebuilder
Go
242
star
53

kpng

Reworking kube-proxy's architecture
Go
235
star
54

ingress2gateway

Convert Ingress resources to Gateway API resources
Go
225
star
55

cloud-provider-azure

Cloud provider for Azure
Go
222
star
56

blixt

Layer 4 Kubernetes load-balancer
Rust
220
star
57

aws-encryption-provider

APIServer encryption provider, backed by AWS KMS
Go
192
star
58

mcs-api

This repository hosts the Multi-Cluster Service APIs. Providers can import packages in this repo to ensure their multi-cluster service controller implementations will be compatible with MCS data planes.
Go
184
star
59

ip-masq-agent

Manage IP masquerade on nodes
Go
180
star
60

zeitgeist

Zeitgeist: the language-agnostic dependency checker
Go
168
star
61

cluster-api-provider-gcp

The GCP provider implementation for Cluster API
Go
165
star
62

contributor-playground

Dockerfile
163
star
63

cluster-addons

Addon operators for Kubernetes clusters.
Go
153
star
64

gcp-compute-persistent-disk-csi-driver

The Google Compute Engine Persistent Disk (GCE PD) Container Storage Interface (CSI) Storage Plugin.
Go
151
star
65

azurefile-csi-driver

Azure File CSI Driver
Go
145
star
66

promo-tools

Container and file artifact promotion tooling for the Kubernetes project
Go
136
star
67

azuredisk-csi-driver

Azure Disk CSI Driver
Go
132
star
68

kube-storage-version-migrator

Go
125
star
69

blob-csi-driver

Azure Blob Storage CSI driver
Go
116
star
70

usage-metrics-collector

High fidelity and scalable capacity and usage metrics for Kubernetes clusters
Go
116
star
71

aws-fsx-csi-driver

CSI Driver of Amazon FSx for Lustre https://aws.amazon.com/fsx/lustre/
Go
115
star
72

boskos

Boskos is a resource management service that provides reservation and lifecycle management of a variety of different kinds of resources.
Go
113
star
73

downloadkubernetes

Download kubernetes binaries more easily
Go
110
star
74

sig-windows-tools

Repository for tools and artifacts related to the sig-windows charter in Kubernetes. Scripts to assist kubeadm and wincat and flannel will be hosted here.
PowerShell
108
star
75

cluster-api-operator

Home for Cluster API Operator, a subproject of sig-cluster-lifecycle
Go
107
star
76

cluster-api-provider-digitalocean

The DigitalOcean provider implementation of the Cluster Management API
Go
106
star
77

cluster-api-provider-kubevirt

Cluster API Provider for KubeVirt
Go
96
star
78

cluster-api-provider-packet

Cluster API Provider Packet (now Equinix Metal)
Go
94
star
79

structured-merge-diff

Test cases and implementation for "server-side apply"
Go
92
star
80

slack-infra

Tooling for kubernetes.slack.com
Go
90
star
81

dashboard-metrics-scraper

Container to scrape, store, and retrieve a window of time from the Metrics Server.
Go
84
star
82

apiserver-runtime

Libraries for implementing aggregated apiservers
Go
81
star
83

cli-experimental

Experimental Kubectl libraries and commands.
Go
79
star
84

lwkd

Last Week in Kubernetes Development
HTML
78
star
85

gcp-filestore-csi-driver

The Google Cloud Filestore Container Storage Interface (CSI) Plugin.
Go
78
star
86

kube-scheduler-wasm-extension

All the things to make the scheduler extendable with wasm.
Go
77
star
87

container-object-storage-interface-controller

Container Object Storage Interface (COSI) controller responsible to manage lifecycle of COSI objects.
Go
74
star
88

jobset

JobSet: An API for managing a group of Jobs as a unit
Go
73
star
89

sig-windows-dev-tools

This is a batteries included local development environment for Kubernetes on Windows.
PowerShell
73
star
90

cluster-api-addon-provider-helm

Cluster API Add-on Provider for Helm is a extends the functionality of Cluster API by providing a solution for managing the installation, configuration, upgrade, and deletion of Cluster add-ons using Helm charts.
Go
70
star
91

cloud-provider-equinix-metal

Kubernetes Cloud Provider for Equinix Metal (formerly Packet Cloud Controller Manager)
Go
70
star
92

kernel-module-management

The kernel module management operator builds, signs and loads kernel modules in Kubernetes clusters..
Go
70
star
93

reference-docs

Tools to build reference documentation for Kubernetes APIs and CLIs.
HTML
69
star
94

cluster-api-provider-ibmcloud

Cluster API Provider for IBM Cloud
Go
59
star
95

community-images

kubectl plugin that displays images running in a Kubernetes cluster that were pulled from community owned repositories and warn the user to switch repositories if needed
Go
58
star
96

wg-policy-prototypes

A place for policy work group related proposals and prototypes.
Go
58
star
97

container-object-storage-interface-spec

Container Object Storage (COSI) Specification
Shell
57
star
98

container-object-storage-interface-api

Container Object Storage Interface (COSI) API responsible to define API for COSI objects.
Go
55
star
99

lws

LeaderWorkerSet: An API for deploying a group of pods as a unit of replication
Go
55
star
100

kubectl-validate

Go
54
star