Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 454910f

Browse files
jfrabautetejal29
andauthored
feat: Add https tar.gz remote source for context (#1519)
* feat: Add https tar.gz remote source for context * Add license header * add license header Co-authored-by: Tejal Desai <tejal29@gmail.com>
1 parent c2a919a commit 454910f

3 files changed

Lines changed: 148 additions & 1 deletion

File tree

pkg/buildcontext/buildcontext.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func GetBuildContext(srcContext string, opts BuildOptions) (BuildContext, error)
6161
if util.ValidAzureBlobStorageHost(srcContext) {
6262
return &AzureBlob{context: srcContext}, nil
6363
}
64-
return nil, errors.New("url provided for https context is not in a supported format, please use the https url for Azure Blob Storage")
64+
return &HTTPSTar{context: srcContext}, nil
6565
case TarBuildContextPrefix:
6666
return &Tar{context: context}, nil
6767
}

pkg/buildcontext/https.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

pkg/buildcontext/https_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
"net/http"
21+
"net/http/httptest"
22+
"testing"
23+
)
24+
25+
func TestBuildWithHttpsTar(t *testing.T) {
26+
27+
tests := []struct {
28+
name string
29+
serverHandler http.HandlerFunc
30+
}{
31+
{
32+
name: "test http bad status",
33+
serverHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34+
w.WriteHeader(http.StatusBadRequest)
35+
_, err := w.Write([]byte("corrupted message"))
36+
if err != nil {
37+
t.Fatalf("Error sending response: %v", err)
38+
}
39+
}),
40+
},
41+
{
42+
name: "test http bad data",
43+
serverHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44+
w.WriteHeader(http.StatusOK)
45+
_, err := w.Write([]byte("corrupted message"))
46+
if err != nil {
47+
t.Fatalf("Error sending response: %v", err)
48+
}
49+
}),
50+
},
51+
}
52+
53+
for _, tcase := range tests {
54+
t.Run(tcase.name, func(t *testing.T) {
55+
server := httptest.NewServer(tcase.serverHandler)
56+
defer server.Close()
57+
58+
context := &HTTPSTar{
59+
context: server.URL + "/data.tar.gz",
60+
}
61+
62+
_, err := context.UnpackTarFromBuildContext()
63+
if err == nil {
64+
t.Fatalf("Error expected but not returned: %s", err)
65+
}
66+
})
67+
}
68+
}

0 commit comments

Comments
 (0)