github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/api/handlers/types.go (about)

     1  package handlers
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/containers/common/libimage"
     8  	"github.com/hanks177/podman/v4/pkg/domain/entities"
     9  	docker "github.com/docker/docker/api/types"
    10  	dockerContainer "github.com/docker/docker/api/types/container"
    11  	dockerNetwork "github.com/docker/docker/api/types/network"
    12  	"github.com/docker/go-connections/nat"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  type AuthConfig struct {
    17  	docker.AuthConfig
    18  }
    19  
    20  type ImageInspect struct {
    21  	docker.ImageInspect
    22  }
    23  
    24  type ContainerConfig struct {
    25  	dockerContainer.Config
    26  }
    27  
    28  type LibpodImagesPullReport struct {
    29  	entities.ImagePullReport
    30  }
    31  
    32  // LibpodImagesRemoveReport is the return type for image removal via the rest
    33  // api.
    34  type LibpodImagesRemoveReport struct {
    35  	entities.ImageRemoveReport
    36  	// Image removal requires is to return data and an error.
    37  	Errors []string
    38  }
    39  
    40  type ContainersPruneReport struct {
    41  	docker.ContainersPruneReport
    42  }
    43  
    44  type ContainersPruneReportLibpod struct {
    45  	ID             string `json:"Id"`
    46  	SpaceReclaimed int64  `json:"Size"`
    47  	// Error which occurred during prune operation (if any).
    48  	// This field is optional and may be omitted if no error occurred.
    49  	//
    50  	// Extensions:
    51  	// x-omitempty: true
    52  	// x-nullable: true
    53  	PruneError string `json:"Err,omitempty"`
    54  }
    55  
    56  type LibpodContainersRmReport struct {
    57  	ID string `json:"Id"`
    58  	// Error which occurred during Rm operation (if any).
    59  	// This field is optional and may be omitted if no error occurred.
    60  	//
    61  	// Extensions:
    62  	// x-omitempty: true
    63  	// x-nullable: true
    64  	RmError string `json:"Err,omitempty"`
    65  }
    66  
    67  type Info struct {
    68  	docker.Info
    69  	BuildahVersion     string
    70  	CPURealtimePeriod  bool
    71  	CPURealtimeRuntime bool
    72  	CgroupVersion      string
    73  	Rootless           bool
    74  	SwapFree           int64
    75  	SwapTotal          int64
    76  	Uptime             string
    77  }
    78  
    79  type Container struct {
    80  	docker.Container
    81  	docker.ContainerCreateConfig
    82  }
    83  
    84  type DiskUsage struct {
    85  	docker.DiskUsage
    86  }
    87  
    88  type VolumesPruneReport struct {
    89  	docker.VolumesPruneReport
    90  }
    91  
    92  type ImagesPruneReport struct {
    93  	docker.ImagesPruneReport
    94  }
    95  
    96  type BuildCachePruneReport struct {
    97  	docker.BuildCachePruneReport
    98  }
    99  
   100  type NetworkPruneReport struct {
   101  	docker.NetworksPruneReport
   102  }
   103  
   104  type ConfigCreateResponse struct {
   105  	docker.ConfigCreateResponse
   106  }
   107  
   108  type PushResult struct {
   109  	docker.PushResult
   110  }
   111  
   112  type BuildResult struct {
   113  	docker.BuildResult
   114  }
   115  
   116  type ContainerWaitOKBody struct {
   117  	StatusCode int
   118  	Error      *struct {
   119  		Message string
   120  	}
   121  }
   122  
   123  // CreateContainerConfig used when compatible endpoint creates a container
   124  // swagger:model
   125  type CreateContainerConfig struct {
   126  	Name                   string                         // container name
   127  	dockerContainer.Config                                // desired container configuration
   128  	HostConfig             dockerContainer.HostConfig     // host dependent configuration for container
   129  	NetworkingConfig       dockerNetwork.NetworkingConfig // network configuration for container
   130  	UnsetEnv               []string                       // unset specified default environment variables
   131  	UnsetEnvAll            bool                           // unset all default environment variables
   132  }
   133  
   134  type ContainerTopOKBody struct {
   135  	dockerContainer.ContainerTopOKBody
   136  }
   137  
   138  type PodTopOKBody struct {
   139  	dockerContainer.ContainerTopOKBody
   140  }
   141  
   142  // HistoryResponse provides details on image layers
   143  type HistoryResponse struct {
   144  	ID        string `json:"Id"`
   145  	Created   int64
   146  	CreatedBy string
   147  	Tags      []string
   148  	Size      int64
   149  	Comment   string
   150  }
   151  
   152  type ExecCreateConfig struct {
   153  	docker.ExecConfig
   154  }
   155  
   156  type ExecStartConfig struct {
   157  	Detach bool   `json:"Detach"`
   158  	Tty    bool   `json:"Tty"`
   159  	Height uint16 `json:"h"`
   160  	Width  uint16 `json:"w"`
   161  }
   162  
   163  func ImageDataToImageInspect(ctx context.Context, l *libimage.Image) (*ImageInspect, error) {
   164  	options := &libimage.InspectOptions{WithParent: true, WithSize: true}
   165  	info, err := l.Inspect(context.Background(), options)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	ports, err := portsToPortSet(info.Config.ExposedPorts)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  
   174  	// TODO: many fields in Config still need wiring
   175  	config := dockerContainer.Config{
   176  		User:         info.User,
   177  		ExposedPorts: ports,
   178  		Env:          info.Config.Env,
   179  		Cmd:          info.Config.Cmd,
   180  		Volumes:      info.Config.Volumes,
   181  		WorkingDir:   info.Config.WorkingDir,
   182  		Entrypoint:   info.Config.Entrypoint,
   183  		Labels:       info.Labels,
   184  		StopSignal:   info.Config.StopSignal,
   185  	}
   186  
   187  	rootfs := docker.RootFS{}
   188  	if info.RootFS != nil {
   189  		rootfs.Type = info.RootFS.Type
   190  		rootfs.Layers = make([]string, 0, len(info.RootFS.Layers))
   191  		for _, layer := range info.RootFS.Layers {
   192  			rootfs.Layers = append(rootfs.Layers, string(layer))
   193  		}
   194  	}
   195  
   196  	graphDriver := docker.GraphDriverData{
   197  		Name: info.GraphDriver.Name,
   198  		Data: info.GraphDriver.Data,
   199  	}
   200  	// Add in basic ContainerConfig to satisfy docker-compose
   201  	cc := new(dockerContainer.Config)
   202  	cc.Hostname = info.ID[0:11] // short ID is the hostname
   203  	cc.Volumes = info.Config.Volumes
   204  
   205  	dockerImageInspect := docker.ImageInspect{
   206  		Architecture:    info.Architecture,
   207  		Author:          info.Author,
   208  		Comment:         info.Comment,
   209  		Config:          &config,
   210  		ContainerConfig: cc,
   211  		Created:         l.Created().Format(time.RFC3339Nano),
   212  		DockerVersion:   info.Version,
   213  		GraphDriver:     graphDriver,
   214  		ID:              "sha256:" + l.ID(),
   215  		Metadata:        docker.ImageMetadata{},
   216  		Os:              info.Os,
   217  		OsVersion:       info.Version,
   218  		Parent:          info.Parent,
   219  		RepoDigests:     info.RepoDigests,
   220  		RepoTags:        info.RepoTags,
   221  		RootFS:          rootfs,
   222  		Size:            info.Size,
   223  		Variant:         "",
   224  		VirtualSize:     info.VirtualSize,
   225  	}
   226  	return &ImageInspect{dockerImageInspect}, nil
   227  }
   228  
   229  // portsToPortSet converts libpod's exposed ports to docker's structs
   230  func portsToPortSet(input map[string]struct{}) (nat.PortSet, error) {
   231  	ports := make(nat.PortSet)
   232  	for k := range input {
   233  		proto, port := nat.SplitProtoPort(k)
   234  		switch proto {
   235  		// See the OCI image spec for details:
   236  		// https://github.com/opencontainers/image-spec/blob/e562b04403929d582d449ae5386ff79dd7961a11/config.md#properties
   237  		case "tcp", "":
   238  			p, err := nat.NewPort("tcp", port)
   239  			if err != nil {
   240  				return nil, errors.Wrapf(err, "unable to create tcp port from %s", k)
   241  			}
   242  			ports[p] = struct{}{}
   243  		case "udp":
   244  			p, err := nat.NewPort("udp", port)
   245  			if err != nil {
   246  				return nil, errors.Wrapf(err, "unable to create tcp port from %s", k)
   247  			}
   248  			ports[p] = struct{}{}
   249  		default:
   250  			return nil, errors.Errorf("invalid port proto %q in %q", proto, k)
   251  		}
   252  	}
   253  	return ports, nil
   254  }