build source
This commit is contained in:
commit
ee1fec43ed
4171 changed files with 1351288 additions and 0 deletions
49
internal/k8s/namespace.go
Normal file
49
internal/k8s/namespace.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package k8s
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"github.com/iliaivanov/spec-kit-remote/cmd/dev-pod-api/internal/model"
|
||||
)
|
||||
|
||||
// EnsureNamespace creates a namespace for the user if it doesn't already exist.
|
||||
// Returns nil if the namespace already exists (idempotent).
|
||||
func (c *Client) EnsureNamespace(ctx context.Context, user string) error {
|
||||
ns := &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: model.NamespaceName(user),
|
||||
Labels: map[string]string{
|
||||
"app": "dev-pod",
|
||||
"managed-by": "dev-pod-api",
|
||||
"user": user,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err := c.Clientset.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
if errors.IsAlreadyExists(err) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("create namespace %s: %w", ns.Name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteNamespace deletes the user's namespace and all resources in it.
|
||||
func (c *Client) DeleteNamespace(ctx context.Context, user string) error {
|
||||
nsName := model.NamespaceName(user)
|
||||
err := c.Clientset.CoreV1().Namespaces().Delete(ctx, nsName, metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("delete namespace %s: %w", nsName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue