github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/orchestrator.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  )
     8  
     9  // Orchestrator type acts as an enum describing supported orchestrators.
    10  type Orchestrator string
    11  
    12  const (
    13  	// OrchestratorKubernetes orchestrator
    14  	OrchestratorKubernetes = Orchestrator("kubernetes")
    15  	// OrchestratorSwarm orchestrator
    16  	OrchestratorSwarm = Orchestrator("swarm")
    17  	// OrchestratorAll orchestrator
    18  	OrchestratorAll   = Orchestrator("all")
    19  	orchestratorUnset = Orchestrator("")
    20  
    21  	defaultOrchestrator           = OrchestratorSwarm
    22  	envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
    23  	envVarDockerOrchestrator      = "DOCKER_ORCHESTRATOR"
    24  )
    25  
    26  // HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
    27  func (o Orchestrator) HasKubernetes() bool {
    28  	return o == OrchestratorKubernetes || o == OrchestratorAll
    29  }
    30  
    31  // HasSwarm returns true if defined orchestrator has Swarm capabilities.
    32  func (o Orchestrator) HasSwarm() bool {
    33  	return o == OrchestratorSwarm || o == OrchestratorAll
    34  }
    35  
    36  // HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities.
    37  func (o Orchestrator) HasAll() bool {
    38  	return o == OrchestratorAll
    39  }
    40  
    41  func normalize(value string) (Orchestrator, error) {
    42  	switch value {
    43  	case "kubernetes":
    44  		return OrchestratorKubernetes, nil
    45  	case "swarm":
    46  		return OrchestratorSwarm, nil
    47  	case "", "unset": // unset is the old value for orchestratorUnset. Keep accepting this for backward compat
    48  		return orchestratorUnset, nil
    49  	case "all":
    50  		return OrchestratorAll, nil
    51  	default:
    52  		return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes, swarm or all", value)
    53  	}
    54  }
    55  
    56  // NormalizeOrchestrator parses an orchestrator value and checks if it is valid
    57  func NormalizeOrchestrator(value string) (Orchestrator, error) {
    58  	return normalize(value)
    59  }
    60  
    61  // GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file
    62  // orchestrator value and returns user defined Orchestrator.
    63  func GetStackOrchestrator(flagValue, contextValue, globalDefault string, stderr io.Writer) (Orchestrator, error) {
    64  	// Check flag
    65  	if o, err := normalize(flagValue); o != orchestratorUnset {
    66  		return o, err
    67  	}
    68  	// Check environment variable
    69  	env := os.Getenv(envVarDockerStackOrchestrator)
    70  	if env == "" && os.Getenv(envVarDockerOrchestrator) != "" {
    71  		fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator)
    72  	}
    73  	if o, err := normalize(env); o != orchestratorUnset {
    74  		return o, err
    75  	}
    76  	if o, err := normalize(contextValue); o != orchestratorUnset {
    77  		return o, err
    78  	}
    79  	if o, err := normalize(globalDefault); o != orchestratorUnset {
    80  		return o, err
    81  	}
    82  	// Nothing set, use default orchestrator
    83  	return defaultOrchestrator, nil
    84  }