Skip to content

Commit 8675cfa

Browse files
committed
getting CRDs
Remove the `kubectl describe crds` and replace with getting the CRDs via the correct APIs. This brings CRDs into the support export output on the same level as pods, sts, etc.
1 parent 27ce4b6 commit 8675cfa

File tree

1 file changed

+69
-24
lines changed

1 file changed

+69
-24
lines changed

internal/cmd/export.go

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
networkingv1 "k8s.io/api/networking/v1"
2828
policyv1 "k8s.io/api/policy/v1"
2929
policyv1beta1 "k8s.io/api/policy/v1beta1"
30+
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
3031
apierrors "k8s.io/apimachinery/pkg/api/errors"
3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -341,6 +342,11 @@ Collecting PGO CLI logs...
341342
return err
342343
}
343344

345+
apiExtensionClientSet, err := apiextensionsclientset.NewForConfig(restConfig)
346+
if err != nil {
347+
return err
348+
}
349+
344350
discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig)
345351
if err != nil {
346352
return err
@@ -456,6 +462,12 @@ Collecting PGO CLI logs...
456462
writeInfo(cmd, fmt.Sprintf("Error gathering Namespaced API Resources: %s", err))
457463
}
458464

465+
// Gather CRDs
466+
err = gatherCrds(ctx, apiExtensionClientSet, clusterName, tw, cmd)
467+
if err != nil {
468+
writeInfo(cmd, fmt.Sprintf("Error gathering CRDs: %s", err))
469+
}
470+
459471
// Gather Events
460472
err = gatherEvents(ctx, clientset, namespace, clusterName, tw, cmd)
461473
if err != nil {
@@ -594,30 +606,6 @@ Collecting PGO CLI logs...
594606
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe postgrescluster: %s", err))
595607
}
596608

597-
writeInfo(cmd, "Running kubectl describe crd crunchybridgeclusters...")
598-
err = runKubectlCommand(tw, cmd, clusterName+"/describe/crds/crunchybridgeclusters", "describe", "crds", "crunchybridgeclusters")
599-
if err != nil {
600-
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe crd crunchybridgeclusters: %s", err))
601-
}
602-
603-
writeInfo(cmd, "Running kubectl describe crd pgadmins...")
604-
err = runKubectlCommand(tw, cmd, clusterName+"/describe/crds/pgadmins", "describe", "crds", "pgadmins")
605-
if err != nil {
606-
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe crd pgadmins: %s", err))
607-
}
608-
609-
writeInfo(cmd, "Running kubectl describe crd pgupgrades...")
610-
err = runKubectlCommand(tw, cmd, clusterName+"/describe/crds/pgupgrades", "describe", "crds", "pgupgrades")
611-
if err != nil {
612-
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe crd pgupgrades: %s", err))
613-
}
614-
615-
writeInfo(cmd, "Running kubectl describe crd postgresclusters...")
616-
err = runKubectlCommand(tw, cmd, clusterName+"/describe/crds/postgresclusters", "describe", "crds", "postgresclusters")
617-
if err != nil {
618-
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe crd postgresclusters: %s", err))
619-
}
620-
621609
writeInfo(cmd, "Running kubectl describe clusterrole...")
622610
err = runKubectlCommand(tw, cmd, clusterName+"/describe/clusterrole", "describe", "clusterrole", "postgres-operator")
623611
if err != nil {
@@ -1039,6 +1027,63 @@ func gatherNamespacedAPIResources(ctx context.Context,
10391027
return nil
10401028
}
10411029

1030+
// gatherCrds gathers all the CRDs with a name=pgo label
1031+
func gatherCrds(ctx context.Context,
1032+
clientset *apiextensionsclientset.Clientset,
1033+
clusterName string,
1034+
tw *tar.Writer,
1035+
cmd *cobra.Command,
1036+
) error {
1037+
writeInfo(cmd, "Collecting events...")
1038+
// list, err := clientset.CoreV1().Events(namespace).List(ctx, metav1.ListOptions{})
1039+
1040+
labelSelector := "app.kubernetes.io/name=pgo"
1041+
crds, err := clientset.ApiextensionsV1().CustomResourceDefinitions().List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
1042+
1043+
if err != nil {
1044+
if apierrors.IsForbidden(err) {
1045+
writeInfo(cmd, err.Error())
1046+
return nil
1047+
}
1048+
return err
1049+
}
1050+
1051+
if len(crds.Items) == 0 {
1052+
// If we didn't find any resources, skip
1053+
writeInfo(cmd, fmt.Sprintf("Resource CRDs not found, skipping"))
1054+
return nil
1055+
}
1056+
1057+
// Create a buffer to generate string with the table formatted list
1058+
var buf bytes.Buffer
1059+
if err := printers.NewTablePrinter(printers.PrintOptions{}).
1060+
PrintObj(crds, &buf); err != nil {
1061+
return err
1062+
}
1063+
1064+
// Define the file name/path where the list file will be created and
1065+
// write to the tar
1066+
path := clusterName + "/" + "crds" + "/list"
1067+
if err := writeTar(tw, buf.Bytes(), path, cmd); err != nil {
1068+
return err
1069+
}
1070+
1071+
for _, obj := range crds.Items {
1072+
b, err := yaml.Marshal(obj)
1073+
if err != nil {
1074+
return err
1075+
}
1076+
1077+
path := clusterName + "/" + "crds" + "/" + obj.GetName() + ".yaml"
1078+
if err := writeTar(tw, b, path, cmd); err != nil {
1079+
return err
1080+
}
1081+
}
1082+
1083+
return nil
1084+
1085+
}
1086+
10421087
// gatherEvents gathers all events from a namespace, selects information (based on
10431088
// what kubectl outputs), formats the data then prints to the tar file
10441089
func gatherEvents(ctx context.Context,

0 commit comments

Comments
 (0)