github.com/jenkins-x/test-infra@v0.0.7/kubetest/federation.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 main
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"os/exec"
    23  	"strings"
    24  
    25  	"k8s.io/test-infra/kubetest/util"
    26  )
    27  
    28  /*
    29   multiClusterDeployment type holds the data passed to `--multi-clusters` flag.
    30   The format of value that should be passed to the flag is `[Zone1:]Cluster1[,[ZoneN:]ClusterN]]*`.
    31   Multiple clusters can be specified as a comma separated list.
    32   Zone can be optionally specified along with cluster name as described above in the format.
    33   If zone is not specified along with cluster then cluster would be deployed in default zone.
    34  */
    35  type multiClusterDeployment struct {
    36  	zones    map[string]string
    37  	clusters []string
    38  }
    39  
    40  func (m *multiClusterDeployment) String() string {
    41  	var str string
    42  	for _, cluster := range m.clusters {
    43  		if len(str) != 0 {
    44  			str += ","
    45  		}
    46  		zone, exist := m.zones[cluster]
    47  		if exist {
    48  			str += zone + ":"
    49  		}
    50  		str += cluster
    51  	}
    52  	return str
    53  }
    54  
    55  func (m *multiClusterDeployment) Set(value string) error {
    56  	if len(value) == 0 {
    57  		return errors.New("invalid value passed to --multi-clusters flag, should specify at least one cluster")
    58  	}
    59  
    60  	if m.zones == nil {
    61  		m.zones = make(map[string]string)
    62  	}
    63  	clusterZones := strings.Split(value, ",")
    64  	for _, czTuple := range clusterZones {
    65  		czSlice := strings.SplitN(czTuple, ":", 2)
    66  		if len(czSlice[0]) == 0 || (len(czSlice) == 2 && len(czSlice[1]) == 0) {
    67  			return errors.New("invalid value passed to --multi-clusters flag")
    68  		}
    69  		if len(czSlice) == 2 {
    70  			m.zones[czSlice[1]] = czSlice[0]
    71  			m.clusters = append(m.clusters, czSlice[1])
    72  		} else {
    73  			m.clusters = append(m.clusters, czSlice[0])
    74  		}
    75  	}
    76  	return nil
    77  }
    78  
    79  func (m *multiClusterDeployment) Type() string {
    80  	return "multiClusterDeployment"
    81  }
    82  
    83  func (m *multiClusterDeployment) Enabled() bool {
    84  	return len(m.clusters) > 0
    85  }
    86  
    87  // TODO: Remove below logic in main repo once we drop testing support for
    88  // federation in release branches. ~ around 1.11 release frame.
    89  func useFederationRepo() bool {
    90  	federationRepo := os.Getenv("FEDERATION_REPO")
    91  	if federationRepo == "" {
    92  		return false
    93  	}
    94  	return true
    95  }
    96  
    97  func fedUp() error {
    98  	var cmd string
    99  	if useFederationRepo() {
   100  		cmd = "../federation/deploy/cluster/federation-up.sh"
   101  	} else {
   102  		cmd = "./federation/cluster/federation-up.sh"
   103  	}
   104  	return control.FinishRunning(exec.Command(cmd))
   105  }
   106  
   107  func federationTest(testArgs []string) error {
   108  	var cmd string
   109  	if useFederationRepo() {
   110  		cmd = "../federation/hack/federated-ginkgo-e2e.sh"
   111  	} else {
   112  		cmd = "./hack/federated-ginkgo-e2e.sh"
   113  	}
   114  	testArgs = util.SetFieldDefault(testArgs, "--ginkgo.focus", "\\[Feature:Federation\\]")
   115  	return control.FinishRunning(exec.Command(cmd, testArgs...))
   116  }
   117  
   118  func fedDown() error {
   119  	var cmd string
   120  	if useFederationRepo() {
   121  		cmd = "../federation/deploy/cluster/federation-down.sh"
   122  	} else {
   123  		cmd = "./federation/cluster/federation-down.sh"
   124  	}
   125  	return control.FinishRunning(exec.Command(cmd))
   126  }