k8s.io/kubernetes@v1.29.3/test/e2e/cloud/gcp/gke_node_pools.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 gcp
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os/exec"
    23  
    24  	"k8s.io/kubernetes/test/e2e/feature"
    25  	"k8s.io/kubernetes/test/e2e/framework"
    26  	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
    27  	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
    28  	admissionapi "k8s.io/pod-security-admission/api"
    29  
    30  	"github.com/onsi/ginkgo/v2"
    31  )
    32  
    33  var _ = SIGDescribe("GKE node pools", feature.GKENodePool, func() {
    34  
    35  	f := framework.NewDefaultFramework("node-pools")
    36  	f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
    37  
    38  	ginkgo.BeforeEach(func() {
    39  		e2eskipper.SkipUnlessProviderIs("gke")
    40  	})
    41  
    42  	f.It("should create a cluster with multiple node pools", feature.GKENodePool, func(ctx context.Context) {
    43  		framework.Logf("Start create node pool test")
    44  		testCreateDeleteNodePool(ctx, f, "test-pool")
    45  	})
    46  })
    47  
    48  func testCreateDeleteNodePool(ctx context.Context, f *framework.Framework, poolName string) {
    49  	framework.Logf("Create node pool: %q in cluster: %q", poolName, framework.TestContext.CloudConfig.Cluster)
    50  
    51  	clusterStr := fmt.Sprintf("--cluster=%s", framework.TestContext.CloudConfig.Cluster)
    52  
    53  	out, err := exec.Command("gcloud", "container", "node-pools", "create",
    54  		poolName,
    55  		clusterStr,
    56  		"--num-nodes=2").CombinedOutput()
    57  	framework.Logf("\n%s", string(out))
    58  	if err != nil {
    59  		framework.Failf("Failed to create node pool %q. Err: %v\n%v", poolName, err, string(out))
    60  	}
    61  	framework.Logf("Successfully created node pool %q.", poolName)
    62  
    63  	out, err = exec.Command("gcloud", "container", "node-pools", "list",
    64  		clusterStr).CombinedOutput()
    65  	if err != nil {
    66  		framework.Failf("Failed to list node pools from cluster %q. Err: %v\n%v", framework.TestContext.CloudConfig.Cluster, err, string(out))
    67  	}
    68  	framework.Logf("Node pools:\n%s", string(out))
    69  
    70  	framework.Logf("Checking that 2 nodes have the correct node pool label.")
    71  	nodeCount := nodesWithPoolLabel(ctx, f, poolName)
    72  	if nodeCount != 2 {
    73  		framework.Failf("Wanted 2 nodes with node pool label, got: %v", nodeCount)
    74  	}
    75  	framework.Logf("Success, found 2 nodes with correct node pool labels.")
    76  
    77  	framework.Logf("Deleting node pool: %q in cluster: %q", poolName, framework.TestContext.CloudConfig.Cluster)
    78  	out, err = exec.Command("gcloud", "container", "node-pools", "delete",
    79  		poolName,
    80  		clusterStr,
    81  		"-q").CombinedOutput()
    82  	framework.Logf("\n%s", string(out))
    83  	if err != nil {
    84  		framework.Failf("Failed to delete node pool %q. Err: %v\n%v", poolName, err, string(out))
    85  	}
    86  	framework.Logf("Successfully deleted node pool %q.", poolName)
    87  
    88  	out, err = exec.Command("gcloud", "container", "node-pools", "list",
    89  		clusterStr).CombinedOutput()
    90  	if err != nil {
    91  		framework.Failf("\nFailed to list node pools from cluster %q. Err: %v\n%v", framework.TestContext.CloudConfig.Cluster, err, string(out))
    92  	}
    93  	framework.Logf("\nNode pools:\n%s", string(out))
    94  
    95  	framework.Logf("Checking that no nodes have the deleted node pool's label.")
    96  	nodeCount = nodesWithPoolLabel(ctx, f, poolName)
    97  	if nodeCount != 0 {
    98  		framework.Failf("Wanted 0 nodes with node pool label, got: %v", nodeCount)
    99  	}
   100  	framework.Logf("Success, found no nodes with the deleted node pool's label.")
   101  }
   102  
   103  // nodesWithPoolLabel returns the number of nodes that have the "gke-nodepool"
   104  // label with the given node pool name.
   105  func nodesWithPoolLabel(ctx context.Context, f *framework.Framework, poolName string) int {
   106  	nodeCount := 0
   107  	nodeList, err := e2enode.GetReadySchedulableNodes(ctx, f.ClientSet)
   108  	framework.ExpectNoError(err)
   109  	for _, node := range nodeList.Items {
   110  		if poolLabel := node.Labels["cloud.google.com/gke-nodepool"]; poolLabel == poolName {
   111  			nodeCount++
   112  		}
   113  	}
   114  	return nodeCount
   115  }