util

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: Apache-2.0 Imports: 18 Imported by: 84

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrResourceIsNotReady indicates that the resource is not ready
	ErrResourceIsNotReady = errors.New("Resource is not ready")
	// ErrInvalidPort indicates that the port is invalid
	ErrInvalidPort = errors.New("invalid port")
	// ErrNotFound indicates that the object was not found
	ErrNotFound = errors.New("Object not found")
	// ErrInvalidStatus indicates that the status is invalid
	ErrInvalidStatus = errors.New("invalid status")
	// ErrInvalidEndpoint indicates that the endpoint type is invalid
	ErrInvalidEndpoint = errors.New("invalid endpoint type")
	// ErrCannotUpdateObject indicates that the object cannot be updated
	ErrCannotUpdateObject = errors.New("cannot update object")
	// ErrFieldNotFound indicates that the field was not found in the Secret
	ErrFieldNotFound = errors.New("field not found in Secret")
	// ErrMoreThanOne indicates that only one should exist
	ErrMoreThanOne = errors.New("Only one should exist")
	// ErrNoPodSubdomain indicates that there is no Subdomain or Hostname
	ErrNoPodSubdomain = errors.New("No Subdomain or Hostname")
	// ErrPodsInterfaces indicates that pod interfaces aren't configured
	ErrPodsInterfaces = errors.New("not all pods have interfaces")
)

Functions

func ExecuteTemplate

func ExecuteTemplate(templateFile string, data interface{}) (string, error)

ExecuteTemplate creates a template from the file and execute it with the specified data

func ExecuteTemplateData

func ExecuteTemplateData(templateData string, data interface{}) (string, error)

ExecuteTemplateData creates a template from string and execute it with the specified data

func ExecuteTemplateFile

func ExecuteTemplateFile(filename string, data interface{}) (string, error)

ExecuteTemplateFile - creates a template from the file and execute it with the specified data

func GetAllTemplates

func GetAllTemplates(path string, kind string, templateType string, version string) []string

GetAllTemplates - get all template files

The structur of the folder is, base path, the kind (CRD in lower case),

  • path - base path of the templates folder
  • kind - sub folder for the CRDs templates
  • templateType - TType of the templates. When the templates got rendered and added to a CM this information is e.g. used for the permissions they get mounted into the pod
  • version - if there need to be templates for different versions, they can be stored in a version subdir

Sub directories inside the specified directory with the above parameters get ignored.

func GetEnvVar

func GetEnvVar(key string, baseDefault string) string

GetEnvVar - Get the value associated with key from environment variables, but use baseDefault as a value in case the ENV variable is not defined.

func GetOr

func GetOr(m map[string]interface{}, key, fallback string) interface{}

GetOr returns the value of m[key] if it exists, fallback otherwise. As a special case, it also returns fallback if the value of m[key] is the empty string

func GetStringListFromMap added in v0.4.0

func GetStringListFromMap(in map[string]string, key string) []string

GetStringListFromMap - It returns a list of strings based on a comma separated list assigned to the map key. This is usually invoked to normalize annotation fields where a list of items is expressed with a comma separated list of strings. Example: input: in["additionalSubjectNamesKey"] = "foo.bar,bar.svc,*.foo.bar" output: [foo.bar bar.svc *.foo.bar]

func GetTemplateData

func GetTemplateData(t Template) (map[string]string, error)

GetTemplateData - Renders templates specified via Template struct

Check the TType const and Template type for more details on defining the template.

func GetTemplatesPath

func GetTemplatesPath() (string, error)

GetTemplatesPath get path to templates, either running local or deployed as container

func HashOfInputHashes

func HashOfInputHashes(
	hashes map[string]env.Setter,
) (string, error)

HashOfInputHashes - calculates the overall hash of hashes

func InitMap

func InitMap(m *map[string]string)

InitMap - Inititialise a map to an empty map if it is nil.

func IsJSON

func IsJSON(s string) error

IsJSON check if string is json format

func IsSet

func IsSet(m map[string]interface{}, key string) interface{}

IsSet returns the value of m[key] if key exists, otherwise false Different from getOr because it will return zero values.

func LogErrorForObject

func LogErrorForObject(
	h *helper.Helper,
	err error,
	msg string,
	object metav1.Object,
	params ...interface{},
)

LogErrorForObject - Error logging

func LogForObject

func LogForObject(
	h *helper.Helper,
	msg string,
	object metav1.Object,
	params ...interface{},
)

LogForObject - generic info level logging

func MergeMaps added in v0.4.0

func MergeMaps[K comparable, V any](baseMap map[K]V, extraMaps ...map[K]V) map[K]V

MergeMaps - merge two or more maps NOTE: In case a key exists, the value in the first map is preserved.

func MergeStringMaps

func MergeStringMaps(baseMap map[string]string, extraMaps ...map[string]string) map[string]string

MergeStringMaps - merge two or more string->map maps NOTE: In case a key exists, the value in the first map is preserved.

func ObjectHash

func ObjectHash(i interface{}) (string, error)

ObjectHash creates a deep object hash and return it as a safe encoded string

func RemoveIndex

func RemoveIndex(s []string, index int) []string

RemoveIndex - remove int from slice

func SetHash

func SetHash(
	hashMap map[string]string,
	hashType string,
	hashStr string,
) (map[string]string, bool)

SetHash - set hashStr of type hashType on hashMap if it does not exist or hashStr is different from current stored value. Returns hashMap and bool which indicates if hashMap changed.

func StringInSlice

func StringInSlice(a string, list []string) bool

StringInSlice - is string in slice

func WrapErrorForObject

func WrapErrorForObject(msg string, object client.Object, err error) error

WrapErrorForObject -

Types

type Hash

type Hash struct {
	// Name of hash referencing the parameter
	Name string `json:"name,omitempty"`
	// Hash
	Hash string `json:"hash,omitempty"`
}

Hash - struct to add hashes to status

type List

type List []Pair

List -

func SortStringMapByValue

func SortStringMapByValue(in map[string]string) List

SortStringMapByValue - Creates a sorted List contain key/value of a map[string]string sorted by key

func (List) Len

func (p List) Len() int

func (List) Less

func (p List) Less(i, j int) bool

func (List) Swap

func (p List) Swap(i, j int)

type Pair

type Pair struct {
	Key   string
	Value string
}

Pair -

type TType

type TType string

TType - TemplateType

const (
	// TemplateTypeScripts - scripts type
	TemplateTypeScripts TType = "bin"
	// TemplateTypeConfig - config type
	TemplateTypeConfig TType = "config"
	// TemplateTypeCustom - custom config type, the secret/cm will not get upated as it is exected that the content is owned by a user
	// if the configmap/secret does not exist on first check, it gets created
	TemplateTypeCustom TType = "custom"
	// TemplateTypeNone - none type, don't add configs from a directory, only files from AdditionalData
	TemplateTypeNone TType = "none"
)

type Template

type Template struct {
	Name               string                 // name of the cm/secret to create based of the Template. Check secret/configmap pkg on details how it is used.
	Namespace          string                 // name of the nanmespace to create the cm/secret. Check secret/configmap pkg on details how it is used.
	Type               TType                  // type of the templates, see TTtypes
	InstanceType       string                 // the CRD name in lower case, to separate the templates for each CRD in /templates
	SecretType         corev1.SecretType      // Secrets only, defaults to "Opaque"
	AdditionalTemplate map[string]string      // templates which are common to multiple CRDs can be located in a shared folder and added via this type into the resulting CM/secret
	StringTemplate     map[string]string      // templates to render which are not accessable files, instead read by the caller from some other source, like a secret
	CustomData         map[string]string      // custom data which won't get rendered as a template and just added to the resulting cm/secret
	Labels             map[string]string      // labels to be set on the cm/secret
	Annotations        map[string]string      // Annotations set on cm/secret
	ConfigOptions      map[string]interface{} // map of parameters as input data to render the templates
	SkipSetOwner       bool                   // skip setting ownership on the associated configmap
	Version            string                 // optional version string to separate templates inside the InstanceType/Type directory. E.g. placementapi/config/18.0
}

Template - config map and secret details

Jump to

Keyboard shortcuts

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