Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgresCluster names and namespaces for support #75

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

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 {
tony-landreth marked this conversation as resolved.
Show resolved Hide resolved
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