Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support charts caching #4

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions helm/digest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package helm

import (
"crypto"
"encoding/hex"
"io"
"os"
)

// DigestFile calculates a SHA256 hash (like Docker) for a given file.
//
// It takes the path to the archive file, and returns a string representation of
// the SHA256 sum.
//
// The intended use of this function is to generate a sum of a chart TGZ file.
func DigestFile(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
return Digest(f)
}

// Digest hashes a reader and returns a SHA256 digest.
//
// Helm uses SHA256 as its default hash for all non-cryptographic applications.
func Digest(in io.Reader) (string, error) {
hash := crypto.SHA256.New()
if _, err := io.Copy(hash, in); err != nil {
return "", nil
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
121 changes: 106 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"os"
"path"
"strings"
"time"

Expand All @@ -23,9 +24,20 @@ const (
indexFilename = "index.yaml"
)

var client *github.Client
var (
client *github.Client
cacheDirBase string
chartsCacheDir string
)

func init() {
if env, ok := os.LookupEnv("HELMGITHUB_DEBUG_LOG"); ok {
t, err := os.OpenFile(env, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
log.Panic(err)
}
log.SetOutput(t)
}
token, err := loadGithubToken()
if err != nil {
log.Panic(err)
Expand All @@ -35,18 +47,11 @@ func init() {
)
tc := oauth2.NewClient(context.Background(), ts)
client = github.NewClient(tc)
cacheDirBase = getCacheDirBase()
chartsCacheDir = getChartCacheDir()
}

func main() {
if env, ok := os.LookupEnv("HELMGITHUB_DEBUG_LOG"); ok {
t, err := os.OpenFile(env, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
log.Panic(err)
}
defer t.Close()
log.SetOutput(t)
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

Expand All @@ -65,17 +70,62 @@ func main() {
}
fmt.Println(string(bytes))
} else {
rc, err := fetchArchive(ctx, uri)
cacheFile, err := openCacheFile(uri)
if err != nil {
log.Panic(err)
}
defer rc.Close()
_, err = io.Copy(os.Stdout, rc)
if err != nil {
log.Panic(err)
defer cacheFile.Close()
ok := validateDigest(getArchiveDigest(uri), cacheFile.Name())
if !ok {
resp, err := fetchArchive(ctx, uri)
if err != nil {
_ = os.Remove(cacheFile.Name())
log.Panic(err)
}
defer resp.Close()
_, err = io.Copy(io.MultiWriter(os.Stdout, cacheFile), resp)
if err != nil {
_ = os.Remove(cacheFile.Name())
log.Panic(err)
}
} else {
_, err := io.Copy(os.Stdout, cacheFile)
if err != nil {
log.Panic(err)
}
}
}
}
}

func getArchiveDigest(uri string) string {
_, r := parseOwnerRepository(uri)
bytes, err := os.ReadFile(path.Join(cacheDirBase, r+"-index.yaml"))
if err != nil {
return ""
}
idx := helm.IndexFile{}
if err := yaml.Unmarshal(bytes, &idx); err != nil {
return ""
}
for _, versions := range idx.Entries {
for _, version := range versions {
for _, u := range version.URLs {
if strings.HasSuffix(u, uri) {
return version.Digest
}
}
}
}
return ""
}

func validateDigest(digest string, fileName string) bool {
df, err := helm.DigestFile(fileName)
if err != nil {
log.Panic(err)
}
return digest == df
}

func loadGithubToken() (string, error) {
Expand Down Expand Up @@ -103,6 +153,29 @@ func getIndexBranch() string {
return "gh-pages"
}

func getChartCacheDir() string {
dir := getCacheDirBase()
dir = path.Join(dir, "github", "chart")
if err := os.MkdirAll(dir, 0o777); err != nil {
log.Panic(err)
}
return dir
}

func getCacheDirBase() string {
var dir string
if env, ok := os.LookupEnv("HELM_REPOSITORY_CACHE"); ok {
dir = env
} else {
ucd, err := os.UserCacheDir()
if err != nil {
log.Panic(err)
}
dir = ucd
}
return dir
}

func fetchIndexFile(ctx context.Context, uri string) (helm.IndexFile, error) {
owner, repository := parseOwnerRepository(uri)
contents, _, _, err := client.Repositories.GetContents(ctx, owner, repository, indexFilename, &github.RepositoryContentGetOptions{Ref: getIndexBranch()})
Expand All @@ -121,6 +194,24 @@ func fetchIndexFile(ctx context.Context, uri string) (helm.IndexFile, error) {
return file, nil
}

func openCacheFile(uri string) (*os.File, error) {
artifactName := parseArtifactName(uri)
chartPath := path.Join(chartsCacheDir, artifactName+".tgz")
_, err := os.Stat(chartPath)
if err != nil {
create, err := os.Create(chartPath)
if err != nil {
return nil, err
}
return create, nil
}
open, err := os.Open(chartPath)
if err != nil {
return nil, err
}
return open, nil
}

func fetchArchive(ctx context.Context, uri string) (io.ReadCloser, error) {
owner, repository := parseOwnerRepository(uri)
tag := parseArtifactName(uri)
Expand Down
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "github"
version: "0.1.0"
version: "0.1.1"
usage: "Manage chart repositories on Github"
description: |-
Provides Github Protocol Support
Expand Down