Skip to content

Commit

Permalink
feat(cli): create namespace if necessary when installing a namespace-…
Browse files Browse the repository at this point in the history
…scoped package

Signed-off-by: hanshal101 <[email protected]>
  • Loading branch information
hanshal101 committed Sep 8, 2024
1 parent c570160 commit 700e081
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 47 deletions.
27 changes: 17 additions & 10 deletions cmd/glasskube/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/glasskube/glasskube/internal/clientutils"
"github.com/glasskube/glasskube/internal/namespaces"

"github.com/fatih/color"
"github.com/glasskube/glasskube/api/v1alpha1"
Expand Down Expand Up @@ -52,13 +53,13 @@ var installCmd = &cobra.Command{
ValidArgsFunction: completeAvailablePackageNames,
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
cfg := clicontext.ConfigFromContext(ctx)
config := clicontext.RawConfigFromContext(ctx)
pkgClient := clicontext.PackageClientFromContext(ctx)
dm := cliutils.DependencyManager(ctx)
valueResolver := cliutils.ValueResolver(ctx)
repoClientset := cliutils.RepositoryClientset(ctx)
installer := install.NewInstaller(pkgClient)
cs := clicontext.KubernetesClientFromContext(ctx)

opts := metav1.CreateOptions{}
if installCmdOptions.DryRun {
Expand Down Expand Up @@ -221,15 +222,27 @@ var installCmd = &cobra.Command{
}

if installCmdOptions.NamespaceOptions.Namespace != "" {
_, err := install.IsNamespaceInstalled(
exists, err := namespaces.IsNamespaceInstalled(
ctx,
cfg,
cs,
installCmdOptions.NamespaceOptions.Namespace,
)
if errors.IsNotFound(err) {
if errors.IsNotFound(err) && !exists {
fmt.Printf(" * Namespace %v does not exist and will be created",
installCmdOptions.NamespaceOptions.Namespace,
)
err := namespaces.InstallNamespace(
ctx,
cs,
installCmdOptions.NamespaceOptions.Namespace,
)
if err != nil {
fmt.Fprintf(os.Stderr, "An error occurred in creating the Namespace:\n\n%v\n", err)
cliutils.ExitWithError()
}
} else if err != nil {
fmt.Fprintf(os.Stderr, "An error occurred in the Namespace check:\n\n%v\n", err)
cliutils.ExitWithError()
}
}

Expand All @@ -245,12 +258,6 @@ var installCmd = &cobra.Command{
cancel()
}

err := install.InstallNamespace(ctx, cfg, installCmdOptions.NamespaceOptions.Namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "An error occurred in creating the Namespace:\n\n%v\n", err)
cliutils.ExitWithError()
}

if installCmdOptions.NoWait {
if err := installer.Install(ctx, pkg, opts); err != nil {
fmt.Fprintf(os.Stderr, "An error occurred during installation:\n\n%v\n", err)
Expand Down
38 changes: 38 additions & 0 deletions internal/namespaces/ultils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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) {
_, err := cs.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return false, nil
} else {
return false, err
}
}

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
}
37 changes: 0 additions & 37 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import (
"github.com/glasskube/glasskube/pkg/client"
"github.com/glasskube/glasskube/pkg/condition"
"github.com/glasskube/glasskube/pkg/statuswriter"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

type installer struct {
Expand Down Expand Up @@ -117,37 +114,3 @@ func (i *installer) watch(ctx context.Context, pkg ctrlpkg.Package) (watch.Inter
return nil, fmt.Errorf("unexpected package type: %T", pkg)
}
}

func IsNamespaceInstalled(ctx context.Context, cfg *rest.Config, namespace string) (bool, error) {
cs, err := kubernetes.NewForConfig(cfg)
if err != nil {
return false, err
}

_, err = cs.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
return false, err
}

return true, nil
}

func InstallNamespace(ctx context.Context, cfg *rest.Config, namespace string) error {
cs, err := kubernetes.NewForConfig(cfg)
if err != nil {
return err
}

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 700e081

Please sign in to comment.