Skip to content

Commit f0bb9b9

Browse files
authored
clm: add --create-namespace to apply (#184)
1 parent 16b02da commit f0bb9b9

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

clm/cmd/apply.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import (
1212

1313
"github.com/spf13/cobra"
1414

15+
corev1 "k8s.io/api/core/v1"
1516
apierrors "k8s.io/apimachinery/pkg/api/errors"
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
apitypes "k8s.io/apimachinery/pkg/types"
1619
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1720

1821
"github.com/sap/component-operator-runtime/clm/internal/backoff"
@@ -26,8 +29,9 @@ import (
2629
const applyUsage = `Apply component manifests to Kubernetes cluster`
2730

2831
type applyOptions struct {
29-
valuesSources []string
30-
timeout time.Duration
32+
valuesSources []string
33+
createNamespace bool
34+
timeout time.Duration
3135
}
3236

3337
func newApplyCmd() *cobra.Command {
@@ -60,6 +64,14 @@ func newApplyCmd() *cobra.Command {
6064

6165
ownerId := fullName + "/" + namespace + "/" + name
6266

67+
if err := clnt.Get(context.TODO(), apitypes.NamespacedName{Name: namespace}, &corev1.Namespace{}); apierrors.IsNotFound(err) && options.createNamespace {
68+
if err := clnt.Create(context.TODO(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}); err != nil {
69+
return err
70+
}
71+
} else if err != nil {
72+
return err
73+
}
74+
6375
release, err := releaseClient.Get(context.TODO(), namespace, name)
6476
if err != nil {
6577
if apierrors.IsNotFound(err) {
@@ -145,6 +157,7 @@ func newApplyCmd() *cobra.Command {
145157

146158
flags := cmd.Flags()
147159
flags.StringArrayVarP(&options.valuesSources, "values", "f", nil, "Path to values file in yaml format (can be repeated, values will be merged in order of appearance)")
160+
flags.BoolVar(&options.createNamespace, "create-namespace", false, "Create release namespace if not existing")
148161
flags.DurationVar(&options.timeout, "timeout", 0, "Time to wait for the operation to complete (default is to wait forever)")
149162

150163
return cmd

0 commit comments

Comments
 (0)