1
- package cli
1
+ package kube
2
2
3
3
import (
4
- "github.com/mattfenwick/cyclonus/pkg/kube"
4
+ "os"
5
+ "path/filepath"
6
+
7
+ "github.com/mattfenwick/collections/pkg/builtin"
8
+ "github.com/mattfenwick/collections/pkg/slice"
5
9
"github.com/mattfenwick/cyclonus/pkg/utils"
6
10
"github.com/pkg/errors"
7
11
log "github.com/sirupsen/logrus"
8
12
networkingv1 "k8s.io/api/networking/v1"
9
- "os"
10
- "path/filepath"
11
13
)
12
14
13
- func readPoliciesFromPath (policyPath string ) ([]* networkingv1.NetworkPolicy , error ) {
15
+ func ReadNetworkPoliciesFromPath (policyPath string ) ([]* networkingv1.NetworkPolicy , error ) {
14
16
var allPolicies []* networkingv1.NetworkPolicy
15
17
err := filepath .Walk (policyPath , func (path string , info os.FileInfo , err error ) error {
16
18
if err != nil {
@@ -26,15 +28,27 @@ func readPoliciesFromPath(policyPath string) ([]*networkingv1.NetworkPolicy, err
26
28
return err
27
29
}
28
30
29
- // try parsing a list first
30
- policies , err := utils.ParseYaml [[]* networkingv1.NetworkPolicy ](bytes )
31
+ // TODO try parsing plain yaml list (that is: not a NetworkPolicyList)
32
+ // policies, err := utils.ParseYaml[[]*networkingv1.NetworkPolicy](bytes)
33
+
34
+ // TODO try parsing multiple policies separated by '---' lines
35
+ // policies, err := yaml.ParseMany[networkingv1.NetworkPolicy](bytes)
36
+ // if err == nil {
37
+ // log.Debugf("parsed %d policies from %s", len(policies), path)
38
+ // allPolicies = append(allPolicies, refNetpolList(policies)...)
39
+ // return nil
40
+ // }
41
+ // log.Errorf("unable to parse multiple policies separated by '---' lines: %+v", err)
42
+
43
+ // try parsing a NetworkPolicyList
44
+ policyList , err := utils.ParseYamlStrict [networkingv1.NetworkPolicyList ](bytes )
31
45
if err == nil {
32
- log .Debugf ("parsed %d policies from %s" , len (* policies ), path )
33
- allPolicies = append (allPolicies , * policies ... )
46
+ allPolicies = append (allPolicies , refNetpolList (policyList .Items )... )
34
47
return nil
35
48
}
36
49
37
- log .Debugf ("failed to parse list from %s, falling back to parsing single policy" , path )
50
+ log .Debugf ("unable to parse list of policies: %+v" , err )
51
+
38
52
policy , err := utils.ParseYamlStrict [networkingv1.NetworkPolicy ](bytes )
39
53
if err != nil {
40
54
return errors .WithMessagef (err , "unable to parse single policy from yaml at %s" , path )
@@ -56,18 +70,14 @@ func readPoliciesFromPath(policyPath string) ([]*networkingv1.NetworkPolicy, err
56
70
return allPolicies , nil
57
71
}
58
72
59
- func readPoliciesFromKube (kubeClient * kube. Kubernetes , namespaces []string ) ([]* networkingv1.NetworkPolicy , error ) {
60
- netpols , err := kube . GetNetworkPoliciesInNamespaces (kubeClient , namespaces )
73
+ func ReadNetworkPoliciesFromKube (kubeClient * Kubernetes , namespaces []string ) ([]* networkingv1.NetworkPolicy , error ) {
74
+ netpols , err := GetNetworkPoliciesInNamespaces (kubeClient , namespaces )
61
75
if err != nil {
62
76
return nil , err
63
77
}
64
78
return refNetpolList (netpols ), nil
65
79
}
66
80
67
81
func refNetpolList (refs []networkingv1.NetworkPolicy ) []* networkingv1.NetworkPolicy {
68
- policies := make ([]* networkingv1.NetworkPolicy , len (refs ))
69
- for i := 0 ; i < len (refs ); i ++ {
70
- policies [i ] = & refs [i ]
71
- }
72
- return policies
82
+ return slice .Map (builtin .Reference [networkingv1 .NetworkPolicy ], refs )
73
83
}
0 commit comments