func validateInstances()

in internal/api/v1/authproxyworkload_webhook.go [274:312]


func validateInstances(spec *[]InstanceSpec, f *field.Path) field.ErrorList {
	var errs field.ErrorList
	if len(*spec) == 0 {
		errs = append(errs, field.Invalid(f,
			nil,
			"at least one database instance must be declared"))
		return errs
	}
	for i, inst := range *spec {
		ff := f.Child(fmt.Sprintf("%d", i))
		if inst.Port != nil {
			for _, s := range apivalidation.IsValidPortNum(int(*inst.Port)) {
				errs = append(errs, field.Invalid(ff.Child("port"), inst.Port, s))
			}
		}
		errs = append(errs, validateEnvName(ff.Child("portEnvName"),
			inst.PortEnvName)...)
		errs = append(errs, validateEnvName(ff.Child("hostEnvName"),
			inst.HostEnvName)...)
		errs = append(errs, validateEnvName(ff.Child("unixSocketPathEnvName"),
			inst.UnixSocketPathEnvName)...)

		if inst.UnixSocketPath != "" && !path.IsAbs(inst.UnixSocketPath) {
			errs = append(errs, field.Invalid(ff.Child("unixSocketPath"),
				inst.UnixSocketPath, "must be an absolute path"))
		}
		if inst.UnixSocketPath != "" && (inst.Port != nil || inst.PortEnvName != "") {
			errs = append(errs, field.Invalid(ff.Child("unixSocketPath"),
				inst.UnixSocketPath,
				"unixSocketPath cannot be set when portEnvName or port are set. Databases can be configured to listen for either TCP or Unix socket connections, not both"))
		}
		if inst.UnixSocketPath == "" && inst.Port == nil && inst.PortEnvName == "" {
			errs = append(errs, field.Invalid(f,
				inst.UnixSocketPath,
				"instance must specify at least one of the following: portEnvName, port, or unixSocketPath"))
		}
	}
	return errs
}