-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(ns): initial commit Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): random ns Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): random ns Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): clientset init Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): priritize the ns creation Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): add logs Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): add logs Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): keep it only in k8s.Initialize Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): add log info + delete ns after the tests is done Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update log msg Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update from mainh Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update from main Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update order Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): add && Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): remove second wait Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): remove second wait Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): remove secon2d wait Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): uniq id as ns Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): fix typo+ Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): layout Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): fix non ns Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): remove && Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): clenaup Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update docs Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update command Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update command func Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): comments and tests Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update docs Signed-off-by: Jose Ramon Mañes <[email protected]> * feat(ns): update docs Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: remove comment Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: func if duplicated check Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: check if ns exists Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: move func and update Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: update docs+ Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: update docs Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: add comments Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: add comments Signed-off-by: Jose Ramon Mañes <[email protected]> * fix: add comments Signed-off-by: Jose Ramon Mañes <[email protected]> --------- Signed-off-by: Jose Ramon Mañes <[email protected]>
- Loading branch information
Showing
4 changed files
with
158 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// InitializeNamespace sets up the namespace based on the KNUU_DEDICATED_NAMESPACE environment variable | ||
func InitializeNamespace(identifier string) (string, error) { | ||
namespaceName := "knuu-" + sanitizeName(identifier) | ||
logrus.Debugf("namespace random generated: %s", namespaceName) | ||
if err := createNamespace(Clientset(), namespaceName); err != nil { | ||
return "", fmt.Errorf("failed to create dedicated namespace: %v", err) | ||
} | ||
|
||
logrus.Debugf("full namespace name generated: %s", namespaceName) | ||
|
||
return namespaceName, nil | ||
} | ||
|
||
// createNamespace creates a new namespace if it does not exist | ||
func createNamespace(clientset *kubernetes.Clientset, name string) error { | ||
ctx := context.TODO() | ||
namespace := &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
} | ||
|
||
_, err := clientset.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{}) | ||
if err != nil { | ||
if errors.IsAlreadyExists(err) { | ||
fmt.Printf("Namespace %s already exists, continuing.\n", name) | ||
return nil | ||
} | ||
return fmt.Errorf("error creating namespace %s: %v", name, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters