k8s.io/kubernetes@v1.29.3/test/e2e/cloud/gcp/common/upgrade_context.go (about)

     1  /*
     2  Copyright 2016 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 common
    18  
    19  import (
    20  	"fmt"
    21  	"path"
    22  	"strings"
    23  
    24  	utilversion "k8s.io/apimachinery/pkg/util/version"
    25  	"k8s.io/client-go/discovery"
    26  	"k8s.io/kubernetes/test/e2e/framework"
    27  	e2econfig "k8s.io/kubernetes/test/e2e/framework/config"
    28  	"k8s.io/kubernetes/test/e2e/upgrades"
    29  )
    30  
    31  var (
    32  	upgradeTarget = e2econfig.Flags.String("upgrade-target", "ci/latest", "Version to upgrade to (e.g. 'release/stable', 'release/latest', 'ci/latest', '0.19.1', '0.19.1-669-gabac8c8') if doing an upgrade test.")
    33  	upgradeImage  = e2econfig.Flags.String("upgrade-image", "", "Image to upgrade to (e.g. 'container_vm' or 'gci') if doing an upgrade test.")
    34  )
    35  
    36  // GetUpgradeContext return UpgradeContext for GCP provider.
    37  func GetUpgradeContext(c discovery.DiscoveryInterface) (*upgrades.UpgradeContext, error) {
    38  	current, err := c.ServerVersion()
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	curVer, err := utilversion.ParseSemantic(current.String())
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	upgCtx := &upgrades.UpgradeContext{
    49  		Versions: []upgrades.VersionContext{
    50  			{
    51  				Version:   *curVer,
    52  				NodeImage: framework.TestContext.NodeOSDistro,
    53  			},
    54  		},
    55  	}
    56  
    57  	if len(*upgradeTarget) == 0 {
    58  		return upgCtx, nil
    59  	}
    60  
    61  	next, err := realVersion(*upgradeTarget)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	nextVer, err := utilversion.ParseSemantic(next)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	upgCtx.Versions = append(upgCtx.Versions, upgrades.VersionContext{
    72  		Version:   *nextVer,
    73  		NodeImage: *upgradeImage,
    74  	})
    75  
    76  	return upgCtx, nil
    77  }
    78  
    79  // realVersion turns a version constants into a version string deployable on
    80  // GKE.  See hack/get-build.sh for more information.
    81  func realVersion(s string) (string, error) {
    82  	framework.Logf("Getting real version for %q", s)
    83  	v, _, err := framework.RunCmd(path.Join(framework.TestContext.RepoRoot, "hack/get-build.sh"), "-v", s)
    84  	if err != nil {
    85  		return v, fmt.Errorf("error getting real version for %q: %w", s, err)
    86  	}
    87  	framework.Logf("Version for %q is %q", s, v)
    88  	return strings.TrimPrefix(strings.TrimSpace(v), "v"), nil
    89  }