k8s.io/kubernetes@v1.29.3/test/e2e/upgrades/apps/replicasets.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 apps
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	appsv1 "k8s.io/api/apps/v1"
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  	"k8s.io/kubernetes/test/e2e/framework"
    28  	e2ereplicaset "k8s.io/kubernetes/test/e2e/framework/replicaset"
    29  	"k8s.io/kubernetes/test/e2e/upgrades"
    30  
    31  	"github.com/onsi/ginkgo/v2"
    32  	imageutils "k8s.io/kubernetes/test/utils/image"
    33  )
    34  
    35  const (
    36  	rsName   = "rs"
    37  	scaleNum = 2
    38  )
    39  
    40  // TODO: Test that the replicaset stays available during master (and maybe
    41  // node and cluster upgrades).
    42  
    43  // ReplicaSetUpgradeTest tests that a replicaset survives upgrade.
    44  type ReplicaSetUpgradeTest struct {
    45  	UID types.UID
    46  }
    47  
    48  // Name returns the tracking name of the test.
    49  func (ReplicaSetUpgradeTest) Name() string { return "[sig-apps] replicaset-upgrade" }
    50  
    51  // Setup creates a ReplicaSet and makes sure it's replicas ready.
    52  func (r *ReplicaSetUpgradeTest) Setup(ctx context.Context, f *framework.Framework) {
    53  	c := f.ClientSet
    54  	ns := f.Namespace.Name
    55  	nginxImage := imageutils.GetE2EImage(imageutils.Nginx)
    56  
    57  	ginkgo.By(fmt.Sprintf("Creating replicaset %s in namespace %s", rsName, ns))
    58  	replicaSet := newReplicaSet(rsName, ns, 1, map[string]string{"test": "upgrade"}, "nginx", nginxImage)
    59  	rs, err := c.AppsV1().ReplicaSets(ns).Create(ctx, replicaSet, metav1.CreateOptions{})
    60  	framework.ExpectNoError(err)
    61  
    62  	ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready", rsName))
    63  	framework.ExpectNoError(e2ereplicaset.WaitForReadyReplicaSet(ctx, c, ns, rsName))
    64  
    65  	r.UID = rs.UID
    66  }
    67  
    68  // Test checks whether the replicasets are the same after an upgrade.
    69  func (r *ReplicaSetUpgradeTest) Test(ctx context.Context, f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
    70  	c := f.ClientSet
    71  	ns := f.Namespace.Name
    72  	rsClient := c.AppsV1().ReplicaSets(ns)
    73  
    74  	// Block until upgrade is done
    75  	ginkgo.By(fmt.Sprintf("Waiting for upgrade to finish before checking replicaset %s", rsName))
    76  	<-done
    77  
    78  	// Verify the RS is the same (survives) after the upgrade
    79  	ginkgo.By(fmt.Sprintf("Checking UID to verify replicaset %s survives upgrade", rsName))
    80  	upgradedRS, err := rsClient.Get(ctx, rsName, metav1.GetOptions{})
    81  	framework.ExpectNoError(err)
    82  	if upgradedRS.UID != r.UID {
    83  		framework.ExpectNoError(fmt.Errorf("expected same replicaset UID: %v got: %v", r.UID, upgradedRS.UID))
    84  	}
    85  
    86  	ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready after upgrade", rsName))
    87  	framework.ExpectNoError(e2ereplicaset.WaitForReadyReplicaSet(ctx, c, ns, rsName))
    88  
    89  	// Verify the upgraded RS is active by scaling up the RS to scaleNum and ensuring all pods are Ready
    90  	ginkgo.By(fmt.Sprintf("Scaling up replicaset %s to %d", rsName, scaleNum))
    91  	_, err = e2ereplicaset.UpdateReplicaSetWithRetries(c, ns, rsName, func(rs *appsv1.ReplicaSet) {
    92  		*rs.Spec.Replicas = scaleNum
    93  	})
    94  	framework.ExpectNoError(err)
    95  
    96  	ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready after scaling", rsName))
    97  	framework.ExpectNoError(e2ereplicaset.WaitForReadyReplicaSet(ctx, c, ns, rsName))
    98  }
    99  
   100  // Teardown cleans up any remaining resources.
   101  func (r *ReplicaSetUpgradeTest) Teardown(ctx context.Context, f *framework.Framework) {
   102  	// rely on the namespace deletion to clean up everything
   103  }
   104  
   105  // newReplicaSet returns a new ReplicaSet.
   106  func newReplicaSet(name, namespace string, replicas int32, podLabels map[string]string, imageName, image string) *appsv1.ReplicaSet {
   107  	return &appsv1.ReplicaSet{
   108  		TypeMeta: metav1.TypeMeta{
   109  			Kind:       "ReplicaSet",
   110  			APIVersion: "apps/v1",
   111  		},
   112  		ObjectMeta: metav1.ObjectMeta{
   113  			Namespace: namespace,
   114  			Name:      name,
   115  		},
   116  		Spec: appsv1.ReplicaSetSpec{
   117  			Selector: &metav1.LabelSelector{
   118  				MatchLabels: podLabels,
   119  			},
   120  			Replicas: &replicas,
   121  			Template: v1.PodTemplateSpec{
   122  				ObjectMeta: metav1.ObjectMeta{
   123  					Labels: podLabels,
   124  				},
   125  				Spec: v1.PodSpec{
   126  					Containers: []v1.Container{
   127  						{
   128  							Name:            imageName,
   129  							Image:           image,
   130  							SecurityContext: &v1.SecurityContext{},
   131  						},
   132  					},
   133  				},
   134  			},
   135  		},
   136  	}
   137  }