Skip to content

Commit ec0af8a

Browse files
committed
feat(cli): refactor
Signed-off-by: Pavel Tishkov <[email protected]>
1 parent 582b61a commit ec0af8a

File tree

2 files changed

+23
-194
lines changed

2 files changed

+23
-194
lines changed

src/cli/internal/cmd/collectdebuginfo/collectors.go

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ var coreKinds = map[string]bool{
4545
"PersistentVolumeClaim": true,
4646
"PersistentVolume": true,
4747
"Event": true,
48-
"Service": true,
49-
"ConfigMap": true,
50-
"Secret": true,
5148
}
5249

5350
func (b *DebugBundle) collectVMResources(ctx context.Context, client kubeclient.Client, namespace, vmName string) error {
@@ -364,83 +361,45 @@ func (b *DebugBundle) getInternalResourceList(ctx context.Context, resource, nam
364361
}
365362

366363
func (b *DebugBundle) outputResource(kind, name, namespace string, obj runtime.Object) error {
367-
unstructuredObj, isUnstructured := obj.(*unstructured.Unstructured)
368-
369364
// Output separator if not first resource
370365
if b.resourceCount > 0 {
371366
fmt.Fprintf(b.stdout, "\n---\n")
372367
}
373368
b.resourceCount++
374369

375-
// Convert to JSON first to preserve all fields including TypeMeta (kind, apiVersion, spec, status, etc.)
376-
jsonBytes, err := json.Marshal(obj)
377-
if err != nil {
378-
return fmt.Errorf("failed to marshal %s/%s to JSON: %w", kind, name, err)
370+
// Ensure Kind is set from input if missing
371+
gvk := obj.GetObjectKind().GroupVersionKind()
372+
if gvk.Kind == "" {
373+
gvk.Kind = kind
374+
obj.GetObjectKind().SetGroupVersionKind(gvk)
379375
}
380376

381-
// Always parse JSON and ensure apiVersion and kind are present
382-
// This handles cases where TypeMeta might not be serialized properly
383-
var jsonObj map[string]interface{}
384-
if err := json.Unmarshal(jsonBytes, &jsonObj); err == nil {
385-
needsUpdate := false
386-
// If apiVersion is missing, try to get it from the object itself
387-
if _, ok := jsonObj["apiVersion"]; !ok {
388-
var apiVersion string
389-
390-
// For unstructured objects, get apiVersion directly
391-
if isUnstructured {
392-
apiVersion = unstructuredObj.GetAPIVersion()
393-
} else {
394-
// For typed objects, get apiVersion from GVK
395-
// Objects from cluster should have GVK set correctly
396-
gvk := obj.GetObjectKind().GroupVersionKind()
397-
398-
// If GVK is empty, try to get it from scheme
399-
if gvk.Kind == "" || (gvk.Group == "" && gvk.Version == "") {
400-
// Try to get GVK from scheme
401-
if gvks, _, err := kubeclient.Scheme.ObjectKinds(obj); err == nil && len(gvks) > 0 {
402-
gvk = gvks[0]
403-
// Set GVK on the object for future use
404-
obj.GetObjectKind().SetGroupVersionKind(gvk)
405-
}
406-
}
407-
408-
// Use GroupVersion().String() which automatically formats as "group/version" or "version"
409-
// This works for both custom resources (group/version) and core resources (version only)
410-
apiVersion = gvk.GroupVersion().String()
411-
}
412-
413-
// If we got a valid apiVersion, use it
414-
if apiVersion != "" && apiVersion != "/" {
415-
jsonObj["apiVersion"] = apiVersion
416-
needsUpdate = true
417-
} else if coreKinds[kind] {
418-
// Fallback: for core Kubernetes resources, use "v1"
419-
jsonObj["apiVersion"] = coreAPIVersion
420-
needsUpdate = true
421-
}
422-
}
423-
// Ensure kind is also present
424-
if _, ok := jsonObj["kind"]; !ok {
425-
jsonObj["kind"] = kind
426-
needsUpdate = true
427-
}
428-
// Re-marshal with apiVersion and kind if needed
429-
if needsUpdate {
430-
jsonBytes, err = json.Marshal(jsonObj)
431-
if err != nil {
432-
return fmt.Errorf("failed to re-marshal %s/%s to JSON: %w", kind, name, err)
433-
}
377+
// If GroupVersion is missing/empty, try to get from scheme
378+
if gvk.GroupVersion().Empty() {
379+
gvks, _, err := kubeclient.Scheme.ObjectKinds(obj)
380+
if err == nil && len(gvks) > 0 {
381+
gvk = gvks[0]
382+
obj.GetObjectKind().SetGroupVersionKind(gvk)
383+
} else if coreKinds[kind] {
384+
// Fallback for core Kubernetes resources if scheme doesn't know about them
385+
gvk = schema.GroupVersionKind{Group: "", Version: coreAPIVersion, Kind: kind}
386+
obj.GetObjectKind().SetGroupVersionKind(gvk)
434387
}
435388
}
436389

437-
// Convert JSON to YAML - this preserves all fields
390+
// Marshal to JSON (now with TypeMeta if set)
391+
jsonBytes, err := json.Marshal(obj)
392+
if err != nil {
393+
return fmt.Errorf("failed to marshal %s/%s to JSON: %w", kind, name, err)
394+
}
395+
396+
// Convert to YAML
438397
yamlBytes, err := yaml.JSONToYAML(jsonBytes)
439398
if err != nil {
440399
return fmt.Errorf("failed to convert %s/%s to YAML: %w", kind, name, err)
441400
}
442401

443-
// Output comment and full YAML resource
402+
// Output
444403
fmt.Fprintf(b.stdout, "# %d. %s: %s\n%s", b.resourceCount, kind, name, string(yamlBytes))
445404

446405
return nil

src/cli/internal/cmd/debugbundle/debugbundle.go

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)