Skip to content

Commit

Permalink
Add check function for DaemonSet
Browse files Browse the repository at this point in the history
Signed-off-by: Loic Devulder <[email protected]>
  • Loading branch information
ldevulder committed Sep 27, 2024
1 parent 76d56ed commit ff34f3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
21 changes: 20 additions & 1 deletion kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,33 @@ 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)
}

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"`
Expand Down
17 changes: 17 additions & 0 deletions rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff34f3f

Please sign in to comment.