k8s.io/kubernetes@v1.29.3/test/e2e/framework/providers/gcp.go (about)

     1  /*
     2  Copyright 2014 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 providers
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"path"
    24  
    25  	"k8s.io/kubernetes/test/e2e/framework"
    26  	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
    27  )
    28  
    29  const etcdImage = "3.5.12-0"
    30  
    31  // EtcdUpgrade upgrades etcd on GCE.
    32  func EtcdUpgrade(targetStorage, targetVersion string) error {
    33  	switch framework.TestContext.Provider {
    34  	case "gce":
    35  		return etcdUpgradeGCE(targetStorage, targetVersion)
    36  	default:
    37  		return fmt.Errorf("EtcdUpgrade() is not implemented for provider %s", framework.TestContext.Provider)
    38  	}
    39  }
    40  
    41  func etcdUpgradeGCE(targetStorage, targetVersion string) error {
    42  	env := append(
    43  		os.Environ(),
    44  		"TEST_ETCD_VERSION="+targetVersion,
    45  		"STORAGE_BACKEND="+targetStorage,
    46  		"TEST_ETCD_IMAGE="+etcdImage)
    47  
    48  	_, _, err := framework.RunCmdEnv(env, GCEUpgradeScript(), "-l", "-M")
    49  	return err
    50  }
    51  
    52  // LocationParamGKE returns parameter related to location for gcloud command.
    53  func LocationParamGKE() string {
    54  	if framework.TestContext.CloudConfig.MultiMaster {
    55  		// GKE Regional Clusters are being tested.
    56  		return fmt.Sprintf("--region=%s", framework.TestContext.CloudConfig.Region)
    57  	}
    58  	return fmt.Sprintf("--zone=%s", framework.TestContext.CloudConfig.Zone)
    59  }
    60  
    61  // MasterUpgradeGKE upgrades master node to the specified version on GKE.
    62  func MasterUpgradeGKE(ctx context.Context, namespace string, v string) error {
    63  	framework.Logf("Upgrading master to %q", v)
    64  	args := []string{
    65  		"container",
    66  		"clusters",
    67  		fmt.Sprintf("--project=%s", framework.TestContext.CloudConfig.ProjectID),
    68  		LocationParamGKE(),
    69  		"upgrade",
    70  		framework.TestContext.CloudConfig.Cluster,
    71  		"--master",
    72  		fmt.Sprintf("--cluster-version=%s", v),
    73  		"--quiet",
    74  	}
    75  	_, _, err := framework.RunCmd("gcloud", framework.AppendContainerCommandGroupIfNeeded(args)...)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	e2enode.WaitForSSHTunnels(ctx, namespace)
    81  
    82  	return nil
    83  }
    84  
    85  // GCEUpgradeScript returns path of script for upgrading on GCE.
    86  func GCEUpgradeScript() string {
    87  	if len(framework.TestContext.GCEUpgradeScript) == 0 {
    88  		return path.Join(framework.TestContext.RepoRoot, "cluster/gce/upgrade.sh")
    89  	}
    90  	return framework.TestContext.GCEUpgradeScript
    91  }