|
| 1 | +/* |
| 2 | +Copyright 2018 Google LLC |
| 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 buildcontext |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "net/http" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + |
| 26 | + "github.com/GoogleContainerTools/kaniko/pkg/constants" |
| 27 | + "github.com/GoogleContainerTools/kaniko/pkg/util" |
| 28 | + "github.com/sirupsen/logrus" |
| 29 | +) |
| 30 | + |
| 31 | +// HTTPSTar struct for https tar.gz files processing |
| 32 | +type HTTPSTar struct { |
| 33 | + context string |
| 34 | +} |
| 35 | + |
| 36 | +// UnpackTarFromBuildContext downloads context file from https server |
| 37 | +func (h *HTTPSTar) UnpackTarFromBuildContext() (directory string, err error) { |
| 38 | + |
| 39 | + logrus.Info("Retrieving https tar file") |
| 40 | + |
| 41 | + // Create directory and target file for downloading the context file |
| 42 | + directory = constants.BuildContextDir |
| 43 | + tarPath := filepath.Join(directory, constants.ContextTar) |
| 44 | + file, err := util.CreateTargetTarfile(tarPath) |
| 45 | + if err != nil { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // Download tar file from remote https server |
| 50 | + // and save it into the target tar file |
| 51 | + resp, err := http.Get(h.context) |
| 52 | + if err != nil { |
| 53 | + return |
| 54 | + } |
| 55 | + defer func() { |
| 56 | + if closeErr := resp.Body.Close(); err == nil && closeErr != nil { |
| 57 | + err = closeErr |
| 58 | + } |
| 59 | + }() |
| 60 | + |
| 61 | + if resp.StatusCode != http.StatusOK { |
| 62 | + return directory, fmt.Errorf("HTTPSTar bad status from server: %s", resp.Status) |
| 63 | + } |
| 64 | + |
| 65 | + if _, err = io.Copy(file, resp.Body); err != nil { |
| 66 | + return tarPath, err |
| 67 | + } |
| 68 | + |
| 69 | + logrus.Info("Retrieved https tar file") |
| 70 | + |
| 71 | + if err = util.UnpackCompressedTar(tarPath, directory); err != nil { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + logrus.Info("Extracted https tar file") |
| 76 | + |
| 77 | + // Remove the tar so it doesn't interfere with subsequent commands |
| 78 | + return directory, os.Remove(tarPath) |
| 79 | +} |
0 commit comments