Skip to content

Commit

Permalink
PostgresCluster names and namespaces for support (#75)
Browse files Browse the repository at this point in the history
PostgresCluster names and namespaces for support

Adds all reachable postgrescluster names and their
namespaces to support export.

Issue: PGO-272
  • Loading branch information
tony-landreth authored Oct 30, 2023
1 parent 072ebe2 commit 5ed7bcf
Show file tree
Hide file tree
Showing 3 changed files with 49 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
40 changes: 40 additions & 0 deletions internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,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 @@ -315,6 +316,7 @@ Collecting PGO CLI logs...
if err != nil {
return err
}

get, err := postgresClient.Namespace(namespace).Get(ctx,
clusterName, metav1.GetOptions{})
if err != nil || get == nil {
Expand Down Expand Up @@ -355,6 +357,10 @@ Collecting PGO CLI logs...
// PGO CLI version
err = gatherPGOCLIVersion(ctx, clusterName, tw, cmd)

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

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

func gatherPostgresClusterNames(clusterName string, ctx context.Context, cmd *cobra.Command, tw *tar.Writer, client dynamic.NamespaceableResourceInterface) error {
result, err := client.List(ctx, metav1.ListOptions{})

if err != nil {
return err
}

data := []byte{}
for _, item := range result.Items {
ns, found, err := unstructured.NestedString(item.Object, "metadata", "namespace")
if !found {
return fmt.Errorf("key not found: metadata.namespace")
}
if err != nil {
return err
}
name, found, err := unstructured.NestedString(item.Object, "metadata", "name")
if !found {
return fmt.Errorf("key not found: metadata.name")
}
if err != nil {
return err
}
data = append(data, []byte("Namespace: "+ns+"\t"+"Cluster: "+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 5ed7bcf

Please sign in to comment.