Skip to content

Commit 723cf26

Browse files
darestemrajagopal
andauthored
feat: add job to collect nginx-specific CRDs (#7)
Co-authored-by: Madhu Rajagopal <[email protected]>
1 parent cfdba36 commit 723cf26

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

pkg/crds/crd.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package crds
2+
3+
type Crd struct {
4+
Resource string
5+
Group string
6+
Version string
7+
}
8+
9+
func GetCRDList() []Crd {
10+
crdList := []Crd{
11+
{
12+
Resource: "apdoslogconfs",
13+
Group: "appprotectdos.f5.com",
14+
Version: "v1beta1",
15+
},
16+
{
17+
Resource: "apdospolicies",
18+
Group: "appprotectdos.f5.com",
19+
Version: "v1beta1",
20+
},
21+
{
22+
Resource: "dosprotectedresources",
23+
Group: "appprotectdos.f5.com",
24+
Version: "v1beta1",
25+
},
26+
{
27+
Resource: "aplogconfs",
28+
Group: "appprotect.f5.com",
29+
Version: "v1beta1",
30+
},
31+
{
32+
Resource: "appolicies",
33+
Group: "appprotect.f5.com",
34+
Version: "v1beta1",
35+
},
36+
{
37+
Resource: "apusersigs",
38+
Group: "appprotect.f5.com",
39+
Version: "v1beta1",
40+
},
41+
{
42+
Resource: "globalconfigurations",
43+
Group: "k8s.nginx.org",
44+
Version: "v1",
45+
},
46+
{
47+
Resource: "policies",
48+
Group: "k8s.nginx.org",
49+
Version: "v1",
50+
},
51+
{
52+
Resource: "transportservers",
53+
Group: "k8s.nginx.org",
54+
Version: "v1",
55+
},
56+
{
57+
Resource: "virtualserverroutes",
58+
Group: "k8s.nginx.org",
59+
Version: "v1",
60+
},
61+
{
62+
Resource: "virtualservers",
63+
Group: "k8s.nginx.org",
64+
Version: "v1",
65+
},
66+
{
67+
Resource: "cosarara",
68+
Group: "k8s.nginx.org",
69+
Version: "v1",
70+
},
71+
}
72+
return crdList
73+
}
74+

pkg/data_collector/data_collector.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"context"
88
"fmt"
99
helmClient "github.com/mittwald/go-helm-client"
10+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/crds"
1011
"io"
1112
corev1 "k8s.io/api/core/v1"
1213
crdClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
14+
"k8s.io/apimachinery/pkg/runtime/schema"
1315
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416
"k8s.io/client-go/kubernetes"
1517
"k8s.io/client-go/kubernetes/scheme"
@@ -211,6 +213,28 @@ func (c *DataCollector) PodExecutor(namespace string, pod string, command []stri
211213
}
212214
}
213215

216+
func (c *DataCollector) QueryCRD(crd crds.Crd, namespace string, ctx context.Context) ([]byte, error) {
217+
218+
schemeGroupVersion := schema.GroupVersion{Group: crd.Group, Version: crd.Version}
219+
negotiatedSerializer := scheme.Codecs.WithoutConversion()
220+
c.K8sRestConfig.APIPath = "apis"
221+
c.K8sRestConfig.GroupVersion = &schemeGroupVersion
222+
c.K8sRestConfig.NegotiatedSerializer = negotiatedSerializer
223+
224+
client, err := rest.RESTClientFor(c.K8sRestConfig)
225+
226+
if err != nil {
227+
return nil, err
228+
}
229+
230+
result := client.Get().
231+
Namespace(namespace). // Specify the namespace if needed
232+
Resource(crd.Resource).
233+
Do(ctx)
234+
235+
return result.Raw()
236+
}
237+
214238
func (c *DataCollector) AllNamespacesExist() bool {
215239
var allExist = true
216240
for _, namespace := range c.Namespaces {

pkg/jobs/job_list.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/crds"
89
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
910
"io"
1011
corev1 "k8s.io/api/core/v1"
@@ -415,6 +416,26 @@ func JobList() []Job {
415416
ch <- jobResult
416417
},
417418
},
419+
{
420+
Name: "crd-objects",
421+
Timeout: time.Second * 10,
422+
Execute: func(dc *data_collector.DataCollector, ctx context.Context, ch chan JobResult) {
423+
jobResult := JobResult{Files: make(map[string][]byte), Error: nil}
424+
for _, namespace := range dc.Namespaces {
425+
for _, crd := range crds.GetCRDList() {
426+
result, err := dc.QueryCRD(crd, namespace, ctx)
427+
if err != nil {
428+
dc.Logger.Printf("\tCRD %s.%s/%s could not be collected in namespace %s: %v\n", crd.Resource, crd.Group, crd.Version, namespace, err)
429+
} else {
430+
var jsonResult bytes.Buffer
431+
_ = json.Indent(&jsonResult, result, "", " ")
432+
jobResult.Files[path.Join(dc.BaseDir, "crds", namespace, crd.Resource+".json")] = jsonResult.Bytes()
433+
}
434+
}
435+
}
436+
ch <- jobResult
437+
},
438+
},
418439
}
419440
return jobList
420441
}

0 commit comments

Comments
 (0)