kubecm

package module
v2.0.0-...-9971990 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 13 Imported by: 0

README

kubecm

Package kubecm implements GoFrame gcfg.Adapter using kubernetes configmap.

Limit

glang version >= v.18

Installation

go get -u gitee.com/clannad_sk/webkit/contrib/config/kubecm/v2

Example

Example configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-configmap
data:
  config.yaml: |
    # HTTP service.
    server:
      address:          ":8888"
      openapiPath:      "/api.json"
      swaggerPath:      "/swagger"
      accessLogEnabled: true

Note the configmap name test-configmap, and its item name in data config.yaml.

Create a custom boot package

If you wish using configuration from kubernetes configmap globally, it is strongly recommended creating a custom boot package in very top import, which sets the Adapter of default configuration instance before any other package boots.

Running in pod (common scenario)
package boot

import (
	"gitee.com/clannad_sk/webkit/contrib/config/kubecm/v2"
	"gitee.com/clannad_sk/webkit/v2/frame/g"
	"gitee.com/clannad_sk/webkit/v2/os/gctx"
)

const (
	configmapName       = "test-configmap"
	dataItemInConfigmap = "config.yaml"
)

func init() {
	var (
		err error
		ctx = gctx.GetInitCtx()
	)
	// Create kubecm Client that implements gcfg.Adapter.
	adapter, err := kubecm.New(gctx.GetInitCtx(), kubecm.Config{
		ConfigMap: configmapName,
		DataItem:  dataItemInConfigmap,
	})
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}

	// Change the adapter of default configuration instance.
	g.Cfg().SetAdapter(adapter)
}
Running out of pod (often testing scenario)
package boot

import (
	"gitee.com/clannad_sk/webkit/contrib/config/kubecm/v2"
	"gitee.com/clannad_sk/webkit/v2/frame/g"
	"gitee.com/clannad_sk/webkit/v2/os/gctx"
	"k8s.io/client-go/kubernetes"
)

const (
	namespace              = "default"
	configmapName          = "test-configmap"
	dataItemInConfigmap    = "config.yaml"
	kubeConfigFilePathJohn = `/Users/john/.kube/config`
)

func init() {
	var (
		err        error
		ctx        = gctx.GetInitCtx()
		kubeClient *kubernetes.Clientset
	)
	// Create kubernetes client.
	// It is optional creating kube client when its is running in pod.
	kubeClient, err = kubecm.NewKubeClientFromPath(ctx, kubeConfigFilePathJohn)
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}
	// Create kubecm Client that implements gcfg.Adapter.
	adapter, err := kubecm.New(gctx.GetInitCtx(), kubecm.Config{
		ConfigMap:  configmapName,
		DataItem:   dataItemInConfigmap,
		Namespace:  namespace,  // It is optional specifying namespace when its is running in pod.
		KubeClient: kubeClient, // It is optional specifying kube client when its is running in pod.
	})
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}

	// Change the adapter of default configuration instance.
	g.Cfg().SetAdapter(adapter)

}

Import boot package in top of main

It is strongly recommended import your boot package in top of your main.go.

Note the top import: _ "gitee.com/clannad_sk/webkit/example/config/kubecm/boot_in_pod" .

package main

import (
	_ "gitee.com/clannad_sk/webkit/example/config/kubecm/boot_in_pod"

	"gitee.com/clannad_sk/webkit/v2/frame/g"
	"gitee.com/clannad_sk/webkit/v2/os/gctx"
)

func main() {
	var ctx = gctx.GetInitCtx()

	// Available checks.
	g.Dump(g.Cfg().Available(ctx))

	// All key-value configurations.
	g.Dump(g.Cfg().Data(ctx))

	// Retrieve certain value by key.
	g.Dump(g.Cfg().MustGet(ctx, "server.address"))
}

License

GoFrame kubecm is licensed under the MIT License, 100% free and open-source, forever.

Documentation

Overview

Package kubecm implements gcfg.Adapter using kubernetes configmap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Namespace

func Namespace() string

Namespace retrieves and returns the namespace of current pod. Note that this function should be called in kubernetes pod.

func New

func New(ctx context.Context, config Config) (adapter gcfg.Adapter, err error)

New creates and returns gcfg.Adapter implementing using kubernetes configmap.

func NewDefaultKubeClient

func NewDefaultKubeClient(ctx context.Context) (*kubernetes.Clientset, error)

NewDefaultKubeClient creates and returns a default kubernetes client. It is commonly used when the service is running inside kubernetes cluster.

func NewDefaultKubeConfig

func NewDefaultKubeConfig(ctx context.Context) (*rest.Config, error)

NewDefaultKubeConfig creates and returns a default kubernetes config. It is commonly used when the service is running inside kubernetes cluster.

func NewKubeClientFromConfig

func NewKubeClientFromConfig(ctx context.Context, config *rest.Config) (*kubernetes.Clientset, error)

NewKubeClientFromConfig creates and returns client by given `rest.Config`.

func NewKubeClientFromPath

func NewKubeClientFromPath(ctx context.Context, kubeConfigFilePath string) (*kubernetes.Clientset, error)

NewKubeClientFromPath creates and returns a kubernetes REST client by given `kubeConfigFilePath`.

func NewKubeConfigFromPath

func NewKubeConfigFromPath(ctx context.Context, kubeConfigFilePath string) (*rest.Config, error)

NewKubeConfigFromPath creates and returns rest.Config object from given `kubeConfigFilePath`.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client implements gcfg.Adapter.

func (*Client) Available

func (c *Client) Available(ctx context.Context, configMap ...string) (ok bool)

Available checks and returns the backend configuration service is available. The optional parameter `resource` specifies certain configuration resource.

Note that this function does not return error as it just does simply check for backend configuration service.

func (*Client) Data

func (c *Client) Data(ctx context.Context) (data map[string]interface{}, err error)

Data retrieves and returns all configuration data in current resource as map. Note that this function may lead lots of memory usage if configuration data is too large, you can implement this function if necessary.

func (*Client) Get

func (c *Client) Get(ctx context.Context, pattern string) (value interface{}, err error)

Get retrieves and returns value by specified `pattern` in current resource. Pattern like: "x.y.z" for map item. "x.0.y" for slice item.

type Config

type Config struct {
	ConfigMap  string                `v:"required"` // ConfigMap name.
	DataItem   string                `v:"required"` // DataItem is the key item in Configmap data.
	Namespace  string                // Specify the namespace for configmap.
	RestConfig *rest.Config          // Custom rest config for kube client.
	KubeClient *kubernetes.Clientset // Custom kube client.
	Watch      bool                  // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes.
}

Config for Client.

Jump to

Keyboard shortcuts

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