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 all commits
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
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
}
Comment on lines +502 to +515
Copy link
Member

Choose a reason for hiding this comment

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

🌱 Every PostgresCluster returned from the API will have a namespace and name. There's also no harm in proceeding if we do encounter something unexpected. I think we prefer to gather the remainder of the list rather than give up early.

Suggested change
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
}
ns, _, _ := unstructured.NestedString(item.Object, "metadata", "namespace")
name, _, _ := unstructured.NestedString(item.Object, "metadata", "name")

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