Skip to content

Commit

Permalink
🌱 Automatically create namespace if its not found in hubaddon install…
Browse files Browse the repository at this point in the history
… command (open-cluster-management-io#457)

* Automatically create namespace if its not found in hubaddon install command

Signed-off-by: Rokibul Hasan <[email protected]>

* Remove newline

Signed-off-by: Rokibul Hasan <[email protected]>

* Add `create-namespace` flag

Signed-off-by: Rokibul Hasan <[email protected]>

---------

Signed-off-by: Rokibul Hasan <[email protected]>
  • Loading branch information
RokibulHasan7 authored Oct 30, 2024
1 parent fae815d commit 198563c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/install/hubaddon/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream

cmd.Flags().StringVar(&o.names, "names", "", "Names of the built-in add-on to install (comma separated). The built-in add-ons are: application-manager, governance-policy-framework")
cmd.Flags().StringVar(&o.values.Namespace, "namespace", "open-cluster-management", "Namespace of the built-in add-on to install. Defaults to open-cluster-management")
cmd.Flags().BoolVar(&o.values.CreateNamespace, "create-namespace", false, "If true, automatically create the specified namespace")
cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file")
cmd.Flags().StringVar(&o.bundleVersion, "bundle-version", "default",
"The image version tag to use when deploying the hub add-on(s) (e.g. v0.6.0). Defaults to the latest released version. You can also set \"latest\" to install the latest development version.")
Expand Down
34 changes: 33 additions & 1 deletion pkg/cmd/install/hubaddon/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
package hubaddon

import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"strings"

Expand Down Expand Up @@ -61,6 +65,12 @@ func (o *Options) run() error {

klog.V(3).InfoS("values:", "addon", o.values.HubAddons)

if o.values.CreateNamespace {
if err := o.createNamespace(); err != nil {
return err
}
}

return o.runWithClient()
}

Expand All @@ -87,7 +97,6 @@ func (o *Options) runWithClient() error {
}

fmt.Fprintf(o.Streams.Out, "Installing built-in %s add-on to the Hub cluster...\n", addon)

}

if len(o.outputFile) > 0 {
Expand All @@ -106,3 +115,26 @@ func (o *Options) runWithClient() error {

return nil
}

func (o *Options) createNamespace() error {
clientSet, err := o.ClusteradmFlags.KubectlFactory.KubernetesClientSet()
if err != nil {
return fmt.Errorf("failed to create kubernetes clientSet")
}

ns, err := clientSet.CoreV1().Namespaces().Get(context.Background(), o.values.Namespace, metav1.GetOptions{})
if err != nil && errors.IsNotFound(err) {
ns, err = clientSet.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: o.values.Namespace,
},
}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create namespace %s: %w", ns, err)
}
} else if err != nil {
return fmt.Errorf("failed to get namespace %s: %w", ns, err)
}

return nil
}
3 changes: 2 additions & 1 deletion pkg/cmd/install/hubaddon/scenario/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Values struct {
// Namespace to install
Namespace string
// Version to install
BundleVersion version.VersionBundle
BundleVersion version.VersionBundle
CreateNamespace bool
}

var (
Expand Down

0 comments on commit 198563c

Please sign in to comment.