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 Aug 23, 2024
1 parent 1ff7e04 commit 50da785
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/glasskube/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/glasskube/glasskube/pkg/install"
"github.com/glasskube/glasskube/pkg/statuswriter"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -51,6 +52,7 @@ 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)
Expand Down Expand Up @@ -211,6 +213,19 @@ var installCmd = &cobra.Command{
fmt.Fprintln(os.Stderr, " * Automatic updates will be", bold("not enabled"))
}

if installCmdOptions.NamespaceOptions.Namespace != "" {
_, err := install.IsNamespaceInstalled(
ctx,
cfg,
installCmdOptions.NamespaceOptions.Namespace,
)
if errors.IsNotFound(err) {
fmt.Fprintln(os.Stderr, " * Namespace %v does not exist and will be created",

Check failure on line 223 in cmd/glasskube/cmd/install.go

View workflow job for this annotation

GitHub Actions / build

printf: fmt.Fprintln call has possible Printf formatting directive %v (govet)
installCmdOptions.NamespaceOptions.Namespace,
)
}
}

if len(pkg.GetSpec().Values) > 0 {
fmt.Fprintln(os.Stderr, bold("Configuration:"))
printValueConfigurations(os.Stderr, pkg.GetSpec().Values)
Expand All @@ -223,6 +238,12 @@ var installCmd = &cobra.Command{
cancel()
}

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

Check failure on line 243 in cmd/glasskube/cmd/install.go

View workflow job for this annotation

GitHub Actions / build

`occured` is a misspelling of `occurred` (misspell)
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
37 changes: 37 additions & 0 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ 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 @@ -114,3 +117,37 @@ 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 50da785

Please sign in to comment.