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

chore: release #103

Merged
merged 8 commits into from
Sep 28, 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
1 change: 1 addition & 0 deletions .github/workflows/flow-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
if: steps.semantic.outputs.new_release_published == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HOMEBREW_TAP_REPO_TOKEN: ${{ secrets.MYKSO_BOT_GITHUB_TOKEN }}
RELEASE_NOTES: ${{ steps.semantic.outputs.new_release_notes }}
run: |
goreleaser release --clean \
Expand Down
23 changes: 22 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ archives:
files:
- LICENSE
- README.md
rlcp: true
checksum:
name_template: "checksums.txt"
brews:
- name: myks
description: A configuration framework for Kubernetes applications
folder: Formula
homepage: https://github.com/mykso/myks
license: MIT
commit_author:
name: Mykso Bot
email: [email protected]
dependencies:
- name: git
type: optional
- name: helm
type: optional
- name: vendir
type: optional
- name: ytt
type: optional
repository:
owner: mykso
name: homebrew-tap
token: "{{ .Env.HOMEBREW_TAP_REPO_TOKEN }}"
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ func detectTargetEnvsAndApps(cmd *cobra.Command, args []string) (err error) {
envAppMap, err = globeAllEnvsAndApps.DetectChangedEnvsAndApps(viper.GetString("smart-mode.base-revision"))
if err != nil {
log.Warn().Err(err).Msg("Unable to run Smart Mode. Rendering everything.")
}
if len(envAppMap) == 0 {
} else if len(envAppMap) == 0 {
log.Warn().Msg("Smart Mode did not find any changes. Exiting.")
os.Exit(0)
}
Expand All @@ -111,7 +110,11 @@ func detectTargetEnvsAndApps(cmd *cobra.Command, args []string) (err error) {
for _, env := range strings.Split(args[0], ",") {
envAppMap[env] = appNames
}
} else {
// TODO: Use Globe.EnvironmentBaseDir instead of the hardcoded key
envAppMap["envs"] = appNames
}

default:
err := errors.New("Too many positional arguments")
log.Error().Err(err).Msg("Unable to parse positional arguments")
Expand Down
7 changes: 4 additions & 3 deletions internal/myks/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"os"
"path/filepath"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -59,7 +58,9 @@ func NewApplication(e *Environment, name string, prototypeName string) (*Applica
e: e,
}

if _, err := os.Stat(prototype); err != nil {
if ok, err := isExist(app.Prototype); err != nil {
return app, err
} else if !ok {
return app, errors.New("application prototype does not exist")
}

Expand Down Expand Up @@ -130,7 +131,7 @@ func (a *Application) collectDataFiles() {
a.yttDataFiles = append(a.yttDataFiles, environmentDataFiles...)

protoDataFile := filepath.Join(a.Prototype, a.e.g.ApplicationDataFileName)
if _, err := os.Stat(protoDataFile); err == nil {
if ok, err := isExist(protoDataFile); ok && err == nil {
a.yttDataFiles = append(a.yttDataFiles, protoDataFile)
}

Expand Down
26 changes: 11 additions & 15 deletions internal/myks/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,14 @@ func (g *Globe) createBaseFileStructure(force bool) error {
log.Debug().Str("myks config file", myksConfigFile).Msg("")

if !force {
if _, err := os.Stat(envDir); err == nil {
return ErrBootstrapTargetExists{target: envDir}
}
if _, err := os.Stat(protoDir); err == nil {
return ErrBootstrapTargetExists{target: protoDir}
}
if _, err := os.Stat(renderedDir); err == nil {
return ErrBootstrapTargetExists{target: renderedDir}
}
if _, err := os.Stat(envsGitignoreFile); err == nil {
return ErrBootstrapTargetExists{target: envsGitignoreFile}
}
if _, err := os.Stat(myksConfigFile); err == nil {
return ErrBootstrapTargetExists{target: myksConfigFile}
for _, path := range []string{envDir, protoDir, renderedDir, envsGitignoreFile, myksConfigFile} {
ok, err := isExist(path)
if err != nil {
return err
}
if ok {
return ErrBootstrapTargetExists{target: path}
}
}
}

Expand Down Expand Up @@ -139,7 +133,9 @@ func (g *Globe) createBaseFileStructure(force bool) error {

func (g *Globe) createDataSchemaFile() string {
dataSchemaFileName := filepath.Join(g.RootDir, g.ServiceDirName, g.TempDirName, g.DataSchemaFileName)
if _, err := os.Stat(dataSchemaFileName); err != nil {
if ok, err := isExist(dataSchemaFileName); err != nil {
log.Fatal().Err(err).Msg("Unable to stat data schema file")
} else if !ok {
log.Debug().Msg("Unable to find data schema file, creating one")
if err := os.MkdirAll(filepath.Dir(dataSchemaFileName), 0o750); err != nil {
log.Fatal().Err(err).Msg("Unable to create data schema file directory")
Expand Down
2 changes: 1 addition & 1 deletion internal/myks/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (e *Environment) collectBySubpath(subpath string) []string {
for _, level := range levels {
currentPath = filepath.Join(currentPath, level)
item := filepath.Join(currentPath, subpath)
if _, err := os.Stat(item); err == nil {
if ok, err := isExist(item); ok && err == nil {
items = append(items, item)
}
}
Expand Down
9 changes: 6 additions & 3 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ func New(rootDir string) *Globe {
}

yttLibraryDir := filepath.Join(g.RootDir, g.YttLibraryDirName)
if _, err := os.Stat(yttLibraryDir); err == nil {
if ok, err := isExist(yttLibraryDir); err != nil {
log.Fatal().Err(err).Str("path", yttLibraryDir).Msg("Unable to stat ytt library directory")
} else if ok {
g.extraYttPaths = append(g.extraYttPaths, yttLibraryDir)
}

Expand Down Expand Up @@ -302,8 +304,9 @@ func (g *Globe) collectEnvironmentsInPath(searchPath string) []string {
return err
}
if d != nil && d.IsDir() {
_, err := os.Stat(filepath.Join(path, g.EnvironmentDataFileName))
if err == nil {
if ok, err := isExist(filepath.Join(path, g.EnvironmentDataFileName)); err != nil {
return err
} else if ok {
env, err := NewEnvironment(g, path)
if err == nil {
g.environments[path] = env
Expand Down
9 changes: 5 additions & 4 deletions internal/myks/plugin_argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package myks
import (
"bytes"
_ "embed"
"os"
"path/filepath"
"text/template"

Expand Down Expand Up @@ -78,12 +77,14 @@ func (a *Application) renderArgoCD() (err error) {
yttFiles = append(yttFiles, a.yttDataFiles...)
// 3. Use argocd-specific data values, schemas, and overlays from the prototype
prototypeArgoCDDir := filepath.Join(a.Prototype, a.e.g.ArgoCDDataDirName)
if _, err := os.Stat(prototypeArgoCDDir); err == nil {
if ok, err := isExist(prototypeArgoCDDir); err != nil {
return err
} else if ok {
yttFiles = append(yttFiles, prototypeArgoCDDir)
}
// 3. Collection of environment argocd-specific data values and schemas, and overlays
// 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))...)
// 4. Collection of application argocd-specific data values and schemas, and overlays
// 5. Collection of application argocd-specific data values and schemas, and overlays
yttFiles = append(yttFiles, a.e.collectBySubpath(filepath.Join("_apps", a.Name, a.e.g.ArgoCDDataDirName))...)

res, err := a.yttS(
Expand Down
27 changes: 14 additions & 13 deletions internal/myks/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (a *Application) Render(yamlTemplatingTools []YamlTemplatingTool) (string,
log.Debug().Msg(a.Msg(renderStepName, "Starting"))
outputYaml := ""
lastStepOutputFile := ""
for _, yamlTool := range yamlTemplatingTools {
for nr, yamlTool := range yamlTemplatingTools {
log.Debug().Msg(a.Msg(yamlTool.Ident(), "Starting"))
stepOutputYaml, err := yamlTool.Render(lastStepOutputFile)
if err != nil {
Expand All @@ -48,7 +48,7 @@ func (a *Application) Render(yamlTemplatingTools []YamlTemplatingTool) (string,
} else {
outputYaml = stepOutputYaml
}
lastStepOutputFile, err = a.storeStepResult(outputYaml, yamlTool.Ident(), 1)
lastStepOutputFile, err = a.storeStepResult(outputYaml, yamlTool.Ident(), nr)
if err != nil {
log.Error().Err(err).Msg(a.Msg(yamlTool.Ident(), "Failed to store step result for: "+yamlTool.Ident()))
return "", err
Expand Down Expand Up @@ -108,7 +108,9 @@ func (a *Application) runSliceFormatStore(previousStepFile string) error {
filePath := filepath.Join(destinationDir, fileName)
// FIXME: If a file already exists, we should merge the two documents (probably).
// For now, we just overwrite the file and log a warning.
if _, err := os.Stat(filePath); err == nil {
if ok, err := isExist(filePath); err != nil {
return err
} else if ok {
log.Warn().Str("file", filePath).Msg(a.Msg(sliceStepName, "File already exists, check duplicated resources"))
}
err = writeFile(filePath, data.Bytes())
Expand All @@ -122,7 +124,7 @@ func (a *Application) runSliceFormatStore(previousStepFile string) error {

// storeStepResult saves output of a step to a file in the application's temp directory.
// Returns path to the file or an error.
func (a *Application) storeStepResult(output string, stepName string, stepNumber uint) (string, error) {
func (a *Application) storeStepResult(output string, stepName string, stepNumber int) (string, error) {
fileName := filepath.Join("steps", fmt.Sprintf("%02d-%s.yaml", stepNumber, stepName))
file := a.expandTempPath(fileName)
return file, a.writeTempFile(fileName, output)
Expand All @@ -148,16 +150,13 @@ func genRenderedResourceFileName(resource map[string]interface{}) string {

func (a *Application) getVendoredDir(dirname string) (string, error) {
resourceDir := a.expandPath(filepath.Join(a.e.g.VendorDirName, dirname))
if _, err := os.Stat(resourceDir); err != nil {
if os.IsNotExist(err) {
return "", nil
}

log.Warn().Err(err).Msg(a.Msg(renderStepName, "Unable to find vendor directory: "+resourceDir))
if ok, err := isExist(resourceDir); err != nil {
return "", err
} else if ok {
return resourceDir, nil
} else {
return "", nil
}

return resourceDir, nil
}

// prepareValuesFile generates values.yaml file from ytt data files and ytt templates
Expand All @@ -168,7 +167,9 @@ func (a *Application) prepareValuesFile(dirName string, resourceName string) (st
var valuesFiles []string

prototypeValuesFile := filepath.Join(a.Prototype, valuesFileName)
if _, err := os.Stat(prototypeValuesFile); err == nil {
if ok, err := isExist(prototypeValuesFile); err != nil {
return "", err
} else if ok {
valuesFiles = append(valuesFiles, prototypeValuesFile)
}

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 @@ -47,7 +47,7 @@ func (g *GlobalYtt) Render(previousStepFile string) (string, error) {
return "", nil
}

log.Debug().Msg(g.app.Msg(helmStepName, "Global ytt applied"))
log.Debug().Msg(g.app.Msg(globalYttStepName, "Global ytt applied"))

return yttOutput.Stdout, nil
}
9 changes: 6 additions & 3 deletions internal/myks/render_ytt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package myks

import (
"errors"
"os"
"path/filepath"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -31,12 +30,16 @@ func (y *Ytt) Render(previousStepFile string) (string, error) {
}

vendorYttDir := y.app.expandPath(filepath.Join(y.app.e.g.VendorDirName, y.app.e.g.YttStepDirName))
if _, err := os.Stat(vendorYttDir); err == nil {
if ok, err := isExist(vendorYttDir); err != nil {
return "", err
} else if ok {
yttFiles = append(yttFiles, vendorYttDir)
}

prototypeYttDir := filepath.Join(y.app.Prototype, y.app.e.g.YttStepDirName)
if _, err := os.Stat(prototypeYttDir); err == nil {
if ok, err := isExist(prototypeYttDir); err != nil {
return "", err
} else if ok {
yttFiles = append(yttFiles, prototypeYttDir)
}

Expand Down
11 changes: 9 additions & 2 deletions internal/myks/smart_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (g *Globe) DetectChangedEnvsAndApps(baseRevision string) (EnvAppMap, error)
log.Err(err).Msg(g.Msg("Failed to get diff"))
return nil, err
}
log.Trace().Interface("changedFiles", changedFiles).Msg(g.Msg("Detected changes"))
envAppMap := g.runSmartMode(changedFiles)
log.Info().Msg(g.Msg("Smart mode detected changes"))
for env, apps := range envAppMap {
log.Debug().Str("env", env).Strs("apps", apps).Msg(g.Msg("Detected changes"))
}
Expand Down Expand Up @@ -89,7 +89,14 @@ func (g *Globe) runSmartMode(changedFiles ChangedFiles) EnvAppMap {

extractMatches := func(exprs []*regexp.Regexp, path string) []string {
for _, expr := range exprs {
if submatches := expr.FindStringSubmatch(path); submatches != nil {
submatches := expr.FindStringSubmatch(path)
log.Trace().
Str("pattern", expr.String()).
Str("path", path).
Bool("matched", submatches != nil).
Msg(g.Msg("Extracting submatches"))

if submatches != nil {
return submatches[1:]
}
}
Expand Down
8 changes: 6 additions & 2 deletions internal/myks/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (a *Application) prepareSync() error {
var yttFiles []string

protoVendirDir := filepath.Join(a.Prototype, "vendir")
if _, err := os.Stat(protoVendirDir); err == nil {
if ok, err := isExist(protoVendirDir); err != nil {
return err
} else if ok {
yttFiles = append(yttFiles, protoVendirDir)
}

Expand Down Expand Up @@ -186,7 +188,9 @@ func readVendirDirHashes(vendirConfigFilePath string) (vendirDirHashes, error) {
}

func readSyncFile(vendirSyncFile string) (vendirDirHashes, error) {
if _, err := os.Stat(vendirSyncFile); err != nil {
if ok, err := isExist(vendirSyncFile); err != nil {
return nil, err
} else if !ok {
return vendirDirHashes{}, nil
}

Expand Down
Loading
Loading