-
Notifications
You must be signed in to change notification settings - Fork 7
/
egressgateway_enable.go
224 lines (183 loc) · 6.95 KB
/
egressgateway_enable.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"context"
"fmt"
"io"
"strings"
"time"
"k8s.io/utils/pointer"
"github.com/flomesh-io/fsm/pkg/constants"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
configv1alpha3 "github.com/flomesh-io/fsm/pkg/apis/config/v1alpha3"
"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/action"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
configClientset "github.com/flomesh-io/fsm/pkg/gen/client/config/clientset/versioned"
)
const egressGatewayEnableDescription = `
This command will enable FSM egress-gateway, make sure --mesh-name and --fsm-namespace matches
the release name and namespace of installed FSM, otherwise it doesn't work.
`
type egressGatewayEnableCmd struct {
out io.Writer
kubeClient kubernetes.Interface
dynamicClient dynamic.Interface
configClient configClientset.Interface
mapper meta.RESTMapper
actionConfig *action.Configuration
meshName string
mode string
logLevel string
adminPort int32
port int32
replicas int32
}
func (cmd *egressGatewayEnableCmd) GetActionConfig() *action.Configuration {
return cmd.actionConfig
}
func (cmd *egressGatewayEnableCmd) GetDynamicClient() dynamic.Interface {
return cmd.dynamicClient
}
func (cmd *egressGatewayEnableCmd) GetRESTMapper() meta.RESTMapper {
return cmd.mapper
}
func (cmd *egressGatewayEnableCmd) GetMeshName() string {
return cmd.meshName
}
func newEgressGatewayEnable(actionConfig *action.Configuration, out io.Writer) *cobra.Command {
enableCmd := &egressGatewayEnableCmd{
out: out,
actionConfig: actionConfig,
}
cmd := &cobra.Command{
Use: "enable",
Short: "enable fsm egress-gateway",
Long: egressGatewayEnableDescription,
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, args []string) error {
config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return fmt.Errorf("error fetching kubeconfig: %w", err)
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("could not access Kubernetes cluster, check kubeconfig: %w", err)
}
enableCmd.kubeClient = kubeClient
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return fmt.Errorf("could not access Kubernetes cluster, check kubeconfig: %w", err)
}
enableCmd.dynamicClient = dynamicClient
gr, err := restmapper.GetAPIGroupResources(kubeClient.Discovery())
if err != nil {
return fmt.Errorf("could not access Kubernetes cluster, check kubeconfig: %w", err)
}
mapper := restmapper.NewDiscoveryRESTMapper(gr)
enableCmd.mapper = mapper
configClient, err := configClientset.NewForConfig(config)
if err != nil {
return fmt.Errorf("could not access Kubernetes cluster, check kubeconfig: %w", err)
}
enableCmd.configClient = configClient
return enableCmd.run()
},
}
f := cmd.Flags()
f.StringVar(&enableCmd.meshName, "mesh-name", defaultMeshName, "name for the control plane instance")
f.StringVar(&enableCmd.mode, "mode", constants.EgressGatewayModeHTTP2Tunnel, "mode of the egress-gateway, http2tunnel or sock5")
f.StringVar(&enableCmd.logLevel, "log-level", "error", "log level of egress-gateway")
f.Int32Var(&enableCmd.adminPort, "admin-port", 6060, "admin port of egress-gateway, rarely need to be set manually")
f.Int32Var(&enableCmd.port, "port", 1080, "serving port of egress-gateway")
f.Int32Var(&enableCmd.replicas, "replicas", 1, "replicas of egress-gateway")
//utilruntime.Must(cmd.MarkFlagRequired("mode"))
return cmd
}
func (cmd *egressGatewayEnableCmd) run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if cmd.mode != constants.EgressGatewayModeHTTP2Tunnel && cmd.mode != constants.EgressGatewayModeSock5 {
return fmt.Errorf("mode must be either http2tunnel or socks5")
}
fsmNamespace := settings.Namespace()
debug("Getting mesh config ...")
// get mesh config
mc, err := cmd.configClient.ConfigV1alpha3().MeshConfigs(fsmNamespace).Get(ctx, defaultFsmMeshConfigName, metav1.GetOptions{})
if err != nil {
return err
}
if mc.Spec.EgressGateway.Enabled {
fmt.Fprintf(cmd.out, "egress-gateway is enabled, no action needed\n")
return nil
}
err = updatePresetMeshConfigMap(ctx, cmd.kubeClient, fsmNamespace, map[string]interface{}{
"egressGateway": map[string]interface{}{
"enabled": true,
"logLevel": cmd.logLevel,
"mode": cmd.mode,
"port": cmd.port,
"adminPort": cmd.adminPort,
"replicas": cmd.replicas,
},
})
if err != nil {
return err
}
debug("Updating mesh config ...")
// update mesh config, fsm-mesh-config
mc.Spec.EgressGateway = configv1alpha3.EgressGatewaySpec{
Enabled: true,
LogLevel: cmd.logLevel,
Mode: cmd.mode,
Port: pointer.Int32(cmd.port),
AdminPort: pointer.Int32(cmd.adminPort),
Replicas: pointer.Int32(cmd.replicas),
}
_, err = cmd.configClient.ConfigV1alpha3().MeshConfigs(fsmNamespace).Update(ctx, mc, metav1.UpdateOptions{})
if err != nil {
return err
}
if err := installManifests(cmd, mc, fsmNamespace, constants.KubeVersion119, egressGatewayManifestFiles...); err != nil {
return err
}
time.Sleep(3 * time.Second)
deployment, err := cmd.kubeClient.AppsV1().
Deployments(fsmNamespace).
Get(ctx, constants.FSMEgressGatewayName, metav1.GetOptions{})
if err != nil {
return err
}
if err := waitForDeploymentReady(ctx, cmd.kubeClient, deployment, cmd.out); err != nil {
return err
}
fmt.Fprintf(cmd.out, "egress-gateway is enabled successfully\n")
return nil
}
func (cmd *egressGatewayEnableCmd) ResolveValues(mc *configv1alpha3.MeshConfig, manifestFiles ...string) ([]string, map[string]interface{}, error) {
finalValues := map[string]interface{}{}
imageNameIndex := strings.LastIndex(mc.Spec.Misc.RepoServerImage, "/")
imageTagIndex := strings.LastIndex(mc.Spec.Misc.RepoServerImage, ":")
valuesConfig := []string{
fmt.Sprintf("fsm.egressGateway.enabled=%t", true),
fmt.Sprintf("fsm.egressGateway.logLevel=%s", cmd.logLevel),
fmt.Sprintf("fsm.egressGateway.mode=%s", cmd.mode),
fmt.Sprintf("fsm.egressGateway.port=%d", cmd.port),
fmt.Sprintf("fsm.egressGateway.adminPort=%d", cmd.adminPort),
fmt.Sprintf("fsm.egressGateway.replicas=%d", cmd.replicas),
fmt.Sprintf("fsm.fsmNamespace=%s", mc.GetNamespace()),
fmt.Sprintf("fsm.meshName=%s", cmd.meshName),
fmt.Sprintf("fsm.image.registry=%s", mc.Spec.Image.Registry),
fmt.Sprintf("fsm.image.pullPolicy=%s", mc.Spec.Image.PullPolicy),
fmt.Sprintf("fsm.image.tag=%s", mc.Spec.Image.Tag),
fmt.Sprintf("fsm.repoServer.image.registry=%s", mc.Spec.Misc.RepoServerImage[:imageNameIndex]),
fmt.Sprintf("fsm.repoServer.image.name=%s", mc.Spec.Misc.RepoServerImage[imageNameIndex+1:imageTagIndex]),
fmt.Sprintf("fsm.repoServer.image.tag=%s", mc.Spec.Misc.RepoServerImage[imageTagIndex+1:]),
}
if err := parseVal(valuesConfig, finalValues); err != nil {
return manifestFiles, nil, err
}
return manifestFiles, finalValues, nil
}