Skip to content

Commit 198563c

Browse files
🌱 Automatically create namespace if its not found in hubaddon install command (#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]>
1 parent fae815d commit 198563c

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

‎pkg/cmd/install/hubaddon/cmd.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
4949

5050
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")
5151
cmd.Flags().StringVar(&o.values.Namespace, "namespace", "open-cluster-management", "Namespace of the built-in add-on to install. Defaults to open-cluster-management")
52+
cmd.Flags().BoolVar(&o.values.CreateNamespace, "create-namespace", false, "If true, automatically create the specified namespace")
5253
cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file")
5354
cmd.Flags().StringVar(&o.bundleVersion, "bundle-version", "default",
5455
"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.")

‎pkg/cmd/install/hubaddon/exec.go‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
package hubaddon
33

44
import (
5+
"context"
56
"fmt"
7+
corev1 "k8s.io/api/core/v1"
8+
"k8s.io/apimachinery/pkg/api/errors"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
610
"os"
711
"strings"
812

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

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

68+
if o.values.CreateNamespace {
69+
if err := o.createNamespace(); err != nil {
70+
return err
71+
}
72+
}
73+
6474
return o.runWithClient()
6575
}
6676

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

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

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

107116
return nil
108117
}
118+
119+
func (o *Options) createNamespace() error {
120+
clientSet, err := o.ClusteradmFlags.KubectlFactory.KubernetesClientSet()
121+
if err != nil {
122+
return fmt.Errorf("failed to create kubernetes clientSet")
123+
}
124+
125+
ns, err := clientSet.CoreV1().Namespaces().Get(context.Background(), o.values.Namespace, metav1.GetOptions{})
126+
if err != nil && errors.IsNotFound(err) {
127+
ns, err = clientSet.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
128+
ObjectMeta: metav1.ObjectMeta{
129+
Name: o.values.Namespace,
130+
},
131+
}, metav1.CreateOptions{})
132+
if err != nil {
133+
return fmt.Errorf("failed to create namespace %s: %w", ns, err)
134+
}
135+
} else if err != nil {
136+
return fmt.Errorf("failed to get namespace %s: %w", ns, err)
137+
}
138+
139+
return nil
140+
}

‎pkg/cmd/install/hubaddon/scenario/resources.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type Values struct {
2727
// Namespace to install
2828
Namespace string
2929
// Version to install
30-
BundleVersion version.VersionBundle
30+
BundleVersion version.VersionBundle
31+
CreateNamespace bool
3132
}
3233

3334
var (

0 commit comments

Comments
 (0)