Skip to content

Commit 58392fc

Browse files
committed
feat: Implement CRD client to fetch custom resources
1 parent 64ee657 commit 58392fc

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
gopkg.in/yaml.v3 v3.0.1 // indirect
3838
k8s.io/api v0.29.2 // indirect
3939
k8s.io/apimachinery v0.29.2 // indirect
40+
k8s.io/apiextensions-apiserver v0.29.2
4041
k8s.io/klog/v2 v2.110.1 // indirect
4142
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
4243
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
138138
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
139139
k8s.io/api v0.29.2 h1:hBC7B9+MU+ptchxEqTNW2DkUosJpp1P+Wn6YncZ474A=
140140
k8s.io/api v0.29.2/go.mod h1:sdIaaKuU7P44aoyyLlikSLayT6Vb7bvJNCX105xZXY0=
141+
k8s.io/apiextensions-apiserver v0.29.2 h1:UK3xB5lOWSnhaCk0RFZ0LUacPZz9RY4wi/yt2Iu+btg=
142+
k8s.io/apiextensions-apiserver v0.29.2/go.mod h1:aLfYjpA5p3OwtqNXQFkhJ56TB+spV8Gc4wfMhUA3/b8=
141143
k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8=
142144
k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
143145
k8s.io/client-go v0.29.2 h1:FEg85el1TeZp+/vYJM7hkDlSTFZ+c5nnK44DJ4FyoRg=

internal/jobs/job.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jobs
22

33
import (
44
"context"
5+
crdClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
56
"k8s.io/client-go/kubernetes"
67
"os"
78
"path"
@@ -14,6 +15,12 @@ type Job struct {
1415
RetrieveFunction func(*kubernetes.Clientset, context.Context) []byte
1516
}
1617

18+
type CustomJob struct {
19+
Name string
20+
OutputFile string
21+
RetrieveFunction func(*crdClient.Clientset, context.Context) []byte
22+
}
23+
1724
func (j Job) Collect(baseFolder string, cs *kubernetes.Clientset) {
1825
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
1926
defer cancel()
@@ -26,3 +33,16 @@ func (j Job) Collect(baseFolder string, cs *kubernetes.Clientset) {
2633
defer file.Close()
2734
_, _ = file.Write(result)
2835
}
36+
37+
func (j CustomJob) CustomCollect(baseFolder string, cs *crdClient.Clientset) {
38+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
39+
defer cancel()
40+
result := j.RetrieveFunction(cs, ctx)
41+
42+
fullPathFile := path.Join(baseFolder, j.OutputFile)
43+
os.MkdirAll(path.Dir(fullPathFile), os.ModePerm)
44+
45+
file, _ := os.Create(fullPathFile)
46+
defer file.Close()
47+
_, _ = file.Write(result)
48+
}

internal/jobs/job_list.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jobs
33
import (
44
"context"
55
"encoding/json"
6+
crdClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
67
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
78
"k8s.io/client-go/kubernetes"
89
)
@@ -18,6 +19,24 @@ func K8sJobList() []Job {
1819
return jsonPods
1920
},
2021
},
22+
{
23+
Name: "configmap-list",
24+
OutputFile: "/list/configmaps.json",
25+
RetrieveFunction: func(c *kubernetes.Clientset, ctx context.Context) []byte {
26+
pods, _ := c.CoreV1().ConfigMaps("").List(ctx, v1.ListOptions{})
27+
jsonPods, _ := json.MarshalIndent(pods, "", " ")
28+
return jsonPods
29+
},
30+
},
31+
{
32+
Name: "service-list",
33+
OutputFile: "/list/services.json",
34+
RetrieveFunction: func(c *kubernetes.Clientset, ctx context.Context) []byte {
35+
pods, _ := c.CoreV1().ConfigMaps("").List(ctx, v1.ListOptions{})
36+
jsonPods, _ := json.MarshalIndent(pods, "", " ")
37+
return jsonPods
38+
},
39+
},
2140
{
2241
Name: "server-version",
2342
OutputFile: "/k8s/server_version.json",
@@ -39,3 +58,18 @@ func K8sJobList() []Job {
3958
}
4059
return jobList
4160
}
61+
62+
func K8sCustomJobList() []CustomJob {
63+
jobList := []CustomJob{
64+
{
65+
Name: "crd-list",
66+
OutputFile: "/list/crd.json",
67+
RetrieveFunction: func(c *crdClient.Clientset, ctx context.Context) []byte {
68+
crds, _ := c.ApiextensionsV1().CustomResourceDefinitions().List(ctx, v1.ListOptions{})
69+
jsonCrds, _ := json.MarshalIndent(crds, "", " ")
70+
return jsonCrds
71+
},
72+
},
73+
}
74+
return jobList
75+
}

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"github.com/nginxinc/kubectl-kic-supportpkg/internal/jobs"
6+
crdClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
67
"k8s.io/client-go/kubernetes"
78
"k8s.io/client-go/tools/clientcmd"
89
"k8s.io/client-go/util/homedir"
@@ -32,4 +33,14 @@ func main() {
3233
fmt.Printf("Running %s and collecting the output in %s/%s...\n", job.Name, tmpDir, job.OutputFile)
3334
job.Collect(tmpDir, clientSet)
3435
}
36+
37+
// Create a new clientset for CRDs
38+
crdClientset, err := crdClient.NewForConfig(config)
39+
if err != nil {
40+
panic(err.Error())
41+
}
42+
for _, job := range jobs.K8sCustomJobList() {
43+
fmt.Printf("Running %s and collecting the output in %s/%s...\n", job.Name, tmpDir, job.OutputFile)
44+
job.CustomCollect(tmpDir, crdClientset)
45+
}
3546
}

0 commit comments

Comments
 (0)