variable

package
v4.0.0-alpha.5 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: Apache-2.0 Imports: 24 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) {
		vv, ok := v.(*variable)
		if !ok {
			return nil, errors.New("variable type error")
		}
		result := make(map[string]any)

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

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

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

		return result, nil
	}
}

GetAllVariable get all variable for a given host

View Source
var GetHostMaxLength = func() GetFunc {
	return func(v Variable) (any, error) {
		vv, ok := v.(*variable)
		if !ok {
			return nil, errors.New("variable type error")
		}
		var hostNameMaxLen int
		for k := range vv.value.Hosts {
			hostNameMaxLen = max(len(k), hostNameMaxLen)
		}

		return hostNameMaxLen, nil
	}
}

GetHostMaxLength get the max length for all hosts

View Source
var GetHostnames = func(name []string) GetFunc {
	if len(name) == 0 {
		return emptyGetFunc
	}

	return func(v Variable) (any, error) {
		vv, ok := v.(*variable)
		if !ok {
			return nil, errors.New("variable type error")
		}
		var hs []string
		for _, n := range name {

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

			for gn, gv := range convertGroup(vv.value.Inventory) {
				if gn == n {
					if gvd, ok := gv.([]string); ok {
						hs = mergeSlice(hs, gvd)
					}

					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(vv.value.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(vv.value.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) {
		vv, ok := v.(*variable)
		if !ok {
			return nil, errors.New("variable type error")
		}
		if hostname == "" {
			return vv.value.getParameterVariable(), nil
		}

		return vv.value.getParameterVariable()[hostname], nil
	}
}

GetParamVariable get param variable which is combination of inventory, config. if hostname is empty, return all host's param variable.

View Source
var MergeAllRuntimeVariable = func(data map[string]any, hostName string) MergeFunc {
	return func(v Variable) error {
		vv, ok := v.(*variable)
		if !ok {
			return errors.New("variable type error")
		}

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

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

			cv, ok := curVariable.(map[string]any)
			if !ok {
				return "", errors.New("variable type error")
			}

			return tmpl.ParseString(combineVariables(data, cv), s)
		}); err != nil {
			return err
		}

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

		return nil
	}
}

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

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

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

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

		return nil
	}
}

MergeRemoteVariable merge variable to remote.

View Source
var MergeRuntimeVariable = func(data map[string]any, hosts ...string) MergeFunc {
	if len(data) == 0 || len(hosts) == 0 {

		return emptyMergeFunc
	}

	return func(v Variable) error {
		for _, hostName := range hosts {
			vv, ok := v.(*variable)
			if !ok {
				return errors.New("variable type error")
			}

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

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

				cv, ok := curVariable.(map[string]any)
				if !ok {
					return "", errors.New("variable type error")
				}

				return tmpl.ParseString(combineVariables(data, cv), s)
			}); err != nil {
				return err
			}

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

		return nil
	}
}

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

Functions

func BoolVar

func BoolVar(d map[string]any, args map[string]any, key string) (*bool, error)

BoolVar get bool value by key

func DurationVar

func DurationVar(d map[string]any, args map[string]any, key string) (time.Duration, error)

DurationVar get time.Duration value by key

func Extension2Slice

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

Extension2Slice convert runtime.RawExtension to slice if runtime.RawExtension contains tmpl syntax, parse it.

func Extension2String

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

Extension2String convert runtime.RawExtension to string. if runtime.RawExtension contains tmpl syntax, parse it.

func Extension2Variables

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

Extension2Variables convert runtime.RawExtension to variables

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)

GetFunc get data from variable

type MergeFunc

type MergeFunc func(Variable) error

MergeFunc merge data to variable

type Variable

type Variable interface {
	Get(getFunc GetFunc) (any, error)
	Merge(mergeFunc MergeFunc) error
}

Variable store all vars which pipeline used.

func New

func New(ctx context.Context, client ctrlclient.Client, pipeline kkcorev1.Pipeline, st source.SourceType) (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