k8sutils

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2019 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Go module k8sutils

Module github.com/maffeis/k8sutils provides simple utility functions for developing Kubernetes applications in go. The following example checks whether the go application is running on Kubernetes. In case it is, the available Kubernetes nodes are listed.

import (
	log "github.com/sirupsen/logrus"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/version"
	"k8s.io/client-go/kubernetes"
	"github.com/maffeis/k8sutils"
)

func main() {
	if k8sutils.IsRunningOnDocker() {
		log.Infof("running on Docker")
	} else {
		log.Infof("NOT running on Docker")
	}

	if k8sutils.IsRunningOnKubernetes() {
		var client *kubernetes.Clientset
		var error error

		client, error = k8sutils.KubernetesConfig()

		if error != nil {
			panic(error)
		} else {
			var ver *version.Info

			ver, error = client.ServerVersion()

			log.Infof("running on Kubernetes: %s", ver.String())

			list, err := client.CoreV1().Nodes().List(metav1.ListOptions{})
			if err != nil {
				log.Fatal("cannot retrieve Kubernetes nodes: %s", err.Error())
			}

			for _, node := range list.Items {
				log.Infof("Node: %s", node.Name)
			}
		}
	} else {
		log.Infof("NOT running on Kubernetes")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRunningOnDocker

func IsRunningOnDocker() bool

IsRunningOnDocker checks whether the application is running in a Docker container.

func IsRunningOnKubernetes

func IsRunningOnKubernetes() bool

IsRunningOnKubernetes checks whether the application is running on a Kubernetes cluster.

func KubernetesConfig

func KubernetesConfig() (*kubernetes.Clientset, error)

KubernetesConfig configures the application for Kubernetes.

func LoadSslCert added in v0.1.2

func LoadSslCert(client IClientSet, nameSpace string, secretKey string, crtFile string, keyFile string) error

LoadSslCert loads an SSL certificate and its private key from a Secrets, and writes them to the pod file system. Those files are automatically deleted when the pod is destroyed.

Types

type IClientSet added in v0.1.2

type IClientSet interface {
	CoreV1() corev1.CoreV1Interface
}

IClientSet the abstracts Kubernetes client API

Jump to

Keyboard shortcuts

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