func ValidateIPv4OrHostname()

in operator/pkg/operator/injector/validate.go [113:138]


func ValidateIPv4OrHostname(annotation, service string) error {
	service = strings.TrimSpace(service)
	colonIndex := strings.LastIndex(service, ":")
	host := service
	if colonIndex != -1 {
		host = service[:colonIndex]
	}
	if host == "" {
		return fmt.Errorf("%s error:the service name is nil", annotation)
	}
	ip := net.ParseIP(host)
	if ip != nil && ip.To4() != nil {
		return nil
	}

	match, err := regexp.MatchString(`^([a-zA-Z0-9][a-zA-Z0-9_-]{0,62})(\.[a-zA-Z0-9_][a-zA-Z0-9_-]{0,62})*?$`,
		host)
	if err != nil {
		return fmt.Errorf("%s error:%s", annotation, err.Error())
	}
	if !match {
		return fmt.Errorf("%s=%s error:not a valid ipv4 or hostname", annotation, host)
	}

	return nil
}