go_k8_helm

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: Apache-2.0 Imports: 48 Imported by: 2

README

go_k8_helm

Description

A package of useful functions for managing k8 cluster and helm charts


When to use go_k8_helm

  • when you want to install delete k8 manifest files
  • get information on services and deployments ect
  • deploy / delete /upgrade helm charts
  • Add helm repo

Requirements


Installation and Basic usage

This will take you through the steps to install and get go_k8_helm up and running.

1. Install
go get github.com/Mrpye/go_k8_helm
2. Add to your project
    import "github.com/Mrpye/go_k8_helm"

Examples

1. Get workspace items pods deployments ...
package main

import "github.com/Mrpye/go_k8_helm"

func main() {

	//*****************************************************************************
	// Create the k8 connection
	//you can pass in the kube config file path or leave black for default location
	//*****************************************************************************
	k8, err := go_k8_helm.CreateK8KubeConfig("minikube", "")

	// ********************************
	//Can also use the token connection
	// ********************************
	//k8, err := go_k8_helm.CreateK8Token("localhost","auth",true)

	if err != nil {
		panic(err)
	}
	//****************************
	//get items from the workspace
	//****************************
	result, err := k8.GetServiceIP("default", "my_service")
	if err != nil {
		panic(err)
	}

	for _, s := range result {
		println(s.ServiceName, s.IP, s.Port, s.ServiceType)
	}

}

2. Get service IP
package main

import "github.com/Mrpye/go_k8_helm"

func main() {

	//*****************************************************************************
	// Create the k8 connection
	//you can pass in the kube config file path or leave black for default location
	//*****************************************************************************
	k8, err := go_k8_helm.CreateK8KubeConfig("minikube", "")

	// ********************************
	//Can also use the token connection
	// ********************************
	//k8, err := go_k8_helm.CreateK8Token("localhost","auth",true)

	if err != nil {
		panic(err)
	}
	//****************************
	//get items from the workspace
	//****************************
	result, err := k8.GetServiceIP("default", "my_service")
	if err != nil {
		panic(err)
	}

	for _, s := range result {
		println(s.ServiceName, s.IP, s.Port, s.ServiceType)
	}

}


3. Add helm repo and install/upgrade uninstall helm chart
package main

import "github.com/Mrpye/go_k8_helm"

func main() {

	//*****************************************************************************
	// Create the k8 connection
	//you can pass in the kube config file path or leave black for default location
	//*****************************************************************************
	k8, err := go_k8_helm.CreateK8KubeConfig("minikube", "")

	// ********************************
	//Can also use the token connection
	// ********************************
	//k8, err := go_k8_helm.CreateK8Token("localhost","auth",true)

	if err != nil {
		panic(err)
	}
	//**********
	//Add a repo
	//**********
	err = k8.RepoAdd("my_repo", "https://kubernetes-charts.storage.googleapis.com", "user", "password")
	if err != nil {
		panic(err)
	}
	//***************
	//Update the repo
	//***************
	err = k8.RepoUpdate()
	if err != nil {
		panic(err)
	}

	//***************
	//Deploy a chart
	//***************
	err = k8.DeployHelmChart("my_repo/char", "release_name", "default", map[string]interface{}{"key": "value"})
	if err != nil {
		panic(err)
	}
	//***************
	//Upgrade a chart
	//***************
	err = k8.UpgradeHelmChart("my_repo/char", "release_name", "default", map[string]interface{}{"key": "value"})
	if err != nil {
		panic(err)
	}
	//***************
	//Uninstall a chart
	//***************
	err = k8.UninstallHelmChart("my_repo/char", "release_name")
	if err != nil {
		panic(err)
	}
}



To Do

  • Write tests

Notable 3rd party Libraries

license

go_k8_helm is Apache 2.0 licensed.

Change Log

v0.2.0
  • Update documents
  • Update functions Document
  • Added Delete actions for pods, ns, deployments, service
v0.2.1
  • Added examples
  • Update documents
  • Document functions
  • Added Delete actions for DemonSet, PV, PVC
  • changed way DeleteNS works
v0.2.2
  • Fix a dependance that should not be getting used
cv0.2.3
  • updated the reference for golib to v0.2.2

Documentation

Overview

This package contains helper functions for managing K8s cluster and Helm charts

Creating and deleting K8 manifests yamls
Installing and uninstalling Helm charts
Getting the status of K8s services
Getting the status of K8s deployments
Getting the status of K8s pods
Getting the status of K8s services
Managing Helm releases
Managing Helm repositories

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type K8

type K8 struct {
	DefaultContext     string `json:"default_context" yaml:"default_context" flag:"context c" desc:"The default context to use"`
	ConfigPath         string `json:"config_path" yaml:"config_path" flag:"config_path p" desc:"The path to the kube config file"`
	Host               string `json:"host" yaml:"host" flag:"host h" desc:"The host to connect to"`
	Authorization      string `json:"authorization" yaml:"authorization" flag:"auth a" desc:"The authorization token"`
	UseTokenConnection bool   `` //if true, use the token connection, otherwise use the kube config file
	/* 167-byte string literal not displayed */
	Ignore_ssl bool `json:"ignore_ssl" yaml:"ignore_ssl" flag:"ignore_ssl i" desc:"If true, ignore the ssl connection"`
	// contains filtered or unexported fields
}

K8 is the struct for the k8 connection If you want to use the kube config file, you need to set the DefaultContext and ConfigPath DefaultContext is the default context to use ConfigPath is the path to the kube config file Host is the host to connect to You will need to set the Host and Authorization if you want to use the token connection Authorization is the authorization token UseTokenConnection if true, use the token connection, otherwise use the kube config file Ignore_ssl if true, ignore the ssl connection

func CreateK8 added in v0.2.0

func CreateK8(opts ...K8Option) (*K8, error)

CreateK8 creates a instance of the k8 type Does not create the config and context Use CreateConfigAndContext() to create the config and context opts are the options for the k8 type returns the k8 type returns an error if there is an issue

func CreateK8KubeConfig

func CreateK8KubeConfig(default_context string, config_path string) (*K8, error)

Create a instance of the k8 type default_context is the default context to use config_path is the path to the kube config file

func CreateK8Options

func CreateK8Options(opts ...K8Option) (*K8, error)

CreateK8Options creates a instance of the k8 type opts are the options for the k8 type returns the k8 type returns an error if there is an issue

func CreateK8Token

func CreateK8Token(host string, auth string, ignore_ssl bool) (*K8, error)

CreateK8Token creates a instance of the k8 type host is the host to connect to auth is the authorization token ignore_ssl if true, ignore the ssl connection returns the k8 type returns an error if there is an issue

func (*K8) ApplyYaml

func (m *K8) ApplyYaml(yaml string, ns string) error

ApplyYaml applies a resource using a yaml manifest ctx: context cfg: k8 config yaml: yaml manifest ns: namespace return: error

func (*K8) CheckStatusOf

func (m *K8) CheckStatusOf(ns string, checks []interface{}, not_running bool) (bool, []string, error)

CheckStatusOf checks the status of a list of checks ns: namespace checks: list of checks to perform return: bool, []string, error bool: true if all checks passed []string: list of the results of the checks error: error if any

Example:
	checks := []interface{}{
		"deployment:nginx(.*)",
		"replica:nginx2(.*)",
		"stateful:nginx3(.*)",
		"demon:nginx4(.*)",
		"service:nginx(.*)",
	}

func (*K8) CreateConfigAndContext added in v0.2.0

func (m *K8) CreateConfigAndContext() error

buildRestConfig builds the rest config And add to the k8 type returns error if there is an issue

func (*K8) CreateNS

func (m *K8) CreateNS(ns string) error

CreateNS creates a namespace in a k8 cluster ns: namespace return: error

func (*K8) DeleteDemonSet added in v0.2.1

func (m *K8) DeleteDemonSet(ns string, name string) error

GetDemonSet gets demonsets from a k8 cluster ns: namespace return: appsv1.DaemonSetList, error

func (*K8) DeleteDeployment added in v0.2.0

func (m *K8) DeleteDeployment(ns string, name string) error

func (*K8) DeleteNS

func (m *K8) DeleteNS(ns string) error

DeleteNS deletes a namespace in a k8 cluster ns: namespace return: error Does not delete default namespace

func (*K8) DeletePV added in v0.2.1

func (m *K8) DeletePV(ns string, name string) error

DeletePV deletes a PV - ns is the namespace - name is the name of the PV - returns an error if there is one

func (*K8) DeletePVC added in v0.2.1

func (m *K8) DeletePVC(ns string, name string) error

DeletePVC deletes a PVC - ns is the namespace - name is the name of the PVC - returns an error if there is one

func (*K8) DeletePod added in v0.2.0

func (m *K8) DeletePod(ns string, name string) error

DeletePod deletes a pod from a k8 cluster ns: namespace name: pod name return: error

func (*K8) DeleteSecrets added in v0.2.1

func (m *K8) DeleteSecrets(ns string, name string) error

DeleteSecrets deletes secrets from a k8 cluster ns: namespace name: name of secret return: error

func (*K8) DeleteService added in v0.2.0

func (m *K8) DeleteService(ns string, name string) error

DeleteService deletes a service from a k8 cluster ns: namespace name: name of the service return: error

func (*K8) DeleteStatefulSets added in v0.2.1

func (m *K8) DeleteStatefulSets(ns string, name string) error

DeleteStatefulSets deletes a statefulset from a k8 cluster ns: namespace name: name of the statefulset return: error

func (*K8) DeleteYaml

func (m *K8) DeleteYaml(yaml string, ns string) error

DeleteYaml deletes a resource using a yaml manifest ctx: context cfg: k8 config yaml: yaml manifest ns: namespace return: error

func (*K8) DeployHelmChart

func (m *K8) DeployHelmChart(chart_path string, release_name string, namespace string, configs map[string]interface{}) error

DeployHelmChart deploys a helm chart chart_path is the path to the chart to deploy release_name is the name of the release to deploy namespace is the namespace to deploy the release to configs is a map of values to pass to the chart returns nil error on success

func (*K8) DryRun

func (m *K8) DryRun() bool

DryRun returns the dry_run flag If true, do not execute the command, just print out the command

func (*K8) GetDemonSet

func (m *K8) GetDemonSet(ns string) (*appsv1.DaemonSetList, error)

GetDemonSet gets demonsets from a k8 cluster ns: namespace return: appsv1.DaemonSetList, error

func (*K8) GetDeployments

func (m *K8) GetDeployments(ns string) (*appsv1.DeploymentList, error)

GetDeployments gets deployments from a k8 cluster ns: namespace return: appsv1.DeploymentList, error

func (*K8) GetPods

func (m *K8) GetPods(ns string) (*v1.PodList, error)

GetPods gets pods from a k8 cluster ns: namespace return: v1.PodList, error

func (*K8) GetSecrets

func (m *K8) GetSecrets(ns string) (*v1.SecretList, error)

GetSecrets gets secrets from a k8 cluster ns: namespace return: v1.SecretList, error

func (*K8) GetServiceIP

func (m *K8) GetServiceIP(ns string, regex_service_name string) ([]ServiceDetails, error)

GetServiceIP gets service ip from a k8 cluster ns: namespace regex_service_name: regex to match service name return: v1.ServiceList, error

func (*K8) GetServices

func (m *K8) GetServices(ns string) (*v1.ServiceList, error)

GetServices gets services from a k8 cluster ns: namespace

return: v1.ServiceList, error

func (*K8) GetStatefulSets

func (m *K8) GetStatefulSets(ns string) (*appsv1.StatefulSetList, error)

GetStatefulSets gets statefulsets from a k8 cluster ns: namespace return: appsv1.StatefulSetList, error

func (*K8) PodCopy

func (m *K8) PodCopy(ns string, src string, dst string, container_name string) (string, error)

PodCopy copies a file to and from a pod ns: namespace src: source file dst: destination file container_name: container name return: stdout, error

func (*K8) PodExec

func (m *K8) PodExec(ns string, pod_name string, command string) (string, error)

PodExec executes a command in a pod ns: namespace pod_name: pod name command: command to execute return: stdout, error

func (*K8) ProcessK8File added in v0.2.0

func (m *K8) ProcessK8File(file_data []byte, ns string, apply bool) error

ProcessK8File processes a k8 file with multiple definitions separated with --- file_data: file data ns: namespace apply: apply the file return: error

func (*K8) RepoAdd

func (m *K8) RepoAdd(name string, url string, user string, password string) error

RepoAdd adds a helm repo name is the name of the repo url is the url of the repo user is the user to use for the repo password is the password to use for the repo

func (*K8) RepoUpdate

func (m *K8) RepoUpdate() error

RepoUpdate updates charts for all helm repos returns nil error on success

func (*K8) SetDryRun

func (m *K8) SetDryRun(dry_run bool)

SetDryRun sets the dry_run flag If true, do not execute the command, just print out the command

func (*K8) SetVerbose

func (m *K8) SetVerbose(verbose bool)

SetVerbose sets the verbose flag If true, print out the verbose information

func (*K8) String

func (m *K8) String() string

String returns the string representation of the k8 type

func (*K8) UninstallHelmChart

func (m *K8) UninstallHelmChart(release_name string, namespace string) error

UninstallHelmChart uninstalls a helm chart release_name is the name of the release to uninstall namespace is the namespace to uninstall the release from returns nil error on success

func (*K8) Update

func (m *K8) Update(opts ...K8Option)

Update the k8 Type with the options

func (*K8) UpgradeHelmChart

func (m *K8) UpgradeHelmChart(chart_path string, release_name string, namespace string, configs map[string]interface{}) error

UpgradeHelmChart upgrades a helm chart chart_path is the path to the chart to upgrade release_name is the name of the release to upgrade namespace is the namespace to upgrade the release to configs is a map of values to pass to the chart returns nil error on success

func (*K8) Verbose

func (m *K8) Verbose() bool

Verbose returns the verbose flag

type K8Option

type K8Option func(*K8)

K8Option is the option for the k8 connection

func OptionK8Auth

func OptionK8Auth(auth string) K8Option

OptionK8Auth is the option for the authorization

func OptionK8ConfigPath

func OptionK8ConfigPath(config_path string) K8Option

OptionK8ConfigPath is the option for the config path

func OptionK8DefaultContext

func OptionK8DefaultContext(default_context string) K8Option

OptionK8DefaultContext is the option for the default context

func OptionK8Host

func OptionK8Host(host string) K8Option

OptionK8Host is the option for the host

func OptionK8IgnoreSSL

func OptionK8IgnoreSSL(ignore_ssl bool) K8Option

OptionK8IgnoreSSL is the option for the ignore ssl

func OptionK8UseTokenConnection

func OptionK8UseTokenConnection(use_token_connection bool) K8Option

OptionK8UseTokenConnection is the option for the use token connection

type ServiceDetails added in v0.2.2

type ServiceDetails struct {
	ServiceName string `json:"service_name" yaml:"service_name"`
	ServiceType string `json:"service_type" yaml:"service_type"`
	IP          string `json:"ip" yaml:"ip"`
	Port        int32  `json:"port" yaml:"port"`
}

type SimpleRESTClientGetter

type SimpleRESTClientGetter struct {
	Namespace  string
	KubeConfig rest.Config
}

SimpleRESTClientGetter is a simple implementation of RESTClientGetter that

func NewRESTClientGetter

func NewRESTClientGetter(namespace string, kubeConfig rest.Config) *SimpleRESTClientGetter

NewRESTClientGetter returns a new SimpleRESTClientGetter namespace is the namespace to use for requests kubeConfig is the config to use for requests returns a new SimpleRESTClientGetter

func (*SimpleRESTClientGetter) ToDiscoveryClient

ToDiscoveryClient returns the DiscoveryClient returns the DiscoveryClient and nil error

func (*SimpleRESTClientGetter) ToRESTConfig

func (c *SimpleRESTClientGetter) ToRESTConfig() (*rest.Config, error)

ToRESTConfig returns the RESTConfig returns the RESTConfig and nil error

func (*SimpleRESTClientGetter) ToRESTMapper

func (c *SimpleRESTClientGetter) ToRESTMapper() (meta.RESTMapper, error)

ToRESTMapper returns the RESTMapper returns the RESTMapper and nil error

func (*SimpleRESTClientGetter) ToRawKubeConfigLoader

func (c *SimpleRESTClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig

ToRawKubeConfigLoader returns the RawKubeConfigLoader returns the RawKubeConfigLoader and nil error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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