github.com/cilium/cilium@v1.16.2/pkg/clustermesh/types/types.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package types
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"regexp"
    10  
    11  	"github.com/cilium/cilium/pkg/defaults"
    12  )
    13  
    14  const (
    15  	// ClusterIDMin is the minimum value of the cluster ID
    16  	ClusterIDMin    = 0
    17  	ClusterIDExt511 = 511
    18  
    19  	ClusterIDUnset = ClusterIDMin
    20  )
    21  
    22  // ClusterIDMax is the maximum value of the cluster ID
    23  var ClusterIDMax uint32 = defaults.MaxConnectedClusters
    24  
    25  // A cluster name must respect the following constraints:
    26  // * It must contain at most 32 characters;
    27  // * It must begin and end with a lower case alphanumeric character;
    28  // * It may contain lower case alphanumeric characters and dashes between.
    29  const (
    30  	// clusterNameMaxLength is the maximum allowed length of a cluster name.
    31  	clusterNameMaxLength = 32
    32  	// clusterNameRegexStr is the regex to validate a cluster name.
    33  	clusterNameRegexStr = `^([a-z0-9][-a-z0-9]*)?[a-z0-9]$`
    34  )
    35  
    36  var clusterNameRegex = regexp.MustCompile(clusterNameRegexStr)
    37  
    38  // InitClusterIDMax validates and sets the ClusterIDMax package level variable.
    39  func (c ClusterInfo) InitClusterIDMax() error {
    40  	switch c.MaxConnectedClusters {
    41  	case defaults.MaxConnectedClusters, ClusterIDExt511:
    42  		ClusterIDMax = c.MaxConnectedClusters
    43  	default:
    44  		return fmt.Errorf("--%s=%d is invalid; supported values are [%d, %d]", OptMaxConnectedClusters, c.MaxConnectedClusters, defaults.MaxConnectedClusters, ClusterIDExt511)
    45  	}
    46  	return nil
    47  }
    48  
    49  // ValidateClusterID ensures that the given clusterID is within the configured
    50  // range of the ClusterMesh.
    51  func ValidateClusterID(clusterID uint32) error {
    52  	if clusterID == ClusterIDMin {
    53  		return fmt.Errorf("ClusterID %d is reserved", ClusterIDMin)
    54  	}
    55  
    56  	if clusterID > ClusterIDMax {
    57  		return fmt.Errorf("ClusterID > %d is not supported", ClusterIDMax)
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  // ValidateClusterName validates that the given name matches the cluster name specifications.
    64  func ValidateClusterName(name string) error {
    65  	if name == "" {
    66  		return errors.New("must not be empty")
    67  	}
    68  
    69  	if len(name) > clusterNameMaxLength {
    70  		return fmt.Errorf("must not be more than %d characters", clusterNameMaxLength)
    71  	}
    72  
    73  	if !clusterNameRegex.MatchString(name) {
    74  		return errors.New("must consist of lower case alphanumeric characters and '-', and must start and end with an alphanumeric character")
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  type CiliumClusterConfig struct {
    81  	ID uint32 `json:"id,omitempty"`
    82  
    83  	Capabilities CiliumClusterConfigCapabilities `json:"capabilities,omitempty"`
    84  }
    85  
    86  type CiliumClusterConfigCapabilities struct {
    87  	// Supports per-prefix "synced" canaries
    88  	SyncedCanaries bool `json:"syncedCanaries,omitempty"`
    89  
    90  	// The information concerning the given cluster is cached from an external
    91  	// kvstore (for instance, by kvstoremesh). This implies that keys are stored
    92  	// under the dedicated "cilium/cache" prefix, and all are cluster-scoped.
    93  	Cached bool `json:"cached,omitempty"`
    94  
    95  	// The maximum number of clusters the given cluster can support in a ClusterMesh.
    96  	MaxConnectedClusters uint32 `json:"maxConnectedClusters,omitempty"`
    97  }