Skip to content

Commit

Permalink
fix: reduce usage of pointers to cope with race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebradil committed Sep 13, 2023
1 parent 215ccd3 commit 0997cdd
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 58 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mykso/myks

go 1.20
go 1.21

require (
github.com/alecthomas/chroma v0.10.0
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -107,6 +108,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -139,9 +141,11 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/logrusorgru/aurora/v4 v4.0.0 h1:sRjfPpun/63iADiSvGGjgA1cAYegEWMPCJdUpJYn9JA=
github.com/logrusorgru/aurora/v4 v4.0.0/go.mod h1:lP0iIa2nrnT/qoFXcOZSrZQpJ1o6n2CUf/hyHi2Q4ZQ=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand All @@ -162,6 +166,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
Expand Down
23 changes: 10 additions & 13 deletions internal/myks/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,25 @@ var (
ApplicationLogFormat = "\033[1m[%s > %s > %s]\033[0m %s"
)

func NewApplication(e *Environment, name string, prototypeName string) (*Application, error) {
func NewApplication(e *Environment, name string, prototypeName string) (Application, error) {
if prototypeName == "" {
prototypeName = name
}

prototype := filepath.Join(e.g.PrototypesDir, prototypeName)

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

app := &Application{
app := Application{
Name: name,
Prototype: prototype,
e: e,
}
err := app.Init()
if err != nil {
return nil, err

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

return app, nil
err := app.Init()
return app, err
}

func (a *Application) Init() error {
Expand Down Expand Up @@ -146,9 +143,9 @@ func (a *Application) Msg(step string, msg string) string {
return formattedMessage
}

func (a *Application) runCmd(purpose string, cmd string, stdin io.Reader, args []string) (CmdResult, error) {
func (a *Application) runCmd(step, purpose, cmd string, stdin io.Reader, args []string) (CmdResult, error) {
return runCmd(cmd, stdin, args, func(cmd string, args []string) {
log.Debug().Msg(msgRunCmd(purpose, cmd, args))
log.Debug().Msg(a.Msg(step, msgRunCmd(purpose, cmd, args)))
})
}

Expand Down Expand Up @@ -177,7 +174,7 @@ func (a *Application) mergeValuesYaml(valueFilesYaml string) (CmdResult, error)
}, "--data-values-file="+valueFilesYaml, "--data-values-inspect")
}

func (a *Application) ytt(step string, purpose string, paths []string, args ...string) (CmdResult, error) {
func (a *Application) ytt(step, purpose string, paths []string, args ...string) (CmdResult, error) {
return a.yttS(step, purpose, paths, nil, args...)
}

Expand Down
37 changes: 17 additions & 20 deletions internal/myks/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Environment struct {
// Environment id
Id string
// Applications
Applications []*Application
Applications []Application

// Globe instance
g *Globe
Expand All @@ -34,25 +34,22 @@ type Environment struct {
foundApplications map[string]string
}

func NewEnvironment(g *Globe, dir string) *Environment {
func NewEnvironment(g *Globe, dir string) (Environment, error) {
envDataFile := filepath.Join(dir, g.EnvironmentDataFileName)

env := &Environment{
env := Environment{
Dir: dir,
EnvironmentDataFile: envDataFile,
Applications: []*Application{},
Applications: []Application{},
g: g,
renderedEnvDataFilePath: filepath.Join(dir, g.ServiceDirName, g.RenderedEnvironmentDataFileName),
foundApplications: map[string]string{},
}

// Read an environment id from an environment data file.
// The environment data file must exist and contain an .environment.id field.
if err := env.setId(); err != nil {
return nil
}

return env
err := env.setId()
return env, err
}

func (e *Environment) Init(applicationNames []string) error {
Expand All @@ -72,7 +69,7 @@ func (e *Environment) Init(applicationNames []string) error {

func (e *Environment) Sync(asyncLevel int, vendirSecrets string) error {
return process(asyncLevel, e.Applications, func(item interface{}) error {
app, ok := item.(*Application)
app, ok := item.(Application)
if !ok {
return fmt.Errorf("Unable to cast item to *Application")
}
Expand All @@ -85,15 +82,15 @@ func (e *Environment) Render(asyncLevel int) error {
return err
}
return process(asyncLevel, e.Applications, func(item interface{}) error {
app, ok := item.(*Application)
app, ok := item.(Application)
if !ok {
return fmt.Errorf("Unable to cast item to *Application")
}
yamlTemplatingTools := []YamlTemplatingTool{
&Helm{ident: "helm", app: app, additive: true},
&YttPkg{ident: "ytt-pkg", app: app, additive: true},
&Ytt{ident: "ytt", app: app, additive: false},
&GlobalYtt{ident: "global-ytt", app: app, additive: false},
&Helm{ident: "helm", app: &app, additive: true},
&YttPkg{ident: "ytt-pkg", app: &app, additive: true},
&Ytt{ident: "ytt", app: &app, additive: false},
&GlobalYtt{ident: "global-ytt", app: &app, additive: false},
}
if err := app.RenderAndSlice(yamlTemplatingTools); err != nil {
return err
Expand All @@ -108,18 +105,18 @@ func (e *Environment) SyncAndRender(asyncLevel int, vendirSecrets string) error
return err
}
return process(asyncLevel, e.Applications, func(item interface{}) error {
app, ok := item.(*Application)
app, ok := item.(Application)
if !ok {
return fmt.Errorf("Unable to cast item to *Application")
}
if err := app.Sync(vendirSecrets); err != nil {
return err
}
yamlTemplatingTools := []YamlTemplatingTool{
&Helm{ident: "helm", app: app, additive: true},
&YttPkg{ident: "ytt-pkg", app: app, additive: true},
&Ytt{ident: "ytt", app: app, additive: false},
&GlobalYtt{ident: "global-ytt", app: app, additive: false},
&Helm{ident: "helm", app: &app, additive: true},
&YttPkg{ident: "ytt-pkg", app: &app, additive: true},
&Ytt{ident: "ytt", app: &app, additive: false},
&GlobalYtt{ident: "global-ytt", app: &app, additive: false},
}
if err := app.RenderAndSlice(yamlTemplatingTools); err != nil {
return err
Expand Down
29 changes: 16 additions & 13 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type Globe struct {
GitRepoUrl string

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

// Extra ytt file paths
extraYttPaths []string
Expand All @@ -129,7 +129,7 @@ type VendirCredentials struct {
func New(rootDir string) *Globe {
g := &Globe{
RootDir: rootDir,
environments: make(map[string]*Environment),
environments: make(map[string]Environment),
}
if err := defaults.Set(g); err != nil {
log.Fatal().Err(err).Msg("Unable to set defaults")
Expand Down Expand Up @@ -171,9 +171,9 @@ func (g *Globe) Init(asyncLevel int, searchPaths []string, applicationNames []st
g.collectEnvironments(searchPaths)

return process(asyncLevel, g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
env, ok := item.(Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
return fmt.Errorf("Unable to cast item to Environment")
}
return env.Init(applicationNames)
})
Expand All @@ -185,19 +185,19 @@ func (g *Globe) Sync(asyncLevel int) error {
return err
}
return process(asyncLevel, g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
env, ok := item.(Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
return fmt.Errorf("Unable to cast item to Environment")
}
return env.Sync(asyncLevel, vendirSecrets)
})
}

func (g *Globe) Render(asyncLevel int) error {
return process(asyncLevel, g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
env, ok := item.(Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
return fmt.Errorf("Unable to cast item to Environment")
}
return env.Render(asyncLevel)
})
Expand All @@ -209,9 +209,9 @@ func (g *Globe) SyncAndRender(asyncLevel int) error {
return err
}
return process(asyncLevel, g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
env, ok := item.(Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
return fmt.Errorf("Unable to cast item to Environment")
}
return env.SyncAndRender(asyncLevel, vendirSecrets)
})
Expand Down Expand Up @@ -399,11 +399,14 @@ func (g *Globe) collectEnvironmentsInPath(searchPath string) {
if d != nil && d.IsDir() {
_, err := os.Stat(filepath.Join(path, g.EnvironmentDataFileName))
if err == nil {
env := NewEnvironment(g, path)
if env != nil {
env, err := NewEnvironment(g, path)
if err == nil {
g.environments[path] = env
} else {
log.Debug().Str("path", path).Msg("Unable to collect environment, might be base or parent environment. Skipping")
log.Debug().
Err(err).
Str("path", path).
Msg("Unable to collect environment, might be base or parent environment. Skipping")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/myks/render_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (h *Helm) Render(_ string) (string, error) {
helmArgs = append(helmArgs, "--values", helmValuesFile)
}

res, err := h.app.runCmd("helm template chart", "helm", nil, append(helmArgs, commonHelmArgs...))
res, err := h.app.runCmd(helmStepName, "helm template chart", "helm", nil, append(helmArgs, commonHelmArgs...))
if err != nil {
log.Error().Msg(h.app.Msg(helmStepName, res.Stderr))
return "", err
Expand Down
2 changes: 1 addition & 1 deletion internal/myks/smart_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (g *Globe) InitSmartMode() ([]string, []string, error) {
g.collectEnvironments(nil)

err := process(0, g.environments, func(item interface{}) error {
env, ok := item.(*Environment)
env, ok := item.(Environment)
if !ok {
return fmt.Errorf("Unable to cast item to *Environment")
}
Expand Down
8 changes: 4 additions & 4 deletions internal/myks/smart_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func TestGlobe_findBaseAppUsage(t *testing.T) {
args{
[]string{"app1"},
Globe{
environments: map[string]*Environment{
environments: map[string]Environment{
"env1": {
Id: "env1",
foundApplications: map[string]string{
Expand All @@ -280,7 +280,7 @@ func TestGlobe_findBaseAppUsage(t *testing.T) {
args{
[]string{"app1", "app2"},
Globe{
environments: map[string]*Environment{
environments: map[string]Environment{
"env1": {
Id: "env1",
foundApplications: map[string]string{
Expand All @@ -299,7 +299,7 @@ func TestGlobe_findBaseAppUsage(t *testing.T) {
args{
[]string{"app1", "app2"},
Globe{
environments: map[string]*Environment{
environments: map[string]Environment{
"env1": {
Id: "env1",
foundApplications: map[string]string{
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestGlobe_findBaseAppUsage(t *testing.T) {

func TestGlobe_runSmartMode(t *testing.T) {
g := createGlobe(t)
g.environments = map[string]*Environment{
g.environments = map[string]Environment{
"envs/env1": {
Id: "env1",
foundApplications: map[string]string{
Expand Down
5 changes: 2 additions & 3 deletions internal/myks/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func (a *Application) prepareSync() error {
yttFiles = append(yttFiles, appVendirDirs...)

if len(yttFiles) == 0 {
err := ErrNoVendirConfig
return err
return ErrNoVendirConfig
}

vendirConfig, err := a.ytt(syncStepName, "creating vendir config", yttFiles)
Expand Down Expand Up @@ -154,7 +153,7 @@ func (a *Application) runVendirSync(targetDir string, vendirConfig string, vendi
if directory != "" {
args = append(args, "--directory="+directory)
}
res, err := a.runCmd("vendir sync", "vendir", strings.NewReader(vendirSecrets), args)
res, err := a.runCmd(syncStepName, "vendir sync", "vendir", strings.NewReader(vendirSecrets), args)
if err != nil {
log.Error().Err(err).Str("stdout", res.Stdout).Str("stderr", res.Stderr).Msg(a.Msg(syncStepName, "Unable to sync vendir"))
return err
Expand Down
4 changes: 2 additions & 2 deletions internal/myks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func msgRunCmd(purpose string, cmd string, args []string) string {
return "Running \u001B[34m" + cmd + "\u001B[0m to: \u001B[3m" + purpose + "\u001B[0m\n\u001B[37m" + msg + "\u001B[0m"
}

func runYttWithFilesAndStdin(paths []string, stdin io.Reader, log func(name string, args []string), args ...string) (CmdResult, error) {
func runYttWithFilesAndStdin(paths []string, stdin io.Reader, logFn func(name string, args []string), args ...string) (CmdResult, error) {
if stdin != nil {
paths = append(paths, "-")
}
Expand All @@ -283,7 +283,7 @@ func runYttWithFilesAndStdin(paths []string, stdin io.Reader, log func(name stri
}

cmdArgs = append(cmdArgs, args...)
return runCmd("ytt", stdin, cmdArgs, log)
return runCmd("ytt", stdin, cmdArgs, logFn)
}

func extract[T any](items []T, filterFunc func(cf T) bool) []T {
Expand Down

0 comments on commit 0997cdd

Please sign in to comment.