Skip to content

Commit

Permalink
Added WaitForEntity e2e func
Browse files Browse the repository at this point in the history
  • Loading branch information
f41gh7 committed Jun 18, 2020
1 parent 1ca40f5 commit 6da6770
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 70 deletions.
14 changes: 7 additions & 7 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ type BaseOperatorConf struct {
ClusterDomain string `default:""`
KubeletObject string
}
DisabledServiceMonitorCreation bool `default:"false"`
Host string `default:"0.0.0.0"`
ListenAddress string `default:"0.0.0.0"`
DefaultLabels string `default:"managed-by=vm-operator"`
Labels Labels `ignored:"true"`
LogLevel string
LogFormat string
DisableSelfServiceMonitorCreation bool `default:"false"`
Host string `default:"0.0.0.0"`
ListenAddress string `default:"0.0.0.0"`
DefaultLabels string `default:"managed-by=vm-operator"`
Labels Labels `ignored:"true"`
LogLevel string
LogFormat string
}

func MustGetBaseConfig() *BaseOperatorConf {
Expand Down
99 changes: 41 additions & 58 deletions e2e/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package e2e

import (
"errors"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -9,91 +11,72 @@ import (
"time"
)

func WaitForSts(t *testing.T, kubeclient kubernetes.Interface, namespace, name string, replicas int,
retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
sts, err := kubeclient.AppsV1().StatefulSets(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of StatefulSet: %s in Namespace: %s \n", name, namespace)
return false, nil
}
return false, err
}
var (
NeedToWaitError = fmt.Errorf("needToWaitErr")
)

if int(sts.Status.ReadyReplicas) >= replicas {
return true, nil
}
t.Logf("Waiting for full availability of %s sts (%d/%d)\n", name,
sts.Status.ReadyReplicas, replicas)
return false, nil
})
if err != nil {
return err
}
t.Logf("StatefulSet available (%d/%d)\n", replicas, replicas)
return nil
}
type waitForFunc func() error

func WaitForService(t *testing.T, kubeclient kubernetes.Interface, namespace, name string,
retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
_, err = kubeclient.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of Service: %s in Namespace: %s \n", name, namespace)
return false, nil
}
return false, err
}

return true, nil
})
if err != nil {
waitForService := func()error{
_, err := kubeclient.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
return err
}
t.Logf("Service available (%s)\n", name)
return nil
return waitForEntity(retryInterval,timeout,waitForService)
}

func WaitForConfigMap(t *testing.T, kubeclient kubernetes.Interface, namespace, name string,
retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
_, err = kubeclient.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of ConfigMap: %s in Namespace: %s \n", name, namespace)
return false, nil
}
return false, err
}

return true, nil
})
if err != nil {
return err
waitForCm := func()error {
_, err := kubeclient.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
return err
}
t.Logf("StatefulSet available %s\n", name)
return nil
return waitForEntity(retryInterval,timeout,waitForCm)
}

func WaitForSecret(t *testing.T, kubeclient kubernetes.Interface, namespace, name string,
retryInterval, timeout time.Duration) error {

waitForSecret := func() error {

_, err := kubeclient.CoreV1().Secrets(namespace).Get(name, metav1.GetOptions{})
return err
}
t.Logf("Secret available %s\n", name)
return waitForEntity(retryInterval, timeout, waitForSecret)
}

func WaitForSts(t *testing.T, kubeclient kubernetes.Interface, namespace, name string, replicas int, retryInterval, timeout time.Duration) error {
waitForSts := func() error {
sts, err := kubeclient.AppsV1().StatefulSets(namespace).Get(name, metav1.GetOptions{})
if sts != nil {
if int(sts.Status.ReadyReplicas) < replicas {
t.Logf("Waiting for availability of Sts: %s in Namespace: %s \n", name, namespace)
return NeedToWaitError
}
}
return err
}
t.Logf("Sts available %s\n", name)
return waitForEntity(retryInterval, timeout, waitForSts)
}

func waitForEntity(retryInterval, timeout time.Duration, fn waitForFunc) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
_, err = kubeclient.CoreV1().Secrets(namespace).Get(name, metav1.GetOptions{})
err = fn()
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of Secret: %s in Namespace: %s \n", name, namespace)
if apierrors.IsNotFound(err) || errors.Is(err, NeedToWaitError) {
return false, nil
}
return false, err
}

return true, nil
})
if err != nil {
return err
}
t.Logf("Secret available %s\n", name)
return nil
}
2 changes: 1 addition & 1 deletion pkg/controller/vmagent/vmagent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (r *ReconcileVMAgent) Reconcile(request reconcile.Request) (reconcile.Resul
}

//create servicemonitor for object by default
if !r.opConf.DisabledServiceMonitorCreation {
if !r.opConf.DisableSelfServiceMonitorCreation {
_, err = metrics.CreateServiceMonitors(r.restConf, instance.Namespace, []*corev1.Service{svc})
if err != nil {
if !errors.IsAlreadyExists(err) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/vmalert/vmalert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (r *ReconcileVMAlert) Reconcile(request reconcile.Request) (reconcile.Resul
}

//create servicemonitor for object by default
if !r.opConf.DisabledServiceMonitorCreation {
if !r.opConf.DisableSelfServiceMonitorCreation {
_, err = metrics.CreateServiceMonitors(r.restConf, instance.Namespace, []*corev1.Service{svc})
if err != nil {
if !errors.IsAlreadyExists(err) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/vmsingle/vmsingle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (r *ReconcileVMSingle) Reconcile(request reconcile.Request) (reconcile.Resu
}

//create servicemonitor for object by default
if !r.opConf.DisabledServiceMonitorCreation {
if !r.opConf.DisableSelfServiceMonitorCreation {
_, err = metrics.CreateServiceMonitors(r.restConf, instance.Namespace, []*corev1.Service{svc})
if err != nil {
if !errors.IsAlreadyExists(err) {
Expand Down
4 changes: 2 additions & 2 deletions vars.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Auto Generated vars for package conf
## updated at Tue Jun 16 20:20:48 UTC 2020
## updated at Thu Jun 18 00:18:44 UTC 2020


| varible name | variable default value | variable required | variable description |
Expand Down Expand Up @@ -42,7 +42,7 @@
| VM_VMALERTMANAGER_LOGLEVEL | INFO | false | - |
| VM_VMALERTMANAGER_NAMESPACES | - | false | - |
| VM_VMALERTMANAGER_CLUSTERDOMAIN | - | false | - |
| VM_DISABLEDSERVICEMONITORCREATION | false | false | - |
| VM_DISABLESELFSERVICEMONITORCREATION | false | false | - |
| VM_HOST | 0.0.0.0 | false | - |
| VM_LISTENADDRESS | 0.0.0.0 | false | - |
| VM_DEFAULTLABELS | managed-by=vm-operator | false | - |
Expand Down

0 comments on commit 6da6770

Please sign in to comment.