forked from projectcalico/libcalico-go
/
createRule.go
73 lines (60 loc) · 1.75 KB
/
createRule.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
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
// 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 testutils
import (
"log"
"net"
"strconv"
api "github.com/mangqiqi/libcalico-go/lib/apis/v1"
cnet "github.com/mangqiqi/libcalico-go/lib/net"
"github.com/mangqiqi/libcalico-go/lib/numorstring"
)
// CreateRule takes all fields necessary to create a api.Rule object and returns ingress and egress api.Rules.
func CreateRule(ipv, icmpType, icmpCode int, proto, cidrStr, tag, selector, inAction, eAction string) (api.Rule, api.Rule) {
var protocol numorstring.Protocol
i, err := strconv.Atoi(proto)
if err != nil {
protocol = numorstring.ProtocolFromString(proto)
} else {
protocol = numorstring.ProtocolFromInt(uint8(i))
}
icmp := api.ICMPFields{
Type: &icmpType,
Code: &icmpCode,
}
_, cidr, err := net.ParseCIDR(cidrStr)
if err != nil {
log.Printf("Error parsing CIDR: %v\n", err)
}
src := api.EntityRule{
Tag: tag,
Net: &cnet.IPNet{
*cidr,
},
Selector: selector,
}
inRule := api.Rule{
Action: inAction,
IPVersion: &ipv,
Protocol: &protocol,
ICMP: &icmp,
Source: src,
}
eRule := api.Rule{
Action: eAction,
IPVersion: &ipv,
Protocol: &protocol,
ICMP: &icmp,
}
return inRule, eRule
}