@@ -6,10 +6,13 @@ import (
6
6
7
7
"k8s.io/apimachinery/pkg/api/errors"
8
8
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9
10
"k8s.io/apimachinery/pkg/runtime/schema"
10
11
"k8s.io/client-go/dynamic"
12
+ "k8s.io/klog/v2"
11
13
12
14
"github.com/openshift/insights-operator/pkg/record"
15
+ "github.com/openshift/insights-operator/pkg/utils/anonymize"
13
16
)
14
17
15
18
// GatherMachineSet collects MachineSet information
@@ -52,9 +55,66 @@ func gatherMachineSet(ctx context.Context, dynamicClient dynamic.Interface) ([]r
52
55
}
53
56
records = append (records , record.Record {
54
57
Name : recordName ,
55
- Item : record.ResourceMarshaller {Resource : & machineSets .Items [i ]},
58
+ Item : record.ResourceMarshaller {Resource : anonymizeMachineset ( & machineSets .Items [i ]) },
56
59
})
57
60
}
58
61
59
62
return records , nil
60
63
}
64
+
65
+ func anonymizeMachineset (data * unstructured.Unstructured ) * unstructured.Unstructured {
66
+ fieldsToAnonymize := [][]string {
67
+ {"spec" , "template" , "spec" , "providerSpec" , "value" , "projectID" },
68
+ {"spec" , "template" , "spec" , "providerSpec" , "value" , "region" },
69
+ {"spec" , "template" , "spec" , "providerSpec" , "value" , "placement" , "availabilityZone" },
70
+ {"spec" , "template" , "spec" , "providerSpec" , "value" , "placement" , "region" },
71
+ }
72
+
73
+ for _ , fieldToAnonymize := range fieldsToAnonymize {
74
+ err := anonymize .UnstructuredNestedStringField (data .Object , fieldToAnonymize ... )
75
+ if err != nil {
76
+ klog .Infof ("error during anonymizing machineset: %v" , err )
77
+ }
78
+ }
79
+
80
+ return anonymizeServiceAccounts (data )
81
+ }
82
+
83
+ func anonymizeServiceAccounts (data * unstructured.Unstructured ) * unstructured.Unstructured {
84
+ serviceAccounts , found , err := unstructured .NestedSlice (
85
+ data .Object , "spec" , "template" , "spec" , "providerSpec" , "value" , "serviceAccounts" ,
86
+ )
87
+ if ! found || err != nil {
88
+ klog .Infof ("error during anonymizing machineset: unable to find service accounts %v %v" , found , err )
89
+ return data
90
+ }
91
+
92
+ for i := range serviceAccounts {
93
+ serviceAccount , ok := serviceAccounts [i ].(map [string ]interface {})
94
+ if ! ok {
95
+ klog .Infof ("error during anonymizing machineset: service account is not a map" )
96
+ continue
97
+ }
98
+
99
+ emailI , found := serviceAccount ["email" ]
100
+ if ! found {
101
+ klog .Infof ("error during anonymizing machineset: email was not found in service account map" )
102
+ continue
103
+ }
104
+
105
+ email , ok := emailI .(string )
106
+ if ! ok {
107
+ klog .Infof ("error during anonymizing machineset: email was not a string" )
108
+ continue
109
+ }
110
+
111
+ serviceAccount ["email" ] = anonymize .String (email )
112
+ }
113
+
114
+ err = unstructured .SetNestedSlice (data .Object , serviceAccounts , "spec" , "template" , "spec" , "providerSpec" , "value" , "serviceAccounts" )
115
+ if err != nil {
116
+ klog .Infof ("error during anonymizing machineset: unable to set anonymized service accounts: %v" , err .Error ())
117
+ }
118
+
119
+ return data
120
+ }
0 commit comments