-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (59 loc) · 1.64 KB
/
main.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
// libraryexample1 applies a model as a whole on top of an existing
// ipvs tables model.
//
package main
import (
"fmt"
ipvsctl "github.com/aschmidt75/ipvsctl/integration"
"gopkg.in/yaml.v2"
)
const targetModel string = `
services:
- address: tcp://127.0.0.1:9876
sched: rr
destinations:
- address: 127.0.0.2:12340
forward: nat
- address: 127.0.0.3:12340
forward: nat
`
func main() {
// Unmarshal targetModel into IPVSConfig struct
var newConfig ipvsctl.IPVSConfig
err := yaml.Unmarshal([]byte(targetModel), &newConfig)
if err != nil {
panic(err)
}
// Create IPVSConfig struct
currentConfig := ipvsctl.NewIPVSConfig()
// Get the current table
if err := currentConfig.Get(); err != nil {
panic(err)
}
fmt.Printf("Current ipvs table: %+v\n", currentConfig)
// set up apply options that limit our actions. We want to
// add services and destinations
opts := ipvsctl.ApplyOpts{
KeepWeights: true,
AllowedActions: ipvsctl.ApplyActions{
ipvsctl.ApplyActionAddService: true,
ipvsctl.ApplyActionAddDestination: true,
},
}
// Apply the new configuration. This will automatically build a change
// set and apply only changes that are necessary. That means if this
// is run for the 2nd time, nothing gets changed because it's already in.
if err := currentConfig.Apply(&newConfig, opts); err != nil {
panic(err)
}
if err := currentConfig.Get(); err != nil {
panic(err)
}
fmt.Printf("Current ipvs table:\n")
for _, s := range currentConfig.Services {
fmt.Printf("+ %s (%s)\n", s.Address, s.SchedName)
for _, d := range s.Destinations {
fmt.Printf(" -> %s (%s) w:%d\n", d.Address, d.Forward, d.Weight)
}
}
}