-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (96 loc) · 2.71 KB
/
main.go
File metadata and controls
112 lines (96 loc) · 2.71 KB
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
//
// libraryexample2 uses change sets to modify in-situ instead of replacing the whole configuration.
//
package main
import (
"fmt"
ipvsctl "github.com/aschmidt75/ipvsctl/integration"
)
func main() {
// Create IPVSConfig struct
cfg := ipvsctl.NewIPVSConfig()
// Get the current table
if err := cfg.Get(); err != nil {
panic(err)
}
fmt.Printf("Current ipvs table: %+v\n", cfg)
// set up apply options that limit our actions. We want to
// add services and destinations, later delete services
opts := ipvsctl.ApplyOpts{
KeepWeights: true,
AllowedActions: ipvsctl.ApplyActions{
ipvsctl.ApplyActionAddService: true,
ipvsctl.ApplyActionAddDestination: true,
ipvsctl.ApplyActionDeleteService: true,
},
}
// create a change set with a single change set item: add a service
cs := ipvsctl.NewChangeSet()
cs.AddChange(ipvsctl.ChangeSetItem{
Type: ipvsctl.AddService,
Service: &ipvsctl.Service{
Address: "tcp://127.0.0.1:9876",
SchedName: "rr",
},
})
// could cs.AddChange(...) here as well for other services etc.
fmt.Printf("Change set: %+v\n", cs)
// apply this change
if err := cfg.ApplyChangeSet(cfg, cs, opts); err != nil {
fmt.Println(err)
}
// get ipvs tables again, locate the service we just added
// reason here is because for the modification of existing
// things we need the real structures from ipvs, and not
// just the string handles
if err := cfg.Get(); err != nil {
panic(err)
}
var myService *ipvsctl.Service
for _, s := range cfg.Services {
if s.Address == "tcp://127.0.0.1:9876" {
myService = s
}
}
if myService == nil {
panic("unable to find my service?")
}
// add a destination (could have done that with the first change set as well)
cs2 := ipvsctl.NewChangeSet()
cs2.AddChange(ipvsctl.ChangeSetItem{
Type: ipvsctl.AddDestination,
Service: myService,
Destination: &ipvsctl.Destination{
Address: "127.0.0.2:12340",
Weight: 1000,
Forward: "nat",
},
})
fmt.Printf("Change set: %+v\n", cs)
// apply this change as well
if err := cfg.ApplyChangeSet(cfg, cs2, opts); err != nil {
fmt.Println(err)
}
// get ipvs tables again and print out
if err := cfg.Get(); err != nil {
panic(err)
}
fmt.Printf("Current ipvs table:\n")
for _, s := range cfg.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)
}
}
// delete what we created
cs3 := ipvsctl.NewChangeSet()
cs3.AddChange(ipvsctl.ChangeSetItem{
Type: ipvsctl.DeleteService,
Service: myService,
})
fmt.Printf("Change set: %+v\n", cs)
// apply this change as well
if err := cfg.ApplyChangeSet(cfg, cs3, opts); err != nil {
fmt.Println(err)
}
}