sigs.k8s.io/external-dns@v0.14.1/pkg/apis/externaldns/validation/validation.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package validation
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	"k8s.io/apimachinery/pkg/labels"
    24  
    25  	"sigs.k8s.io/external-dns/pkg/apis/externaldns"
    26  )
    27  
    28  // ValidateConfig performs validation on the Config object
    29  func ValidateConfig(cfg *externaldns.Config) error {
    30  	// TODO: Should probably return field.ErrorList
    31  	if cfg.LogFormat != "text" && cfg.LogFormat != "json" {
    32  		return fmt.Errorf("unsupported log format: %s", cfg.LogFormat)
    33  	}
    34  	if len(cfg.Sources) == 0 {
    35  		return errors.New("no sources specified")
    36  	}
    37  	if cfg.Provider == "" {
    38  		return errors.New("no provider specified")
    39  	}
    40  
    41  	// Azure provider specific validations
    42  	if cfg.Provider == "azure" {
    43  		if cfg.AzureConfigFile == "" {
    44  			return errors.New("no Azure config file specified")
    45  		}
    46  	}
    47  
    48  	// Akamai provider specific validations
    49  	if cfg.Provider == "akamai" {
    50  		if cfg.AkamaiServiceConsumerDomain == "" && cfg.AkamaiEdgercPath != "" {
    51  			return errors.New("no Akamai ServiceConsumerDomain specified")
    52  		}
    53  		if cfg.AkamaiClientToken == "" && cfg.AkamaiEdgercPath != "" {
    54  			return errors.New("no Akamai client token specified")
    55  		}
    56  		if cfg.AkamaiClientSecret == "" && cfg.AkamaiEdgercPath != "" {
    57  			return errors.New("no Akamai client secret specified")
    58  		}
    59  		if cfg.AkamaiAccessToken == "" && cfg.AkamaiEdgercPath != "" {
    60  			return errors.New("no Akamai access token specified")
    61  		}
    62  	}
    63  
    64  	// Infoblox provider specific validations
    65  	if cfg.Provider == "infoblox" {
    66  		if cfg.InfobloxGridHost == "" {
    67  			return errors.New("no Infoblox Grid Manager host specified")
    68  		}
    69  		if cfg.InfobloxWapiPassword == "" {
    70  			return errors.New("no Infoblox WAPI password specified")
    71  		}
    72  	}
    73  
    74  	if cfg.Provider == "dyn" {
    75  		if cfg.DynUsername == "" {
    76  			return errors.New("no Dyn username specified")
    77  		}
    78  		if cfg.DynCustomerName == "" {
    79  			return errors.New("no Dyn customer name specified")
    80  		}
    81  
    82  		if cfg.DynMinTTLSeconds < 0 {
    83  			return errors.New("TTL specified for Dyn is negative")
    84  		}
    85  	}
    86  
    87  	if cfg.Provider == "rfc2136" {
    88  		if cfg.RFC2136MinTTL < 0 {
    89  			return errors.New("TTL specified for rfc2136 is negative")
    90  		}
    91  
    92  		if cfg.RFC2136Insecure && cfg.RFC2136GSSTSIG {
    93  			return errors.New("--rfc2136-insecure and --rfc2136-gss-tsig are mutually exclusive arguments")
    94  		}
    95  
    96  		if cfg.RFC2136GSSTSIG {
    97  			if cfg.RFC2136KerberosPassword == "" || cfg.RFC2136KerberosUsername == "" || cfg.RFC2136KerberosRealm == "" {
    98  				return errors.New("--rfc2136-kerberos-realm, --rfc2136-kerberos-username, and --rfc2136-kerberos-password are required when specifying --rfc2136-gss-tsig option")
    99  			}
   100  		}
   101  
   102  		if cfg.RFC2136BatchChangeSize < 1 {
   103  			return errors.New("batch size specified for rfc2136 cannot be less than 1")
   104  		}
   105  	}
   106  
   107  	if cfg.IgnoreHostnameAnnotation && cfg.FQDNTemplate == "" {
   108  		return errors.New("FQDN Template must be set if ignoring annotations")
   109  	}
   110  
   111  	if len(cfg.TXTPrefix) > 0 && len(cfg.TXTSuffix) > 0 {
   112  		return errors.New("txt-prefix and txt-suffix are mutual exclusive")
   113  	}
   114  
   115  	_, err := labels.Parse(cfg.LabelFilter)
   116  	if err != nil {
   117  		return errors.New("--label-filter does not specify a valid label selector")
   118  	}
   119  	return nil
   120  }