-
Notifications
You must be signed in to change notification settings - Fork 0
/
calico.go
106 lines (97 loc) · 2.89 KB
/
calico.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
package main
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/projectcalico/libcalico-go/lib/api"
"github.com/projectcalico/libcalico-go/lib/numorstring"
"strconv"
)
func (s *Agent) AddCalicoRule(ruleType, profileName, action, protocol, port string) error {
p, err := strconv.ParseUint(port, 10, 16)
if err != nil {
log.WithFields(logrus.Fields{
"err": err,
}).Error("port " + port + " is not valid")
return err
}
if ruleType != "ingress" && ruleType != "egress" {
log.Error("ruleType " + ruleType + " is not ingress or egress ")
return fmt.Errorf("ruleType %s is not ingress or egress", ruleType)
}
profile, err := s.calico.Profiles().Get(api.ProfileMetadata{Name: profileName})
if err != nil {
log.WithFields(logrus.Fields{
"err": err,
}).Error("get profile " + profileName + " error")
return err
}
if ruleType == "ingress" {
var rules []api.Rule
ingressRules := profile.Spec.IngressRules
for _, rule := range ingressRules {
ingressRule := rule
if len(ingressRule.Destination.Ports) > 0 {
for _, destPort := range ingressRule.Destination.Ports {
for i := destPort.MinPort; i <= destPort.MaxPort; i++ {
if uint64(i) == p {
continue
}
tmpRule := ingressRule
tmpRule.Destination.Ports = []numorstring.Port{numorstring.SinglePort(i)}
rules = append(rules, tmpRule)
}
}
} else {
rules = append(rules, ingressRule)
}
}
rule := api.Rule{}
rule.Action = action
proto := numorstring.ProtocolFromString(protocol)
rule.Protocol = &proto
rule.Destination.Ports = []numorstring.Port{numorstring.SinglePort(uint16(p))}
rules = append(rules, rule)
profile.Spec.IngressRules = rules
}
if ruleType == "egress" {
var rules []api.Rule
egressRules := profile.Spec.EgressRules
for _, rule := range egressRules {
egressRule := rule
if len(egressRule.Destination.Ports) > 0 {
for _, destPort := range egressRule.Destination.Ports {
for i := destPort.MinPort; i <= destPort.MaxPort; i++ {
if uint64(i) == p {
continue
}
tmpRule := egressRule
tmpRule.Destination.Ports = []numorstring.Port{numorstring.SinglePort(i)}
rules = append(rules, tmpRule)
}
}
} else {
rules = append(rules, egressRule)
}
}
rule := api.Rule{}
rule.Action = action
proto := numorstring.ProtocolFromString(protocol)
rule.Protocol = &proto
rule.Destination.Ports = []numorstring.Port{numorstring.SinglePort(uint16(p))}
rules = append(rules, rule)
profile.Spec.EgressRules = rules
}
_, err = s.calico.Profiles().Apply(profile)
if err != nil {
log.WithFields(logrus.Fields{
"err": err,
}).Error("apply profile " + profileName + " error")
return err
}
return nil
}
func (s *Agent) ApplyCalicoProfile(virtualIpItem *VirtualIpItem) {
for _, v := range virtualIpItem.ports {
s.AddCalicoRule("ingress", virtualIpItem.appName, "allow", v.proto, v.port)
}
}