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

refactor: use globe config everwhere #192

Merged
merged 9 commits into from
Dec 27, 2023
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
2 changes: 1 addition & 1 deletion internal/myks/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (g *Globe) Bootstrap(force, onlyPrint bool, components []string) error {
func (g *Globe) createBaseFileStructure(force bool) error {
envDir := filepath.Join(g.RootDir, g.EnvironmentBaseDir)
protoDir := filepath.Join(g.RootDir, g.PrototypesDir)
renderedDir := filepath.Join(g.RootDir, g.RenderedDir)
renderedDir := filepath.Join(g.RootDir, g.RenderedEnvsDir)
gitignoreFile := filepath.Join(g.RootDir, ".gitignore")
myksConfigFile := filepath.Join(g.RootDir, ".myks.yaml")

Expand Down
6 changes: 3 additions & 3 deletions internal/myks/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ func (e *Environment) Cleanup() error {
for _, app := range apps {
if _, ok := e.foundApplications[app]; !ok {
log.Info().Str("app", app).Msg(e.Msg("Removing app as it is not configured"))
err := os.RemoveAll(filepath.Join(e.g.RootDir, e.g.RenderedDir, "envs", e.Id, app))
err := os.RemoveAll(filepath.Join(e.g.RootDir, e.g.RenderedEnvsDir, e.Id, app))
if err != nil {
return fmt.Errorf("unable to remove dir: %w", err)
}
err = os.Remove(filepath.Join(e.g.RootDir, e.g.RenderedDir, "argocd", e.Id, getArgoCDAppFileName(app)))
err = os.Remove(filepath.Join(e.g.RootDir, e.g.RenderedArgoDir, e.Id, getArgoCDAppFileName(app)))
if err != nil {
return fmt.Errorf("unable to remove file: %w", err)
}
Expand All @@ -181,7 +181,7 @@ func (e *Environment) Cleanup() error {
// renderedApplications returns list of applications in rendered dir
func (e *Environment) renderedApplications() ([]string, error) {
apps := []string{}
dirEnvRendered := filepath.Join(e.g.RootDir, e.g.RenderedDir, "envs", e.Id)
dirEnvRendered := filepath.Join(e.g.RootDir, e.g.RenderedEnvsDir, e.Id)
files, err := os.ReadDir(dirEnvRendered)
if err != nil {
if os.IsNotExist(err) {
Expand Down
67 changes: 37 additions & 30 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,64 @@ import (
const GlobalLogFormat = "\033[1m[global]\033[0m %s"

// Define the main structure
// Globe configuration
type Globe struct {
/// Globe configuration

// Directory of application-specific configuration
AppsDir string `default:"_apps"`
// Directory of application-specific prototype overwrites
PrototypeOverrideDir string `default:"_proto"`
// Project root directory
RootDir string `default:"."`
// Base directory for environments
EnvironmentBaseDir string `default:"envs"`
// Main branch name
MainBranchName string `default:"main"`
// Prefix for kubernetes namespaces
NamespacePrefix string `default:""`
// Application prototypes directory
PrototypesDir string `default:"prototypes"`
// Ytt library directory name
YttLibraryDirName string `default:"lib"`
// Rendered kubernetes manifests directory
RenderedDir string `default:"rendered"`
// Project root directory
RootDir string `default:"."`
RenderedEnvsDir string `default:"rendered/envs"`
// Rendered argocd manifests directory
RenderedArgoDir string `default:"rendered/argocd"`

/// Globe constants
// Directory of application-specific configuration
AppsDir string `default:"_apps"`
// Directory of environment-specific configuration
EnvsDir string `default:"_env"`
// Directory of application-specific prototype overwrites
PrototypeOverrideDir string `default:"_proto"`

// Application data file name
ApplicationDataFileName string `default:"app-data.ytt.yaml"`
// ArgoCD data directory name
ArgoCDDataDirName string `default:"argocd"`
// Data values schema file name
DataSchemaFileName string `default:"data-schema.ytt.yaml"`
// Application data file name
ApplicationDataFileName string `default:"app-data.ytt.yaml"`
// Environment data file name
EnvironmentDataFileName string `default:"env-data.ytt.yaml"`
// Helm charts directory name
HelmChartsDirName string `default:"charts"`
// Myks runtime data file name
MyksDataFileName string `default:"myks-data.ytt.yaml"`
// Rendered environment data file name
RenderedEnvironmentDataFileName string `default:"env-data.yaml"`
// Static files directory name
StaticFilesDirName string `default:"static"`
// Myks runtime data file name
MyksDataFileName string `default:"myks-data.ytt.yaml"`
// Service directory name
ServiceDirName string `default:".myks"`
// Temporary directory name
TempDirName string `default:"tmp"`

// Rendered vendir config file name
VendirConfigFileName string `default:"vendir.yaml"`
// Rendered vendir lock file name
VendirLockFileName string `default:"vendir.lock.yaml"`
// Rendered vendir sync file name
VendirSyncFileName string `default:"vendir.sync.yaml"`
// Prefix for vendir secret environment variables
VendirSecretEnvPrefix string `default:"VENDIR_SECRET_"`

// Downloaded third-party sources
VendorDirName string `default:"vendor"`
// Ytt library directory name
YttLibraryDirName string `default:"lib"`
// Helm charts directory name
HelmChartsDirName string `default:"charts"`

// Plugin subdirectories
// ArgoCD data directory name
ArgoCDDataDirName string `default:"argocd"`
// Helm step directory name
HelmStepDirName string `default:"helm"`
// Static files directory name
StaticFilesDirName string `default:"static"`
// Vendir step directory name
VendirStepDirName string `default:"vendir"`
// Ytt step directory name
YttPkgStepDirName string `default:"ytt-pkg"`
// Ytt step directory name
Expand All @@ -85,6 +89,9 @@ type Globe struct {
// Git repository URL
GitRepoUrl string

// Prefix for kubernetes namespaces, only used in helm rendering
NamespacePrefix string `default:""`

// Collected environments for processing
environments map[string]*Environment

Expand Down Expand Up @@ -239,8 +246,8 @@ func (g *Globe) Cleanup() error {
legalEnvs[env.Id] = true
}

for _, dir := range [...]string{"argocd", "envs"} {
dirPath := filepath.Join(g.RootDir, g.RenderedDir, dir)
for _, dir := range [...]string{g.RenderedArgoDir, g.RenderedEnvsDir} {
dirPath := filepath.Join(g.RootDir, dir)
files, err := os.ReadDir(dirPath)
if err != nil {
if os.IsNotExist(err) {
Expand Down
8 changes: 4 additions & 4 deletions internal/myks/plugin_argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (e *Environment) renderArgoCD() (err error) {
// 1. Collection of environment main data values and schemas
yttFiles := e.collectBySubpath(e.g.EnvironmentDataFileName)
// 2. Collection of environment argocd-specific data values and schemas, and overlays
yttFiles = append(yttFiles, e.collectBySubpath(filepath.Join("_env", e.g.ArgoCDDataDirName))...)
yttFiles = append(yttFiles, e.collectBySubpath(filepath.Join(e.g.EnvsDir, e.g.ArgoCDDataDirName))...)

res, err := e.yttS(
"create ArgoCD project yaml",
Expand All @@ -55,7 +55,7 @@ func (e *Environment) renderArgoCD() (err error) {
}

func (e *Environment) getArgoCDDestinationDir() string {
return filepath.Join(e.g.RootDir, e.g.RenderedDir, "argocd", e.Id)
return filepath.Join(e.g.RootDir, e.g.RenderedArgoDir, e.Id)
}

func (a *Application) renderArgoCD() (err error) {
Expand All @@ -82,7 +82,7 @@ func (a *Application) renderArgoCD() (err error) {
yttFiles = append(yttFiles, prototypeArgoCDDir)
}
// 4. Collection of environment argocd-specific data values and schemas, and overlays
yttFiles = append(yttFiles, a.e.collectBySubpath(filepath.Join("_env", a.e.g.ArgoCDDataDirName))...)
yttFiles = append(yttFiles, a.e.collectBySubpath(filepath.Join(a.e.g.EnvsDir, a.e.g.ArgoCDDataDirName))...)
// 5. Collection of application argocd-specific data values and schemas, and overlays
yttFiles = append(yttFiles, a.e.collectBySubpath(filepath.Join(a.e.g.AppsDir, a.Name, a.e.g.ArgoCDDataDirName))...)

Expand Down Expand Up @@ -140,7 +140,7 @@ func (a *Application) argoCDPrepareDefaults() (filename string, err error) {
}

func (a *Application) getArgoCDDestinationDir() string {
return filepath.Join(a.e.g.RootDir, a.e.g.RenderedDir, "argocd", a.e.Id)
return filepath.Join(a.e.g.RootDir, a.e.g.RenderedArgoDir, a.e.Id)
}

func getArgoCDEnvFileName(envName string) string {
Expand Down
6 changes: 3 additions & 3 deletions internal/myks/plugin_static_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (a *Application) copyStaticFiles() (err error) {
}
logStaticFiles(staticFilesDirs)
// 2. Static files from prototype overrides
staticFilesDirs = append(staticFilesDirs, a.e.collectBySubpath(filepath.Join("_proto", a.prototypeDirName(), a.e.g.StaticFilesDirName))...)
staticFilesDirs = append(staticFilesDirs, a.e.collectBySubpath(filepath.Join(a.e.g.PrototypeOverrideDir, a.prototypeDirName(), a.e.g.StaticFilesDirName))...)
logStaticFiles(staticFilesDirs)

// 3. Static files from the environment
staticFilesDirs = append(staticFilesDirs, a.e.collectBySubpath(filepath.Join("_env", a.e.g.StaticFilesDirName))...)
staticFilesDirs = append(staticFilesDirs, a.e.collectBySubpath(filepath.Join(a.e.g.EnvsDir, a.e.g.StaticFilesDirName))...)
logStaticFiles(staticFilesDirs)
// 4. Static files from the application
staticFilesDirs = append(staticFilesDirs, a.e.collectBySubpath(filepath.Join("_apps", a.Name, a.e.g.StaticFilesDirName))...)
staticFilesDirs = append(staticFilesDirs, a.e.collectBySubpath(filepath.Join(a.e.g.AppsDir, a.Name, a.e.g.StaticFilesDirName))...)
logStaticFiles(staticFilesDirs)

staticFilesDestination := filepath.Join(a.getDestinationDir(), a.e.g.StaticFilesDirName)
Expand Down
2 changes: 1 addition & 1 deletion internal/myks/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (p PluginCmd) generateEnv(a *Application) (map[string]string, error) {
"MYKS_APP": a.Name,
"MYKS_APP_PROTOTYPE": a.Prototype,
"MYKS_ENV_DIR": a.e.Dir,
"MYKS_RENDERED_APP_DIR": "rendered/envs/" + a.e.Id + "/" + a.Name, // TODO: provide func and use it everywhere
"MYKS_RENDERED_APP_DIR": a.getDestinationDir(),
}

result, err := a.ytt(p.Name(), "get data values", a.yttDataFiles, "--data-values-inspect")
Expand Down
2 changes: 1 addition & 1 deletion internal/myks/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (a *Application) storeStepResult(output string, stepName string, stepNumber
}

func (a *Application) getDestinationDir() string {
return filepath.Join(a.e.g.RootDir, a.e.g.RenderedDir, "envs", a.e.Id, a.Name)
return filepath.Join(a.e.g.RootDir, a.e.g.RenderedEnvsDir, a.e.Id, a.Name)
}

// Generates a file name for each document using kind and name if available
Expand Down
2 changes: 1 addition & 1 deletion internal/myks/render_global_ytt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (g *GlobalYtt) Render(previousStepFile string) (string, error) {
yttFiles = append(yttFiles, previousStepFile)
}

yttFiles = append(yttFiles, g.app.e.collectBySubpath(filepath.Join("_env", g.app.e.g.YttStepDirName))...)
yttFiles = append(yttFiles, g.app.e.collectBySubpath(filepath.Join(g.app.e.g.EnvsDir, g.app.e.g.YttStepDirName))...)

if len(yttFiles) == 0 {
log.Debug().Msg(g.app.Msg(globalYttStepName, "No ytt files found"))
Expand Down
12 changes: 9 additions & 3 deletions internal/myks/smart_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ func (g *Globe) runSmartMode(changedFiles ChangedFiles) EnvAppMap {
}

// Subdirectories of apps and prototypes are named after plugins
plugins := []string{g.YttStepDirName, "helm", "vendir", g.YttPkgStepDirName, g.ArgoCDDataDirName}
plugins := []string{
g.ArgoCDDataDirName,
g.HelmStepDirName,
g.VendirStepDirName,
g.YttPkgStepDirName,
g.YttStepDirName,
}
pluginsPattern := "(?:" + strings.Join(plugins, "|") + ")"

exprMap := map[string][]*regexp.Regexp{
Expand All @@ -72,8 +78,8 @@ func (g *Globe) runSmartMode(changedFiles ChangedFiles) EnvAppMap {
},
// Env search path is the only submatch
"env": {
e("(" + g.EnvironmentBaseDir + ".*)/_env/" + g.YttStepDirName + "/.*"),
e("(" + g.EnvironmentBaseDir + ".*)/_env/" + g.ArgoCDDataDirName + "/.*"),
e("(" + g.EnvironmentBaseDir + ".*)/" + g.EnvsDir + "/" + g.YttStepDirName + "/.*"),
e("(" + g.EnvironmentBaseDir + ".*)/" + g.EnvsDir + "/" + g.ArgoCDDataDirName + "/.*"),
e("(" + g.EnvironmentBaseDir + ".*)/" + g.EnvironmentDataFileName),
},
// Prototype name is the only submatch
Expand Down
2 changes: 1 addition & 1 deletion internal/myks/smart_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestGlobe_runSmartMode(t *testing.T) {

for env, apps := range tt.rendered {
for _, app := range apps {
dir := filepath.Join(tmpDir, g.RenderedDir, "envs", env, app)
dir := filepath.Join(tmpDir, g.RenderedEnvsDir, env, app)
if err := createDirectory(dir); err != nil {
t.Errorf("failed to create directory %s", dir)
}
Expand Down
Loading