-
Notifications
You must be signed in to change notification settings - Fork 72
/
connect.go
114 lines (96 loc) · 4.09 KB
/
connect.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
103
104
105
106
107
108
109
110
111
112
113
114
package connect
import (
"context"
"github.com/redhat-developer/app-services-cli/internal/build"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/core/servicecontext"
"github.com/redhat-developer/app-services-cli/pkg/shared/cluster"
"github.com/redhat-developer/app-services-cli/pkg/shared/cluster/kubeclient"
"github.com/redhat-developer/app-services-cli/pkg/shared/cluster/v1alpha"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/redhat-developer/app-services-cli/pkg/shared/servicespec"
"github.com/spf13/cobra"
)
type options struct {
Connection factory.ConnectionFunc
Logger logging.Logger
IO *iostreams.IOStreams
localizer localize.Localizer
Context context.Context
ServiceContext servicecontext.IContext
kubeconfigLocation string
namespace string
offlineAccessToken string
forceCreationWithoutAsk bool
serviceName string
serviceType string
ignoreContext bool
}
func NewConnectCommand(f *factory.Factory) *cobra.Command {
opts := &options{
Connection: f.Connection,
Logger: f.Logger,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
ServiceContext: f.ServiceContext,
}
cmd := &cobra.Command{
Use: "connect",
Short: opts.localizer.MustLocalize("cluster.connect.cmd.shortDescription"),
Long: opts.localizer.MustLocalize("cluster.connect.cmd.longDescription"),
Example: opts.localizer.MustLocalize("cluster.connect.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return runConnect(opts)
},
}
cmd.Flags().StringVar(&opts.kubeconfigLocation, "kubeconfig", "", opts.localizer.MustLocalize("cluster.common.flag.kubeconfig.description"))
cmd.Flags().StringVar(&opts.offlineAccessToken, "token", "", opts.localizer.MustLocalize("cluster.common.flag.offline.token.description", localize.NewEntry("OfflineTokenURL", build.OfflineTokenURL)))
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"))
cmd.Flags().StringVar(&opts.serviceName, "service-name", "", opts.localizer.MustLocalize("cluster.common.flag.serviceName.description"))
cmd.Flags().StringVar(&opts.serviceType, "service-type", "", opts.localizer.MustLocalize("cluster.common.flag.serviceType.description"))
cmd.Flags().BoolVar(&opts.ignoreContext, "ignore-context", false, opts.localizer.MustLocalize("cluster.common.flag.ignoreContext.description"))
_ = cmd.RegisterFlagCompletionFunc("service-type", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return servicespec.AllServiceLabels, cobra.ShellCompDirectiveNoSpace
})
return cmd
}
func runConnect(opts *options) error {
conn, err := opts.Connection()
if err != nil {
return err
}
cliProperties := v1alpha.CommandEnvironment{
IO: opts.IO,
Logger: opts.Logger,
Localizer: opts.localizer,
Connection: conn,
Context: opts.Context,
ServiceContext: opts.ServiceContext,
}
kubeClients, err := kubeclient.NewKubernetesClusterClients(&cliProperties, opts.kubeconfigLocation)
if err != nil {
return err
}
clusterAPI := cluster.KubernetesClusterAPIImpl{
KubernetesClients: kubeClients,
CommandEnvironment: &cliProperties,
}
arguments := &v1alpha.ConnectOperationOptions{
OfflineAccessToken: opts.offlineAccessToken,
ForceCreationWithoutAsk: opts.forceCreationWithoutAsk,
Namespace: opts.namespace,
ServiceType: opts.serviceType,
ServiceName: opts.serviceName,
IgnoreContext: opts.ignoreContext,
}
err = clusterAPI.ExecuteConnect(arguments)
if err != nil {
return err
}
return nil
}