/
gather_image_pruners.go
72 lines (66 loc) · 2.2 KB
/
gather_image_pruners.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
package clusterconfig
import (
"context"
"fmt"
"strings"
imageregistryv1client "github.com/openshift/client-go/imageregistry/clientset/versioned"
imageregistryv1 "github.com/openshift/client-go/imageregistry/clientset/versioned/typed/imageregistry/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"github.com/openshift/insights-operator/pkg/record"
)
// GatherClusterImagePruner Collects the image pruner configuration.
//
// ### API Reference
// None
//
// ### Sample data
// - docs/insights-archive-sample/clusteroperator/imageregistry.operator.openshift.io/imagepruner/cluster.json
//
// ### Location in archive
// | Version | Path |
// | --------- | ----------------------------------------------------------------------------------- |
// | < 4.6.0 | config/imagepruner.json |
// | >= 4.6.0 | config/clusteroperator/{group}/{kind}/{name}.json |
//
// ### Config ID
// `clusterconfig/image_pruners`
//
// ### Released version
// - 4.5.0
//
// ### Backported versions
// None
//
// ### Changes
// None
func (g *Gatherer) GatherClusterImagePruner(ctx context.Context) ([]record.Record, []error) {
registryClient, err := imageregistryv1client.NewForConfig(g.gatherKubeConfig)
if err != nil {
return nil, []error{err}
}
return gatherClusterImagePruner(ctx, registryClient.ImageregistryV1())
}
func gatherClusterImagePruner(ctx context.Context, registryClient imageregistryv1.ImageregistryV1Interface) ([]record.Record, []error) {
pruner, err := registryClient.ImagePruners().Get(ctx, "cluster", metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, []error{err}
}
// TypeMeta is empty - see https://github.com/kubernetes/kubernetes/issues/3030
kinds, _, err := registryScheme.ObjectKinds(pruner)
if err != nil {
return nil, []error{err}
}
if len(kinds) > 1 {
klog.Warningf("More kinds for image registry pruner operator resource %s", kinds)
}
objKind := kinds[0]
return []record.Record{{
Name: fmt.Sprintf("config/clusteroperator/%s/%s/%s", objKind.Group, strings.ToLower(objKind.Kind), pruner.Name),
Item: record.ResourceMarshaller{Resource: pruner},
}}, nil
}