variable

package
v4.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GetAllVariable = func(hostName string) GetFunc {
	return func(v Variable) (any, error) {
		if _, ok := v.(*variable); !ok {
			return nil, fmt.Errorf("variable type error")
		}
		data := v.(*variable).value
		result := make(map[string]any)

		result = combineVariables(result, data.Hosts[hostName].RuntimeVars)

		result = combineVariables(result, data.Hosts[hostName].RemoteVars)

		if vv, ok := data.getParameterVariable()[hostName]; ok {
			result = combineVariables(result, vv.(map[string]any))
		}

		return result, nil
	}
}
View Source
var GetHostnames = func(name []string) GetFunc {
	return func(v Variable) (any, error) {
		if _, ok := v.(*variable); !ok {
			return nil, fmt.Errorf("variable type error")
		}
		data := v.(*variable).value

		var hs []string
		for _, n := range name {

			if _, ok := data.Hosts[n]; ok {
				hs = append(hs, n)
			}

			for gn, gv := range convertGroup(data.Inventory) {
				if gn == n {
					hs = mergeSlice(hs, gv.([]string))
					break
				}
			}

			regexForIndex := regexp.MustCompile(`^(.*)\[\d\]$`)
			if match := regexForIndex.FindStringSubmatch(strings.TrimSpace(n)); match != nil {
				index, err := strconv.Atoi(match[2])
				if err != nil {
					klog.V(4).ErrorS(err, "convert index to int error", "index", match[2])
					return nil, err
				}
				if group, ok := convertGroup(data.Inventory)[match[1]].([]string); ok {
					if index >= len(group) {
						return nil, fmt.Errorf("index %v out of range for group %s", index, group)
					}
					hs = append(hs, group[index])
				}
			}

			regexForRandom := regexp.MustCompile(`^(.+?)\s*\|\s*random$`)
			if match := regexForRandom.FindStringSubmatch(strings.TrimSpace(n)); match != nil {
				if group, ok := convertGroup(data.Inventory)[match[1]].([]string); ok {
					hs = append(hs, group[rand.Intn(len(group))])
				}
			}
		}

		return hs, nil
	}
}

GetHostnames get all hostnames from a group or host

View Source
var GetParamVariable = func(hostname string) GetFunc {
	return func(v Variable) (any, error) {
		if _, ok := v.(*variable); !ok {
			return nil, fmt.Errorf("variable type error")
		}
		data := v.(*variable).value
		if hostname == "" {
			return data.getParameterVariable(), nil
		}
		return data.getParameterVariable()[hostname], nil
	}
}

GetParamVariable get param variable which is combination of inventory, config.

View Source
var MergeAllRuntimeVariable = func(hostName string, vd map[string]any) MergeFunc {
	return func(v Variable) error {
		vv := v.(*variable).value

		curVariable, err := v.Get(GetAllVariable(hostName))
		if err != nil {
			return err
		}

		if err := parseVariable(vd, func(s string) (string, error) {

			return tmpl.ParseString(combineVariables(vd, curVariable.(map[string]any)), s)
		}); err != nil {
			return err
		}

		for h := range vv.Hosts {
			if _, ok := v.(*variable); !ok {
				return fmt.Errorf("variable type error")
			}
			hv := vv.Hosts[h]
			hv.RuntimeVars = combineVariables(hv.RuntimeVars, vd)
			vv.Hosts[h] = hv
		}

		return nil
	}
}

MergeAllRuntimeVariable parse variable by specific host and merge to all hosts.

View Source
var MergeRemoteVariable = func(hostname string, data map[string]any) MergeFunc {
	return func(v Variable) error {
		if _, ok := v.(*variable); !ok {
			return fmt.Errorf("variable type error")
		}
		vv := v.(*variable).value

		if hostname == "" {
			return fmt.Errorf("when merge source is remote. HostName cannot be empty")
		}
		if _, ok := vv.Hosts[hostname]; !ok {
			return fmt.Errorf("when merge source is remote. HostName %s not exist", hostname)
		}

		if hv := vv.Hosts[hostname]; len(hv.RemoteVars) == 0 {
			hv.RemoteVars = data
			vv.Hosts[hostname] = hv
		}

		return nil
	}
}

MergeRemoteVariable merge variable to remote.

View Source
var MergeRuntimeVariable = func(hostName string, vd map[string]any) MergeFunc {
	return func(v Variable) error {
		vv := v.(*variable).value

		curVariable, err := v.Get(GetAllVariable(hostName))
		if err != nil {
			return err
		}

		if err := parseVariable(vd, func(s string) (string, error) {

			return tmpl.ParseString(combineVariables(vd, curVariable.(map[string]any)), s)
		}); err != nil {
			return err
		}

		if _, ok := v.(*variable); !ok {
			return fmt.Errorf("variable type error")
		}
		hv := vv.Hosts[hostName]
		hv.RuntimeVars = combineVariables(hv.RuntimeVars, vd)
		vv.Hosts[hostName] = hv

		return nil
	}
}

MergeRuntimeVariable parse variable by specific host and merge to the host.

Functions

func Extension2Slice

func Extension2Slice(d map[string]any, ext runtime.RawExtension) []any

Extension2Slice convert extension to slice

func Extension2String

func Extension2String(d map[string]any, ext runtime.RawExtension) (string, error)

func Extension2Variables

func Extension2Variables(ext runtime.RawExtension) map[string]any

Extension2Variables convert extension to variables

func GetValue

func GetValue(value map[string]any, keys string) any

GetValue from VariableData by key path

func IntVar

func IntVar(d map[string]any, vars map[string]any, key string) (int, error)

IntVar get int value by key

func StringSliceVar

func StringSliceVar(d map[string]any, vars map[string]any, key string) ([]string, error)

StringSliceVar get string slice value by key

func StringVar

func StringVar(d map[string]any, args map[string]any, key string) (string, error)

StringVar get string value by key

Types

type GetFunc

type GetFunc func(Variable) (any, error)

type MergeFunc

type MergeFunc func(Variable) error

type Variable

type Variable interface {
	Key() string
	Get(GetFunc) (any, error)
	Merge(MergeFunc) error
}

func New

func New(client ctrlclient.Client, pipeline kubekeyv1.Pipeline) (Variable, error)

New variable. generate value from config args. and render to source.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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