/
cmd.go
91 lines (79 loc) · 3.52 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Contributors to the Open Cluster Management project
package cluster
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/stolostron/cm-cli/pkg/cmd/attach/cluster/scenario"
genericclioptionscm "github.com/stolostron/cm-cli/pkg/genericclioptions"
"github.com/stolostron/cm-cli/pkg/helpers"
clusteradmhelpers "open-cluster-management.io/clusteradm/pkg/helpers"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
var example = `
# Attach a cluster
%[1]s attach cluster --values values.yaml
# Attach a cluster with overwritting the cluster name
%[1]s attach cluster --cluster mycluster --values values.yaml
# Attach a cluster with overwritting the cluster name as arg
%[1]s attach cluster mycluster --values values.yaml
`
const (
scenarioDirectory = "attach"
)
var valuesTemplatePath = filepath.Join(scenarioDirectory, "values-template.yaml")
var valuesDefaultPath = filepath.Join(scenarioDirectory, "values-default.yaml")
// NewCmd provides a cobra command wrapping NewCmdImportCluster
func NewCmd(cmFlags *genericclioptionscm.CMFlags, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(cmFlags, streams)
cmd := &cobra.Command{
Use: "cluster",
Short: "Import a cluster",
Example: fmt.Sprintf(example, helpers.GetExampleHeader()),
SilenceUsage: true,
PreRunE: func(c *cobra.Command, args []string) error {
clusteradmhelpers.DryRunMessage(cmFlags.DryRun)
isSupported, err := helpers.IsSupported(o.CMFlags)
if err != nil {
return err
}
if !isSupported {
return fmt.Errorf("this command '%s %s' is only available on %s or %s",
helpers.GetExampleHeader(),
strings.Join(os.Args[1:], " "),
helpers.RHACM,
helpers.MCE)
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
if err := o.complete(c, args); err != nil {
return err
}
if err := o.validate(); err != nil {
return err
}
if err := o.run(); err != nil {
return err
}
return nil
},
}
cmd.SetUsageTemplate(clusteradmhelpers.UsageTempate(cmd, scenario.GetScenarioResourcesReader(), valuesTemplatePath))
cmd.Flags().StringVar(&o.valuesPath, "values", "", "The files containing the values")
cmd.Flags().StringVar(&o.clusterName, "cluster", "", "Name of the cluster")
cmd.Flags().StringVar(&o.clusterServer, "cluster-server", "", "cluster server url of the cluster to import")
cmd.Flags().StringVar(&o.clusterToken, "cluster-token", "", "token to access the cluster to import")
cmd.Flags().StringVar(&o.clusterKubeConfig, "cluster-kubeconfig", "", "path to the kubeconfig the cluster to import")
cmd.Flags().StringVar(&o.clusterKubeContext, "cluster-kubecontext", "", "context to use in the kubeconfig to import the cluster")
cmd.Flags().StringVar(&o.clusterKubeConfigContent, "cluster-kubeconfig-content", "", "content of the kubeconfig the cluster to import")
cmd.Flags().StringVar(&o.importFile, "import-file", "", "the file path and prefix which will contain the import yaml files for manual import")
cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file")
cmd.Flags().BoolVar(&o.waitAgent, "wait", false, "Wait until the klusterlet agent is installed")
//Not implemented as it requires to import all addon packages
// cmd.Flags().BoolVar(&o.waitAddOns, "wait-addons", false, "Wait until the klusterlet agent and the addons are is installed")
cmd.Flags().IntVar(&o.timeout, "timeout", 180, "Timeout to get the klusterlet agent or addons ready in seconds")
return cmd
}