Skip to content

Commit

Permalink
feat: add git branch detection and refactor data schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebradil committed Jun 14, 2023
1 parent 39965c5 commit 05e41d4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ application: {}
environment:
#! Unique identifier of the environment, required by myks.
#@schema/validation min_len=1
#@schema/nullable
id: ""
#! List of applications to be deployed in the environment, required by myks.
applications:
Expand All @@ -34,10 +35,13 @@ helm:
myks:
applicationDataFileName: ""
applicationNames: [""]
dataSchemaFileName: ""
environmentBaseDir: ""
environmentDataFileName: ""
gitRepoUrl: ""
gitRepoBranch: ""
helmChartsDirName: ""
myksDataFileName: ""
namespacePrefix: ""
prototypesDir: ""
renderedDir: ""
Expand Down
4 changes: 2 additions & 2 deletions internal/myks/assets/prototypes/argocd/vendir/base.ytt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ directories:
contents:
- path: .
git:
url: #@ data.values.git.url
ref: #@ data.values.git.version
url: #@ data.values.application.url
ref: #@ data.values.application.version
includePaths:
- manifests/ha/install.yaml
newRootPath: manifests/ha
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#@data/values-schema
---
git:
#@overlay/match-child-defaults missing_ok=True
application:
#! WARNING: The order of the keys (alphabetical) is important for renovate.
#! When changed, renovate won't be able to detect the new version.
#! See renovate.json for more details.
Expand Down
10 changes: 6 additions & 4 deletions internal/myks/assets/prototypes/httpbingo/vendir/base.ytt.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#@ load("@ytt:data", "data")

#@ app = data.values.application
---
apiVersion: vendir.k14s.io/v1alpha1
kind: Config
directories:
- path: #@ "charts/" + data.values.chart.name
- path: #@ "charts/" + app.name
contents:
- path: .
helmChart:
name: #@ data.values.chart.name
version: #@ data.values.chart.version
name: #@ app.name
version: #@ app.version
repository:
url: #@ data.values.chart.url
url: #@ app.url
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#@data/values-schema
---
chart:
#@overlay/match-child-defaults missing_ok=True
application:
#! WARNING: The order of the keys (alphabetical) is important for renovate.
#! When changed, renovate won't be able to detect the new version.
#! See renovate.json for more details.
Expand Down
58 changes: 43 additions & 15 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
yaml "gopkg.in/yaml.v3"
)

//go:embed assets/env-data.ytt.yaml
//go:embed assets/data-schema.ytt.yaml
var dataSchema []byte

//go:embed assets/envs_gitignore
Expand Down Expand Up @@ -68,18 +68,22 @@ type Globe struct {
YttLibraryDirName string `default:"lib" yaml:"yttLibraryDirName"`
// Myks runtime config file name
MyksDataFileName string `default:"myks-data.ytt.yaml" yaml:"myksDataFileName"`
// Data values schema file name
DataSchemaFileName string `default:"data-schema.ytt.yaml" yaml:"dataSchemaFileName"`

/// User input

// Paths to scan for environments
SearchPaths []string
SearchPaths []string `yaml:"searchPaths"`
// Application names to process
ApplicationNames []string
ApplicationNames []string `yaml:"applicationNames"`

/// Runtime data

// Git repository URL
GitRepoUrl string `yaml:"gitRepoUrl"`
// Git repository branch
GitRepoBranch string `yaml:"gitRepoBranch"`

// Collected environments for processing
environments map[string]*Environment
Expand All @@ -97,21 +101,14 @@ func New(rootDir string) *Globe {
log.Fatal().Err(err).Msg("Unable to set defaults")
}

yttLibraryDir := filepath.Join(g.RootDir, g.YttLibraryDirName)
if _, err := os.Stat(yttLibraryDir); err == nil {
g.extraYttPaths = append(g.extraYttPaths, yttLibraryDir)
}

if configFileName, err := g.dumpConfigAsYaml(); err != nil {
log.Warn().Err(err).Msg("Unable to dump config as yaml")
} else {
g.extraYttPaths = append(g.extraYttPaths, configFileName)
}

if err := g.setGitRepoUrl(); err != nil {
log.Warn().Err(err).Msg("Unable to set git repo url")
}

if err := g.setGitRepoBranch(); err != nil {
log.Warn().Err(err).Msg("Unable to set git repo branch")
}

log.Debug().Interface("globe", g).Msg("Globe config")
return g
}
Expand All @@ -120,6 +117,26 @@ func (g *Globe) Init(searchPaths []string, applicationNames []string) error {
g.SearchPaths = searchPaths
g.ApplicationNames = applicationNames

yttLibraryDir := filepath.Join(g.RootDir, g.YttLibraryDirName)
if _, err := os.Stat(yttLibraryDir); err == nil {
g.extraYttPaths = append(g.extraYttPaths, yttLibraryDir)
}

dataSchemaFileName := filepath.Join(g.RootDir, g.EnvironmentBaseDir, g.DataSchemaFileName)
if _, err := os.Stat(dataSchemaFileName); err != nil {
log.Warn().Err(err).Msg("Unable to find data schema file, creating one")
if err := os.WriteFile(dataSchemaFileName, dataSchema, 0o644); err != nil {
log.Fatal().Err(err).Msg("Unable to create data schema file")
}
}
g.extraYttPaths = append(g.extraYttPaths, dataSchemaFileName)

if configFileName, err := g.dumpConfigAsYaml(); err != nil {
log.Warn().Err(err).Msg("Unable to dump config as yaml")
} else {
g.extraYttPaths = append(g.extraYttPaths, configFileName)
}

g.collectEnvironments(searchPaths)

return processItemsInParallel(g.environments, func(item interface{}) error {
Expand Down Expand Up @@ -213,7 +230,7 @@ func (g *Globe) createBaseFileStructure() error {
envDir := filepath.Join(g.RootDir, g.EnvironmentBaseDir)
protoDir := filepath.Join(g.RootDir, g.PrototypesDir)
renderedDir := filepath.Join(g.RootDir, g.RenderedDir)
dataSchemaFile := filepath.Join(envDir, g.EnvironmentDataFileName)
dataSchemaFile := filepath.Join(envDir, g.DataSchemaFileName)
envsGitignoreFile := filepath.Join(envDir, ".gitignore")

log.Debug().Str("environments directory", envDir).Msg("")
Expand Down Expand Up @@ -319,6 +336,17 @@ func (g *Globe) setGitRepoUrl() error {
return nil
}

func (g *Globe) setGitRepoBranch() error {
if g.GitRepoBranch == "" {
result, err := runCmd("git", nil, []string{"rev-parse", "--abbrev-ref", "HEAD"})
if err != nil {
return err
}
g.GitRepoBranch = strings.Trim(result.Stdout, "\n")
}
return nil
}

func (g *Globe) ytt(paths []string, args ...string) (CmdResult, error) {
return g.yttS(paths, nil, args...)
}
Expand Down

0 comments on commit 05e41d4

Please sign in to comment.