configmap

package
v0.0.0-...-44c1ba5 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

README

Kubernetes ConfigMap Source (configmap)

The configmap source reads config from a kubernetes configmap key/values

Kubernetes ConfigMap Format

The configmap source expects keys under a namespace default to default and a confimap default to micro

// we recommend to setup your variables from multiples files example:
$ kubectl create configmap micro --namespace default --from-file=./testdata

// verify if were set correctly with
$ kubectl get configmap micro --namespace default
{
    "apiVersion": "v1",
    "data": {
        "config": "host=0.0.0.0\nport=1337",
        "mongodb": "host=127.0.0.1\nport=27017\nuser=user\npassword=password",
        "redis": "url=redis://127.0.0.1:6379/db01"
    },
    "kind": "ConfigMap",
    "metadata": {
        ...
        "name": "micro",
        "namespace": "default",
        ...
    }
}

Keys are split on \n and = this is because the way kubernetes saves the data is map[string][string].

// the example above "mongodb": "host=127.0.0.1\nport=27017\nuser=user\npassword=password" will be accessible as:
conf.Get("mongodb", "host") // 127.0.0.1
conf.Get("mongodb", "port") // 27017

New Source

Specify source with data

configmapSource := configmap.NewSource(
	// optionally specify a namespace; default to default
	configmap.WithNamespace("kube-public"),
	// optionally specify name for ConfigMap; defaults micro
	configmap.WithName("micro-config"),
    // optionally strip the provided path to a kube config file mostly used outside of a cluster, defaults to "" for in cluster support.
    configmap.WithConfigPath($HOME/.kube/config),
)

Load Source

Load the source into config

// Create new config
conf := config.NewConfig()

// Load file source
conf.Load(configmapSource)

Running Go Tests

Requirements

Have a kubernetes cluster running (external or minikube) have a valid kubeconfig file.

// Setup testing configmaps feel free to remove them after testing.
$ cd source/configmap
$ kubectl create configmap micro --from-file=./testdata
$ kubectl create configmap micro --from-file=./testdata --namespace kube-public
$ kubectl create configmap micro-config --from-file=./testdata
$ kubectl create configmap micro-config --from-file=./testdata --namespace kube-public
$ go test -v -cover
// To clean up the testing configmaps
$ kubectl delete configmap micro --all-namespaces
$ kubectl delete configmap micro-config --all-namespaces

Todos

  • add more test cases including watchers
  • add support for prefixing either using namespace or a custom string passed as WithPrefix
  • a better way to test without manual setup from the user.
  • add test examples.
  • open to suggestions and feedback please let me know what else should I add.

stay tuned for kubernetes secret support as an source.

Documentation

Overview

Package configmap config is an interface for dynamic configuration.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultName       = "micro"
	DefaultConfigPath = ""
	DefaultNamespace  = "default"
)

Functions

func NewSource

func NewSource(opts ...source.Option) source.Source

func WithConfigPath

func WithConfigPath(s string) source.Option

WithConfigPath option for setting a custom path to kubeconfig

func WithName

func WithName(s string) source.Option

func WithNamespace

func WithNamespace(s string) source.Option

Types

This section is empty.

Jump to

Keyboard shortcuts

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