Skip to content

Commit

Permalink
Add job-run-id to ds (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
corest committed Dec 26, 2023
1 parent 2a5516e commit e65a0fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions pkg/compatibility/compatibility.go
Expand Up @@ -2,6 +2,7 @@ package compatibility

import (
"context"
"fmt"
"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -34,19 +35,20 @@ func (c *Compatibility) Run() error {
dsName := "kubeshark-kernel-version"
namespace := "default"
timeout := 2 * time.Minute
jobRunId := fmt.Sprintf("job-run-%d", time.Now().Unix())

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

c.logger.Debugf("creating %s/%s daemonset", namespace, dsName)
_, err = c.createUnameDaemonSet(dsName, namespace)
_, err = c.createUnameDaemonSet(dsName, namespace, jobRunId)
if err != nil {
return err
}
c.logger.Debugf("daemonset %s/%s created", namespace, dsName)

c.logger.Debugf("waiting for pods in %s/%s daemonset to start", namespace, dsName)
pods, err := c.waitForDaemonSetPodsRunning(ctx, dsName, namespace)
pods, err := c.waitForDaemonSetPodsRunning(ctx, dsName, namespace, jobRunId)
if err != nil {
return err
}
Expand Down
16 changes: 11 additions & 5 deletions pkg/compatibility/daemonset.go
Expand Up @@ -14,22 +14,27 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
)

func (c *Compatibility) createUnameDaemonSet(dsName, namespace string) (*appsv1.DaemonSet, error) {
func (c *Compatibility) createUnameDaemonSet(dsName, namespace, jobRunId string) (*appsv1.DaemonSet, error) {
ds := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: dsName,
Namespace: namespace,
Labels: map[string]string{
"job-run-id": jobRunId,
},
},
Spec: appsv1.DaemonSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": dsName,
"app": dsName,
"job-run-id": jobRunId,
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": dsName,
"app": dsName,
"job-run-id": jobRunId,
},
},
Spec: corev1.PodSpec{
Expand All @@ -53,14 +58,15 @@ func (c *Compatibility) createUnameDaemonSet(dsName, namespace string) (*appsv1.
return ds, nil
}

func (c *Compatibility) waitForDaemonSetPodsRunning(ctx context.Context, dsName, namespace string) ([]corev1.Pod, error) {
func (c *Compatibility) waitForDaemonSetPodsRunning(ctx context.Context, dsName, namespace, jobRunId string) ([]corev1.Pod, error) {
var dsPods []corev1.Pod
timeout := 5 * time.Second
immediate := true
labelSelector := fmt.Sprintf("app=%s,job-run-id=%s", dsName, jobRunId)

err := wait.PollUntilContextCancel(ctx, timeout, immediate, func(ctx context.Context) (bool, error) {
pods, err := c.clientset.CoreV1().Pods("").List(ctx, metav1.ListOptions{
LabelSelector: "app=" + dsName,
LabelSelector: labelSelector,
})
if err != nil {
return false, err
Expand Down

0 comments on commit e65a0fc

Please sign in to comment.