-
Notifications
You must be signed in to change notification settings - Fork 1.1k
container refactor #3960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-robot
merged 1 commit into
cri-o:master
from
wgahnagl:container-refactoring-6
Sep 23, 2020
Merged
container refactor #3960
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,22 @@ package container | |
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/containers/libpod/v2/pkg/annotations" | ||
| "github.com/containers/storage/pkg/stringid" | ||
| "github.com/cri-o/cri-o/internal/lib" | ||
| "github.com/cri-o/cri-o/internal/lib/sandbox" | ||
| oci "github.com/cri-o/cri-o/internal/oci" | ||
| "github.com/cri-o/cri-o/internal/storage" | ||
| "github.com/cri-o/cri-o/utils" | ||
| rspec "github.com/opencontainers/runtime-spec/specs-go" | ||
| "github.com/opencontainers/runtime-tools/generate" | ||
| "github.com/opencontainers/selinux/go-selinux/label" | ||
| "github.com/pkg/errors" | ||
| "github.com/sirupsen/logrus" | ||
|
|
@@ -62,6 +72,19 @@ type Container interface { | |
| // SelinuxLabel returns the container's SelinuxLabel | ||
| // it takes the sandbox's label, which it falls back upon | ||
| SelinuxLabel(string) ([]string, error) | ||
|
|
||
| // spec functions | ||
|
|
||
| // returns the spec | ||
| Spec() *generate.Generator | ||
|
|
||
| // SpecAddMount adds a mount to the container's spec | ||
| // it takes the rspec mount object | ||
| // if there is already a mount at the path specified, it removes it. | ||
| SpecAddMount(rspec.Mount) | ||
|
|
||
| // SpecAddAnnotations adds annotations to the spec. | ||
| SpecAddAnnotations(sandbox *sandbox.Sandbox, containerVolume []oci.ContainerVolume, mountPoint, configStopSignal string, imageResult *storage.ImageResult, isSystemd, systemdHasCollectMode bool) error | ||
| } | ||
|
|
||
| // container is the hidden default type behind the Container interface | ||
|
|
@@ -72,13 +95,117 @@ type container struct { | |
| id string | ||
| name string | ||
| privileged bool | ||
| spec generate.Generator | ||
| } | ||
|
|
||
| // New creates a new, empty Sandbox instance | ||
| func New(ctx context.Context) Container { | ||
| func New(ctx context.Context) (Container, error) { | ||
| spec, err := generate.New("linux") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &container{ | ||
| ctx: ctx, | ||
| ctx: ctx, | ||
| spec: spec, | ||
| }, nil | ||
| } | ||
|
|
||
| // SpecAddMount adds a specified mount to the spec | ||
| func (c *container) SpecAddMount(r rspec.Mount) { | ||
| c.spec.RemoveMount(r.Destination) | ||
| c.spec.AddMount(r) | ||
| } | ||
|
|
||
| // SpecAddAnnotation adds all annotations to the spec | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| func (c *container) SpecAddAnnotations(sb *sandbox.Sandbox, containerVolumes []oci.ContainerVolume, mountPoint, configStopSignal string, imageResult *storage.ImageResult, isSystemd, systemdHasCollectMode bool) (err error) { | ||
| // Copied from k8s.io/kubernetes/pkg/kubelet/kuberuntime/labels.go | ||
| const podTerminationGracePeriodLabel = "io.kubernetes.pod.terminationGracePeriod" | ||
|
|
||
| kubeAnnotations := c.Config().GetAnnotations() | ||
| created := time.Now() | ||
| labels := c.Config().GetLabels() | ||
|
|
||
| image, err := c.Image() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| logPath, err := c.LogPath(sb.LogDir()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.spec.AddAnnotation(annotations.Image, image) | ||
| c.spec.AddAnnotation(annotations.ImageName, imageResult.Name) | ||
| c.spec.AddAnnotation(annotations.ImageRef, imageResult.ID) | ||
| c.spec.AddAnnotation(annotations.Name, c.Name()) | ||
| c.spec.AddAnnotation(annotations.ContainerID, c.ID()) | ||
| c.spec.AddAnnotation(annotations.SandboxID, sb.ID()) | ||
| c.spec.AddAnnotation(annotations.SandboxName, sb.Name()) | ||
| c.spec.AddAnnotation(annotations.ContainerType, annotations.ContainerTypeContainer) | ||
| c.spec.AddAnnotation(annotations.LogPath, logPath) | ||
| c.spec.AddAnnotation(annotations.TTY, strconv.FormatBool(c.Config().Tty)) | ||
| c.spec.AddAnnotation(annotations.Stdin, strconv.FormatBool(c.Config().Stdin)) | ||
| c.spec.AddAnnotation(annotations.StdinOnce, strconv.FormatBool(c.Config().StdinOnce)) | ||
| c.spec.AddAnnotation(annotations.ResolvPath, sb.ResolvPath()) | ||
| c.spec.AddAnnotation(annotations.ContainerManager, lib.ContainerManagerCRIO) | ||
| c.spec.AddAnnotation(annotations.MountPoint, mountPoint) | ||
| c.spec.AddAnnotation(annotations.SeccompProfilePath, c.Config().GetLinux().GetSecurityContext().GetSeccompProfilePath()) | ||
| c.spec.AddAnnotation(annotations.Created, created.Format(time.RFC3339Nano)) | ||
|
|
||
| metadataJSON, err := json.Marshal(c.Config().GetMetadata()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.spec.AddAnnotation(annotations.Metadata, string(metadataJSON)) | ||
|
|
||
| labelsJSON, err := json.Marshal(labels) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.spec.AddAnnotation(annotations.Labels, string(labelsJSON)) | ||
|
|
||
| volumesJSON, err := json.Marshal(containerVolumes) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.spec.AddAnnotation(annotations.Volumes, string(volumesJSON)) | ||
|
|
||
| kubeAnnotationsJSON, err := json.Marshal(kubeAnnotations) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.spec.AddAnnotation(annotations.Annotations, string(kubeAnnotationsJSON)) | ||
|
|
||
| for k, v := range kubeAnnotations { | ||
| c.spec.AddAnnotation(k, v) | ||
| } | ||
| for k, v := range labels { | ||
| c.spec.AddAnnotation(k, v) | ||
| } | ||
| for idx, ip := range sb.IPs() { | ||
| c.spec.AddAnnotation(fmt.Sprintf("%s.%d", annotations.IP, idx), ip) | ||
| } | ||
|
|
||
| if isSystemd { | ||
| if t, ok := kubeAnnotations[podTerminationGracePeriodLabel]; ok { | ||
| // currently only supported by systemd, see | ||
| // https://github.com/opencontainers/runc/pull/2224 | ||
| c.spec.AddAnnotation("org.systemd.property.TimeoutStopUSec", "uint64 "+t+"000000") // sec to usec | ||
| } | ||
| if systemdHasCollectMode { | ||
| c.spec.AddAnnotation("org.systemd.property.CollectMode", "'inactive-or-failed'") | ||
| } | ||
| } | ||
|
|
||
| if configStopSignal != "" { | ||
| // this key is defined in image-spec conversion document at https://github.com/opencontainers/image-spec/pull/492/files#diff-8aafbe2c3690162540381b8cdb157112R57 | ||
| c.spec.AddAnnotation("org.opencontainers.image.stopSignal", configStopSignal) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *container) Spec() *generate.Generator { | ||
| return &c.spec | ||
| } | ||
|
|
||
| // SetConfig sets the configuration to the container and validates it | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we have two docstrings for SpecAddMount. I think one is enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/SpecAddMount/AddMount/?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's similar comments over other functions in the file, I think I we should either remove all of them or keep all of them.