forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
199 lines (173 loc) · 5.19 KB
/
utils.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2017-2019 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 utils
import (
"bufio"
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/kelseyhightower/envconfig"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/felix/calc"
"github.com/projectcalico/felix/ipsets"
"github.com/projectcalico/felix/rules"
"github.com/projectcalico/libcalico-go/lib/apiconfig"
client "github.com/projectcalico/libcalico-go/lib/clientv3"
"github.com/projectcalico/libcalico-go/lib/options"
"github.com/projectcalico/libcalico-go/lib/selector"
)
type EnvConfig struct {
FelixImage string `default:"calico/felix:latest"`
EtcdImage string `default:"quay.io/coreos/etcd"`
K8sImage string `default:"gcr.io/google_containers/hyperkube-amd64:v1.10.4"`
TyphaImage string `default:"calico/typha:latest"` // Note: this is overridden in the Makefile!
BusyboxImage string `default:"busybox:latest"`
}
var Config EnvConfig
func init() {
err := envconfig.Process("fv", &Config)
if err != nil {
panic(err)
}
log.WithField("config", Config).Info("Loaded config")
}
var Ctx = context.Background()
var NoOptions = options.SetOptions{}
func Run(command string, args ...string) {
_ = run(true, command, args...)
}
func RunMayFail(command string, args ...string) error {
return run(false, command, args...)
}
var currentTestOutput = []string{}
var LastRunOutput string
func run(checkNoError bool, command string, args ...string) error {
outputBytes, err := Command(command, args...).CombinedOutput()
currentTestOutput = append(currentTestOutput, fmt.Sprintf("Command: %v %v\n", command, args))
currentTestOutput = append(currentTestOutput, string(outputBytes))
LastRunOutput = string(outputBytes)
if err != nil {
log.WithFields(log.Fields{
"command": command,
"args": args,
"output": string(outputBytes)}).WithError(err).Warning("Command failed")
}
if checkNoError {
Expect(err).NotTo(HaveOccurred())
}
return err
}
func AddToTestOutput(args ...string) {
currentTestOutput = append(currentTestOutput, args...)
}
var _ = BeforeEach(func() {
currentTestOutput = []string{}
})
var _ = AfterEach(func() {
if CurrentGinkgoTestDescription().Failed {
os.Stdout.WriteString("\n===== begin output from failed test =====\n")
for _, output := range currentTestOutput {
os.Stdout.WriteString(output)
}
os.Stdout.WriteString("===== end output from failed test =====\n\n")
}
})
func RunCommand(command string, args ...string) error {
cmd := Command(command, args...)
log.Infof("Running '%s %s'", cmd.Path, strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput()
log.Infof("output: %v", string(output))
return err
}
func Command(name string, args ...string) *exec.Cmd {
log.WithFields(log.Fields{
"command": name,
"commandArgs": args,
}).Info("Creating Command.")
return exec.Command(name, args...)
}
func LogOutput(cmd *exec.Cmd, name string) error {
outPipe, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("Getting StdoutPipe failed for %s: %v", name, err)
}
errPipe, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("Getting StderrPipe failed for %s: %v", name, err)
}
stdoutReader := bufio.NewReader(outPipe)
stderrReader := bufio.NewReader(errPipe)
go func() {
for {
line, err := stdoutReader.ReadString('\n')
if err != nil {
log.WithError(err).Infof("End of %s stdout", name)
return
}
log.Infof("%s stdout: %s", name, strings.TrimSpace(string(line)))
}
}()
go func() {
for {
line, err := stderrReader.ReadString('\n')
if err != nil {
log.WithError(err).Infof("End of %s stderr", name)
return
}
log.Infof("%s stderr: %s", name, strings.TrimSpace(string(line)))
}
}()
return nil
}
func GetEtcdClient(etcdIP string) client.Interface {
client, err := client.New(apiconfig.CalicoAPIConfig{
Spec: apiconfig.CalicoAPIConfigSpec{
DatastoreType: apiconfig.EtcdV3,
EtcdConfig: apiconfig.EtcdConfig{
EtcdEndpoints: "http://" + etcdIP + ":2379",
},
},
})
Expect(err).NotTo(HaveOccurred())
return client
}
func IPSetIDForSelector(rawSelector string) string {
sel, err := selector.Parse(rawSelector)
Expect(err).ToNot(HaveOccurred())
ipSetData := calc.IPSetData{
Selector: sel,
}
setID := ipSetData.UniqueID()
return setID
}
func IPSetNameForSelector(ipVersion int, rawSelector string) string {
setID := IPSetIDForSelector(rawSelector)
var ipFamily ipsets.IPFamily
if ipVersion == 4 {
ipFamily = ipsets.IPFamilyV4
} else {
ipFamily = ipsets.IPFamilyV6
}
ipVerConf := ipsets.NewIPVersionConfig(
ipFamily,
rules.IPSetNamePrefix,
nil,
nil,
)
return ipVerConf.NameForMainIPSet(setID)
}