Skip to content

Commit

Permalink
Improve namespace creation - namespace is now created at startup)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mtze committed Sep 23, 2023
1 parent 8db721a commit 161938d
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions HadesScheduler/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,28 @@ type JobScheduler interface {

type K8sScheduler struct{}

var clientset *kubernetes.Clientset
var namespace *corev1.Namespace

func init() {
var err error

clientset = initializeKubeconfig()
namespace, err = createNamespace(clientset, "hadestesting")

if err != nil {

// TODO: Check if the error is that the namespace already exists - if yes just use it

log.WithError(err).Error("Failed to create hades namespace - Terminating")
os.Exit(1)
}
}

func (k *K8sScheduler) ScheduleJob(buildJob payload.BuildJob) error {

log.Infof("Scheduling job %s", buildJob.BuildConfig.ExecutionContainer)

clientset := initializeKubeconfig()
namespace := createNamespace(clientset, "hadestesting")

nsName := namespace.Name
jobName := "testjob"
jobImage := buildJob.BuildConfig.ExecutionContainer
Expand Down Expand Up @@ -94,23 +109,26 @@ func getPods(clientset *kubernetes.Clientset, namespace string) *corev1.PodList
return pods
}

func createNamespace(clientset *kubernetes.Clientset, namespace string) *corev1.Namespace {
func createNamespace(clientset *kubernetes.Clientset, namespace string) (*corev1.Namespace, error) {
log.Infof("Creating namespace %s", namespace)

ns, err := clientset.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: namespace,
},
}, v1.CreateOptions{})
ns, err := clientset.CoreV1().Namespaces().Create(
context.Background(),
&corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: namespace,
},
}, v1.CreateOptions{})

if err != nil {
log.WithError(err).Error("error creating namespace")
return nil, err
}

// sleep for 5 seconds to give the namespace time to be created
time.Sleep(5 * time.Second)

return ns
return ns, nil
}

func getNamespaces(clientset *kubernetes.Clientset) *corev1.NamespaceList {
Expand Down

0 comments on commit 161938d

Please sign in to comment.