From 198563c1e6f4d6b7fb7524f4ba8e34bc071e91cf Mon Sep 17 00:00:00 2001 From: Rokibul Hasan Date: Wed, 30 Oct 2024 20:28:17 +0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20=20Automatically=20create=20name?= =?UTF-8?q?space=20if=20its=20not=20found=20in=20hubaddon=20install=20comm?= =?UTF-8?q?and=20(#457)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Automatically create namespace if its not found in hubaddon install command Signed-off-by: Rokibul Hasan * Remove newline Signed-off-by: Rokibul Hasan * Add `create-namespace` flag Signed-off-by: Rokibul Hasan --------- Signed-off-by: Rokibul Hasan --- pkg/cmd/install/hubaddon/cmd.go | 1 + pkg/cmd/install/hubaddon/exec.go | 34 ++++++++++++++++++- .../install/hubaddon/scenario/resources.go | 3 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/install/hubaddon/cmd.go b/pkg/cmd/install/hubaddon/cmd.go index 254790fdb..d82ca9f3c 100644 --- a/pkg/cmd/install/hubaddon/cmd.go +++ b/pkg/cmd/install/hubaddon/cmd.go @@ -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.") diff --git a/pkg/cmd/install/hubaddon/exec.go b/pkg/cmd/install/hubaddon/exec.go index c4f643a74..ee667de59 100644 --- a/pkg/cmd/install/hubaddon/exec.go +++ b/pkg/cmd/install/hubaddon/exec.go @@ -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" @@ -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() } @@ -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 { @@ -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 +} diff --git a/pkg/cmd/install/hubaddon/scenario/resources.go b/pkg/cmd/install/hubaddon/scenario/resources.go index 79f9e2fd2..510162948 100644 --- a/pkg/cmd/install/hubaddon/scenario/resources.go +++ b/pkg/cmd/install/hubaddon/scenario/resources.go @@ -27,7 +27,8 @@ type Values struct { // Namespace to install Namespace string // Version to install - BundleVersion version.VersionBundle + BundleVersion version.VersionBundle + CreateNamespace bool } var (