...

Source file src/github.com/redhat-developer/odo/pkg/podman/component.go

Documentation: github.com/redhat-developer/odo/pkg/podman

     1  package podman
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	corev1 "k8s.io/api/core/v1"
    10  	"k8s.io/klog"
    11  
    12  	"github.com/redhat-developer/odo/pkg/api"
    13  	odolabels "github.com/redhat-developer/odo/pkg/labels"
    14  	"github.com/redhat-developer/odo/pkg/odo/commonflags"
    15  )
    16  
    17  // ListPodsReport contains the result of the `podman pod ps --format json` command
    18  type ListPodsReport struct {
    19  	Name       string
    20  	Labels     map[string]string
    21  	Containers []ListPodsContainer `json:"Containers,omitempty"`
    22  }
    23  
    24  type ListPodsContainer struct {
    25  	Names string `json:"Names,omitempty"`
    26  }
    27  
    28  func (o *PodmanCli) ListAllComponents() ([]api.ComponentAbstract, error) {
    29  	cmd := exec.Command(o.podmanCmd, append(o.containerRunGlobalExtraArgs, "pod", "ps", "--format", "json", "--filter", "status=running")...)
    30  	klog.V(3).Infof("executing %v", cmd.Args)
    31  	out, err := cmd.Output()
    32  	if err != nil {
    33  		if exiterr, ok := err.(*exec.ExitError); ok {
    34  			err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
    35  		}
    36  		return nil, err
    37  	}
    38  
    39  	var list []ListPodsReport
    40  	if err = json.Unmarshal(out, &list); err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	for _, pod := range list {
    45  		klog.V(5).Infof("\npod name: %s", pod.Name)
    46  		klog.V(5).Infof("labels:")
    47  		for k, v := range pod.Labels {
    48  			klog.V(5).Infof(" - %s: %s", k, v)
    49  		}
    50  	}
    51  
    52  	var components []api.ComponentAbstract
    53  
    54  	for _, pod := range list {
    55  
    56  		labels := pod.Labels
    57  
    58  		// Figure out the correct name to use
    59  		// if there is no instance label (app.kubernetes.io/instance),
    60  		// we SKIP the resource as it is not a component essential for Kubernetes.
    61  		name := odolabels.GetComponentName(labels)
    62  		if name == "" {
    63  			continue
    64  		}
    65  
    66  		// Get the component type (if there is any..)
    67  		componentType, err := odolabels.GetProjectType(labels, nil)
    68  		if err != nil || componentType == "" {
    69  			componentType = api.TypeUnknown
    70  		}
    71  
    72  		managedBy := odolabels.GetManagedBy(labels)
    73  		managedByVersion := odolabels.GetManagedByVersion(labels)
    74  
    75  		// Generate the appropriate "component" with all necessary information
    76  		component := api.ComponentAbstract{
    77  			Name:             name,
    78  			ManagedBy:        managedBy,
    79  			Type:             componentType,
    80  			ManagedByVersion: managedByVersion,
    81  			//lint:ignore SA1019 we need to output the deprecated value, before to remove it in a future release
    82  			RunningOn: commonflags.PlatformPodman,
    83  			Platform:  commonflags.PlatformPodman,
    84  		}
    85  		mode := odolabels.GetMode(labels)
    86  		if mode != "" {
    87  			component.RunningIn = api.NewRunningModes()
    88  			component.RunningIn.AddRunningMode(api.RunningMode(strings.ToLower(mode)))
    89  		}
    90  		components = append(components, component)
    91  	}
    92  
    93  	return components, nil
    94  }
    95  
    96  func (o *PodmanCli) GetPodUsingComponentName(componentName string) (*corev1.Pod, error) {
    97  	podSelector := fmt.Sprintf("component=%s", componentName)
    98  	return o.GetRunningPodFromSelector(podSelector)
    99  }
   100  

View as plain text