2018-01-09 18:57:14 +00:00
|
|
|
/*
|
|
|
|
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 utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
apps "k8s.io/api/apps/v1"
|
2018-01-09 18:57:14 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
type UpdateReplicaSetFunc func(d *apps.ReplicaSet)
|
2018-01-09 18:57:14 +00:00
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
func UpdateReplicaSetWithRetries(c clientset.Interface, namespace, name string, applyUpdate UpdateReplicaSetFunc, logf LogfFn, pollInterval, pollTimeout time.Duration) (*apps.ReplicaSet, error) {
|
|
|
|
var rs *apps.ReplicaSet
|
2018-01-09 18:57:14 +00:00
|
|
|
var updateErr error
|
|
|
|
pollErr := wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
|
|
|
|
var err error
|
2018-07-18 14:47:22 +00:00
|
|
|
if rs, err = c.AppsV1().ReplicaSets(namespace).Get(name, metav1.GetOptions{}); err != nil {
|
2018-01-09 18:57:14 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// Apply the update, then attempt to push it to the apiserver.
|
|
|
|
applyUpdate(rs)
|
2018-07-18 14:47:22 +00:00
|
|
|
if rs, err = c.AppsV1().ReplicaSets(namespace).Update(rs); err == nil {
|
2018-01-09 18:57:14 +00:00
|
|
|
logf("Updating replica set %q", name)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
updateErr = err
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if pollErr == wait.ErrWaitTimeout {
|
|
|
|
pollErr = fmt.Errorf("couldn't apply the provided updated to replicaset %q: %v", name, updateErr)
|
|
|
|
}
|
|
|
|
return rs, pollErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify .Status.Replicas is equal to .Spec.Replicas
|
2018-07-18 14:47:22 +00:00
|
|
|
func WaitRSStable(t *testing.T, clientSet clientset.Interface, rs *apps.ReplicaSet, pollInterval, pollTimeout time.Duration) error {
|
2018-01-09 18:57:14 +00:00
|
|
|
desiredGeneration := rs.Generation
|
|
|
|
if err := wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
|
2018-07-18 14:47:22 +00:00
|
|
|
newRS, err := clientSet.AppsV1().ReplicaSets(rs.Namespace).Get(rs.Name, metav1.GetOptions{})
|
2018-01-09 18:57:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return newRS.Status.ObservedGeneration >= desiredGeneration && newRS.Status.Replicas == *rs.Spec.Replicas, nil
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("failed to verify .Status.Replicas is equal to .Spec.Replicas for replicaset %q: %v", rs.Name, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-06 22:33:18 +00:00
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
func UpdateReplicaSetStatusWithRetries(c clientset.Interface, namespace, name string, applyUpdate UpdateReplicaSetFunc, logf LogfFn, pollInterval, pollTimeout time.Duration) (*apps.ReplicaSet, error) {
|
|
|
|
var rs *apps.ReplicaSet
|
2018-03-06 22:33:18 +00:00
|
|
|
var updateErr error
|
|
|
|
pollErr := wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
|
|
|
|
var err error
|
2018-07-18 14:47:22 +00:00
|
|
|
if rs, err = c.AppsV1().ReplicaSets(namespace).Get(name, metav1.GetOptions{}); err != nil {
|
2018-03-06 22:33:18 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// Apply the update, then attempt to push it to the apiserver.
|
|
|
|
applyUpdate(rs)
|
2018-07-18 14:47:22 +00:00
|
|
|
if rs, err = c.AppsV1().ReplicaSets(namespace).UpdateStatus(rs); err == nil {
|
2018-03-06 22:33:18 +00:00
|
|
|
logf("Updating replica set %q", name)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
updateErr = err
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if pollErr == wait.ErrWaitTimeout {
|
|
|
|
pollErr = fmt.Errorf("couldn't apply the provided update to replicaset %q: %v", name, updateErr)
|
|
|
|
}
|
|
|
|
return rs, pollErr
|
|
|
|
}
|