github.com/spotahome/redis-operator@v1.2.4/api/redisfailover/v1/validate.go (about)

     1  package v1
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  )
     8  
     9  const (
    10  	maxNameLength = 48
    11  )
    12  
    13  // Validate set the values by default if not defined and checks if the values given are valid
    14  func (r *RedisFailover) Validate() error {
    15  	if len(r.Name) > maxNameLength {
    16  		return fmt.Errorf("name length can't be higher than %d", maxNameLength)
    17  	}
    18  
    19  	if r.Bootstrapping() {
    20  		if r.Spec.BootstrapNode.Host == "" {
    21  			return errors.New("BootstrapNode must include a host when provided")
    22  		}
    23  
    24  		if r.Spec.BootstrapNode.Port == "" {
    25  			r.Spec.BootstrapNode.Port = strconv.Itoa(defaultRedisPort)
    26  		}
    27  		r.Spec.Redis.CustomConfig = deduplicateStr(append(bootstrappingRedisCustomConfig, r.Spec.Redis.CustomConfig...))
    28  	} else {
    29  		r.Spec.Redis.CustomConfig = deduplicateStr(append(defaultRedisCustomConfig, r.Spec.Redis.CustomConfig...))
    30  	}
    31  
    32  	if r.Spec.Redis.Image == "" {
    33  		r.Spec.Redis.Image = defaultImage
    34  	}
    35  
    36  	if r.Spec.Sentinel.Image == "" {
    37  		r.Spec.Sentinel.Image = defaultImage
    38  	}
    39  
    40  	if r.Spec.Redis.Replicas <= 0 {
    41  		r.Spec.Redis.Replicas = defaultRedisNumber
    42  	}
    43  
    44  	if r.Spec.Redis.Port <= 0 {
    45  		r.Spec.Redis.Port = defaultRedisPort
    46  	}
    47  
    48  	if r.Spec.Sentinel.Replicas <= 0 {
    49  		r.Spec.Sentinel.Replicas = defaultSentinelNumber
    50  	}
    51  
    52  	if r.Spec.Redis.Exporter.Image == "" {
    53  		r.Spec.Redis.Exporter.Image = defaultExporterImage
    54  	}
    55  
    56  	if r.Spec.Sentinel.Exporter.Image == "" {
    57  		r.Spec.Sentinel.Exporter.Image = defaultSentinelExporterImage
    58  	}
    59  
    60  	if len(r.Spec.Sentinel.CustomConfig) == 0 {
    61  		r.Spec.Sentinel.CustomConfig = defaultSentinelCustomConfig
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func deduplicateStr(strSlice []string) []string {
    68  	allKeys := make(map[string]bool)
    69  	list := []string{}
    70  	for _, item := range strSlice {
    71  		if _, value := allKeys[item]; !value {
    72  			allKeys[item] = true
    73  			list = append(list, item)
    74  		}
    75  	}
    76  	return list
    77  }