Skip to content

Commit 65d80b9

Browse files
authored
Merge pull request #265 from jetstack/open-shift-route-filter
2 parents 42c7cca + dbb8300 commit 65d80b9

File tree

3 files changed

+121
-18
lines changed

3 files changed

+121
-18
lines changed

pkg/datagatherer/k8s/dynamic.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/jetstack/preflight/api"
11-
"github.com/jetstack/preflight/pkg/datagatherer"
1210
"github.com/pkg/errors"
1311
"github.com/pmylund/go-cache"
1412
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -19,6 +17,9 @@ import (
1917
"k8s.io/client-go/dynamic/dynamicinformer"
2018
"k8s.io/client-go/kubernetes/scheme"
2119
k8scache "k8s.io/client-go/tools/cache"
20+
21+
"github.com/jetstack/preflight/api"
22+
"github.com/jetstack/preflight/pkg/datagatherer"
2223
)
2324

2425
// ConfigDynamic contains the configuration for the data-gatherer.
@@ -274,16 +275,16 @@ func redactList(list []*api.GatheredResource) error {
274275

275276
resource := item
276277

278+
// Redact item if it is a:
277279
for _, gvk := range gvks {
278-
// If this item is a Secret then we need to redact it.
280+
// secret object
279281
if gvk.Kind == "Secret" && (gvk.Group == "core" || gvk.Group == "") {
280282
Select(SecretSelectedFields, resource)
281283

282-
// break when the object has been processed as a secret, no
283-
// other kinds have redact modifications
284-
break
284+
// route object
285+
} else if gvk.Kind == "Route" && gvk.Group == "route.openshift.io" {
286+
Select(RouteSelectedFields, resource)
285287
}
286-
287288
}
288289

289290
// remove managedFields from all resources

pkg/datagatherer/k8s/fieldfilter.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ var SecretSelectedFields = []string{
2525
"/data/ca.crt",
2626
}
2727

28+
// RouteSelectedFields is the list of fields sent from OpenShift Route objects to the
29+
// backend
30+
var RouteSelectedFields = []string{
31+
"kind",
32+
"apiVersion",
33+
"metadata.annotations",
34+
"metadata.name",
35+
"metadata.namespace",
36+
"metadata.ownerReferences",
37+
"metadata.selfLink",
38+
"metadata.uid",
39+
"spec.host",
40+
"spec.to.kind",
41+
"spec.to.port",
42+
"spec.to.name",
43+
"spec.to.weight",
44+
"spec.tls.termination",
45+
"spec.tls.certificate",
46+
"spec.tls.caCertificate",
47+
"spec.tls.destinationCACertificate",
48+
"spec.tls.insecureEdgeTerminationPolicy",
49+
"spec.wildcardPolicy",
50+
"status",
51+
}
52+
2853
// RedactFields are removed from all objects
2954
var RedactFields = []string{
3055
"metadata.managedFields",

pkg/datagatherer/k8s/fieldfilter_test.go

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
)
99

1010
func TestSelect(t *testing.T) {
11-
resource := &unstructured.Unstructured{
11+
// secret objects
12+
secretResource := &unstructured.Unstructured{
1213
Object: map[string]interface{}{
1314
"apiVersion": "v1",
1415
"kind": "Secret",
@@ -27,7 +28,7 @@ func TestSelect(t *testing.T) {
2728
},
2829
}
2930

30-
fieldsToSelect := []string{
31+
secretFieldsToSelect := []string{
3132
"apiVersion",
3233
"kind",
3334
"metadata.name",
@@ -36,13 +37,7 @@ func TestSelect(t *testing.T) {
3637
"/data/tls.crt",
3738
}
3839

39-
err := Select(fieldsToSelect, resource)
40-
if err != nil {
41-
t.Fatalf("unexpected error: %s", err)
42-
}
43-
44-
bytes, err := json.MarshalIndent(resource, "", " ")
45-
expectedJSON := `{
40+
secretExpectedJSON := `{
4641
"apiVersion": "v1",
4742
"data": {
4843
"tls.crt": "cert data"
@@ -54,8 +49,90 @@ func TestSelect(t *testing.T) {
5449
},
5550
"type": "kubernetes.io/tls"
5651
}`
57-
if string(bytes) != expectedJSON {
58-
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(bytes), expectedJSON)
52+
// route objects
53+
routeResource := &unstructured.Unstructured{
54+
Object: map[string]interface{}{
55+
"apiVersion": "v1",
56+
"kind": "Route",
57+
"metadata": map[string]interface{}{
58+
"name": "example",
59+
"annotations": map[string]string{
60+
"kubectl.kubernetes.io/last-applied-configuration": "secret",
61+
},
62+
},
63+
"spec": map[string]interface{}{
64+
"host": "www.example.com",
65+
"to": map[string]string{
66+
"kind": "Service",
67+
"name": "frontend",
68+
},
69+
"tls": map[string]interface{}{
70+
"termination": "reencrypt",
71+
"key": "secret",
72+
"certificate": "cert data",
73+
"caCertificate": "caCert data",
74+
"destinationCACertificate": "destinationCaCert data",
75+
},
76+
},
77+
},
78+
}
79+
80+
routeFieldsToSelect := []string{
81+
"apiVersion",
82+
"kind",
83+
"metadata.name",
84+
"spec.host",
85+
"spec.to.kind",
86+
"spec.to.name",
87+
"spec.tls.termination",
88+
"spec.tls.certificate",
89+
"spec.tls.caCertificate",
90+
"spec.tls.destinationCACertificate",
91+
}
92+
93+
routeExpectedJSON := `{
94+
"apiVersion": "v1",
95+
"kind": "Route",
96+
"metadata": {
97+
"name": "example"
98+
},
99+
"spec": {
100+
"host": "www.example.com",
101+
"tls": {
102+
"caCertificate": "caCert data",
103+
"certificate": "cert data",
104+
"destinationCACertificate": "destinationCaCert data",
105+
"termination": "reencrypt"
106+
},
107+
"to": {
108+
"kind": "Service",
109+
"name": "frontend"
110+
}
111+
}
112+
}`
113+
114+
tests := map[string]struct {
115+
resource *unstructured.Unstructured
116+
fieldsToSelect []string
117+
expectedJSON string
118+
}{
119+
"secret": {secretResource, secretFieldsToSelect, secretExpectedJSON},
120+
"route": {routeResource, routeFieldsToSelect, routeExpectedJSON},
121+
}
122+
123+
for name, test := range tests {
124+
err := Select(test.fieldsToSelect, test.resource)
125+
if err != nil {
126+
t.Fatalf("unexpected error: %s", err)
127+
}
128+
129+
bytes, err := json.MarshalIndent(test.resource, "", " ")
130+
131+
t.Run(name, func(t *testing.T) {
132+
if string(bytes) != test.expectedJSON {
133+
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(bytes), test.expectedJSON)
134+
}
135+
})
59136
}
60137
}
61138

0 commit comments

Comments
 (0)