This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 886
add support to import image from docker-daemon in rkt #3939
Open
surajssd
wants to merge
2
commits into
rkt:master
Choose a base branch
from
surajssd:import-docker-daemon-image
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2018 The rkt Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package distribution | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
d2acommon "github.com/appc/docker2aci/lib/common" | ||
) | ||
|
||
const ( | ||
TypeDockerDaemon Type = "docker-daemon" | ||
) | ||
|
||
func init() { | ||
Register(TypeDockerDaemon, NewDockerDaemon) | ||
} | ||
|
||
type DockerDaemon struct { | ||
url string // a valid docker reference URL | ||
parsedURL *d2acommon.ParsedDockerURL | ||
|
||
full string // the full string representation for equals operations | ||
simple string // the user friendly (simple) string representation | ||
} | ||
|
||
func NewDockerDaemon(u *url.URL) (Distribution, error) { | ||
dp, err := parseCIMD(u) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse URI: %q: %v", u.String(), err) | ||
} | ||
if dp.Type != TypeDocker { | ||
return nil, fmt.Errorf("wrong distribution type: %q", dp.Type) | ||
} | ||
|
||
parsed, err := d2acommon.ParseDockerURL(dp.Data) | ||
if err != nil { | ||
return nil, fmt.Errorf("bad docker URL %q: %v", dp.Data, err) | ||
} | ||
|
||
return &DockerDaemon{ | ||
url: dp.Data, | ||
parsedURL: parsed, | ||
simple: SimpleDockerRef(parsed), | ||
full: FullDockerRef(parsed), | ||
}, nil | ||
} | ||
|
||
func NewDockerDaemonFromString(ds string) (Distribution, error) { | ||
urlStr := NewCIMDString(TypeDocker, distDockerVersion, ds) | ||
u, err := url.Parse(urlStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewDockerDaemon(u) | ||
} | ||
|
||
func (d *DockerDaemon) CIMD() *url.URL { | ||
uriStr := NewCIMDString(TypeDocker, distDockerVersion, d.url) | ||
// Create a copy of the URL | ||
u, err := url.Parse(uriStr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return u | ||
} | ||
|
||
func (d *DockerDaemon) String() string { | ||
return d.simple | ||
} | ||
|
||
func (d *DockerDaemon) Equals(dist Distribution) bool { | ||
d2, ok := dist.(*DockerDaemon) | ||
if !ok { | ||
return false | ||
} | ||
|
||
return d.full == d2.full | ||
} | ||
|
||
// ReferenceURL returns the docker reference URL. | ||
func (d *DockerDaemon) ReferenceURL() string { | ||
return d.url | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2018 The rkt Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package image | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
docker2aci "github.com/appc/docker2aci/lib" | ||
d2acommon "github.com/appc/docker2aci/lib/common" | ||
dockerCli "github.com/docker/docker/client" | ||
) | ||
|
||
type dockerDaemonFetcher struct { | ||
*Fetcher | ||
ImageName string | ||
} | ||
|
||
func (d *dockerDaemonFetcher) Hash() (string, error) { | ||
// image verification is not supported for docker images so adding this check | ||
// incase the user has forgotten to give such a flag then indicate such | ||
if !d.InsecureFlags.SkipImageCheck() { | ||
return "", fmt.Errorf("signature verification for docker images is not supported (try --insecure-options=image)") | ||
} | ||
|
||
// fetch the image from docker's store as tar file | ||
tarFilePath, tarCleaner, err := d.dockerToTar() | ||
if err != nil { | ||
return "", err | ||
} | ||
defer tarCleaner() | ||
|
||
// convert the tar file into rkt readable ACI format | ||
aciFilePath, aciCleaner, err := d.tarToACI(tarFilePath) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer aciCleaner() | ||
|
||
// now that we have the ACI format image import into rkt store | ||
return d.importToRktStore(aciFilePath) | ||
} | ||
|
||
// dockerToTar fetches the image from docker's store and save it as | ||
// a tar file in temporary place | ||
func (d *dockerDaemonFetcher) dockerToTar() (string, func(), error) { | ||
// create a docker client to interact with docker daemon | ||
cli, err := dockerCli.NewEnvClient() | ||
if err != nil { | ||
return "", nil, fmt.Errorf("creating the docker client: %v", err) | ||
} | ||
|
||
// fetch the image from docker's store | ||
tar, err := cli.ImageSave( | ||
context.Background(), | ||
[]string{d.ImageName}, | ||
) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("fetching the image from docker store: %v", err) | ||
} | ||
defer tar.Close() | ||
|
||
// create a temporary file to copy the tar data we just received | ||
tmpTarFile, err := d.S.TmpFile() | ||
if err != nil { | ||
return "", nil, fmt.Errorf("creating tar file: %v", err) | ||
} | ||
defer tmpTarFile.Close() | ||
|
||
// now copy that tar content into a temporary file | ||
if _, err = io.Copy(tmpTarFile, tar); err != nil { | ||
return "", nil, fmt.Errorf("copying to tar file: %v", err) | ||
} | ||
tmpTarFile.Close() | ||
|
||
path := tmpTarFile.Name() | ||
return path, func() { | ||
os.Remove(path) | ||
}, nil | ||
} | ||
|
||
// tarToACI converts the tar file fetched from docker's store into | ||
// rkt readable ACI image format | ||
func (d *dockerDaemonFetcher) tarToACI(tarFilePath string) (string, func(), error) { | ||
// we will save all the temporary artifacts in this directory | ||
tempDir, err := d.S.TmpDir() | ||
if err != nil { | ||
return "", nil, fmt.Errorf("creating temporary directory: %v", err) | ||
} | ||
|
||
// Now convert that tar file into aci | ||
out, err := docker2aci.ConvertSavedFile(tarFilePath, docker2aci.FileConfig{ | ||
CommonConfig: docker2aci.CommonConfig{ | ||
Squash: true, | ||
OutputDir: tempDir, | ||
TmpDir: tempDir, | ||
Compression: d2acommon.GzipCompression, | ||
}, | ||
}) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("converting tar to aci: %v", err) | ||
} | ||
|
||
return out[0], func() { | ||
os.RemoveAll(tempDir) | ||
}, nil | ||
} | ||
|
||
func (d *dockerDaemonFetcher) importToRktStore(aciFilePath string) (string, error) { | ||
// TODO: implement the way to handle the image security options | ||
return d.fetchSingleImageByPath(aciFilePath, nil) | ||
} |
This file contains 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
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.
aciCleaner()
called twice? (line 45 and 52)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.
oops, I can't read. ignore me