Skip to content

AbsaOSS/gopkg

Repository files navigation

gopkg

Absa Go package library

Absa library

controller

package controller extends sigs.k8s.io/controller-runtime os.Getenv.

Overview

Package contains ReconcileResult which provides abstraction over reconciliation loop management.

Usage

const reconcileSeconds = 10
result := utils.NewReconcileResult(time.Duration(reconcileSeconds) * time.Second)
...
func (r *MyReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
	...
	if finish {
		// sucesfuly stop reconciliation loop
		return result.Stop()
	}
	if err := doSomething(); err != nil {
		// requeue loop immediately with error
		return result.RequeueError(err)
	}
	// requeue loop after reconcileSeconds 
	return result.Requeue()
}

dns

DNS contains helper functions related to DNS.

Overview

Package currently contains Dig function retrieving slice of tuples <IP address, A record > for specific FQDN.

Usage

	edgeDNSServer := "8.8.8.8"
	fqdn := "google.com"
	result, err := Dig(edgeDNSServer, fqdn)

env

Environment variable helper extending standard os.Getenv.

Overview

Package contains several functions which returns the typed env variable for the given key and falls back to the default value if not set.

Usage

noProxy := env.GetEnvAsStringOrFallback("NO_PROXY", "*.foo.bar.com")
 
runOnStart := env.GetEnvAsStringOrFallback("RUN_ON_START", true)

k8s

Package k8s provides extensions for k8s apimachinery/pkg/apis

Overview

Package contains following functions:

  • MergeAnnotations(target *metav1.ObjectMeta, source *metav1.ObjectMeta) adds or updates annotations from defaultSource to defaultTarget
  • ContainsAnnotations(target *metav1.ObjectMeta, source *metav1.ObjectMeta) bool checks if defaultTarget contains all annotations of defaultSource Any kubernetes resource having ObjectMeta can be passed.

Usage

if !k8s.ContainsAnnotations(currentIngress.ObjectMeta, expectedIngress.ObjectMeta) {
    MergeAnnotations(currentIngress.ObjectMeta, expectedIngress.ObjectMeta)
}

shell

Shell command runner

Overview

Package shell is used for running executables and returns command output for further processing. It is a synchronized wrapper around the standard Go command package, which allows you to define environment variables in a native way and returns the output as a return value.

Usage

cmd := shell.Command{
	Command: "sh",
	Args: []string{"-c", "terraform apply -auto-approve tfplan"},
	Env: map[string]string{"name":"test"},
}
o, _ = shell.Execute(cmd)

string

Package string provide helper functions related to string

Overview

Package string contains extensions of standard strings and formatting functions:

  • ToString(v interface{}) string ToString converts type to formatted string. If value is struct, function returns formatted JSON.

Usage

log.Debug().Msgf("current config: %s",utils.ToString(config))