This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
loader.go
91 lines (80 loc) · 2.16 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package handler
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"github.com/google/go-github/v24/github"
"golang.org/x/oauth2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
)
type Loader interface {
Load(ctx context.Context, repo, path, ref string) (runtime.Object, error)
}
type GithubLoader struct {
*github.Client
}
func NewGithubLoader(token, baseURL, uploadURL string) (*GithubLoader, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
client := github.NewClient(oauth2.NewClient(ctx, ts))
if baseURL != "" {
bu, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
client.BaseURL = bu
}
if uploadURL != "" {
uu, err := url.Parse(uploadURL)
if err != nil {
return nil, err
}
client.UploadURL = uu
}
return &GithubLoader{client}, nil
}
// Apply downloads a manifest from repo specified by owner and name at given
// ref. Ref and path can be a SHA, branch, or tag.
func (l *GithubLoader) Load(ctx context.Context, repo, path, ref string) (runtime.Object, error) {
var (
parts = strings.SplitN(repo, "/", 2)
owner = parts[0]
name = parts[1]
)
var options *github.RepositoryContentGetOptions
if ref != "" {
options = &github.RepositoryContentGetOptions{
Ref: ref,
}
}
file, err := l.Client.Repositories.DownloadContents(ctx, owner, name, path, options)
if err != nil {
return nil, fmt.Errorf("Couldn't get file %s from %s/%s at %s: %s", path, owner, name, ref, err)
}
defer file.Close()
return Decode(file)
}
// Decode reads a reader and parses the stream as runtime.Object.
func Decode(r io.Reader) (runtime.Object, error) {
content, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("Couldn't read file: %s", err)
}
jcontent, err := yaml.ToJSON(content)
if err != nil {
return nil, fmt.Errorf("Couldn't translate yaml to json: %s", err)
}
obj, _, err := unstructured.UnstructuredJSONScheme.Decode(jcontent, nil, nil)
if err != nil {
return nil, fmt.Errorf("Couldn't decode manifest: %s", err)
}
return obj, nil
}