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

Cache index calls #5

Merged
merged 2 commits into from
Aug 22, 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
76 changes: 51 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import (
)

const (
protocol = "github"
githubHost = "github.com"
indexFilename = "index.yaml"
protocol = "github"
githubHost = "github.com"
indexFilename = "index.yaml"
annotationDownloaded = "wandera.com/helm-github/downloaded"
indexCacheDuration = 60 * time.Second
timeout = 30 * time.Second
)

var (
Expand Down Expand Up @@ -52,18 +55,34 @@ func init() {
}

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

if len(os.Args) == 5 {
uri := strings.TrimPrefix(os.Args[4], protocol+"://")
if strings.HasSuffix(uri, indexFilename) {
file, ok := getCachedIndexFile(uri)
if ok && file.Annotations != nil {
parsed, err := time.Parse(time.RFC3339, file.Annotations[annotationDownloaded])
if err == nil && time.Since(parsed) < indexCacheDuration {
bytes, err := yaml.Marshal(&file)
if err != nil {
log.Panic(err)
}
fmt.Println(string(bytes))
return
}
}
file, err := fetchIndexFile(ctx, uri)
if err != nil {
log.Panic(err)
}
file = switchProtocolToGithub(file)
file.SortEntries()
if file.Annotations == nil {
file.Annotations = make(map[string]string)
}
file.Annotations[annotationDownloaded] = time.Now().Format(time.RFC3339)
bytes, err := yaml.Marshal(&file)
if err != nil {
log.Panic(err)
Expand All @@ -75,37 +94,31 @@ func main() {
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 {
if validateDigest(getArchiveDigest(uri), cacheFile.Name()) {
_, err := io.Copy(os.Stdout, cacheFile)
if err != nil {
log.Panic(err)
}
return
}
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)
}
}
}
}

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 {
idx, ok := getCachedIndexFile(uri)
if !ok {
return ""
}
for _, versions := range idx.Entries {
Expand All @@ -120,6 +133,19 @@ func getArchiveDigest(uri string) string {
return ""
}

func getCachedIndexFile(uri string) (helm.IndexFile, bool) {
_, r := parseOwnerRepository(uri)
bytes, err := os.ReadFile(path.Join(cacheDirBase, r+"-index.yaml"))
if err != nil {
return helm.IndexFile{}, false
}
idx := helm.IndexFile{}
if err := yaml.Unmarshal(bytes, &idx); err != nil {
return helm.IndexFile{}, false
}
return idx, true
}

func validateDigest(digest string, fileName string) bool {
df, err := helm.DigestFile(fileName)
if err != nil {
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.1"
version: "0.1.2"
usage: "Manage chart repositories on Github"
description: |-
Provides Github Protocol Support
Expand Down