-
Notifications
You must be signed in to change notification settings - Fork 18
/
nlb_service_add.go
176 lines (152 loc) · 4.95 KB
/
nlb_service_add.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
package cmd
import (
"errors"
"fmt"
"strings"
"time"
exov2 "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
var nlbServiceAddCmd = &cobra.Command{
Use: "add NLB-NAME|ID SERVICE-NAME",
Short: "Add a service to a Network Load Balancer",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{
"healthcheck-interval",
"healthcheck-retries",
"healthcheck-timeout",
"instance-pool-id",
"port",
"protocol",
"strategy",
"zone",
})
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
nlbRef = args[0]
name = args[1]
)
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
description, err := cmd.Flags().GetString("description")
if err != nil {
return err
}
instancePoolID, err := cmd.Flags().GetString("instance-pool-id")
if err != nil {
return err
}
protocol, err := cmd.Flags().GetString("protocol")
if err != nil {
return err
}
port, err := cmd.Flags().GetUint16("port")
if err != nil {
return err
}
targetPort, err := cmd.Flags().GetUint16("target-port")
if err != nil {
return err
}
if targetPort == 0 {
targetPort = port
}
strategy, err := cmd.Flags().GetString("strategy")
if err != nil {
return err
}
healthcheckMode, err := cmd.Flags().GetString("healthcheck-mode")
if err != nil {
return err
}
healthcheckURI, err := cmd.Flags().GetString("healthcheck-uri")
if err != nil {
return err
}
if strings.HasPrefix(healthcheckMode, "http") && healthcheckURI == "" {
return errors.New(`an healthcheck URI is required in "http(s)" mode`)
}
healthcheckPort, err := cmd.Flags().GetUint16("healthcheck-port")
if err != nil {
return err
}
if healthcheckPort == 0 {
healthcheckPort = targetPort
}
healthcheckInterval, err := cmd.Flags().GetInt64("healthcheck-interval")
if err != nil {
return err
}
healthcheckTimeout, err := cmd.Flags().GetInt64("healthcheck-timeout")
if err != nil {
return err
}
healthcheckRetries, err := cmd.Flags().GetInt64("healthcheck-retries")
if err != nil {
return err
}
healthcheckTLSSNI, err := cmd.Flags().GetString("healthcheck-tls-sni")
if err != nil {
return err
}
if healthcheckTLSSNI != "" && healthcheckMode != "https" {
return errors.New(`a healthcheck TLS SNI can only be specified in https mode`)
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
nlb, err := lookupNLB(ctx, zone, nlbRef)
if err != nil {
return err
}
svc, err := nlb.AddService(ctx, &exov2.NetworkLoadBalancerService{
Name: name,
Description: description,
InstancePoolID: instancePoolID,
Protocol: protocol,
Port: port,
TargetPort: targetPort,
Strategy: strategy,
Healthcheck: exov2.NetworkLoadBalancerServiceHealthcheck{
Mode: healthcheckMode,
Port: healthcheckPort,
URI: healthcheckURI,
Interval: time.Duration(healthcheckInterval) * time.Second,
Timeout: time.Duration(healthcheckTimeout) * time.Second,
Retries: healthcheckRetries,
TLSSNI: healthcheckTLSSNI,
},
})
if err != nil {
return fmt.Errorf("unable to add service: %s", err)
}
if !gQuiet {
return output(showNLBService(zone, nlb.ID, svc.ID))
}
return nil
},
}
func init() {
nlbServiceAddCmd.Flags().StringP("zone", "z", "", "Network Load Balancer zone")
nlbServiceAddCmd.Flags().String("instance-pool-id", "",
"ID of the Instance Pool to forward traffic to")
nlbServiceAddCmd.Flags().String("description", "", "service description")
nlbServiceAddCmd.Flags().String("protocol", "tcp", "protocol of the service (tcp|udp)")
nlbServiceAddCmd.Flags().Uint16("port", 0, "service port")
nlbServiceAddCmd.Flags().Uint16("target-port", 0, "port to forward traffic to on target instances (defaults to service port)")
nlbServiceAddCmd.Flags().String("strategy", "round-robin", "load balancing strategy (round-robin|source-hash)")
nlbServiceAddCmd.Flags().String("healthcheck-mode", "tcp", "service health checking mode (tcp|http|https)")
nlbServiceAddCmd.Flags().String("healthcheck-uri", "", "service health checking URI (required in http(s) mode)")
nlbServiceAddCmd.Flags().Uint16("healthcheck-port", 0, "service health checking port (defaults to target port)")
nlbServiceAddCmd.Flags().Int64("healthcheck-interval", 10, "service health checking interval in seconds")
nlbServiceAddCmd.Flags().Int64("healthcheck-timeout", 5, "service health checking timeout in seconds")
nlbServiceAddCmd.Flags().Int64("healthcheck-retries", 1, "service health checking retries")
nlbServiceAddCmd.Flags().String("healthcheck-tls-sni", "", "service health checking server name to present with SNI in https mode")
nlbServiceCmd.AddCommand(nlbServiceAddCmd)
}