Skip to content

Commit 27e8aa4

Browse files
committed
use generic json/yaml parsing functions
1 parent dca5e1d commit 27e8aa4

File tree

5 files changed

+25
-51
lines changed

5 files changed

+25
-51
lines changed

pkg/cli/analyze.go

+7-17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package cli
22

33
import (
4-
"encoding/json"
54
"fmt"
5+
collections "github.com/mattfenwick/collections/pkg"
66
"github.com/mattfenwick/cyclonus/pkg/connectivity/probe"
77
"github.com/mattfenwick/cyclonus/pkg/generator"
88
"github.com/mattfenwick/cyclonus/pkg/linter"
9-
"io/ioutil"
109
"strings"
1110

1211
"github.com/mattfenwick/cyclonus/pkg/kube"
@@ -175,12 +174,9 @@ type QueryTargetPod struct {
175174

176175
func QueryTargets(explainedPolicies *matcher.Policy, podPath string, pods []*QueryTargetPod) {
177176
if podPath != "" {
178-
var podsFromFile []*QueryTargetPod
179-
bs, err := ioutil.ReadFile(podPath)
177+
podsFromFile, err := utils.ParseJsonFromFile[[]*QueryTargetPod](podPath)
180178
utils.DoOrDie(err)
181-
err = json.Unmarshal(bs, &podsFromFile)
182-
utils.DoOrDie(err)
183-
pods = append(pods, podsFromFile...)
179+
pods = append(pods, *podsFromFile...)
184180
}
185181

186182
for _, pod := range pods {
@@ -213,16 +209,13 @@ func QueryTargetHelper(policies *matcher.Policy, pod *QueryTargetPod) (*matcher.
213209
}
214210

215211
func QueryTraffic(explainedPolicies *matcher.Policy, trafficPath string) {
216-
var allTraffics []*matcher.Traffic
217212
if trafficPath == "" {
218213
logrus.Fatalf("%+v", errors.Errorf("path to traffic file required for QueryTraffic command"))
219214
}
220-
allTrafficBytes, err := ioutil.ReadFile(trafficPath)
221-
utils.DoOrDie(err)
222-
err = json.Unmarshal(allTrafficBytes, &allTraffics)
215+
allTraffics, err := utils.ParseJsonFromFile[[]*matcher.Traffic](trafficPath)
223216
utils.DoOrDie(err)
224217

225-
for _, traffic := range allTraffics {
218+
for _, traffic := range *allTraffics {
226219
fmt.Printf("Traffic:\n%s\n", traffic.Table())
227220

228221
result := explainedPolicies.IsTrafficAllowed(traffic)
@@ -237,11 +230,8 @@ type SyntheticProbeConnectivityConfig struct {
237230

238231
func ProbeSyntheticConnectivity(explainedPolicies *matcher.Policy, modelPath string, kubePods []v1.Pod, kubeNamespaces []v1.Namespace) {
239232
if modelPath != "" {
240-
bs, err := ioutil.ReadFile(modelPath)
241-
utils.DoOrDie(errors.Wrapf(err, "unable to read file %s", modelPath))
242-
config := &SyntheticProbeConnectivityConfig{}
243-
err = json.Unmarshal(bs, &config)
244-
utils.DoOrDie(errors.Wrapf(err, "unable to unmarshal json"))
233+
config, err := utils.ParseJsonFromFile[SyntheticProbeConnectivityConfig](modelPath)
234+
utils.DoOrDie(err)
245235

246236
jobBuilder := &probe.JobBuilder{TimeoutSeconds: 10}
247237

pkg/cli/probe.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import (
88
"github.com/mattfenwick/cyclonus/pkg/utils"
99
"github.com/pkg/errors"
1010
"github.com/spf13/cobra"
11-
"io/ioutil"
1211
v1 "k8s.io/api/core/v1"
1312
networkingv1 "k8s.io/api/networking/v1"
1413
"k8s.io/apimachinery/pkg/util/intstr"
15-
"sigs.k8s.io/yaml"
1614
"strings"
1715
)
1816

@@ -101,14 +99,9 @@ func RunProbeCommand(args *ProbeArgs) {
10199
actions := []*generator.Action{generator.ReadNetworkPolicies(args.ServerNamespaces)}
102100

103101
if args.PolicyPath != "" {
104-
policyBytes, err := ioutil.ReadFile(args.PolicyPath)
102+
kubePolicy, err := utils.ParseYamlFromFile[networkingv1.NetworkPolicy](args.PolicyPath)
105103
utils.DoOrDie(err)
106-
107-
var kubePolicy networkingv1.NetworkPolicy
108-
err = yaml.Unmarshal(policyBytes, &kubePolicy)
109-
utils.DoOrDie(err)
110-
111-
actions = append(actions, generator.CreatePolicy(&kubePolicy))
104+
actions = append(actions, generator.CreatePolicy(kubePolicy))
112105
}
113106

114107
printer := connectivity.Printer{

pkg/cli/utils.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package cli
22

33
import (
44
"github.com/mattfenwick/cyclonus/pkg/kube"
5+
"github.com/mattfenwick/cyclonus/pkg/utils"
56
"github.com/pkg/errors"
67
log "github.com/sirupsen/logrus"
7-
"io/ioutil"
88
networkingv1 "k8s.io/api/networking/v1"
99
"os"
1010
"path/filepath"
11-
"sigs.k8s.io/yaml"
1211
)
1312

1413
func readPoliciesFromPath(policyPath string) ([]*networkingv1.NetworkPolicy, error) {
@@ -22,25 +21,23 @@ func readPoliciesFromPath(policyPath string) ([]*networkingv1.NetworkPolicy, err
2221
return nil
2322
}
2423
log.Debugf("walking path %s", path)
25-
bytes, err := ioutil.ReadFile(path)
24+
bytes, err := utils.ReadFileBytes(path)
2625
if err != nil {
27-
return errors.Wrapf(err, "unable to read file %s", path)
26+
return err
2827
}
2928

3029
// try parsing a list first
31-
var policies []*networkingv1.NetworkPolicy
32-
err = yaml.Unmarshal(bytes, &policies)
30+
policies, err := utils.ParseYaml[[]*networkingv1.NetworkPolicy](bytes)
3331
if err == nil {
34-
log.Debugf("parsed %d policies from %s", len(policies), path)
35-
allPolicies = append(allPolicies, policies...)
32+
log.Debugf("parsed %d policies from %s", len(*policies), path)
33+
allPolicies = append(allPolicies, *policies...)
3634
return nil
3735
}
3836

3937
log.Debugf("failed to parse list from %s, falling back to parsing single policy", path)
40-
var policy *networkingv1.NetworkPolicy
41-
err = yaml.UnmarshalStrict(bytes, &policy)
38+
policy, err := utils.ParseYamlStrict[networkingv1.NetworkPolicy](bytes)
4239
if err != nil {
43-
return errors.Wrapf(err, "unable to unmarshal single policy from yaml at %s", path)
40+
return errors.WithMessagef(err, "unable to parse single policy from yaml at %s", path)
4441
}
4542

4643
log.Debugf("parsed single policy from %s: %+v", path, policy)

pkg/matcher/policy_tests.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
. "github.com/onsi/gomega"
77
v1 "k8s.io/api/core/v1"
88
networkingv1 "k8s.io/api/networking/v1"
9-
"sigs.k8s.io/yaml"
109
)
1110

1211
func RunPolicyTests() {
13-
allowAllOnSCTPSerialized := `
12+
allowAllOnSCTPSerializedYaml := `
1413
apiVersion: networking.k8s.io/v1
1514
kind: NetworkPolicy
1615
metadata:
@@ -23,10 +22,9 @@ spec:
2322
podSelector: {}
2423
policyTypes:
2524
- Ingress`
26-
var kubePolicy *networkingv1.NetworkPolicy
27-
err := yaml.Unmarshal([]byte(allowAllOnSCTPSerialized), &kubePolicy)
25+
allowAllOnSCTPSerializedPolicy, err := utils.ParseYaml[networkingv1.NetworkPolicy]([]byte(allowAllOnSCTPSerializedYaml))
2826
utils.DoOrDie(err)
29-
allowAllOnSCTP := BuildNetworkPolicies(true, []*networkingv1.NetworkPolicy{kubePolicy})
27+
allowAllOnSCTP := BuildNetworkPolicies(true, []*networkingv1.NetworkPolicy{allowAllOnSCTPSerializedPolicy})
3028

3129
Describe("Allowing a protocol should implicitly deny other protocols from pods", func() {
3230
It("should not allow TCP", func() {
@@ -125,7 +123,7 @@ spec:
125123
})
126124

127125
Describe("Policy allowing egress to ips", func() {
128-
allowAllOnSCTPSerialized := `
126+
policyYaml := `
129127
apiVersion: networking.k8s.io/v1
130128
kind: NetworkPolicy
131129
metadata:
@@ -149,8 +147,7 @@ spec:
149147
pod: a
150148
policyTypes:
151149
- Egress`
152-
var kubePolicy *networkingv1.NetworkPolicy
153-
err := yaml.Unmarshal([]byte(allowAllOnSCTPSerialized), &kubePolicy)
150+
kubePolicy, err := utils.ParseYaml[networkingv1.NetworkPolicy]([]byte(policyYaml))
154151
utils.DoOrDie(err)
155152
policy := BuildNetworkPolicies(true, []*networkingv1.NetworkPolicy{kubePolicy})
156153

@@ -196,8 +193,7 @@ spec:
196193
pod: a
197194
policyTypes:
198195
- Ingress`
199-
var kubePolicy *networkingv1.NetworkPolicy
200-
err := yaml.Unmarshal([]byte(policyYaml), &kubePolicy)
196+
kubePolicy, err := utils.ParseYaml[networkingv1.NetworkPolicy]([]byte(policyYaml))
201197
utils.DoOrDie(err)
202198
policy := BuildNetworkPolicies(true, []*networkingv1.NetworkPolicy{kubePolicy})
203199

pkg/recipes/recipe.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
v1 "k8s.io/api/core/v1"
1010
networkingv1 "k8s.io/api/networking/v1"
1111
"k8s.io/apimachinery/pkg/util/intstr"
12-
"sigs.k8s.io/yaml"
1312
)
1413

1514
type Recipe struct {
@@ -22,10 +21,9 @@ type Recipe struct {
2221
func (r *Recipe) Policies() []*networkingv1.NetworkPolicy {
2322
var policies []*networkingv1.NetworkPolicy
2423
for _, yamlString := range r.PolicyYamls {
25-
netpol := networkingv1.NetworkPolicy{}
26-
err := yaml.Unmarshal([]byte(yamlString), &netpol)
24+
netpol, err := utils.ParseYaml[networkingv1.NetworkPolicy]([]byte(yamlString))
2725
utils.DoOrDie(err)
28-
policies = append(policies, &netpol)
26+
policies = append(policies, netpol)
2927
}
3028
return policies
3129
}

0 commit comments

Comments
 (0)