github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/kubetest/bash.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  	"fmt"
    21  	"os"
    22  	"os/exec"
    23  	"strconv"
    24  	"time"
    25  )
    26  
    27  type bashDeployer struct {
    28  	clusterIPRange string
    29  }
    30  
    31  var _ deployer = &bashDeployer{}
    32  
    33  func newBash(clusterIPRange *string) *bashDeployer {
    34  	if *clusterIPRange == "" {
    35  		if numNodes, err := strconv.Atoi(os.Getenv("NUM_NODES")); err == nil {
    36  			*clusterIPRange = getClusterIPRange(numNodes)
    37  		}
    38  	}
    39  	b := &bashDeployer{*clusterIPRange}
    40  	return b
    41  }
    42  
    43  func (b *bashDeployer) Up() error {
    44  	// TODO(shashidharatd): Remove below logic of choosing the scripts to run from federation
    45  	// repo once the k8s deployment in federation jobs moves to kubernetes-anywhere
    46  	var script string
    47  	if useFederationRepo() {
    48  		script = "../federation/hack/e2e-internal/e2e-up.sh"
    49  	} else {
    50  		script = "./hack/e2e-internal/e2e-up.sh"
    51  	}
    52  	cmd := exec.Command(script)
    53  	cmd.Env = os.Environ()
    54  	cmd.Env = append(cmd.Env, fmt.Sprintf("CLUSTER_IP_RANGE=%s", b.clusterIPRange))
    55  	return control.FinishRunning(cmd)
    56  }
    57  
    58  func (b *bashDeployer) IsUp() error {
    59  	var cmd string
    60  	if useFederationRepo() {
    61  		cmd = "../federation/hack/e2e-internal/e2e-status.sh"
    62  	} else {
    63  		cmd = "./hack/e2e-internal/e2e-status.sh"
    64  	}
    65  	return control.FinishRunning(exec.Command(cmd))
    66  }
    67  
    68  func (b *bashDeployer) DumpClusterLogs(localPath, gcsPath string) error {
    69  	return defaultDumpClusterLogs(localPath, gcsPath)
    70  }
    71  
    72  func (b *bashDeployer) TestSetup() error {
    73  	return nil
    74  }
    75  
    76  func (b *bashDeployer) Down() error {
    77  	var cmd string
    78  	if useFederationRepo() {
    79  		cmd = "../federation/hack/e2e-internal/e2e-down.sh"
    80  	} else {
    81  		cmd = "./hack/e2e-internal/e2e-down.sh"
    82  	}
    83  	return control.FinishRunning(exec.Command(cmd))
    84  }
    85  
    86  func (b *bashDeployer) GetClusterCreated(gcpProject string) (time.Time, error) {
    87  	res, err := control.Output(exec.Command(
    88  		"gcloud",
    89  		"compute",
    90  		"instance-groups",
    91  		"list",
    92  		"--project="+gcpProject,
    93  		"--format=json(name,creationTimestamp)"))
    94  	if err != nil {
    95  		return time.Time{}, fmt.Errorf("list instance-group failed : %v", err)
    96  	}
    97  
    98  	created, err := getLatestClusterUpTime(string(res))
    99  	if err != nil {
   100  		return time.Time{}, fmt.Errorf("parse time failed : got gcloud res %s, err %v", string(res), err)
   101  	}
   102  	return created, nil
   103  }
   104  
   105  // Calculates the cluster IP range based on the no. of nodes in the cluster.
   106  // Note: This mimics the function get-cluster-ip-range used by kube-up script.
   107  func getClusterIPRange(numNodes int) string {
   108  	suggestedRange := "10.64.0.0/14"
   109  	if numNodes > 1000 {
   110  		suggestedRange = "10.64.0.0/13"
   111  	}
   112  	if numNodes > 2000 {
   113  		suggestedRange = "10.64.0.0/12"
   114  	}
   115  	if numNodes > 4000 {
   116  		suggestedRange = "10.64.0.0/11"
   117  	}
   118  	return suggestedRange
   119  }