func ResolveTraceJobTarget(clientset kubernetes.Interface, resource, container, targetNamespace string) (*TraceJobTarget, error) {
	target := TraceJobTarget{}
	var resourceType string
	var resourceID string
	resourceParts := strings.Split(resource, "/")
	if len(resourceParts) < 1 || len(resourceParts) > 2 {
		return nil, fmt.Errorf("Invalid resource string %s", resource)
	} else if len(resourceParts) == 1 {
		resourceType = "node"
		resourceID = resourceParts[0]
	} else if len(resourceParts) == 2 {
		resourceType = resourceParts[0]
		resourceID = resourceParts[1]
	}
	switch resourceType {
	case "node":
		nodeClient := clientset.CoreV1().Nodes()
		allocatable, err := NodeIsAllocatable(clientset, resourceID)
		if err != nil {
			return nil, err
		}
		if !allocatable {
			return nil, errors.NewErrorUnallocatable(fmt.Sprintf("Node %s is not allocatable", resourceID))
		}
		node, err := nodeClient.Get(context.TODO(), resourceID, metav1.GetOptions{})
		if err != nil {
			return nil, errors.NewErrorInvalid(fmt.Sprintf("Failed to locate a node for %s %v", resourceID, err))
		}
		labels := node.GetLabels()
		val, ok := labels["kubernetes.io/hostname"]
		if !ok {
			return nil, errors.NewErrorInvalid(fmt.Sprintf("label kubernetes.io/hostname not found in node"))
		}
		target.Node = val
		return &target, nil
	case "pod":
		podClient := clientset.CoreV1().Pods(targetNamespace)
		pod, err := podClient.Get(context.TODO(), resourceID, metav1.GetOptions{})
		if err != nil {
			return nil, errors.NewErrorInvalid(fmt.Sprintf("Could not locate pod %s", resourceID))
		}
		allocatable, err := NodeIsAllocatable(clientset, pod.Spec.NodeName)
		if err != nil || !allocatable {
			return nil, errors.NewErrorUnallocatable(fmt.Sprintf("Pod %s is not scheduled on an allocatable node", resourceID))
		}
		err = resolvePodToTarget(podClient, resourceID, container, targetNamespace, &target)
		if err != nil {
			return nil, err
		}
		return &target, nil
	case "deploy", "deployment":
		deployClient := clientset.AppsV1().Deployments(targetNamespace)
		deployment, err := deployClient.Get(context.TODO(), resourceID, metav1.GetOptions{})
		if err != nil {
			return nil, err
		}
		labelMap, err := metav1.LabelSelectorAsMap(deployment.Spec.Selector)
		if err != nil {
			return nil, err
		}
		podClient := clientset.CoreV1().Pods(targetNamespace)
		pods, err := podClient.List(context.TODO(), metav1.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap).String()})
		if err != nil {
			return nil, err
		}
		var selectedPod *v1.Pod
		for _, pod := range pods.Items {
			allocatable, err := NodeIsAllocatable(clientset, pod.Spec.NodeName)
			if err != nil {
				continue
			}
			if allocatable {
				selectedPod = &pod
				break
			}
		}
		if selectedPod == nil {
			return nil, errors.NewErrorUnallocatable(fmt.Sprintf("No pods for deployment %s were on allocatable nodes", resourceID))
		}
		err = resolvePodToTarget(podClient, selectedPod.Name, container, targetNamespace, &target)
		if err != nil {
			return nil, err
		}
		return &target, nil
	default:
		return nil, errors.NewErrorInvalid(fmt.Sprintf("Unsupported resource type %s for %s\n", resourceType, resourceID))
	}
	return &target, nil
}
/*
ResolveTraceJobTarget will:

- take a kubernetes resource, possibly namespaced, and resolve it to a concrete node that the tracejob can run on
- if the resource is a node, the scope of the trace will be the entire node
- if the resource is a pod, or something that can be resolved to a pod:
- resource is namespaced, the targetNamespace is required
- if the pod has multiple containers, a container name is required, otherwise the only container will be used

clientset       - a kubernetes clientset, used for resolving the target resource
resource        - a string, indicating the kubernetes resource to be traced. Currently supported: node, pod
container       - (optional) the container to trace, required only if needed to disambiguate
targetNamespace - (optional) the target namespace of a given resource, required if the resource is namespaced
package tracejob
import (
	"context"
	"fmt"
	"strings"
	v1 "k8s.io/api/core/v1"
	"github.com/iovisor/kubectl-trace/pkg/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/client-go/kubernetes"
	corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
type TraceJobTarget struct {
	Node        string  
	PodUID      string  
	ContainerID string  
}