Skip to content

Commit

Permalink
PostgresCluster names and namespaces for support
Browse files Browse the repository at this point in the history
Adds all reachable postgrescluster names and their
namespaces to support export.

Issue: PGO-272
  • Loading branch information
Anthony Landreth committed Oct 30, 2023
1 parent a7009c6 commit 92536d3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/content/reference/pgo_support_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ kubectl pgo support export daisy --monitoring-namespace another-namespace --outp
| Note: No data or k8s secrets are collected.
└────────────────────────────────────────────────────────────────
Collecting PGO CLI version...
Collecting names and namespaces for PostgresClusters...
Collecting current Kubernetes context...
Collecting Kubernetes version...
Collecting nodes...
Expand Down
49 changes: 49 additions & 0 deletions internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -147,6 +148,17 @@ var otherNamespacedResources = []schema.GroupVersionResource{{
Resource: "limitranges",
}}

type PostgresClusterList struct {
Clusters []PostgresClusterItem `json:"items"`
}
type PostgresClusterItem struct {
Metadata PostgresClusterMetadata `json:"metadata"`
}
type PostgresClusterMetadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}

// newSupportCommand returns the support subcommand of the PGO plugin.
func newSupportExportCommand(config *internal.Config) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -234,6 +246,7 @@ kubectl pgo support export daisy --monitoring-namespace another-namespace --outp
| Note: No data or k8s secrets are collected.
└────────────────────────────────────────────────────────────────
Collecting PGO CLI version...
Collecting names and namespaces for PostgresClusters...
Collecting current Kubernetes context...
Collecting Kubernetes version...
Collecting nodes...
Expand Down Expand Up @@ -355,6 +368,10 @@ Collecting PGO CLI logs...
// PGO CLI version
err = gatherPGOCLIVersion(ctx, clusterName, tw, cmd)

if err == nil {
err = gatherPostgresClusterNames(clusterName, ctx, cmd, tw, clientset)
}

// Current Kubernetes context
if err == nil {
err = gatherKubeContext(ctx, config, clusterName, tw, cmd)
Expand Down Expand Up @@ -484,6 +501,38 @@ func gatherPGOCLIVersion(_ context.Context,
return nil
}

func gatherPostgresClusterNames(clusterName string, ctx context.Context, cmd *cobra.Command, tw *tar.Writer, clientset *kubernetes.Clientset) error {
restclient := clientset.CoreV1().RESTClient()
result := restclient.Get().AbsPath("apis/postgres-operator.crunchydata.com/v1beta1/").Resource("postgresclusters").Do(ctx)

if err := result.Error(); err != nil {
return err
}

raw, err := result.Raw()
if err != nil {
return err
}

var list PostgresClusterList
err = json.Unmarshal(raw, &list)
if err != nil {
return err
}

data := []byte{}
for _, item := range list.Clusters {
data = append(data, []byte("Namespace: "+item.Metadata.Namespace+"\t"+"Cluster: "+item.Metadata.Name+"\n")...)
}

path := clusterName + "/cluster-names"
if err := writeTar(tw, data, path, cmd); err != nil {
return err
}

return nil
}

// gatherKubeContext collects the current Kubernetes context
func gatherKubeContext(_ context.Context,
config *internal.Config,
Expand Down
9 changes: 8 additions & 1 deletion testing/kuttl/e2e/support-export/01--support_export.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ commands:
exit 1
}
# check that the cluster-names file exist and is not empty
if [[ ! -s ./kuttl-support-cluster/cluster-names ]]
then
echo "Expected cluster-names file to not be empty"
eval "$CLEANUP"
exit 1
fi
# check that the context file exist and is not empty
if [[ ! -s ./kuttl-support-cluster/current-context ]]
then
Expand All @@ -26,7 +34,6 @@ commands:
exit 1
fi
# check for expected gzip compression level
FILE_INFO=$(file ./crunchy_k8s_support_export_*.tar.gz)
[[ "${FILE_INFO}" == *'gzip compressed data, max compression'* ]] || {
Expand Down

0 comments on commit 92536d3

Please sign in to comment.