github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/domain/infra/abi/generate.go (about)

     1  package abi
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  
     8  	"github.com/containers/podman/v2/libpod"
     9  	"github.com/containers/podman/v2/libpod/define"
    10  	"github.com/containers/podman/v2/pkg/domain/entities"
    11  	"github.com/containers/podman/v2/pkg/systemd/generate"
    12  	"github.com/ghodss/yaml"
    13  	"github.com/pkg/errors"
    14  	k8sAPI "k8s.io/api/core/v1"
    15  )
    16  
    17  func (ic *ContainerEngine) GenerateSystemd(ctx context.Context, nameOrID string, options entities.GenerateSystemdOptions) (*entities.GenerateSystemdReport, error) {
    18  	// First assume it's a container.
    19  	ctr, ctrErr := ic.Libpod.LookupContainer(nameOrID)
    20  	if ctrErr == nil {
    21  		// Generate the unit for the container.
    22  		name, content, err := generate.ContainerUnit(ctr, options)
    23  		if err != nil {
    24  			return nil, err
    25  		}
    26  		return &entities.GenerateSystemdReport{Units: map[string]string{name: content}}, nil
    27  	}
    28  
    29  	// If it's not a container, we either have a pod or garbage.
    30  	pod, err := ic.Libpod.LookupPod(nameOrID)
    31  	if err != nil {
    32  		err = errors.Wrap(ctrErr, err.Error())
    33  		return nil, errors.Wrapf(err, "%s does not refer to a container or pod", nameOrID)
    34  	}
    35  
    36  	// Generate the units for the pod and all its containers.
    37  	units, err := generate.PodUnits(pod, options)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return &entities.GenerateSystemdReport{Units: units}, nil
    42  }
    43  
    44  func (ic *ContainerEngine) GenerateKube(ctx context.Context, nameOrID string, options entities.GenerateKubeOptions) (*entities.GenerateKubeReport, error) {
    45  	var (
    46  		pod          *libpod.Pod
    47  		podYAML      *k8sAPI.Pod
    48  		err          error
    49  		ctr          *libpod.Container
    50  		servicePorts []k8sAPI.ServicePort
    51  		serviceYAML  k8sAPI.Service
    52  	)
    53  	// Get the container in question.
    54  	ctr, err = ic.Libpod.LookupContainer(nameOrID)
    55  	if err != nil {
    56  		pod, err = ic.Libpod.LookupPod(nameOrID)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		podYAML, servicePorts, err = pod.GenerateForKube()
    61  	} else {
    62  		if len(ctr.Dependencies()) > 0 {
    63  			return nil, errors.Wrapf(define.ErrNotImplemented, "containers with dependencies")
    64  		}
    65  		podYAML, err = ctr.GenerateForKube()
    66  	}
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	if options.Service {
    72  		serviceYAML = libpod.GenerateKubeServiceFromV1Pod(podYAML, servicePorts)
    73  	}
    74  
    75  	content, err := generateKubeOutput(podYAML, &serviceYAML)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	return &entities.GenerateKubeReport{Reader: bytes.NewReader(content)}, nil
    81  }
    82  
    83  func generateKubeOutput(podYAML *k8sAPI.Pod, serviceYAML *k8sAPI.Service) ([]byte, error) {
    84  	var (
    85  		output            []byte
    86  		marshalledPod     []byte
    87  		marshalledService []byte
    88  		err               error
    89  	)
    90  
    91  	marshalledPod, err = yaml.Marshal(podYAML)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	if serviceYAML != nil {
    97  		marshalledService, err = yaml.Marshal(serviceYAML)
    98  		if err != nil {
    99  			return nil, err
   100  		}
   101  	}
   102  
   103  	header := `# Generation of Kubernetes YAML is still under development!
   104  #
   105  # Save the output of this file and use kubectl create -f to import
   106  # it into Kubernetes.
   107  #
   108  # Created with podman-%s
   109  `
   110  	podmanVersion, err := define.GetVersion()
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	output = append(output, []byte(fmt.Sprintf(header, podmanVersion.Version))...)
   116  	output = append(output, marshalledPod...)
   117  	if serviceYAML != nil {
   118  		output = append(output, []byte("---\n")...)
   119  		output = append(output, marshalledService...)
   120  	}
   121  
   122  	return output, nil
   123  }