-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: added central caching #257
Changes from 17 commits
32cfcfc
667822a
18b4e05
8c9e116
6cdf637
fd8eb38
5907433
1b5c412
0df9421
7663d7b
d62def2
c8bc474
873571a
8db1af3
9014544
b82632f
4b6648e
6845a1e
fba69cb
8677cf2
3eb64cc
2517d3a
f56c255
0421c9d
0bded65
8a8559e
40e7baf
a9a3a3c
b39e452
e5dc74b
46ca903
c4934cc
63d4ff1
5d2e288
4eb84a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ bin/* | |
dist | ||
.idea | ||
.run | ||
/myks | ||
**/.myks/tmp | ||
.myks | ||
.vendor | ||
coverage.txt | ||
vendor/ |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package myks | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type CacheNamer interface { | ||
Name(path string, vendirConfig map[string]interface{}) (string, error) | ||
} | ||
|
||
func findCacheNamer(config map[string]interface{}) CacheNamer { | ||
if config["helmChart"] != nil { | ||
return HelmCacheNamer{} | ||
} | ||
if config["directory"] != nil { | ||
return DirectoryCacheNamer{} | ||
} | ||
if config["git"] != nil { | ||
return GitCacheNamer{} | ||
} | ||
return DefaultCacheNamer{} | ||
} | ||
|
||
type DefaultCacheNamer struct{} | ||
|
||
func (h DefaultCacheNamer) Name(path string, config map[string]interface{}) (string, error) { | ||
yaml, err := sortYaml(config) | ||
if err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprintf("%s-%s", filepath.Base(path), hashString(yaml)), nil | ||
} | ||
|
||
type HelmCacheNamer struct{} | ||
|
||
func (h HelmCacheNamer) Name(_ string, config map[string]interface{}) (string, error) { | ||
yaml, err := sortYaml(config) | ||
if err != nil { | ||
return "", err | ||
} | ||
if config["helmChart"] == nil { | ||
return "", fmt.Errorf("expected vendir config for helm chart, but did not find helmChart yaml key") | ||
} | ||
helmChart := config["helmChart"].(map[string]interface{}) | ||
var version, chartName string | ||
if helmChart["name"] == nil { | ||
return "", fmt.Errorf("expected name in vendir config for helm chart, but did not find it") | ||
} | ||
if helmChart["version"] == nil { | ||
return "", fmt.Errorf("expected version in vendir config for helm chart, but did not find it") | ||
} | ||
chartName = helmChart["name"].(string) | ||
version = helmChart["version"].(string) | ||
return fmt.Sprintf("%s-%s-%s-%s", "helm", chartName, version, hashString(yaml)), nil | ||
} | ||
|
||
type GitCacheNamer struct{} | ||
|
||
func (h GitCacheNamer) Name(_ string, config map[string]interface{}) (string, error) { | ||
yaml, err := sortYaml(config) | ||
if err != nil { | ||
return "", err | ||
} | ||
if config["git"] == nil { | ||
return "", fmt.Errorf("expected vendir config for git, but did not find git yaml key") | ||
} | ||
git := config["git"].(map[string]interface{}) | ||
var repoUrl, ref string | ||
if git["url"] == nil { | ||
return "", fmt.Errorf("expected url in vendir config for git, but did not find it") | ||
} | ||
if git["ref"] == nil { | ||
return "", fmt.Errorf("expected ref in vendir config for git, but did not find it") | ||
} | ||
repoUrl = git["url"].(string) | ||
ref = git["ref"].(string) | ||
dir, err := urlSlug(repoUrl) | ||
if err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprintf("%s-%s-%s-%s", "git", dir, refSlug(ref), hashString(yaml)), nil | ||
} | ||
|
||
type DirectoryCacheNamer struct{} | ||
|
||
func (h DirectoryCacheNamer) Name(_ string, vendirConfig map[string]interface{}) (string, error) { | ||
yaml, err := sortYaml(vendirConfig) | ||
if err != nil { | ||
return "", err | ||
} | ||
if vendirConfig["directory"] == nil { | ||
return "", fmt.Errorf("expected vendir config for helm chart, but did not find directory yaml key") | ||
} | ||
directory := vendirConfig["directory"].(map[string]interface{}) | ||
var path string | ||
if directory["path"] == nil { | ||
return "", fmt.Errorf("expected path in vendir config for local directory, but did not find it") | ||
} | ||
path = directory["path"].(string) | ||
return fmt.Sprintf("%s-%s", directorySlug(path), hashString(yaml)), nil | ||
} | ||
|
||
func directorySlug(dirPath string) string { | ||
if len(dirPath) == 0 { | ||
return "" | ||
} | ||
// we aim for readability in the cache dir here, rather than uniqueness, given that the cache dir name will also | ||
// include the config digest | ||
return fmt.Sprintf("%s-%s", "dir", filepath.Base(dirPath)) | ||
} | ||
|
||
func urlSlug(repoUrl string) (string, error) { | ||
if len(repoUrl) == 0 { | ||
return "", nil | ||
} | ||
parsedURL, err := url.Parse(repoUrl) | ||
if err != nil { | ||
return "", err | ||
} | ||
path := parsedURL.Path | ||
return filepath.Base(path), nil | ||
} | ||
|
||
func refSlug(ref string) string { | ||
if len(ref) == 0 { | ||
return "" | ||
} | ||
ref = strings.ReplaceAll(ref, "/", "-") | ||
return filepath.Base(ref) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about moving this under the
sync
"namespace"?