forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_update.go
119 lines (100 loc) · 3.3 KB
/
service_update.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
// Copyright 2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bufio"
"fmt"
"net"
"os"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/common/types"
"github.com/spf13/cobra"
)
var (
addRev bool
idU uint64
frontend string
backends []string
)
// service_updateCmd represents the service_update command
var service_updateCmd = &cobra.Command{
Use: "update",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
updateService(cmd, args)
},
}
func init() {
serviceCmd.AddCommand(service_updateCmd)
service_updateCmd.Flags().BoolVarP(&addRev, "rev", "", true, "Add reverse translation")
service_updateCmd.Flags().Uint64VarP(&idU, "id", "", 0, "Identifier")
service_updateCmd.Flags().StringVarP(&frontend, "frontend", "", "", "Frontend address")
service_updateCmd.Flags().StringSliceVarP(&backends, "backends", "", []string{}, "Backend addresses")
}
func parseFrontendAddress(address string) (*models.FrontendAddress, net.IP) {
frontend, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
Fatalf("Unable to parse frontend address: %s\n", err)
}
// FIXME support more than TCP
return &models.FrontendAddress{
IP: frontend.IP.String(),
Port: uint16(frontend.Port),
Protocol: models.FrontendAddressProtocolTCP,
}, frontend.IP
}
func updateService(cmd *cobra.Command, args []string) {
id := int64(idU)
fa, faIP := parseFrontendAddress(frontend)
svc := &models.Service{
ID: id,
FrontendAddress: fa,
BackendAddresses: []*models.BackendAddress{},
Flags: &models.ServiceFlags{
DirectServerReturn: addRev,
},
}
if len(backends) == 0 {
fmt.Printf("Reading backend list from stdin...\n")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
backends = append(backends, scanner.Text())
}
}
for _, backend := range backends {
beAddr, err := net.ResolveTCPAddr("tcp", backend)
if err != nil {
Fatalf("Cannot parse backend address \"%s\": %s", backend, err)
}
be, err := types.NewL3n4Addr(types.TCP, beAddr.IP, uint16(beAddr.Port))
if err != nil {
Fatalf("Unable to create a new L3n4Addr for backend %s: %s", backend, err)
}
if be.IsIPv6() && faIP.To4() != nil {
Fatalf("Address mismatch between frontend and backend %s", backend)
}
if fa.Port == 0 && beAddr.Port != 0 {
Fatalf("L4 backend found (%v) with L3 frontend", beAddr)
}
ba := be.GetBackendModel()
svc.BackendAddresses = append(svc.BackendAddresses, ba)
}
if created, err := client.PutServiceID(id, svc); err != nil {
Fatalf("Cannot add/update service: %s", err)
} else if created {
fmt.Printf("Added service with %d backends\n", len(svc.BackendAddresses))
} else {
fmt.Printf("Updated service with %d backends\n", len(svc.BackendAddresses))
}
}