Skip to content

Commit e05d222

Browse files
Merge pull request #166 from natiiix/volumesnapshot
Bug 1873101: Gather VolumeSnapshot CRD
2 parents 80024bb + eb0bd60 commit e05d222

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+29764
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
2121
google.golang.org/appengine v1.6.1 // indirect
2222
k8s.io/api v0.17.1
23+
k8s.io/apiextensions-apiserver v0.17.1
2324
k8s.io/apimachinery v0.17.1
2425
k8s.io/client-go v11.0.0+incompatible
2526
k8s.io/component-base v0.17.1

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh
544544
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
545545
k8s.io/api v0.17.1 h1:i46MidoDOE9tvQ0TTEYggf3ka/pziP1+tHI/GFVeJao=
546546
k8s.io/api v0.17.1/go.mod h1:zxiAc5y8Ngn4fmhWUtSxuUlkfz1ixT7j9wESokELzOg=
547+
k8s.io/apiextensions-apiserver v0.17.1 h1:Gw6zQgmKyyNrFMtVpRBNEKE8p35sDBI7Tq1ImxGS+zU=
547548
k8s.io/apiextensions-apiserver v0.17.1/go.mod h1:DRIFH5x3jalE4rE7JP0MQKby9zdYk9lUJQuMmp+M/L0=
548549
k8s.io/apimachinery v0.17.1 h1:zUjS3szTxoUjTDYNvdFkYt2uMEXLcthcbp+7uZvWhYM=
549550
k8s.io/apimachinery v0.17.1/go.mod h1:b9qmWdKlLuU9EBh+06BtLcSf/Mu89rWL33naRxs1uZg=

pkg/controller/operator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"k8s.io/klog"
1010

11+
apixv1beta1client "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
1112
"k8s.io/apimachinery/pkg/runtime"
1213
"k8s.io/apimachinery/pkg/runtime/schema"
1314
"k8s.io/client-go/kubernetes"
@@ -114,6 +115,11 @@ func (s *Support) Run(ctx context.Context, controller *controllercmd.ControllerC
114115
return err
115116
}
116117

118+
crdClient, err := apixv1beta1client.NewForConfig(gatherKubeConfig)
119+
if err != nil {
120+
return err
121+
}
122+
117123
// ensure the insight snapshot directory exists
118124
if _, err := os.Stat(s.StoragePath); err != nil && os.IsNotExist(err) {
119125
if err := os.MkdirAll(s.StoragePath, 0777); err != nil {
@@ -136,7 +142,7 @@ func (s *Support) Run(ctx context.Context, controller *controllercmd.ControllerC
136142

137143
// the gatherers periodically check the state of the cluster and report any
138144
// config to the recorder
139-
configPeriodic := clusterconfig.New(gatherConfigClient, gatherKubeClient.CoreV1(), gatherKubeClient.CertificatesV1beta1(), metricsClient, registryClient.ImageregistryV1())
145+
configPeriodic := clusterconfig.New(gatherConfigClient, gatherKubeClient.CoreV1(), gatherKubeClient.CertificatesV1beta1(), metricsClient, registryClient.ImageregistryV1(), crdClient)
140146
periodic := periodic.New(configObserver, recorder, map[string]gather.Interface{
141147
"config": configPeriodic,
142148
})

pkg/gather/clusterconfig/clusterconfig.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
corev1 "k8s.io/api/core/v1"
19+
apixv1beta1client "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
1920
"k8s.io/apimachinery/pkg/api/errors"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"k8s.io/apimachinery/pkg/runtime"
@@ -88,19 +89,21 @@ type Gatherer struct {
8889
metricsClient rest.Interface
8990
certClient certificatesv1beta1.CertificatesV1beta1Interface
9091
registryClient imageregistryv1.ImageregistryV1Interface
92+
crdClient apixv1beta1client.ApiextensionsV1beta1Interface
9193
lock sync.Mutex
9294
lastVersion *configv1.ClusterVersion
9395
}
9496

9597
// New creates new Gatherer
9698
func New(client configv1client.ConfigV1Interface, coreClient corev1client.CoreV1Interface, certClient certificatesv1beta1.CertificatesV1beta1Interface, metricsClient rest.Interface,
97-
registryClient imageregistryv1.ImageregistryV1Interface) *Gatherer {
99+
registryClient imageregistryv1.ImageregistryV1Interface, crdClient apixv1beta1client.ApiextensionsV1beta1Interface) *Gatherer {
98100
return &Gatherer{
99101
client: client,
100102
coreClient: coreClient,
101103
certClient: certClient,
102104
metricsClient: metricsClient,
103105
registryClient: registryClient,
106+
crdClient: crdClient,
104107
}
105108
}
106109

@@ -126,6 +129,7 @@ func (i *Gatherer) Gather(ctx context.Context, recorder record.Interface) error
126129
GatherClusterIngress(i),
127130
GatherClusterProxy(i),
128131
GatherCertificateSigningRequests(i),
132+
GatherCRD(i),
129133
)
130134
}
131135

@@ -665,6 +669,36 @@ func GatherCertificateSigningRequests(i *Gatherer) func() ([]record.Record, []er
665669
}
666670
}
667671

672+
// GatherCRD collects the specified Custom Resource Definitions.
673+
//
674+
// The following CRDs are gathered:
675+
// - volumesnapshots.snapshot.storage.k8s.io (10745 bytes)
676+
// - volumesnapshotcontents.snapshot.storage.k8s.io (13149 bytes)
677+
//
678+
// The CRD sizes above are in the raw (uncompressed) state.
679+
//
680+
// Location in archive: config/crd/
681+
func GatherCRD(i *Gatherer) func() ([]record.Record, []error) {
682+
return func() ([]record.Record, []error) {
683+
toBeCollected := []string{
684+
"volumesnapshots.snapshot.storage.k8s.io",
685+
"volumesnapshotcontents.snapshot.storage.k8s.io",
686+
}
687+
records := []record.Record{}
688+
for _, crdName := range toBeCollected {
689+
crd, err := i.crdClient.CustomResourceDefinitions().Get(crdName, metav1.GetOptions{})
690+
if err != nil {
691+
return []record.Record{}, []error{err}
692+
}
693+
records = append(records, record.Record{
694+
Name: fmt.Sprintf("config/crd/%s", crd.Name),
695+
Item: record.JSONMarshaller{Object: crd},
696+
})
697+
}
698+
return records, []error{}
699+
}
700+
}
701+
668702
func (i *Gatherer) gatherNamespaceEvents(namespace string) ([]record.Record, []error) {
669703
// do not accidentally collect events for non-openshift namespace
670704
if !strings.HasPrefix(namespace, "openshift-") {

pkg/gather/clusterconfig/clusterconfig_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
imageregistryv1 "github.com/openshift/api/imageregistry/v1"
1313
corev1 "k8s.io/api/core/v1"
14+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
15+
apixv1beta1clientfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
1416
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1517
"k8s.io/apimachinery/pkg/runtime"
1618
kubefake "k8s.io/client-go/kubernetes/fake"
@@ -435,6 +437,47 @@ func TestGatherContainerImages(t *testing.T) {
435437
}
436438
}
437439

440+
func TestCollectVolumeSnapshotCRD(t *testing.T) {
441+
expectedRecords := map[string]v1beta1.CustomResourceDefinition{
442+
"config/crd/volumesnapshots.snapshot.storage.k8s.io": {ObjectMeta: metav1.ObjectMeta{Name: "volumesnapshots.snapshot.storage.k8s.io"}},
443+
"config/crd/volumesnapshotcontents.snapshot.storage.k8s.io": {ObjectMeta: metav1.ObjectMeta{Name: "volumesnapshotcontents.snapshot.storage.k8s.io"}},
444+
}
445+
446+
crdNames := []string{
447+
"unrelated.custom.resource.definition.k8s.io",
448+
"volumesnapshots.snapshot.storage.k8s.io",
449+
"volumesnapshotcontents.snapshot.storage.k8s.io",
450+
"another.irrelevant.custom.resource.definition.k8s.io",
451+
"this.should.not.be.gathered.k8s.io",
452+
}
453+
454+
crdClientset := apixv1beta1clientfake.NewSimpleClientset()
455+
456+
for _, name := range crdNames {
457+
crdClientset.ApiextensionsV1beta1().CustomResourceDefinitions().Create(&v1beta1.CustomResourceDefinition{
458+
ObjectMeta: metav1.ObjectMeta{Name: name},
459+
})
460+
}
461+
462+
gatherer := &Gatherer{crdClient: crdClientset.ApiextensionsV1beta1()}
463+
records, errs := GatherCRD(gatherer)()
464+
if len(errs) != 0 {
465+
t.Fatalf("gather CRDs resulted in error: %#v", errs)
466+
}
467+
468+
if len(records) != len(expectedRecords) {
469+
t.Fatalf("unexpected number of records gathered: %d (expected %d)", len(records), len(expectedRecords))
470+
}
471+
472+
for _, rec := range records {
473+
if expectedItem, ok := expectedRecords[rec.Name]; !ok {
474+
t.Fatalf("unexpected gathered record name: %q", rec.Name)
475+
} else if reflect.DeepEqual(rec.Item, expectedItem) {
476+
t.Fatalf("gathered record %q has different item value than unexpected", rec.Name)
477+
}
478+
}
479+
}
480+
438481
func ExampleGatherMostRecentMetrics_Test() {
439482
b, err := ExampleMostRecentMetrics()
440483
if err != nil {

vendor/k8s.io/apiextensions-apiserver/LICENSE

Lines changed: 202 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)