Skip to content

Commit

Permalink
feat(cli): namespace creation on install
Browse files Browse the repository at this point in the history
Signed-off-by: christoph <[email protected]>
  • Loading branch information
christophenne committed Sep 16, 2024
1 parent d6bb683 commit 6c6a27c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
13 changes: 9 additions & 4 deletions cmd/glasskube/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/api/core/v1"

"github.com/glasskube/glasskube/internal/clientutils"
"github.com/glasskube/glasskube/internal/namespaces"
Expand Down Expand Up @@ -225,7 +225,7 @@ var installCmd = &cobra.Command{

createNamespace := false
if installCmdOptions.NamespaceOptions.Namespace != "" {
if ok, err := namespaces.IsNamespaceInstalled(ctx, cs, installCmdOptions.NamespaceOptions.Namespace); !ok {
if ok, err := namespaces.Exists(ctx, cs, installCmdOptions.NamespaceOptions.Namespace); !ok {
fmt.Fprintf(os.Stderr, " * Namespace %v does not exist and will be created\n",
installCmdOptions.NamespaceOptions.Namespace)
createNamespace = true
Expand All @@ -248,8 +248,13 @@ var installCmd = &cobra.Command{
}

if createNamespace {
err := namespaces.InstallNamespace(ctx, cs, installCmdOptions.NamespaceOptions.Namespace)
if err != nil && !apierrors.IsAlreadyExists(err) {
ns := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: installCmdOptions.NamespaceOptions.Namespace,
},
}
_, err := cs.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
if err != nil {
fmt.Fprintf(os.Stderr, "An error occurred in creating the Namespace:\n\n%v\n", err)
cliutils.ExitWithError()
}
Expand Down
18 changes: 1 addition & 17 deletions internal/namespaces/ultils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package namespaces
import (
"context"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

func IsNamespaceInstalled(ctx context.Context, cs *kubernetes.Clientset, namespace string) (bool, error) {
func Exists(ctx context.Context, cs *kubernetes.Clientset, namespace string) (bool, error) {
_, err := cs.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
Expand All @@ -21,18 +20,3 @@ func IsNamespaceInstalled(ctx context.Context, cs *kubernetes.Clientset, namespa

return true, nil
}

func InstallNamespace(ctx context.Context, cs *kubernetes.Clientset, namespace string) error {
ns := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}

_, err := cs.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
if err != nil {
return err
}

return nil
}

0 comments on commit 6c6a27c

Please sign in to comment.