Missing Create Namespace
This commit is contained in:
65
infrastructure/namespace.go
Normal file
65
infrastructure/namespace.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user