/
gather_machine_configs.go
81 lines (75 loc) · 2.3 KB
/
gather_machine_configs.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
package clusterconfig
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
"github.com/openshift/insights-operator/pkg/record"
)
// GatherMachineConfigs Collects `MachineConfigs` definitions. Following data is intentionally removed from the definitions:
// - `spec.config.storage.files`
// - `spec.config.passwd.users`
//
// ### API Reference
// - https://docs.openshift.com/container-platform/4.7/rest_api/machine_apis/machineconfig-machineconfiguration-openshift-io-v1.html
//
// ### Sample data
// - docs/insights-archive-sample/config/machineconfigs/75-worker-sap-data-intelligence.json
//
// ### Location in archive
// - `config/machineconfigs/{resource}.json`
//
// ### Config ID
// `clusterconfig/machine_configs`
//
// ### Released version
// - 4.9.0
//
// ### Backported versions
// - 4.8.5
//
// ### Changes
// None
func (g *Gatherer) GatherMachineConfigs(ctx context.Context) ([]record.Record, []error) {
gatherDynamicClient, err := dynamic.NewForConfig(g.gatherKubeConfig)
if err != nil {
return nil, []error{err}
}
return gatherMachineConfigs(ctx, gatherDynamicClient)
}
func gatherMachineConfigs(ctx context.Context, dynamicClient dynamic.Interface) ([]record.Record, []error) {
mcList, err := dynamicClient.Resource(machineConfigGroupVersionResource).List(ctx, metav1.ListOptions{})
if errors.IsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, []error{err}
}
records := []record.Record{}
var errs []error
for i := range mcList.Items {
mc := mcList.Items[i]
// remove the sensitive content by overwriting the values
err := unstructured.SetNestedField(mc.Object, nil, "spec", "config", "storage", "files")
if err != nil {
klog.Errorf("unable to set nested field: %v", err)
errs = append(errs, err)
}
err = unstructured.SetNestedField(mc.Object, nil, "spec", "config", "passwd", "users")
if err != nil {
klog.Errorf("unable to set nested field: %v", err)
errs = append(errs, err)
}
records = append(records, record.Record{
Name: fmt.Sprintf("config/machineconfigs/%s", mc.GetName()),
Item: record.ResourceMarshaller{Resource: &mc},
})
}
if len(errs) > 0 {
return records, errs
}
return records, nil
}