-
Notifications
You must be signed in to change notification settings - Fork 13
/
kubernetes_cluster.go
102 lines (85 loc) · 2.82 KB
/
kubernetes_cluster.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
92
93
94
95
96
97
98
99
100
101
102
package types
import (
"context"
"errors"
"fmt"
"os"
lsv1alpha1 "github.com/gardener/landscaper/apis/core/v1alpha1"
"github.com/go-logr/logr"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"sigs.k8s.io/yaml"
"github.com/gardener/landscapercli/pkg/logger"
"github.com/gardener/landscapercli/pkg/util"
)
type kubernetesClusterOpts struct {
name string
namespace string
targetKubeconfigPath string
//outputPath is the path to write the installation.yaml to
outputPath string
}
func NewKubernetesClusterCommand(ctx context.Context) *cobra.Command {
opts := &kubernetesClusterOpts{}
cmd := &cobra.Command{
Use: "kubernetes-cluster --name [name] --namespace [namespace] " +
"--target-kubeconfig [path to target kubeconfig]",
Args: cobra.NoArgs,
Aliases: []string{"k8s-cluster"},
Example: "landscaper-cli targets create kubernetes-cluster --name my-target --namespace my-namespace " +
"--target-kubeconfig kubeconfig.yaml",
Short: "create a target of type " + string(lsv1alpha1.KubernetesClusterTargetType),
Run: func(cmd *cobra.Command, args []string) {
if err := opts.Complete(args); err != nil {
cmd.PrintErr(err.Error())
os.Exit(1)
}
if err := opts.run(ctx, cmd, logger.Log); err != nil {
cmd.PrintErr(err.Error())
os.Exit(1)
}
},
}
cmd.SetOut(os.Stdout)
opts.AddFlags(cmd.Flags())
return cmd
}
func (o *kubernetesClusterOpts) Complete(args []string) error {
if o.name == "" {
return errors.New("--name must be defined")
}
if o.targetKubeconfigPath == "" {
return errors.New("--target-kubeconfig must be defined")
}
return nil
}
func (o *kubernetesClusterOpts) run(ctx context.Context, cmd *cobra.Command, log logr.Logger) error {
target, err := util.BuildKubernetesClusterTarget(o.name, o.namespace, o.targetKubeconfigPath)
if err != nil {
return fmt.Errorf("cannot build target object: %w", err)
}
marshaledYaml, err := yaml.Marshal(target)
if err != nil {
return fmt.Errorf("cannot marshal target yaml: %w", err)
}
if o.outputPath == "" {
cmd.Println(string(marshaledYaml))
} else {
f, err := os.Create(o.outputPath)
if err != nil {
return fmt.Errorf("error creating file %s: %w", o.outputPath, err)
}
_, err = f.Write(marshaledYaml)
if err != nil {
return fmt.Errorf("error writing file %s: %w", o.outputPath, err)
}
cmd.Printf("Wrote target to %s", o.outputPath)
}
return nil
}
func (o *kubernetesClusterOpts) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.name, "name", "", "name of the target")
fs.StringVar(&o.namespace, "namespace", "", "namespace of the target (optional)")
fs.StringVar(&o.targetKubeconfigPath, "target-kubeconfig", "", "path to the kubeconfig where the created target object will point to")
fs.StringVarP(&o.outputPath, "output-file", "o", "", "file path for the resulting target yaml")
}