Skip to content

Commit

Permalink
Refactor the k8s job scheduling function to use the buildjob directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Mtze committed Sep 23, 2023
1 parent 3c6622e commit 5cebc0f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
27 changes: 10 additions & 17 deletions HadesScheduler/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -186,25 +177,27 @@ 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{
Template: corev1.PodTemplateSpec{
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,
Expand All @@ -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
}
34 changes: 30 additions & 4 deletions HadesScheduler/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"testing"

"github.com/Mtze/HadesCI/shared/payload"
)

func TestKubeconfigInitialization(t *testing.T) {
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions shared/payload/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 5cebc0f

Please sign in to comment.