forked from cilium/cilium
/
service_update.go
148 lines (125 loc) · 4.13 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
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
// 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"
"strconv"
"strings"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/loadbalancer"
"github.com/spf13/cobra"
)
var (
addRev bool
idU uint64
frontend string
backends []string
)
// serviceUpdateCmd represents the service_update command
var serviceUpdateCmd = &cobra.Command{
Use: "update",
Short: "Update a service",
Run: func(cmd *cobra.Command, args []string) {
updateService(cmd, args)
},
}
func init() {
serviceCmd.AddCommand(serviceUpdateCmd)
serviceUpdateCmd.Flags().BoolVarP(&addRev, "rev", "", true, "Add reverse translation")
serviceUpdateCmd.Flags().Uint64VarP(&idU, "id", "", 0, "Identifier")
serviceUpdateCmd.Flags().StringVarP(&frontend, "frontend", "", "", "Frontend address")
serviceUpdateCmd.Flags().StringSliceVarP(&backends, "backends", "", []string{}, "Backend address or addresses followed by optional weight (<IP:Port>[/weight])")
}
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)
var spec *models.ServiceSpec
svc, err := client.GetServiceID(id)
switch {
case err == nil && (svc.Status == nil || svc.Status.Realized == nil):
Fatalf("Cannot update service %d: empty state", id)
case err == nil:
spec = svc.Status.Realized
fmt.Printf("Updating existing service with id '%v'\n", id)
default:
spec = &models.ServiceSpec{ID: id}
fmt.Printf("Creating new service with id '%v'\n", id)
}
// This can happen when we create a new service or when the service returned
// to us has no flags set
if spec.Flags == nil {
spec.Flags = &models.ServiceSpecFlags{}
}
spec.FrontendAddress = fa
spec.Flags.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())
}
}
spec.BackendAddresses = nil
for _, backend := range backends {
tmp := strings.Split(backend, "/")
if len(tmp) > 2 {
Fatalf("Incorrect backend specification %s\n", backend)
}
addr := tmp[0]
weight := uint64(0)
if len(tmp) == 2 {
var err error
weight, err = strconv.ParseUint(tmp[1], 10, 16)
if err != nil {
Fatalf("Error converting weight %s\n", err)
}
}
beAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
Fatalf("Cannot parse backend address \"%s\": %s", backend, err)
}
// Backend ID will be set by the daemon
be := loadbalancer.NewLBBackEnd(0, loadbalancer.TCP, beAddr.IP, uint16(beAddr.Port), uint16(weight))
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()
spec.BackendAddresses = append(spec.BackendAddresses, ba)
}
if created, err := client.PutServiceID(id, spec); err != nil {
Fatalf("Cannot add/update service: %s", err)
} else if created {
fmt.Printf("Added service with %d backends\n", len(spec.BackendAddresses))
} else {
fmt.Printf("Updated service with %d backends\n", len(spec.BackendAddresses))
}
}