mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
Add smart retries to resource creations in testing framework
This commit is contained in:
@@ -81,15 +81,7 @@ func WaitUntilPodIsScheduled(c clientset.Interface, name, namespace string, time
|
||||
func RunPodAndGetNodeName(c clientset.Interface, pod *v1.Pod, timeout time.Duration) (string, error) {
|
||||
name := pod.Name
|
||||
namespace := pod.Namespace
|
||||
var err error
|
||||
// Create a Pod
|
||||
for i := 0; i < retries; i++ {
|
||||
_, err = c.CoreV1().Pods(namespace).Create(pod)
|
||||
if err == nil || apierrs.IsAlreadyExists(err) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil && !apierrs.IsAlreadyExists(err) {
|
||||
if err := CreatePodWithRetries(c, namespace, pod); err != nil {
|
||||
return "", err
|
||||
}
|
||||
p, err := WaitUntilPodIsScheduled(c, name, namespace, timeout)
|
||||
@@ -325,8 +317,7 @@ func (config *DeploymentConfig) create() error {
|
||||
|
||||
config.applyTo(&deployment.Spec.Template)
|
||||
|
||||
_, err := config.Client.ExtensionsV1beta1().Deployments(config.Namespace).Create(deployment)
|
||||
if err != nil {
|
||||
if err := CreateDeploymentWithRetries(config.Client, config.Namespace, deployment); err != nil {
|
||||
return fmt.Errorf("Error creating deployment: %v", err)
|
||||
}
|
||||
config.RCConfigLog("Created deployment with name: %v, namespace: %v, replica count: %v", deployment.Name, config.Namespace, removePtr(deployment.Spec.Replicas))
|
||||
@@ -396,8 +387,7 @@ func (config *ReplicaSetConfig) create() error {
|
||||
|
||||
config.applyTo(&rs.Spec.Template)
|
||||
|
||||
_, err := config.Client.ExtensionsV1beta1().ReplicaSets(config.Namespace).Create(rs)
|
||||
if err != nil {
|
||||
if err := CreateReplicaSetWithRetries(config.Client, config.Namespace, rs); err != nil {
|
||||
return fmt.Errorf("Error creating replica set: %v", err)
|
||||
}
|
||||
config.RCConfigLog("Created replica set with name: %v, namespace: %v, replica count: %v", rs.Name, config.Namespace, removePtr(rs.Spec.Replicas))
|
||||
@@ -463,8 +453,7 @@ func (config *JobConfig) create() error {
|
||||
|
||||
config.applyTo(&job.Spec.Template)
|
||||
|
||||
_, err := config.Client.BatchV1().Jobs(config.Namespace).Create(job)
|
||||
if err != nil {
|
||||
if err := CreateJobWithRetries(config.Client, config.Namespace, job); err != nil {
|
||||
return fmt.Errorf("Error creating job: %v", err)
|
||||
}
|
||||
config.RCConfigLog("Created job with name: %v, namespace: %v, parallelism/completions: %v", job.Name, config.Namespace, job.Spec.Parallelism)
|
||||
@@ -584,8 +573,7 @@ func (config *RCConfig) create() error {
|
||||
|
||||
config.applyTo(rc.Spec.Template)
|
||||
|
||||
_, err := config.Client.CoreV1().ReplicationControllers(config.Namespace).Create(rc)
|
||||
if err != nil {
|
||||
if err := CreateRCWithRetries(config.Client, config.Namespace, rc); err != nil {
|
||||
return fmt.Errorf("Error creating replication controller: %v", err)
|
||||
}
|
||||
config.RCConfigLog("Created replication controller with name: %v, namespace: %v, replica count: %v", rc.Name, config.Namespace, removePtr(rc.Spec.Replicas))
|
||||
@@ -795,7 +783,6 @@ func (config *RCConfig) start() error {
|
||||
// List only pods from a given replication controller.
|
||||
options := metav1.ListOptions{LabelSelector: label.String()}
|
||||
if pods, err := config.Client.CoreV1().Pods(metav1.NamespaceAll).List(options); err == nil {
|
||||
|
||||
for _, pod := range pods.Items {
|
||||
config.RCConfigLog("Pod %s\t%s\t%s\t%s", pod.Name, pod.Spec.NodeName, pod.Status.Phase, pod.DeletionTimestamp)
|
||||
}
|
||||
@@ -823,8 +810,7 @@ func StartPods(c clientset.Interface, replicas int, namespace string, podNamePre
|
||||
pod.ObjectMeta.Labels["name"] = podName
|
||||
pod.ObjectMeta.Labels["startPodsID"] = startPodsID
|
||||
pod.Spec.Containers[0].Name = podName
|
||||
_, err := c.CoreV1().Pods(namespace).Create(&pod)
|
||||
if err != nil {
|
||||
if err := CreatePodWithRetries(c, namespace, &pod); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1020,14 +1006,10 @@ func MakePodSpec() v1.PodSpec {
|
||||
}
|
||||
|
||||
func makeCreatePod(client clientset.Interface, namespace string, podTemplate *v1.Pod) error {
|
||||
var err error
|
||||
for attempt := 0; attempt < retries; attempt++ {
|
||||
if _, err := client.CoreV1().Pods(namespace).Create(podTemplate); err == nil {
|
||||
return nil
|
||||
}
|
||||
glog.Errorf("Error while creating pod, maybe retry: %v", err)
|
||||
if err := CreatePodWithRetries(client, namespace, podTemplate); err != nil {
|
||||
return fmt.Errorf("Error creating pod: %v", err)
|
||||
}
|
||||
return fmt.Errorf("Terminal error while creating pod, won't retry: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreatePod(client clientset.Interface, namespace string, podCount int, podTemplate *v1.Pod) error {
|
||||
@@ -1065,14 +1047,10 @@ func createController(client clientset.Interface, controllerName, namespace stri
|
||||
},
|
||||
},
|
||||
}
|
||||
var err error
|
||||
for attempt := 0; attempt < retries; attempt++ {
|
||||
if _, err := client.CoreV1().ReplicationControllers(namespace).Create(rc); err == nil {
|
||||
return nil
|
||||
}
|
||||
glog.Errorf("Error while creating rc, maybe retry: %v", err)
|
||||
if err := CreateRCWithRetries(client, namespace, rc); err != nil {
|
||||
return fmt.Errorf("Error creating replication controller: %v", err)
|
||||
}
|
||||
return fmt.Errorf("Terminal error while creating rc, won't retry: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewCustomCreatePodStrategy(podTemplate *v1.Pod) TestPodCreateStrategy {
|
||||
@@ -1127,8 +1105,7 @@ func (config *SecretConfig) Run() error {
|
||||
secret.StringData[k] = v
|
||||
}
|
||||
|
||||
_, err := config.Client.CoreV1().Secrets(config.Namespace).Create(secret)
|
||||
if err != nil {
|
||||
if err := CreateSecretWithRetries(config.Client, config.Namespace, secret); err != nil {
|
||||
return fmt.Errorf("Error creating secret: %v", err)
|
||||
}
|
||||
config.LogFunc("Created secret %v/%v", config.Namespace, config.Name)
|
||||
@@ -1186,8 +1163,7 @@ func (config *ConfigMapConfig) Run() error {
|
||||
configMap.Data[k] = v
|
||||
}
|
||||
|
||||
_, err := config.Client.CoreV1().ConfigMaps(config.Namespace).Create(configMap)
|
||||
if err != nil {
|
||||
if err := CreateConfigMapWithRetries(config.Client, config.Namespace, configMap); err != nil {
|
||||
return fmt.Errorf("Error creating configmap: %v", err)
|
||||
}
|
||||
config.LogFunc("Created configmap %v/%v", config.Namespace, config.Name)
|
||||
@@ -1266,12 +1242,12 @@ func (config *DaemonConfig) Run() error {
|
||||
},
|
||||
}
|
||||
|
||||
_, err := config.Client.ExtensionsV1beta1().DaemonSets(config.Namespace).Create(daemon)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating DaemonSet %v: %v", config.Name, err)
|
||||
if err := CreateDaemonSetWithRetries(config.Client, config.Namespace, daemon); err != nil {
|
||||
return fmt.Errorf("Error creating daemonset: %v", err)
|
||||
}
|
||||
|
||||
var nodes *v1.NodeList
|
||||
var err error
|
||||
for i := 0; i < retries; i++ {
|
||||
// Wait for all daemons to be running
|
||||
nodes, err = config.Client.CoreV1().Nodes().List(metav1.ListOptions{ResourceVersion: "0"})
|
||||
|
||||
Reference in New Issue
Block a user