/
gather_machine_sets.go
130 lines (113 loc) · 3.95 KB
/
gather_machine_sets.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
package clusterconfig
import (
"context"
"fmt"
"github.com/openshift/insights-operator/pkg/record"
"github.com/openshift/insights-operator/pkg/utils/anonymize"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
)
// GatherMachineSet Collects `MachineSet` information.
//
// ### API Reference
// - https://github.com/openshift/api/blob/master/machine/v1beta1/types_machineset.go
// - https://docs.openshift.com/container-platform/4.3/rest_api/index.html#machineset-v1beta1-machine-openshift-io
//
// ### Sample data
// - docs/insights-archive-sample/d50d0126-c90b-4428-a75f-dc08cd02960a-worker-test
//
// ### Location in archive
// - `config//machinesets/{resource}`
//
// ### Config ID
// `clusterconfig/machine_sets`
//
// ### Released version
// - 4.6.0
//
// ### Backported versions
// - 4.4.29+
// - 4.5.15+
//
// ### Changes
// None
func (g *Gatherer) GatherMachineSet(ctx context.Context) ([]record.Record, []error) {
dynamicClient, err := dynamic.NewForConfig(g.gatherKubeConfig)
if err != nil {
return nil, []error{err}
}
return gatherMachineSet(ctx, dynamicClient)
}
func gatherMachineSet(ctx context.Context, dynamicClient dynamic.Interface) ([]record.Record, []error) {
gvr := schema.GroupVersionResource{Group: "machine.openshift.io", Version: "v1beta1", Resource: "machinesets"}
machineSets, err := dynamicClient.Resource(gvr).List(ctx, metav1.ListOptions{})
if errors.IsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, []error{err}
}
var records []record.Record
for i, ms := range machineSets.Items {
recordName := fmt.Sprintf("machinesets/%s", ms.GetName())
if ms.GetNamespace() != "" {
recordName = fmt.Sprintf("machinesets/%s/%s", ms.GetNamespace(), ms.GetName())
}
records = append(records, record.Record{
Name: recordName,
Item: record.ResourceMarshaller{Resource: anonymizeMachineset(&machineSets.Items[i])},
})
}
return records, nil
}
func anonymizeMachineset(data *unstructured.Unstructured) *unstructured.Unstructured {
fieldsToAnonymize := [][]string{
{"spec", "template", "spec", "providerSpec", "value", "projectID"},
{"spec", "template", "spec", "providerSpec", "value", "region"},
{"spec", "template", "spec", "providerSpec", "value", "placement", "availabilityZone"},
{"spec", "template", "spec", "providerSpec", "value", "placement", "region"},
}
for _, fieldToAnonymize := range fieldsToAnonymize {
err := anonymize.UnstructuredNestedStringField(data.Object, fieldToAnonymize...)
if err != nil {
klog.Infof("error during anonymizing machineset: %v", err)
}
}
return anonymizeServiceAccounts(data)
}
func anonymizeServiceAccounts(data *unstructured.Unstructured) *unstructured.Unstructured {
serviceAccounts, found, err := unstructured.NestedSlice(
data.Object, "spec", "template", "spec", "providerSpec", "value", "serviceAccounts",
)
if !found || err != nil {
klog.Infof("error during anonymizing machineset: unable to find service accounts %v %v", found, err)
return data
}
for i := range serviceAccounts {
serviceAccount, ok := serviceAccounts[i].(map[string]interface{})
if !ok {
klog.Infof("error during anonymizing machineset: service account is not a map")
continue
}
emailI, found := serviceAccount["email"]
if !found {
klog.Infof("error during anonymizing machineset: email was not found in service account map")
continue
}
email, ok := emailI.(string)
if !ok {
klog.Infof("error during anonymizing machineset: email was not a string")
continue
}
serviceAccount["email"] = anonymize.String(email)
}
err = unstructured.SetNestedSlice(data.Object, serviceAccounts, "spec", "template", "spec", "providerSpec", "value", "serviceAccounts")
if err != nil {
klog.Infof("error during anonymizing machineset: unable to set anonymized service accounts: %v", err.Error())
}
return data
}