Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit 63b5251

Browse files
authored
Allow ship to be pointed to git file paths (#646)
1 parent a5119f8 commit 63b5251

File tree

8 files changed

+131
-15
lines changed

8 files changed

+131
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"v1": {
3+
"config": {},
4+
"upstream": "github.com/replicatedhq/test-charts/blob/3427d6997bd150c60caa00ba0298fdfe17e3ed04/plain-k8s/frontend-deployment.yaml",
5+
"metadata": null,
6+
"contentSHA": "9fa025c3fdbcc02c7de8e9c74c8058d16b2aada04917895685504b387563afda"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
kind: ""
2+
apiversion: ""
3+
resources:
4+
- plain-k8s/frontend-deployment.yaml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: apps/v1 # for k8s versions before 1.9.0 use apps/v1beta2 and before 1.8.0 use extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: frontend
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: guestbook
9+
tier: frontend
10+
replicas: 3
11+
template:
12+
metadata:
13+
labels:
14+
app: guestbook
15+
tier: frontend
16+
spec:
17+
containers:
18+
- name: php-redis
19+
image: gcr.io/google-samples/gb-frontend:v4
20+
resources:
21+
requests:
22+
cpu: 100m
23+
memory: 100Mi
24+
env:
25+
- name: GET_HOSTS_FROM
26+
value: dns
27+
# If your cluster config does not include a dns service, then to
28+
# instead access environment variables to find service host
29+
# info, comment out the 'value: dns' line above, and uncomment the
30+
# line below:
31+
# value: env
32+
ports:
33+
- containerPort: 80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
kind: ""
2+
apiversion: ""
3+
bases:
4+
- ../../base
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: frontend
5+
spec:
6+
replicas: 3
7+
selector:
8+
matchLabels:
9+
app: guestbook
10+
tier: frontend
11+
template:
12+
metadata:
13+
labels:
14+
app: guestbook
15+
tier: frontend
16+
spec:
17+
containers:
18+
- env:
19+
- name: GET_HOSTS_FROM
20+
value: dns
21+
image: gcr.io/google-samples/gb-frontend:v4
22+
name: php-redis
23+
ports:
24+
- containerPort: 80
25+
resources:
26+
requests:
27+
cpu: 100m
28+
memory: 100Mi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
upstream: "github.com/replicatedhq/test-charts/blob/3427d6997bd150c60caa00ba0298fdfe17e3ed04/plain-k8s/frontend-deployment.yaml"
2+
args: []
3+
skip_cleanup: false

pkg/specs/githubclient/client.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11+
"path"
1112
"path/filepath"
1213
"strings"
1314

@@ -125,17 +126,6 @@ func (g *GithubClient) downloadAndExtractFiles(
125126
}
126127

127128
switch header.Typeflag {
128-
case tar.TypeDir:
129-
dirName := strings.Join(strings.Split(header.Name, "/")[1:], "/")
130-
if !strings.HasPrefix(dirName, basePath) {
131-
continue
132-
}
133-
basePathFound = true
134-
135-
dirName = strings.TrimPrefix(dirName, basePath)
136-
if err := g.fs.MkdirAll(filepath.Join(filePath, dirName), 0755); err != nil {
137-
return errors.Wrapf(err, "extract tar gz, mkdir")
138-
}
139129
case tar.TypeReg:
140130
// need this in a func because defer in a loop was leaking handles
141131
err := func() error {
@@ -145,7 +135,13 @@ func (g *GithubClient) downloadAndExtractFiles(
145135
}
146136
basePathFound = true
147137

148-
fileName = strings.TrimPrefix(fileName, basePath)
138+
if fileName != basePath {
139+
fileName = strings.TrimPrefix(fileName, basePath)
140+
}
141+
dirPath, _ := path.Split(fileName)
142+
if err := g.fs.MkdirAll(filepath.Join(filePath, dirPath), 0755); err != nil {
143+
return errors.Wrapf(err, "extract tar gz, mkdir")
144+
}
149145
outFile, err := g.fs.Create(filepath.Join(filePath, fileName))
150146
if err != nil {
151147
return errors.Wrapf(err, "extract tar gz, create")
@@ -177,7 +173,7 @@ func decodeGitHubURL(chartPath string) (owner string, repo string, branch string
177173
branch = ""
178174
path = ""
179175
if len(splitPath) > 3 {
180-
if splitPath[3] == "tree" {
176+
if splitPath[3] == "tree" || splitPath[3] == "blob" {
181177
branch = splitPath[4]
182178
path = strings.Join(splitPath[5:], "/")
183179
} else {

pkg/specs/githubclient/client_test.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ func TestGithubClient(t *testing.T) {
4242

4343
var _ = Describe("GithubClient", func() {
4444
client, mux, serverURL, teardown = setupGitClient()
45-
mux.HandleFunc("/repos/o/r/tarball", func(w http.ResponseWriter, r *http.Request) {
45+
redirectArchive := func(w http.ResponseWriter, r *http.Request) {
4646
http.Redirect(w, r, serverURL+"/archive.tar.gz", http.StatusFound)
4747
return
48-
})
48+
}
49+
mux.HandleFunc("/repos/o/r/tarball/", redirectArchive)
50+
mux.HandleFunc("/repos/o/r/tarball", redirectArchive)
51+
4952
mux.HandleFunc("/archive.tar.gz", func(w http.ResponseWriter, r *http.Request) {
5053
archiveData := `H4sIAJKjXFsAA+3WXW6CQBQFYJbCBmrv/D831ce+uIOpDtGEKQaoibt3qERbEmiNI6TxfC8TIwkXTg65lfW73D3ZcrXZ7t1zcg9EZJRKv059OonL09lKmRDcMM6k0SkxSYolqbrLNB2fVW3LMIoPr2DounBZlg383z7H+fwnqp/5v25sWc8O1ucR7xHeh5ZyKH9xzl+TDPkroylJKeIMvR48//fw8PC4Ov1fLl7mb4uZX8e8xzX9V4Y1/RdMof9jyIpi6hFgQp3+1y78tLWrYm6CV+1/oum/JqGx/42hN/+12+XFwbuPsA7euA3++v1n/LL/sZA/JyM4vv9juMQ89SQwhd7+V67cb1fu5vInf9n/zLf+y6b/nDP0fwxtzFOPAQAAAAAAAAAAAACRHQEZehxJACgAAA==`
5154
dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(archiveData))
@@ -127,6 +130,43 @@ var _ = Describe("GithubClient", func() {
127130
Expect(err.Error()).To(Equal("http://gitlab.com/o/r is not a Github URL"))
128131
})
129132
})
133+
134+
Context("With a url path to a single file at the base of the repo", func() {
135+
It("should fetch and persist the file", func() {
136+
validGithubURLSingle := "github.com/o/r/blob/master/Chart.yaml"
137+
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
138+
gitClient := &GithubClient{
139+
client: client,
140+
fs: mockFs,
141+
logger: log.NewNopLogger(),
142+
}
143+
144+
err := gitClient.GetFiles(context.Background(), validGithubURLSingle, constants.HelmChartPath)
145+
146+
chart, err := gitClient.fs.ReadFile(path.Join(constants.HelmChartPath, "Chart.yaml"))
147+
Expect(err).NotTo(HaveOccurred())
148+
149+
Expect(string(chart)).To(Equal("bar"))
150+
})
151+
})
152+
153+
Context("With a url path to a single nested file", func() {
154+
It("should fetch and persist the file", func() {
155+
validGithubURLSingle := "github.com/o/r/blob/master/templates/service.yml"
156+
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
157+
gitClient := &GithubClient{
158+
client: client,
159+
fs: mockFs,
160+
logger: log.NewNopLogger(),
161+
}
162+
163+
err := gitClient.GetFiles(context.Background(), validGithubURLSingle, constants.HelmChartPath)
164+
chart, err := gitClient.fs.ReadFile(path.Join(constants.HelmChartPath, "templates", "service.yml"))
165+
Expect(err).NotTo(HaveOccurred())
166+
167+
Expect(string(chart)).To(Equal("service"))
168+
})
169+
})
130170
})
131171

132172
Describe("decodeGitHubURL", func() {

0 commit comments

Comments
 (0)