-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
centralize the kustomize walking logic, upgrade linting
- Loading branch information
Showing
15 changed files
with
372 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dotenv_if_exists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
run: | ||
tests: false | ||
|
||
linters: | ||
enable: | ||
- bodyclose | ||
- durationcheck | ||
- errcheck | ||
- gosimple | ||
- govet | ||
- ineffassign | ||
- staticcheck | ||
- unparam | ||
- unused | ||
- usestdlibvars | ||
- usetesting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
earthly 0.8.15 | ||
golang 1.22.7 | ||
golangci-lint 1.62.2 | ||
golangci-lint 1.63.4 | ||
helm 3.16.3 | ||
helm-cr 1.6.1 | ||
helm-ct 3.11.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,22 @@ | ||
package appdir | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/rs/zerolog/log" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
"github.com/zapier/kubechecks/pkg/kustomize" | ||
) | ||
|
||
type patchJson6902 struct { | ||
Path string `yaml:"path"` | ||
type processor struct { | ||
appName string | ||
result *AppDirectory | ||
} | ||
|
||
func isGoGetterIsh(s string) bool { | ||
if strings.HasPrefix(s, "github.com/") { | ||
return true | ||
} | ||
|
||
if strings.HasPrefix(s, "https://") { | ||
return true | ||
} | ||
|
||
if strings.HasPrefix(s, "http://") { | ||
return true | ||
} | ||
|
||
return false | ||
func (p *processor) AddDir(dir string) error { | ||
p.result.addDir(p.appName, dir) | ||
return nil | ||
} | ||
|
||
func walkKustomizeFiles(result *AppDirectory, fs fs.FS, appName, dirpath string) error { | ||
kustomizeFile := filepath.Join(dirpath, "kustomization.yaml") | ||
|
||
var ( | ||
err error | ||
|
||
kustomize struct { | ||
Bases []string `yaml:"bases"` | ||
Resources []string `yaml:"resources"` | ||
PatchesJson6902 []patchJson6902 `yaml:"patchesJson6902"` | ||
PatchesStrategicMerge []string `yaml:"patchesStrategicMerge"` | ||
} | ||
) | ||
|
||
reader, err := fs.Open(kustomizeFile) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
|
||
return errors.Wrap(err, "failed to open file") | ||
} | ||
|
||
bytes, err := io.ReadAll(reader) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to read file") | ||
} | ||
|
||
if err = yaml.Unmarshal(bytes, &kustomize); err != nil { | ||
return errors.Wrap(err, "failed to unmarshal file") | ||
} | ||
|
||
for _, resource := range kustomize.Resources { | ||
if isGoGetterIsh(resource) { | ||
// no reason to walk remote files, since they can't be changed | ||
continue | ||
} | ||
|
||
var relPath string | ||
if len(resource) >= 1 && resource[0] == '/' { | ||
relPath = resource[1:] | ||
} else { | ||
relPath = filepath.Join(dirpath, resource) | ||
} | ||
|
||
file, err := fs.Open(relPath) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to read %s", relPath) | ||
} | ||
stat, err := file.Stat() | ||
if err != nil { | ||
log.Warn().Err(err).Msgf("failed to stat %s", relPath) | ||
} | ||
|
||
if !stat.IsDir() { | ||
result.addFile(appName, relPath) | ||
continue | ||
} | ||
|
||
result.addDir(appName, relPath) | ||
if err = walkKustomizeFiles(result, fs, appName, relPath); err != nil { | ||
log.Warn().Err(err).Msgf("failed to read kustomize.yaml from resources in %s", relPath) | ||
} | ||
} | ||
|
||
for _, basePath := range kustomize.Bases { | ||
if isGoGetterIsh(basePath) { | ||
// no reason to walk remote files, since they can't be changed | ||
continue | ||
} | ||
|
||
relPath := filepath.Join(dirpath, basePath) | ||
result.addDir(appName, relPath) | ||
if err = walkKustomizeFiles(result, fs, appName, relPath); err != nil { | ||
log.Warn().Err(err).Msgf("failed to read kustomize.yaml from bases in %s", relPath) | ||
} | ||
} | ||
|
||
for _, patchFile := range kustomize.PatchesStrategicMerge { | ||
relPath := filepath.Join(dirpath, patchFile) | ||
result.addFile(appName, relPath) | ||
} | ||
|
||
for _, patch := range kustomize.PatchesJson6902 { | ||
relPath := filepath.Join(dirpath, patch.Path) | ||
result.addFile(appName, relPath) | ||
} | ||
|
||
func (p *processor) AddFile(file string) error { | ||
p.result.addFile(p.appName, file) | ||
return nil | ||
} | ||
|
||
var _ kustomize.Processor = new(processor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package argo_client | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/zapier/kubechecks/pkg/kustomize" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
type processor struct { | ||
repoRoot string | ||
tempDir string | ||
repoFS filesys.FileSystem | ||
} | ||
|
||
func (p processor) AddDir(s string) error { | ||
return nil | ||
} | ||
|
||
func (p processor) AddFile(relPath string) error { | ||
absDepPath := filepath.Clean(filepath.Join(p.repoRoot, relPath)) | ||
|
||
// Get relative path from repo root | ||
relPath, err := filepath.Rel(p.repoRoot, absDepPath) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to get relative path for %s", absDepPath) | ||
} | ||
|
||
// check if the file exists in the temp directory | ||
// skip copying if it exists | ||
tempPath := filepath.Join(p.tempDir, relPath) | ||
if _, err := os.Stat(tempPath); err == nil { | ||
return nil | ||
} | ||
|
||
dstdir := filepath.Dir(tempPath) | ||
if err := os.MkdirAll(dstdir, 0o777); err != nil { | ||
return errors.Wrap(err, "failed to make directories") | ||
} | ||
|
||
r, err := os.Open(absDepPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer r.Close() // ignore error: file was opened read-only. | ||
|
||
w, err := os.Create(tempPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
// Report the error, if any, from Close, but do so | ||
// only if there isn't already an outgoing error. | ||
if c := w.Close(); err == nil { | ||
err = c | ||
} | ||
}() | ||
|
||
_, err = io.Copy(w, r) | ||
return errors.Wrap(err, "failed to copy file") | ||
} | ||
|
||
var _ kustomize.Processor = new(processor) |
Oops, something went wrong.