mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
add replicaset upgrade test
This commit is contained in:
parent
fc1bd98c09
commit
db2c027154
@ -18,16 +18,14 @@ package framework
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
|
|
||||||
|
"k8s.io/api/core/v1"
|
||||||
extensions "k8s.io/api/extensions/v1beta1"
|
extensions "k8s.io/api/extensions/v1beta1"
|
||||||
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
clientset "k8s.io/client-go/kubernetes"
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
|
||||||
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
|
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
|
||||||
testutils "k8s.io/kubernetes/test/utils"
|
testutils "k8s.io/kubernetes/test/utils"
|
||||||
)
|
)
|
||||||
@ -57,47 +55,6 @@ func CheckNewRSAnnotations(c clientset.Interface, ns, deploymentName string, exp
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete a ReplicaSet and all pods it spawned
|
|
||||||
func DeleteReplicaSet(clientset clientset.Interface, internalClientset internalclientset.Interface, ns, name string) error {
|
|
||||||
By(fmt.Sprintf("deleting ReplicaSet %s in namespace %s", name, ns))
|
|
||||||
rs, err := clientset.Extensions().ReplicaSets(ns).Get(name, metav1.GetOptions{})
|
|
||||||
if err != nil {
|
|
||||||
if apierrs.IsNotFound(err) {
|
|
||||||
Logf("ReplicaSet %s was already deleted: %v", name, err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
startTime := time.Now()
|
|
||||||
err = clientset.ExtensionsV1beta1().ReplicaSets(ns).Delete(name, &metav1.DeleteOptions{})
|
|
||||||
if apierrs.IsNotFound(err) {
|
|
||||||
Logf("ReplicaSet %s was already deleted: %v", name, err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
deleteRSTime := time.Now().Sub(startTime)
|
|
||||||
Logf("Deleting RS %s took: %v", name, deleteRSTime)
|
|
||||||
if err == nil {
|
|
||||||
err = waitForReplicaSetPodsGone(clientset, rs)
|
|
||||||
}
|
|
||||||
terminatePodTime := time.Now().Sub(startTime) - deleteRSTime
|
|
||||||
Logf("Terminating ReplicaSet %s pods took: %v", name, terminatePodTime)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// waitForReplicaSetPodsGone waits until there are no pods reported under a
|
|
||||||
// ReplicaSet selector (because the pods have completed termination).
|
|
||||||
func waitForReplicaSetPodsGone(c clientset.Interface, rs *extensions.ReplicaSet) error {
|
|
||||||
return wait.PollImmediate(Poll, 2*time.Minute, func() (bool, error) {
|
|
||||||
selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
|
|
||||||
ExpectNoError(err)
|
|
||||||
options := metav1.ListOptions{LabelSelector: selector.String()}
|
|
||||||
if pods, err := c.Core().Pods(rs.Namespace).List(options); err == nil && len(pods.Items) == 0 {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WaitForReadyReplicaSet waits until the replicaset has all of its replicas ready.
|
// WaitForReadyReplicaSet waits until the replicaset has all of its replicas ready.
|
||||||
func WaitForReadyReplicaSet(c clientset.Interface, ns, name string) error {
|
func WaitForReadyReplicaSet(c clientset.Interface, ns, name string) error {
|
||||||
err := wait.Poll(Poll, pollShortTimeout, func() (bool, error) {
|
err := wait.Poll(Poll, pollShortTimeout, func() (bool, error) {
|
||||||
@ -119,3 +76,35 @@ func RunReplicaSet(config testutils.ReplicaSetConfig) error {
|
|||||||
config.ContainerDumpFunc = LogFailedContainers
|
config.ContainerDumpFunc = LogFailedContainers
|
||||||
return testutils.RunReplicaSet(config)
|
return testutils.RunReplicaSet(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewReplicaSet(name, namespace string, replicas int32, podLabels map[string]string, imageName, image string) *extensions.ReplicaSet {
|
||||||
|
return &extensions.ReplicaSet{
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
Kind: "ReplicaSet",
|
||||||
|
APIVersion: "extensions/v1beta1",
|
||||||
|
},
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Namespace: namespace,
|
||||||
|
Name: name,
|
||||||
|
},
|
||||||
|
Spec: extensions.ReplicaSetSpec{
|
||||||
|
Selector: &metav1.LabelSelector{
|
||||||
|
MatchLabels: podLabels,
|
||||||
|
},
|
||||||
|
Replicas: &replicas,
|
||||||
|
Template: v1.PodTemplateSpec{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Labels: podLabels,
|
||||||
|
},
|
||||||
|
Spec: v1.PodSpec{
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{
|
||||||
|
Name: imageName,
|
||||||
|
Image: image,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -42,6 +42,7 @@ import (
|
|||||||
var upgradeTests = []upgrades.Test{
|
var upgradeTests = []upgrades.Test{
|
||||||
&upgrades.ServiceUpgradeTest{},
|
&upgrades.ServiceUpgradeTest{},
|
||||||
&upgrades.SecretUpgradeTest{},
|
&upgrades.SecretUpgradeTest{},
|
||||||
|
&apps.ReplicaSetUpgradeTest{},
|
||||||
&apps.StatefulSetUpgradeTest{},
|
&apps.StatefulSetUpgradeTest{},
|
||||||
&apps.DeploymentUpgradeTest{},
|
&apps.DeploymentUpgradeTest{},
|
||||||
&apps.JobUpgradeTest{},
|
&apps.JobUpgradeTest{},
|
||||||
|
@ -11,6 +11,7 @@ go_library(
|
|||||||
"daemonsets.go",
|
"daemonsets.go",
|
||||||
"deployments.go",
|
"deployments.go",
|
||||||
"job.go",
|
"job.go",
|
||||||
|
"replicasets.go",
|
||||||
"statefulset.go",
|
"statefulset.go",
|
||||||
],
|
],
|
||||||
deps = [
|
deps = [
|
||||||
@ -28,6 +29,7 @@ go_library(
|
|||||||
"//vendor/k8s.io/api/extensions/v1beta1:go_default_library",
|
"//vendor/k8s.io/api/extensions/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
101
test/e2e/upgrades/apps/replicasets.go
Normal file
101
test/e2e/upgrades/apps/replicasets.go
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package upgrades
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
extensions "k8s.io/api/extensions/v1beta1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"k8s.io/kubernetes/test/e2e/framework"
|
||||||
|
"k8s.io/kubernetes/test/e2e/upgrades"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
imageutils "k8s.io/kubernetes/test/utils/image"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
interval = 10 * time.Second
|
||||||
|
timeout = 5 * time.Minute
|
||||||
|
rsName = "rs"
|
||||||
|
scaleNum = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: Test that the replicaset stays available during master (and maybe
|
||||||
|
// node and cluster upgrades).
|
||||||
|
|
||||||
|
// ReplicaSetUpgradeTest tests that a replicaset survives upgrade.
|
||||||
|
type ReplicaSetUpgradeTest struct {
|
||||||
|
UID types.UID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ReplicaSetUpgradeTest) Name() string { return "[sig-apps] replicaset-upgrade" }
|
||||||
|
|
||||||
|
func (r *ReplicaSetUpgradeTest) Setup(f *framework.Framework) {
|
||||||
|
c := f.ClientSet
|
||||||
|
ns := f.Namespace.Name
|
||||||
|
nginxImage := imageutils.GetE2EImage(imageutils.NginxSlim)
|
||||||
|
|
||||||
|
By(fmt.Sprintf("Creating replicaset %s in namespace %s", rsName, ns))
|
||||||
|
replicaSet := framework.NewReplicaSet(rsName, ns, 1, map[string]string{"test": "upgrade"}, "nginx", nginxImage)
|
||||||
|
rs, err := c.Extensions().ReplicaSets(ns).Create(replicaSet)
|
||||||
|
framework.ExpectNoError(err)
|
||||||
|
|
||||||
|
By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready", rsName))
|
||||||
|
framework.ExpectNoError(framework.WaitForReadyReplicaSet(c, ns, rsName))
|
||||||
|
|
||||||
|
r.UID = rs.UID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checks whether the replicasets are the same after an upgrade.
|
||||||
|
func (r *ReplicaSetUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
|
||||||
|
c := f.ClientSet
|
||||||
|
ns := f.Namespace.Name
|
||||||
|
rsClient := c.Extensions().ReplicaSets(ns)
|
||||||
|
|
||||||
|
// Block until upgrade is done
|
||||||
|
By(fmt.Sprintf("Waiting for upgrade to finish before checking replicaset %s", rsName))
|
||||||
|
<-done
|
||||||
|
|
||||||
|
// Verify the RS is the same (survives) after the upgrade
|
||||||
|
By(fmt.Sprintf("Checking UID to verify replicaset %s survives upgrade", rsName))
|
||||||
|
upgradedRS, err := rsClient.Get(rsName, metav1.GetOptions{})
|
||||||
|
framework.ExpectNoError(err)
|
||||||
|
if upgradedRS.UID != r.UID {
|
||||||
|
framework.ExpectNoError(fmt.Errorf("expected same replicaset UID: %v got: %v", r.UID, upgradedRS.UID))
|
||||||
|
}
|
||||||
|
|
||||||
|
By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready after upgrade", rsName))
|
||||||
|
framework.ExpectNoError(framework.WaitForReadyReplicaSet(c, ns, rsName))
|
||||||
|
|
||||||
|
// Verify the upgraded RS is active by scaling up the RS to scaleNum and ensuring all pods are Ready
|
||||||
|
By(fmt.Sprintf("Scaling up replicaset %s to %d", rsName, scaleNum))
|
||||||
|
_, err = framework.UpdateReplicaSetWithRetries(c, ns, rsName, func(rs *extensions.ReplicaSet) {
|
||||||
|
*rs.Spec.Replicas = scaleNum
|
||||||
|
})
|
||||||
|
framework.ExpectNoError(err)
|
||||||
|
|
||||||
|
By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready after scaling", rsName))
|
||||||
|
framework.ExpectNoError(framework.WaitForReadyReplicaSet(c, ns, rsName))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Teardown cleans up any remaining resources.
|
||||||
|
func (r *ReplicaSetUpgradeTest) Teardown(f *framework.Framework) {
|
||||||
|
// rely on the namespace deletion to clean up everything
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user