-
Notifications
You must be signed in to change notification settings - Fork 72
/
clean.go
90 lines (77 loc) · 2.79 KB
/
clean.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
package clean
import (
"context"
"github.com/redhat-developer/app-services-cli/pkg/cluster"
"github.com/redhat-developer/app-services-cli/pkg/cluster/kubeclient"
"github.com/redhat-developer/app-services-cli/pkg/cluster/v1alpha"
"github.com/redhat-developer/app-services-cli/pkg/connection"
"github.com/redhat-developer/app-services-cli/pkg/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/localize"
"github.com/redhat-developer/app-services-cli/internal/config"
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory"
"github.com/redhat-developer/app-services-cli/pkg/logging"
"github.com/spf13/cobra"
// Get all auth schemes
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
type options struct {
Config config.IConfig
Connection factory.ConnectionFunc
Logger logging.Logger
IO *iostreams.IOStreams
localizer localize.Localizer
Context context.Context
kubeconfig string
namespace string
forceCreationWithoutAsk bool
}
func NewCleanCommand(f *factory.Factory) *cobra.Command {
opts := &options{
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
}
cmd := &cobra.Command{
Use: "clean",
Short: opts.localizer.MustLocalize("cluster.clean.shortDescription"),
Long: opts.localizer.MustLocalize("cluster.clean.longDescription"),
Example: opts.localizer.MustLocalize("cluster.clean.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return runStatus(opts)
},
}
cmd.Flags().StringVar(&opts.kubeconfig, "kubeconfig", "", opts.localizer.MustLocalize("cluster.common.flag.kubeconfig.description"))
cmd.Flags().StringVarP(&opts.namespace, "namespace", "n", "", opts.localizer.MustLocalize("cluster.common.flag.namespace.description"))
cmd.Flags().BoolVarP(&opts.forceCreationWithoutAsk, "yes", "y", false, opts.localizer.MustLocalize("cluster.common.flag.yes.description"))
return cmd
}
func runStatus(opts *options) error {
conn, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
env := v1alpha.CommandEnvironment{
IO: opts.IO,
Logger: opts.Logger,
Localizer: opts.localizer,
Config: opts.Config,
Connection: conn,
Context: opts.Context,
}
kubeClients, err := kubeclient.NewKubernetesClusterClients(&env, opts.kubeconfig)
if err != nil {
return err
}
clusterAPI := cluster.KubernetesClusterAPIImpl{
KubernetesClients: kubeClients,
CommandEnvironment: &env,
}
return clusterAPI.ExecuteClean(&v1alpha.CleanOperationOptions{
ForceDeleteWithoutAsk: opts.forceCreationWithoutAsk,
Namespace: opts.namespace,
})
}