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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package types
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/spf13/pflag"
    11  
    12  	"github.com/cilium/cilium/pkg/defaults"
    13  	"github.com/cilium/cilium/pkg/logging/logfields"
    14  )
    15  
    16  const (
    17  	// OptClusterName is the name of the OptClusterName option
    18  	OptClusterName = "cluster-name"
    19  
    20  	// OptClusterID is the name of the OptClusterID option
    21  	OptClusterID = "cluster-id"
    22  
    23  	// OptMaxConnectedClusters is the name of the OptMaxConnectedClusters option
    24  	OptMaxConnectedClusters = "max-connected-clusters"
    25  )
    26  
    27  // ClusterInfo groups together the ClusterID and the ClusterName
    28  type ClusterInfo struct {
    29  	ID                   uint32 `mapstructure:"cluster-id"`
    30  	Name                 string `mapstructure:"cluster-name"`
    31  	MaxConnectedClusters uint32 `mapstructure:"max-connected-clusters"`
    32  }
    33  
    34  // DefaultClusterInfo represents the default ClusterInfo values.
    35  var DefaultClusterInfo = ClusterInfo{
    36  	ID:                   0,
    37  	Name:                 defaults.ClusterName,
    38  	MaxConnectedClusters: defaults.MaxConnectedClusters,
    39  }
    40  
    41  // Flags implements the cell.Flagger interface, to register the given flags.
    42  func (def ClusterInfo) Flags(flags *pflag.FlagSet) {
    43  	flags.Uint32(OptClusterID, def.ID, "Unique identifier of the cluster")
    44  	flags.String(OptClusterName, def.Name, "Name of the cluster. It must consist of at most 32 lower case alphanumeric characters and '-', start and end with an alphanumeric character.")
    45  	flags.Uint32(OptMaxConnectedClusters, def.MaxConnectedClusters, "Maximum number of clusters to be connected in a clustermesh. Increasing this value will reduce the maximum number of identities available. Valid configurations are [255, 511].")
    46  }
    47  
    48  // Validate validates that the ClusterID is in the valid range (including ClusterID == 0),
    49  // and that the ClusterName is different from the default value if the ClusterID != 0.
    50  func (c ClusterInfo) Validate(log logrus.FieldLogger) error {
    51  	if c.ID < ClusterIDMin || c.ID > ClusterIDMax {
    52  		return fmt.Errorf("invalid cluster id %d: must be in range %d..%d",
    53  			c.ID, ClusterIDMin, ClusterIDMax)
    54  	}
    55  
    56  	return c.validateName(log)
    57  }
    58  
    59  // ValidateStrict validates that the ClusterID is in the valid range, but not 0,
    60  // and that the ClusterName is different from the default value.
    61  func (c ClusterInfo) ValidateStrict(log logrus.FieldLogger) error {
    62  	if err := ValidateClusterID(c.ID); err != nil {
    63  		return err
    64  	}
    65  
    66  	return c.validateName(log)
    67  }
    68  
    69  func (c ClusterInfo) validateName(log logrus.FieldLogger) error {
    70  	if err := ValidateClusterName(c.Name); err != nil {
    71  		log.WithField(logfields.ClusterName, c.Name).WithError(err).
    72  			Error("Invalid cluster name. This may cause degraded functionality, and will be strictly forbidden starting from Cilium v1.17")
    73  	}
    74  
    75  	if c.ID != 0 && c.Name == defaults.ClusterName {
    76  		return fmt.Errorf("cannot use default cluster name (%s) with option %s",
    77  			defaults.ClusterName, OptClusterID)
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  // ExtendedClusterMeshEnabled returns true if MaxConnectedClusters value has
    84  // been set to a value larger than the default 255.
    85  func (c ClusterInfo) ExtendedClusterMeshEnabled() bool {
    86  	return c.MaxConnectedClusters != defaults.MaxConnectedClusters
    87  }
    88  
    89  // ValidateRemoteConfig validates the remote CiliumClusterConfig to ensure
    90  // compatibility with this cluster's configuration.
    91  func (c ClusterInfo) ValidateRemoteConfig(config CiliumClusterConfig) error {
    92  	if err := ValidateClusterID(config.ID); err != nil {
    93  		return err
    94  	}
    95  
    96  	if c.ExtendedClusterMeshEnabled() && (c.MaxConnectedClusters != config.Capabilities.MaxConnectedClusters) {
    97  		return fmt.Errorf("mismatched MaxConnectedClusters; local=%d, remote=%d", c.MaxConnectedClusters, config.Capabilities.MaxConnectedClusters)
    98  	}
    99  
   100  	return nil
   101  }