refactor(recipe): load load compoes config where its used

This commit is contained in:
2024-07-08 12:31:39 +02:00
parent 99da8d4e57
commit c1b03bcbd7
16 changed files with 263 additions and 230 deletions

View File

@ -19,13 +19,13 @@ import (
var Warn = "warn"
var Critical = "critical"
type LintFunction func(recipe.Recipe) (bool, error)
type LintFunction func(recipe.Recipe2) (bool, error)
// SkipFunction determines whether the LintFunction is run or not. It should
// not take the lint rule level into account because some rules are always an
// error but may depend on some additional context of the recipe configuration.
// This function aims to cover those additional cases.
type SkipFunction func(recipe.Recipe) (bool, error)
type SkipFunction func(recipe.Recipe2) (bool, error)
// LintRule is a linting rule which helps a recipe maintainer avoid common
// problems in their recipe configurations. We aim to highlight things that
@ -42,7 +42,7 @@ type LintRule struct {
}
// Skip implements the SkipFunction for the lint rule.
func (l LintRule) Skip(recipe recipe.Recipe) bool {
func (l LintRule) Skip(recipe recipe.Recipe2) bool {
if l.SkipCondition != nil {
ok, err := l.SkipCondition(recipe)
if err != nil {
@ -173,7 +173,7 @@ var LintRules = map[string][]LintRule{
// LintForErrors lints specifically for errors and not other levels. This is
// used in code paths such as "app deploy" to avoid nasty surprises but not for
// the typical linting commands, which do handle other levels.
func LintForErrors(recipe recipe.Recipe) error {
func LintForErrors(recipe recipe.Recipe2) error {
log.Debugf("linting for critical errors in %s configs", recipe.Name)
for level := range LintRules {
@ -201,15 +201,19 @@ func LintForErrors(recipe recipe.Recipe) error {
return nil
}
func LintComposeVersion(recipe recipe.Recipe) (bool, error) {
if recipe.Config.Version == "3.8" {
func LintComposeVersion(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
if config.Version == "3.8" {
return true, nil
}
return true, nil
}
func LintEnvConfigPresent(r recipe.Recipe) (bool, error) {
func LintEnvConfigPresent(r recipe.Recipe2) (bool, error) {
r2 := recipe.Get2(r.Name)
if _, err := os.Stat(r2.SampleEnvPath); !os.IsNotExist(err) {
return true, nil
@ -218,8 +222,12 @@ func LintEnvConfigPresent(r recipe.Recipe) (bool, error) {
return false, nil
}
func LintAppService(recipe recipe.Recipe) (bool, error) {
for _, service := range recipe.Config.Services {
func LintAppService(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for _, service := range config.Services {
if service.Name == "app" {
return true, nil
}
@ -232,7 +240,7 @@ func LintAppService(recipe recipe.Recipe) (bool, error) {
// confirms that there is no "DOMAIN=..." in the .env.sample configuration of
// the recipe. This typically means that no domain is required to deploy and
// therefore no matching traefik deploy label will be present.
func LintTraefikEnabledSkipCondition(r recipe.Recipe) (bool, error) {
func LintTraefikEnabledSkipCondition(r recipe.Recipe2) (bool, error) {
r2 := recipe.Get2(r.Name)
sampleEnv, err := r2.SampleEnv()
if err != nil {
@ -246,8 +254,12 @@ func LintTraefikEnabledSkipCondition(r recipe.Recipe) (bool, error) {
return false, nil
}
func LintTraefikEnabled(recipe recipe.Recipe) (bool, error) {
for _, service := range recipe.Config.Services {
func LintTraefikEnabled(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for _, service := range config.Services {
for label := range service.Deploy.Labels {
if label == "traefik.enable" {
if service.Deploy.Labels[label] == "true" {
@ -260,8 +272,12 @@ func LintTraefikEnabled(recipe recipe.Recipe) (bool, error) {
return false, nil
}
func LintHealthchecks(recipe recipe.Recipe) (bool, error) {
for _, service := range recipe.Config.Services {
func LintHealthchecks(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for _, service := range config.Services {
if service.HealthCheck == nil {
return false, nil
}
@ -270,8 +286,12 @@ func LintHealthchecks(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintAllImagesTagged(recipe recipe.Recipe) (bool, error) {
for _, service := range recipe.Config.Services {
func LintAllImagesTagged(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for _, service := range config.Services {
img, err := reference.ParseNormalizedNamed(service.Image)
if err != nil {
return false, err
@ -284,8 +304,12 @@ func LintAllImagesTagged(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintNoUnstableTags(recipe recipe.Recipe) (bool, error) {
for _, service := range recipe.Config.Services {
func LintNoUnstableTags(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for _, service := range config.Services {
img, err := reference.ParseNormalizedNamed(service.Image)
if err != nil {
return false, err
@ -307,8 +331,12 @@ func LintNoUnstableTags(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintSemverLikeTags(recipe recipe.Recipe) (bool, error) {
for _, service := range recipe.Config.Services {
func LintSemverLikeTags(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for _, service := range config.Services {
img, err := reference.ParseNormalizedNamed(service.Image)
if err != nil {
return false, err
@ -330,8 +358,12 @@ func LintSemverLikeTags(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintImagePresent(recipe recipe.Recipe) (bool, error) {
for _, service := range recipe.Config.Services {
func LintImagePresent(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for _, service := range config.Services {
if service.Image == "" {
return false, nil
}
@ -339,7 +371,7 @@ func LintImagePresent(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintHasPublishedVersion(recipe recipe.Recipe) (bool, error) {
func LintHasPublishedVersion(recipe recipe.Recipe2) (bool, error) {
catl, err := recipePkg.ReadRecipeCatalogue(false)
if err != nil {
log.Fatal(err)
@ -357,7 +389,7 @@ func LintHasPublishedVersion(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintMetadataFilledIn(r recipe.Recipe) (bool, error) {
func LintMetadataFilledIn(r recipe.Recipe2) (bool, error) {
r2 := recipe.Get2(r.Name)
features, category, err := recipe.GetRecipeFeaturesAndCategory(r2)
if err != nil {
@ -379,10 +411,14 @@ func LintMetadataFilledIn(r recipe.Recipe) (bool, error) {
return true, nil
}
func LintAbraShVendors(recipe recipe.Recipe) (bool, error) {
for _, service := range recipe.Config.Services {
func LintAbraShVendors(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for _, service := range config.Services {
if len(service.Configs) > 0 {
abraSh := path.Join(config.RECIPES_DIR, recipe.Name, "abra.sh")
abraSh := path.Join(recipe.Dir, "abra.sh")
if _, err := os.Stat(abraSh); err != nil {
if os.IsNotExist(err) {
return false, err
@ -394,7 +430,7 @@ func LintAbraShVendors(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintHasRecipeRepo(recipe recipe.Recipe) (bool, error) {
func LintHasRecipeRepo(recipe recipe.Recipe2) (bool, error) {
url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, recipe.Name)
res, err := http.Get(url)
@ -409,8 +445,12 @@ func LintHasRecipeRepo(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintSecretLengths(recipe recipe.Recipe) (bool, error) {
for name := range recipe.Config.Secrets {
func LintSecretLengths(recipe recipe.Recipe2) (bool, error) {
config, err := recipe.GetComposeConfig(nil)
if err != nil {
return false, err
}
for name := range config.Secrets {
if len(name) > 12 {
return false, fmt.Errorf("secret %s is longer than 12 characters", name)
}
@ -419,7 +459,7 @@ func LintSecretLengths(recipe recipe.Recipe) (bool, error) {
return true, nil
}
func LintValidTags(recipe recipe.Recipe) (bool, error) {
func LintValidTags(recipe recipe.Recipe2) (bool, error) {
recipeDir := path.Join(config.RECIPES_DIR, recipe.Name)
repo, err := git.PlainOpen(recipeDir)