From 161938df474eab9b3f87f2e22b8b0e797cee14ea Mon Sep 17 00:00:00 2001 From: Matthias Linhuber Date: Sat, 23 Sep 2023 17:51:11 +0200 Subject: [PATCH] Improve namespace creation - namespace is now created at startup) --- HadesScheduler/k8s.go | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/HadesScheduler/k8s.go b/HadesScheduler/k8s.go index d04d37c..01a1be5 100644 --- a/HadesScheduler/k8s.go +++ b/HadesScheduler/k8s.go @@ -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 @@ -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 {