Skip to content

Commit

Permalink
Merge pull request #14254 from flouthoc/api-allow-remote
Browse files Browse the repository at this point in the history
api: make no-op `remote` functional in `/libpod/build`
  • Loading branch information
openshift-merge-robot authored May 17, 2022
2 parents 4a78992 + 6f9155c commit 105c6c7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
49 changes: 43 additions & 6 deletions pkg/api/handlers/compat/images_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
Registry string `schema:"registry"`
Rm bool `schema:"rm"`
RusageLogFile string `schema:"rusagelogfile"`
Remote string `schema:"remote"`
Seccomp string `schema:"seccomp"`
Secrets string `schema:"secrets"`
SecurityOpt string `schema:"securityopt"`
Expand Down Expand Up @@ -169,14 +170,50 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {

// convert addcaps formats
containerFiles := []string{}
if _, found := r.URL.Query()["dockerfile"]; found {
var m = []string{}
if err := json.Unmarshal([]byte(query.Dockerfile), &m); err != nil {
// it's not json, assume just a string
m = []string{filepath.Join(contextDirectory, query.Dockerfile)}
// Tells if query paramemter `dockerfile` is set or not.
dockerFileSet := false
if utils.IsLibpodRequest(r) && query.Remote != "" {
// The context directory could be a URL. Try to handle that.
anchorDir, err := ioutil.TempDir(parse.GetTempDir(), "libpod_builder")
if err != nil {
utils.InternalServerError(w, err)
}
tempDir, subDir, err := buildahDefine.TempDirForURL(anchorDir, "buildah", query.Remote)
if err != nil {
utils.InternalServerError(w, err)
}
if tempDir != "" {
// We had to download it to a temporary directory.
// Delete it later.
defer func() {
if err = os.RemoveAll(tempDir); err != nil {
// We are deleting this on server so log on server end
// client does not have to worry about server cleanup.
logrus.Errorf("Cannot delete downloaded temp dir %q: %s", tempDir, err)
}
}()
contextDirectory = filepath.Join(tempDir, subDir)
} else {
// Nope, it was local. Use it as is.
absDir, err := filepath.Abs(query.Remote)
if err != nil {
utils.BadRequest(w, "remote", query.Remote, err)
}
contextDirectory = absDir
}
containerFiles = m
} else {
if _, found := r.URL.Query()["dockerfile"]; found {
var m = []string{}
if err := json.Unmarshal([]byte(query.Dockerfile), &m); err != nil {
// it's not json, assume just a string
m = []string{filepath.Join(contextDirectory, query.Dockerfile)}
}
containerFiles = m
dockerFileSet = true
}
}

if !dockerFileSet {
containerFiles = []string{filepath.Join(contextDirectory, "Dockerfile")}
if utils.IsLibpodRequest(r) {
containerFiles = []string{filepath.Join(contextDirectory, "Containerfile")}
Expand Down
4 changes: 4 additions & 0 deletions test/apiv2/10-images.at
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ t POST "libpod/build?dockerfile=containerfile" $CONTAINERFILE_TAR application/js
t POST "build?dockerfile=containerfile" $CONTAINERFILE_TAR application/json 200 \
.stream~"STEP 1/1: FROM $IMAGE"

# Libpod: allow building from url: https://github.com/alpinelinux/docker-alpine.git and must ignore any provided tar
t POST "libpod/build?remote=https%3A%2F%2Fgithub.com%2Falpinelinux%2Fdocker-alpine.git" $CONTAINERFILE_TAR 200 \
.stream~"STEP 1/5: FROM alpine:3.14"

# Build api response header must contain Content-type: application/json
t POST "build?dockerfile=containerfile" $CONTAINERFILE_TAR application/json 200
response_headers=$(cat "$WORKDIR/curl.headers.out")
Expand Down

0 comments on commit 105c6c7

Please sign in to comment.