Kubernetes config maps backed KV store for stateful apps
APACHE-2.0 License
Use Kubernetes config maps as key/value store! When to use k8s-kv:
When not to use k8s-kv:
Package API:
// Pyt key/value pair into the store
Put(key string, value []byte) error
// Get value of the specified key
Get(key string) (value []byte, err error)
// Delete key/value pair from the store
Delete(key string) error
// List all key/value pairs under specified prefix
List(prefix string) (data map[string][]byte, err error)
// Delete config map (results in deleted data)
Teardown() error
Usage example:
Get minikube or your favourite k8s environment running.
In your app you will probably want to use this: https://github.com/kubernetes/client-go/tree/master/examples/in-cluster-client-configuration
Get ConfigMaps interface and supply it to this lib:
package main
import (
"fmt"
"github.com/rusenask/k8s-kv/kv"
"k8s.io/client-go/kubernetes"
core_v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/clientcmd"
)
// get ConfigMapInterface to access config maps in "default" namespace
func getImplementer() (implementer core_v1.ConfigMapInterface) {
cfg, err := clientcmd.BuildConfigFromFlags("", ".kubeconfig") // in your app you could replace it with in-cluster-config
if err != nil {
panic(err)
}
client, err := kubernetes.NewForConfig(cfg)
if err != nil {
panic(err)
}
return client.ConfigMaps("default")
}
func main() {
impl := getImplementer()
// getting acces to k8s-kv. "my-app" will become a label
// for this config map, this way it's easier to manage configs
// "bucket1" will be config map's name and represent one entry in config maps list
kvdb, err := kv.New(impl, "my-app", "bucket1")
if err != nil {
panic(err)
}
// insert a key "foo" with value "hello kubernetes world"
kvdb.Put("foo", []byte("hello kubernetes world"))
// get value of key "foo"
stored, _ := kvdb.Get("foo")
fmt.Println(string(stored))
}