From 5cebc0fb2175d17aa85f71e3bf90ecc6dfc9aa15 Mon Sep 17 00:00:00 2001 From: Matthias Linhuber Date: Sat, 23 Sep 2023 22:31:48 +0200 Subject: [PATCH] Refactor the k8s job scheduling function to use the buildjob directly --- HadesScheduler/k8s.go | 27 ++++++++++----------------- HadesScheduler/k8s_test.go | 34 ++++++++++++++++++++++++++++++---- shared/payload/payload.go | 2 ++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/HadesScheduler/k8s.go b/HadesScheduler/k8s.go index 0f2c97a..f9740a0 100644 --- a/HadesScheduler/k8s.go +++ b/HadesScheduler/k8s.go @@ -57,16 +57,7 @@ func (k *K8sScheduler) ScheduleJob(buildJob payload.BuildJob) error { log.Infof("Scheduling job %s", buildJob.BuildConfig.ExecutionContainer) - nsName := namespace.Name - jobName := "testjob" - jobImage := buildJob.BuildConfig.ExecutionContainer - cmd := "sleep 100" - - job, err := createJob(clientset, - nsName, - &jobName, - &jobImage, - &cmd) + job, err := createJob(clientset, namespace.Name, buildJob) if err != nil { log.WithError(err).Error("error creating job") @@ -186,15 +177,17 @@ func deleteNamespace(clientset *kubernetes.Clientset, namespace string) { } } -func createJob(clientset *kubernetes.Clientset, namespace string, jobName *string, image *string, cmd *string) (*batchv1.Job, error) { - log.Infof("Creating job %s in namespace %s", *jobName, namespace) +func createJob(clientset *kubernetes.Clientset, namespace string, buildJob payload.BuildJob) (*batchv1.Job, error) { + log.Infof("Creating job %v in namespace %s", buildJob, namespace) + + buildCommand := "sleep 10" jobs := clientset.BatchV1().Jobs(namespace) var backOffLimit int32 = 0 jobSpec := &batchv1.Job{ ObjectMeta: metav1.ObjectMeta{ - Name: *jobName, + Name: buildJob.Name, Namespace: namespace, }, Spec: batchv1.JobSpec{ @@ -202,9 +195,9 @@ func createJob(clientset *kubernetes.Clientset, namespace string, jobName *strin Spec: corev1.PodSpec{ Containers: []corev1.Container{ { - Name: *jobName, - Image: *image, - Command: strings.Split(*cmd, " "), + Name: buildJob.Name, + Image: buildJob.BuildConfig.ExecutionContainer, + Command: strings.Split(buildCommand, " "), }, }, RestartPolicy: corev1.RestartPolicyNever, @@ -221,7 +214,7 @@ func createJob(clientset *kubernetes.Clientset, namespace string, jobName *strin } //print job details - log.Infof("Created K8s job %s successfully", jobName) + log.Infof("Created K8s job %s successfully", buildJob.Name) log.Debugf("Job details: %v", job) return job, nil } diff --git a/HadesScheduler/k8s_test.go b/HadesScheduler/k8s_test.go index 329c58d..e6c8d69 100644 --- a/HadesScheduler/k8s_test.go +++ b/HadesScheduler/k8s_test.go @@ -2,6 +2,8 @@ package main import ( "testing" + + "github.com/Mtze/HadesCI/shared/payload" ) func TestKubeconfigInitialization(t *testing.T) { @@ -31,10 +33,34 @@ func TestDeleteNamespace(t *testing.T) { func TestCreateJob(t *testing.T) { client := initializeKubeconfig() + testBuildJob := payload.BuildJob{ + Name: "Test Build", + Credentials: struct { + Username string `json:"username" binding:"required"` + Password string `json:"password" binding:"required"` + }{ + Username: "testuser", + Password: "testpassword", + }, + BuildConfig: struct { + Repositories []payload.Repository `json:"repositories" binding:"required,dive"` + ExecutionContainer string `json:"executionContainer" binding:"required"` + }{ + Repositories: []payload.Repository{ + { + Path: "/tmp/testrepo1", + URL: "https://github.com/testuser/testrepo1.git", + }, + { + Path: "/tmp/testrepo2", + URL: "https://github.com/testuser/testrepo2.git", + }, + }, + ExecutionContainer: "docker", + }, + } + namespace := "default" - jobName := "testJob" - jobImage := "ubuntu" - cmd := "sleep 100" - createJob(client, namespace, &jobName, &jobImage, &cmd) + createJob(client, namespace, testBuildJob) } diff --git a/shared/payload/payload.go b/shared/payload/payload.go index 06d0d39..f4dd570 100644 --- a/shared/payload/payload.go +++ b/shared/payload/payload.go @@ -2,6 +2,8 @@ package payload type BuildJob struct { // TODO: We need a build ID or something to identify the build + Name string `json:"name" binding:"required"` + Credentials struct { Username string `json:"username" binding:"required"` Password string `json:"password" binding:"required"`