diff --git a/kubectl/kubectl.go b/kubectl/kubectl.go index 8750070..5d73352 100644 --- a/kubectl/kubectl.go +++ b/kubectl/kubectl.go @@ -657,7 +657,6 @@ func (k *Kubectl) ApplyJSON(namespace string, name string, v interface{}) error // Delete calls kubectl with the given arguments func (k *Kubectl) Delete(args ...string) error { - if _, err := runBinary(kubeCtlCmd, append([]string{"delete"}, args...)...); err != nil { return errors.Wrapf(err, "Deleting resource: %s", args) } @@ -665,6 +664,26 @@ func (k *Kubectl) Delete(args ...string) error { return nil } +// WaitForDaemonSet blocks until daemonset matching the label is available in the specified namespace. It fails after the timeout. +func (k *Kubectl) WaitForDaemonSet(namespace string, label string) error { + return wait.PollImmediate(k.PollInterval, k.PollTimeout, func() (bool, error) { + return k.DaemonSetReady(namespace, label) + }) +} + +// DaemonSetReady returns true if daemontset by that label is present in the given namespace +func (k *Kubectl) DaemonSetReady(namespace string, label string) (bool, error) { + out, err := runBinary(kubeCtlCmd, "rollout", "status", "daemonset", "--namespace", namespace, "--selector", label) + if err == nil { + if !strings.Contains(string(out), "successfully rolled out") { + return false, err + } + return true, nil + } + + return false, errors.Wrapf(err, "Kubectl rollout failed for label %s. %s", label, string(out)) +} + // ConfigMap defines a kube ConfigMap type ConfigMap struct { APIVersion string `json:"apiVersion" yaml:"apiVersion"` diff --git a/rancher/rancher.go b/rancher/rancher.go index bc48bac..b883de5 100644 --- a/rancher/rancher.go +++ b/rancher/rancher.go @@ -180,6 +180,23 @@ func SetRole(ns, name, pool, role string, value bool) error { return c.setCluster(ns) } +/** + * Check daemonset status + * @remarks This function checks the status of a daemonset based on checkList + * @param k Already defined Kubectl context + * @param checkList Array of paired namespaces/labels to check + * @example CheckDaemonSet(k, [][]string{{"kube-system", "k8s-app=canal"}}) + * @returns Nothing or an error + */ +func CheckDaemonSet(k *kubectl.Kubectl, checkList [][]string) error { + for _, check := range checkList { + if err := k.WaitForDaemonSet(check[0], check[1]); err != nil { + return err + } + } + return nil +} + /** * Check pod status * @remarks This function checks the status of a pod based on checkList