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

feat: added central caching #257

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
32cfcfc
feat: added central caching
fritzduchardt Mar 24, 2024
667822a
Merge branch 'main' into central-cache
fritzduchardt Mar 24, 2024
18b4e05
Formatting
fritzduchardt Mar 24, 2024
8c9e116
Merge branch 'central-cache' of github.com:mykso/myks into central-cache
fritzduchardt Mar 24, 2024
6cdf637
Merge branch 'main' into central-cache
Zebradil Mar 31, 2024
fd8eb38
Ensure that all service files are stored under .myks
fritzduchardt Mar 31, 2024
5907433
Ignore .myks and .vendor
fritzduchardt Mar 31, 2024
1b5c412
Remove obsolete service files
fritzduchardt Mar 31, 2024
0df9421
Adressed some linting issues
fritzduchardt Mar 31, 2024
7663d7b
Merge branch 'central-cache' of github.com:mykso/myks into central-cache
fritzduchardt Mar 31, 2024
d62def2
Remove obsolete service files
fritzduchardt Mar 31, 2024
c8bc474
Adressed some more linting issues
fritzduchardt Mar 31, 2024
873571a
Switch hash function for shorter safer hash value
fritzduchardt Mar 31, 2024
8db1af3
Fix test
fritzduchardt Mar 31, 2024
9014544
Move and rename cache dir
fritzduchardt Apr 7, 2024
b82632f
Made cached directory names more readable
fritzduchardt Apr 7, 2024
4b6648e
Added global flag to disable cache
fritzduchardt Apr 7, 2024
6845a1e
fix hash calculation
Zebradil Apr 7, 2024
fba69cb
Merge remote-tracking branch 'origin/main' into central-cache
Zebradil Apr 7, 2024
8677cf2
fix hash calculation stronger
Zebradil Apr 7, 2024
3eb64cc
small refactoring
Zebradil Apr 7, 2024
2517d3a
change variable name to avoid shadowing one from outer scope
Zebradil Apr 7, 2024
f56c255
fix hash in tests
Zebradil Apr 7, 2024
0421c9d
sync only one contents item per directory in vendir config
Zebradil Apr 7, 2024
0bded65
fix multi-chart vendir configs, simplify code
Zebradil Apr 8, 2024
8a8559e
linter warnings
Zebradil Apr 8, 2024
40e7baf
Merge branch 'main' into central-cache
Zebradil Apr 11, 2024
a9a3a3c
Ensure sync is always run after cache invalidation.
fritzduchardt Apr 14, 2024
b39e452
Merge remote-tracking branch 'origin/main' into central-cache
Zebradil Apr 20, 2024
e5dc74b
Merge remote-tracking branch 'origin/main' into central-cache
Zebradil Apr 20, 2024
46ca903
Merge remote-tracking branch 'origin/main' into central-cache
Zebradil Apr 20, 2024
c4934cc
move integration tests charts deeper into _lib
Zebradil Apr 21, 2024
63d4ff1
add currently failing test case with multiple sources
Zebradil Apr 21, 2024
5d2e288
fix integration tests
Zebradil Apr 22, 2024
4eb84a5
fix integration tests
Zebradil Apr 22, 2024
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
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ bin/*
dist
.idea
.run
/myks
**/.myks/tmp
.myks
.vendor
coverage.txt
vendor/

This file was deleted.

12 changes: 0 additions & 12 deletions examples/default/envs/mykso/dev/_apps/argocd/.myks/vendir.yaml

This file was deleted.

This file was deleted.

20 changes: 0 additions & 20 deletions examples/default/envs/mykso/dev/_apps/goldpinger/.myks/vendir.yaml

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions examples/default/envs/mykso/dev/_apps/httpbingo/.myks/vendir.yaml

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions examples/simple/envs/dev/_apps/httpbingo/.myks/helm/httpbingo.yaml

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions examples/simple/envs/dev/_apps/httpbingo/.myks/vendir.yaml

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions examples/simple/envs/prod/_apps/httpbingo/.myks/vendir.yaml

This file was deleted.

14 changes: 3 additions & 11 deletions internal/myks/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,18 @@ func (a *Application) Init() error {
return nil
}

func (a *Application) expandPath(path string) string {
return filepath.Join(a.e.Dir, a.e.g.AppsDir, a.Name, path)
}

func (a *Application) expandServicePath(path string) string {
return filepath.Join(a.e.Dir, a.e.g.AppsDir, a.Name, a.e.g.ServiceDirName, path)
return filepath.Join(a.e.g.ServiceDirName, a.e.Dir, a.e.g.AppsDir, a.Name, path)
}

func (a *Application) expandTempPath(path string) string {
return a.expandServicePath(filepath.Join(a.e.g.TempDirName, path))
func (a *Application) expandVendirCache(path string) string {
return filepath.Join(a.e.g.ServiceDirName, a.e.g.VendirCache, path)
}

func (a *Application) writeServiceFile(name string, content string) error {
return writeFile(a.expandServicePath(name), []byte(content))
}

func (a *Application) writeTempFile(name string, content string) error {
return writeFile(a.expandTempPath(name), []byte(content))
}

func (a *Application) collectDataFiles() {
environmentDataFiles := a.e.collectBySubpath(a.e.g.EnvironmentDataFileName)
a.yttDataFiles = append(a.yttDataFiles, environmentDataFiles...)
Expand Down
5 changes: 5 additions & 0 deletions internal/myks/assets/data-schema.ytt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ yttPkg:
render:
#! If true, the render output file names will include the namespace
includeNamespace: false
#! Configuration options for caching during vendir sync step.
cache:
#! If true, the cache is enabled for all apps per default and can be selectively disabled with the vendir lazy flag.
#! If false, the cache is disabled for all apps per default and can be selectively enabled with the vendir lazy flag.
enabled: true
Comment on lines +115 to +119
Copy link
Member

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"?

Suggested change
#! Configuration options for caching during vendir sync step.
cache:
#! If true, the cache is enabled for all apps per default and can be selectively disabled with the vendir lazy flag.
#! If false, the cache is disabled for all apps per default and can be selectively enabled with the vendir lazy flag.
enabled: true
#! Configuration options for the sync step.
sync:
cache:
#! If true, the cache is enabled for all apps per default and can be selectively disabled with the vendir lazy flag.
#! If false, the cache is disabled for all apps per default and can be selectively enabled with the vendir lazy flag.
enabled: true

#! Myks configuration and runtime data.
#! Default values for these options are set by myks.
myks:
Expand Down
133 changes: 133 additions & 0 deletions internal/myks/cache.go
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)
}
Loading
Loading