Skip to content

Commit

Permalink
Add a globalflag --create-namespace for vineyardctl to create the nam…
Browse files Browse the repository at this point in the history
…espace if needed. (#1243)

Signed-off-by: Ye Cao <caoye.cao@alibaba-inc.com>
  • Loading branch information
dashanji committed Mar 10, 2023
1 parent 73b4cb4 commit d826908
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions k8s/cmd/commands/create/create_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ var createBackupCmd = &cobra.Command{
}
client := util.KubernetesClient()

util.CreateNamespaceIfNotExist(client)

backup, err := buildBackupJob()
if err != nil {
log.Fatal(err, "failed to build backup job")
Expand Down
1 change: 1 addition & 0 deletions k8s/cmd/commands/create/create_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var createOperationCmd = &cobra.Command{
util.AssertNoArgs(cmd, args)

client := util.KubernetesClient()
util.CreateNamespaceIfNotExist(client)
operation := buildOperation()

if err := util.Create(client, operation, func(operation *v1alpha1.Operation) bool {
Expand Down
1 change: 1 addition & 0 deletions k8s/cmd/commands/create/create_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var createRecoverCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
util.AssertNoArgs(cmd, args)
client := util.KubernetesClient()
util.CreateNamespaceIfNotExist(client)

recover, err := buildRecoverJob()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions k8s/cmd/commands/deploy/deploy_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var deployOperatorCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
util.AssertNoArgs(cmd, args)
client := util.KubernetesClient()
util.CreateNamespaceIfNotExist(client)

operatorManifests, err := util.BuildKustomizeInDir(util.GetKustomizeDir())
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions k8s/cmd/commands/deploy/deploy_vineyard_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var deployVineyardDeploymentCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
util.AssertNoArgs(cmd, args)
client := util.KubernetesClient()
util.CreateNamespaceIfNotExist(client)

if err := applyVineyarddFromTemplate(client); err != nil {
log.Fatal(err, "failed to apply vineyardd resources from template")
Expand Down
2 changes: 1 addition & 1 deletion k8s/cmd/commands/deploy/deploy_vineyardd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ var deployVineyarddCmd = &cobra.Command{
Example: deployVineyarddExample,
Run: func(cmd *cobra.Command, args []string) {
util.AssertNoArgsOrInput(cmd, args)

// Check if the input is coming from stdin
str, err := util.ReadJsonFromStdin(args)
if err != nil {
Expand All @@ -128,6 +127,7 @@ var deployVineyarddCmd = &cobra.Command{
}

client := util.KubernetesClient()
util.CreateNamespaceIfNotExist(client)

vineyardd, err := BuildVineyard()
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion k8s/cmd/commands/flags/global_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ var (
// Wait indicates whether to wait for the kubernetes resource to be ready
Wait bool

// CreateNamespace indicates whether to create the namespace if it does not exist
CreateNamespace bool

// DeleteNamespace indicates whether to delete the namespace
DeleteNamespace bool

// DumpUsage
DumpUsage bool
GenDoc bool

GenDoc bool
)

// defaultKubeConfig return the default kubeconfig path
Expand Down Expand Up @@ -66,6 +73,9 @@ func ApplyGlobalFlags(cmd *cobra.Command) {
cmd.PersistentFlags().
BoolVarP(&Wait, "wait", "", true,
"wait for the kubernetes resource to be ready, default true")
cmd.PersistentFlags().
BoolVarP(&CreateNamespace, "create-namespace", "", false,
"create the namespace if it does not exist, default false")
cmd.Flags().BoolVarP(&DumpUsage, "dump-usage", "j", false, "Dump usage in JSON")
cmd.Flags().
BoolVarP(&GenDoc, "gen-doc", "g", false, "Generate reference docs, e.g., './references.md'")
Expand Down
15 changes: 15 additions & 0 deletions k8s/cmd/commands/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

corev1 "k8s.io/api/core/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -159,3 +161,16 @@ func DeleteWithContext(
func Wait(condition wait.ConditionFunc) error {
return wait.PollImmediate(defaultWaitInternal, defaultWaitTimeout, condition)
}

// CreateNamespaceIfNotExist creates namespace if it does not exist
func CreateNamespaceIfNotExist(c client.Client) {
if !flags.CreateNamespace {
return
}
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: flags.GetDefaultVineyardNamespace(),
},
}
_ = CreateIfNotExists(c, &namespace)
}
25 changes: 25 additions & 0 deletions k8s/cmd/references.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ drivers.
### Options

```
--create-namespace create the namespace if it does not exist, default false
-j, --dump-usage Dump usage in JSON
-g, --gen-doc Generate reference docs, e.g., './references.md'
-h, --help help for vineyardctl
Expand Down Expand Up @@ -65,6 +66,7 @@ Create a vineyard job on kubernetes.
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -110,6 +112,7 @@ Delete the vineyard components on kubernetes.
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -160,6 +163,7 @@ Deploy a vineyard component on kubernetes.
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -227,6 +231,7 @@ vineyardctl inject [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -280,6 +285,7 @@ vineyardctl manager [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -317,6 +323,7 @@ schedule a workload or a workerflow to a vineyard cluster.
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -429,6 +436,7 @@ vineyardctl create backup [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -493,6 +501,7 @@ vineyardctl create operation [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -535,6 +544,7 @@ vineyardctl create recover [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -573,6 +583,7 @@ vineyardctl delete backup [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -616,6 +627,7 @@ vineyardctl delete cert-manager [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -654,6 +666,7 @@ vineyardctl delete operation [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -700,6 +713,7 @@ vineyardctl delete operator [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -737,6 +751,7 @@ vineyardctl delete recover [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -775,6 +790,7 @@ vineyardctl delete vineyard-cluster [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -818,6 +834,7 @@ vineyardctl delete vineyard-deployment [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -859,6 +876,7 @@ vineyardctl delete vineyardd [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -911,6 +929,7 @@ vineyardctl deploy cert-manager [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -970,6 +989,7 @@ vineyardctl deploy operator [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -1008,6 +1028,7 @@ vineyardctl deploy vineyard-cluster [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -1086,6 +1107,7 @@ vineyardctl deploy vineyard-deployment [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -1229,6 +1251,7 @@ vineyardctl deploy vineyardd [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -1269,6 +1292,7 @@ vineyardctl schedule workflow [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down Expand Up @@ -1380,6 +1404,7 @@ vineyardctl schedule workload [flags]
### Options inherited from parent commands

```
--create-namespace create the namespace if it does not exist, default false
--kubeconfig string kubeconfig path for the kubernetes cluster (default "$HOME/.kube/config")
-n, --namespace string the namespace for operation (default "vineyard-system")
--wait wait for the kubernetes resource to be ready, default true (default true)
Expand Down

0 comments on commit d826908

Please sign in to comment.