-
Notifications
You must be signed in to change notification settings - Fork 20
/
nlb_service_add.go
135 lines (112 loc) · 4.53 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
package cmd
import (
"errors"
"fmt"
"strings"
"time"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type nlbServiceAddCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"add"`
NetworkLoadBalancer string `cli-arg:"#" cli-usage:"LOAD-BALANCER-NAME|ID"`
Name string `cli-arg:"#" cli-usage:"SERVICE-NAME"`
Description string `cli-usage:"service description"`
HealthcheckInterval int64 `cli-usage:"service health checking interval in seconds"`
HealthcheckMode string `cli-usage:"service health checking mode (tcp|http|https)"`
HealthcheckPort int64 `cli-usage:"service health checking port (defaults to target port)"`
HealthcheckRetries int64 `cli-usage:"service health checking retries"`
HealthcheckTLSSNI string `cli-flag:"healthcheck-tls-sni" cli-usage:"service health checking server name to present with SNI in https mode"`
HealthcheckTimeout int64 `cli-usage:"service health checking timeout in seconds"`
HealthcheckURI string `cli-usage:"service health checking URI (required in http(s) mode)"`
InstancePool string `cli-usage:"name or ID of the Instance Pool to forward traffic to"`
Port int64 `cli-usage:"service port"`
Protocol string `cli-usage:"service network protocol (tcp|udp)"`
Strategy string `cli-usage:"load balancing strategy (round-robin|source-hash)"`
TargetPort int64 `cli-usage:"port to forward traffic to on target instances (defaults to service port)"`
Zone string `cli-short:"z" cli-usage:"Network Load Balancer zone"`
}
func (c *nlbServiceAddCmd) cmdAliases() []string { return nil }
func (c *nlbServiceAddCmd) cmdShort() string { return "Add a service to a Network Load Balancer" }
func (c *nlbServiceAddCmd) cmdLong() string {
return fmt.Sprintf(`This command adds a service to a Network Load Balancer.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&nlbServiceShowOutput{}), ", "))
}
func (c *nlbServiceAddCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *nlbServiceAddCmd) cmdRun(_ *cobra.Command, _ []string) error {
var (
port = uint16(c.Port)
targetPort = uint16(c.TargetPort)
hcPort = uint16(c.HealthcheckPort)
hcInterval = time.Duration(c.HealthcheckInterval) * time.Second
hcTimeout = time.Duration(c.HealthcheckTimeout) * time.Second
)
service := &egoscale.NetworkLoadBalancerService{
Description: nonEmptyStringPtr(c.Description),
Healthcheck: &egoscale.NetworkLoadBalancerServiceHealthcheck{
Interval: &hcInterval,
Mode: &c.HealthcheckMode,
Port: &hcPort,
Retries: &c.HealthcheckRetries,
TLSSNI: nonEmptyStringPtr(c.HealthcheckTLSSNI),
Timeout: &hcTimeout,
URI: nonEmptyStringPtr(c.HealthcheckURI),
},
Name: &c.Name,
Port: &port,
Protocol: &c.Protocol,
Strategy: &c.Strategy,
TargetPort: &targetPort,
}
if strings.HasPrefix(*service.Healthcheck.Mode, "http") && *service.Healthcheck.URI == "" {
return errors.New(`an healthcheck URI is required in "http(s)" mode`)
}
if *service.TargetPort == 0 {
service.TargetPort = service.Port
}
if *service.Healthcheck.Port == 0 {
service.Healthcheck.Port = service.TargetPort
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
nlb, err := cs.FindNetworkLoadBalancer(ctx, c.Zone, c.NetworkLoadBalancer)
if err != nil {
return fmt.Errorf("error retrieving Network Load Balancer: %w", err)
}
instancePool, err := cs.FindInstancePool(ctx, c.Zone, c.InstancePool)
if err != nil {
return fmt.Errorf("error retrieving Instance Pool: %w", err)
}
service.InstancePoolID = instancePool.ID
decorateAsyncOperation(fmt.Sprintf("Adding service %q...", c.Name), func() {
service, err = cs.CreateNetworkLoadBalancerService(ctx, c.Zone, nlb, service)
})
if err != nil {
return err
}
if !gQuiet {
return (&nlbServiceShowCmd{
cliCommandSettings: c.cliCommandSettings,
NetworkLoadBalancer: *nlb.ID,
Service: *service.ID,
Zone: c.Zone,
}).cmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(nlbServiceCmd, &nlbServiceAddCmd{
cliCommandSettings: defaultCLICmdSettings(),
HealthcheckInterval: 10,
HealthcheckMode: "tcp",
HealthcheckRetries: 1,
HealthcheckTimeout: 5,
Protocol: "tcp",
Strategy: "round-robin",
}))
}