kubegen

package module
v0.0.0-...-dcb86a5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2024 License: BSD-3-Clause Imports: 26 Imported by: 0

README

kube-gen

latest 0.4.0 license Check Commit

kube-gen is a template rendering tool that generates files and executes commands based on Kubernetes events and metadata.

Installation

Binary Download

See the Releases page

Docker Container

kube-gen can be run as a standalone container, or bundled in a container with other applications.

Images are available on Dockerhub:

$ docker run kylemcc/kube-gen ...

Or Github Package Registry:

$ docker run ghcr.io/kylemcc/kube-gen/kube-gen ...

Usage

When run with no arguments (or with -h/-help), kube-gen prints the following usage message.

$ kube-gen
Usage: kube-gen [options] <template> [<output>]

Render templates using Kubernetes metadata and events

Options:
  -host string
        If not set will use kubeconfig. If using proxy - set it to http://localhost:8001
  -interval int

  -kubeconfig string
        (optional) absolute path to the kubeconfig file (default "/Users/kyle/.kube/config")
  -log-cmd
        log the output of the pre/post commands (default true)
  -overwrite
        overwrite the output file if it exists (default true)
  -post-cmd string
        command to run after template generation in complete
  -pre-cmd string
        command to run before template generation
  -quiet
        when set to true, nothing is logged
  -type value
        types of resources to pull [pods, services, endpoints] - May be specified multiple times. If not specified, all types will be returned
  -version
        display version information
  -wait string
        <minimum>[:<maximum>] - the minimum and optional maximum time to wait after an event fires.E.g.: 500ms:5s
  -watch
        watch for new events

Arguments:
  template: path or URL of the template file to render, or - to read from STDIN
  output: (Optional) path to write the rendered content. If not specified,
          rendered content is printed to STDOUT. By default, this file will
          be overwritten if it exists. Use -overwrite=false to return an
          error instead
Authentication / Connecting to the Kubernetes API

By default, kube-gen will look for a kubeconfig file at $HOME/.kube/config. A different kubeconfig file may be specified by using the -kubeconfig flag. Alternatively, kube-gen provides a -host flag that, if set, will supersede the -kubeconfig. The -host flag is best paired with kubectl proxy, which listens on 127.0.0.1:8001 by default. The -host flag may also be set to the value of kube-apiserver's --insecure-bind-address / --insecure-port.

Watching for changes

The -watch flag configures kube-gen to watch the API for changes to Services, Pods, and Endpoints (support for other types is forthcoming). This mode is useul when combined with the -pre-cmd, -post-cmd, and -wait parameters.

Template Language

kube-gen supports templates written in Go`s text/template language. It supports all of the built in functions, as well as numerous custom functions described below. Many of the custom functions (and the documentation for those functions) have been borrowed from docker-gen. Those functions, along with the accompanying License and Copyright are located in the dockergen_template_functions.go source file.

Documentation

Overview

Package kubegen implements a templating and file generation based on Kubernetes events and metadata

Index

Constants

This section is empty.

Variables

View Source
var Funcs = template.FuncMap{
	"add":           add,
	"allPodsReady":  allPodsReady,
	"anyPodReady":   anyPodReady,
	"closest":       arrayClosest,
	"coalesce":      coalesce,
	"combine":       combine,
	"dir":           dirList,
	"exists":        exists,
	"first":         first,
	"groupBy":       groupBy,
	"groupByKeys":   groupByKeys,
	"groupByMulti":  groupByMulti,
	"hasPrefix":     strings.HasPrefix,
	"hasSuffix":     strings.HasSuffix,
	"hasField":      hasField,
	"intersect":     intersect,
	"isPodReady":    isPodReady,
	"isValidJson":   isValidJSON,
	"json":          marshalJSON,
	"pathJoin":      filepath.Join,
	"pathJoinSlice": pathJoinSlice,
	"keys":          keys,
	"last":          last,
	"dict":          dict,
	"mapContains":   mapContains,
	"parseBool":     strconv.ParseBool,
	"parseJson":     unmarshalJSON,
	"parseJsonSafe": unmarshalJSONSafe,
	"readyPods":     readyPods,
	"replace":       strings.Replace,
	"shell":         execShell,
	"slice":         slice,
	"split":         strings.Split,
	"splitN":        strings.SplitN,
	"strContains":   strings.Contains,
	"trim":          strings.TrimSpace,
	"trimPrefix":    strings.TrimPrefix,
	"trimSuffix":    strings.TrimSuffix,
	"values":        values,
	"when":          when,
	"where":         where,
	"whereExist":    whereExist,
	"whereNotExist": whereNotExist,
	"whereAny":      whereAny,
	"whereAll":      whereAll,
}

Functions

func IsPodReady

func IsPodReady(pod *kapi.Pod) bool

IsPodReady returns true if a pod is ready; false otherwise.

Types

type Config

type Config struct {
	Host               string
	Kubeconfig         string
	TemplatePath       string
	TemplateString     string
	Output             string
	Overwrite          bool
	Watch              bool
	PreCmd             string
	PostCmd            string
	LogCmdOutput       bool
	Interval           int
	MinWait            time.Duration
	MaxWait            time.Duration
	ResourceTypes      []string
	UseInClusterConfig bool
}

type Context

type Context struct {
	Pods      []kapi.Pod
	Services  []kapi.Service
	Endpoints []kapi.Endpoints
}

func (*Context) Env

func (c *Context) Env() map[string]string

TODO: if running in k8s, make annotations on containing pod available

type Generator

type Generator interface {
	Generate() error
}

func NewGenerator

func NewGenerator(c Config) (Generator, error)

type ShellResult

type ShellResult struct {
	Success bool
	Stdout  string
	Stderr  string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL