github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/validate.go (about)

     1  package kube
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/jenkins-x/jx-api/pkg/client/clientset/versioned"
     8  	"github.com/jenkins-x/jx/v2/pkg/util"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/util/validation"
    11  )
    12  
    13  const (
    14  	OptionName      = "name"
    15  	OptionNamespace = "namespace"
    16  )
    17  
    18  func ValidateSubDomain(val interface{}) error {
    19  	str, ok := val.(string)
    20  	if !ok {
    21  		return fmt.Errorf("Expected some text!")
    22  	}
    23  	if strings.TrimSpace(str) == "" {
    24  		return fmt.Errorf("Value is required")
    25  	}
    26  	errors := validation.IsDNS1123Subdomain(str)
    27  	if len(errors) > 0 {
    28  		return fmt.Errorf(strings.Join(errors, "/n"))
    29  	}
    30  	return nil
    31  }
    32  
    33  func ValidateName(val interface{}) error {
    34  	str, ok := val.(string)
    35  	if !ok {
    36  		return fmt.Errorf("Expected some text!")
    37  	}
    38  	if strings.TrimSpace(str) == "" {
    39  		return fmt.Errorf("Value is required")
    40  	}
    41  	errors := validation.IsDNS1123Label(str)
    42  	if len(errors) > 0 {
    43  		return fmt.Errorf(strings.Join(errors, "/n"))
    44  	}
    45  	return nil
    46  }
    47  
    48  func ValidSubDomainOption(option string, value string) error {
    49  	if value != "" {
    50  		errors := validation.IsDNS1123Subdomain(value)
    51  		if len(errors) > 0 {
    52  			return util.InvalidOptionf(option, value, strings.Join(errors, "/n"))
    53  		}
    54  	}
    55  	return nil
    56  }
    57  
    58  func ValidNameOption(option string, value string) error {
    59  	if value != "" {
    60  		errors := validation.IsDNS1123Label(value)
    61  		if len(errors) > 0 {
    62  			return util.InvalidOptionf(option, value, strings.Join(errors, "/n"))
    63  		}
    64  	}
    65  	return nil
    66  }
    67  
    68  func ValidateEnvironmentDoesNotExist(jxClient versioned.Interface, ns string, str string) error {
    69  	if str != "" {
    70  		_, err := jxClient.JenkinsV1().Environments(ns).Get(str, metav1.GetOptions{})
    71  		if err == nil {
    72  			return fmt.Errorf("Environment %s already exists!", str)
    73  		}
    74  	}
    75  	return nil
    76  }