github.com/containerd/Containerd@v1.4.13/services/containers/helpers.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package containers
    18  
    19  import (
    20  	api "github.com/containerd/containerd/api/services/containers/v1"
    21  	"github.com/containerd/containerd/containers"
    22  )
    23  
    24  func containersToProto(containers []containers.Container) []api.Container {
    25  	var containerspb []api.Container
    26  
    27  	for _, image := range containers {
    28  		containerspb = append(containerspb, containerToProto(&image))
    29  	}
    30  
    31  	return containerspb
    32  }
    33  
    34  func containerToProto(container *containers.Container) api.Container {
    35  	return api.Container{
    36  		ID:     container.ID,
    37  		Labels: container.Labels,
    38  		Image:  container.Image,
    39  		Runtime: &api.Container_Runtime{
    40  			Name:    container.Runtime.Name,
    41  			Options: container.Runtime.Options,
    42  		},
    43  		Spec:        container.Spec,
    44  		Snapshotter: container.Snapshotter,
    45  		SnapshotKey: container.SnapshotKey,
    46  		CreatedAt:   container.CreatedAt,
    47  		UpdatedAt:   container.UpdatedAt,
    48  		Extensions:  container.Extensions,
    49  	}
    50  }
    51  
    52  func containerFromProto(containerpb *api.Container) containers.Container {
    53  	var runtime containers.RuntimeInfo
    54  	if containerpb.Runtime != nil {
    55  		runtime = containers.RuntimeInfo{
    56  			Name:    containerpb.Runtime.Name,
    57  			Options: containerpb.Runtime.Options,
    58  		}
    59  	}
    60  	return containers.Container{
    61  		ID:          containerpb.ID,
    62  		Labels:      containerpb.Labels,
    63  		Image:       containerpb.Image,
    64  		Runtime:     runtime,
    65  		Spec:        containerpb.Spec,
    66  		Snapshotter: containerpb.Snapshotter,
    67  		SnapshotKey: containerpb.SnapshotKey,
    68  		Extensions:  containerpb.Extensions,
    69  	}
    70  }