Missing Create Namespace

This commit is contained in:
mr
2026-03-18 16:43:44 +01:00
parent e4834db518
commit 6c0b07b49d
6 changed files with 134 additions and 58 deletions

View File

@@ -0,0 +1,65 @@
package infrastructure
import (
"context"
"oc-datacenter/conf"
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/tools"
)
// ---------------------------------------------------------------------------
// Kubernetes namespace helper
// ---------------------------------------------------------------------------
func CreateNamespace(ns string) error {
/*
* This function is used to create a namespace.
* It takes the following parameters:
* - ns: the namespace you want to create
*/
logger := oclib.GetLogger()
serv, err := tools.NewKubernetesService(
conf.GetConfig().KubeHost+":"+conf.GetConfig().KubePort, conf.GetConfig().KubeCA,
conf.GetConfig().KubeCert, conf.GetConfig().KubeData)
if err != nil {
return nil
}
c := context.Background()
ok, err := serv.GetNamespace(c, ns)
if ok != nil && err == nil {
logger.Debug().Msg("A namespace with name " + ns + " already exists")
return nil
}
if err != nil {
return err
}
err = serv.CreateNamespace(c, ns)
if err != nil {
return err
}
err = serv.CreateServiceAccount(c, ns)
if err != nil {
return err
}
role := "argo-role"
err = serv.CreateRole(c, ns, role,
[][]string{
{"coordination.k8s.io"},
{""},
{""}},
[][]string{
{"leases"},
{"secrets"},
{"pods"}},
[][]string{
{"get", "create", "update"},
{"get"},
{"patch"}})
if err != nil {
return err
}
return serv.CreateRoleBinding(c, ns, "argo-role-binding", role)
}