-
Notifications
You must be signed in to change notification settings - Fork 7
/
gateway_enable.go
144 lines (117 loc) · 4.07 KB
/
gateway_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
package main
import (
"context"
"fmt"
"io"
"github.com/flomesh-io/fsm/pkg/version"
nsigClientset "github.com/flomesh-io/fsm/pkg/gen/client/namespacedingress/clientset/versioned"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
configClientset "github.com/flomesh-io/fsm/pkg/gen/client/config/clientset/versioned"
)
const gatewayEnableDescription = `
This command will enable FSM gateway, make sure --mesh-name and --fsm-namespace matches
the release name and namespace of installed FSM, otherwise it doesn't work.
`
type gatewayEnableCmd struct {
out io.Writer
kubeClient kubernetes.Interface
configClient configClientset.Interface
nsigClient nsigClientset.Interface
meshName string
logLevel string
}
func newGatewayEnable(out io.Writer) *cobra.Command {
enableCmd := &gatewayEnableCmd{
out: out,
}
cmd := &cobra.Command{
Use: "enable",
Short: "enable fsm gateway",
Long: gatewayEnableDescription,
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
configClient, err := configClientset.NewForConfig(config)
if err != nil {
return fmt.Errorf("could not access Kubernetes cluster, check kubeconfig: %w", err)
}
enableCmd.configClient = configClient
nsigClient, err := nsigClientset.NewForConfig(config)
if err != nil {
return fmt.Errorf("could not access Kubernetes cluster, check kubeconfig: %w", err)
}
enableCmd.nsigClient = nsigClient
return enableCmd.run()
},
}
f := cmd.Flags()
f.StringVar(&enableCmd.meshName, "mesh-name", defaultMeshName, "name for the control plane instance")
f.StringVar(&enableCmd.logLevel, "log-level", "error", "log level of gateway")
//utilruntime.Must(cmd.MarkFlagRequired("mesh-name"))
return cmd
}
func (cmd *gatewayEnableCmd) run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if !version.IsSupportedK8sVersionForGatewayAPI(cmd.kubeClient) {
return fmt.Errorf("kubernetes server version %s is not supported, requires at least %s",
version.ServerVersion.String(), version.MinK8sVersionForGatewayAPI.String())
}
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
}
// check if gateway is enabled, if yes, just return
// TODO: check if GatewayClass is installed and if there's any running gateway instances
if mc.Spec.GatewayAPI.Enabled {
fmt.Fprintf(cmd.out, "Gatweway is enabled already, no action needed\n")
return nil
}
debug("Deleting FSM Ingress resources ...")
err = deleteIngressResources(ctx, cmd.kubeClient, fsmNamespace, cmd.meshName)
if err != nil {
return err
}
debug("Deleting FSM NamespacedIngress resources ...")
err = deleteNamespacedIngressResources(ctx, cmd.nsigClient)
if err != nil {
return err
}
err = updatePresetMeshConfigMap(ctx, cmd.kubeClient, fsmNamespace, map[string]interface{}{
"ingress.enabled": false,
"ingress.namespaced": false,
"gatewayAPI.enabled": true,
"gatewayAPI.logLevel": cmd.logLevel,
})
if err != nil {
return err
}
debug("Updating mesh config ...")
// update mesh config, fsm-mesh-config
mc.Spec.Ingress.Enabled = false
mc.Spec.Ingress.Namespaced = false
mc.Spec.GatewayAPI.Enabled = true
mc.Spec.GatewayAPI.LogLevel = cmd.logLevel
_, err = cmd.configClient.ConfigV1alpha3().MeshConfigs(fsmNamespace).Update(ctx, mc, metav1.UpdateOptions{})
if err != nil {
return err
}
if err := restartFSMController(ctx, cmd.kubeClient, fsmNamespace, cmd.out); err != nil {
return err
}
fmt.Fprintf(cmd.out, "Gateway is enabled successfully\n")
return nil
}