Skip to content

Commit

Permalink
Migrated the CLI to use the new validation framework
Browse files Browse the repository at this point in the history
  • Loading branch information
jdob committed Jan 19, 2024
1 parent cd93286 commit 70fd4bc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ podman run --rm -it \
volume to make the build artifacts available after the container completes. In this example, a directory named
`_build` will be created in the image configuration directory and will persist after EIB finishes. This directory
will contain subdirectories storing the respective artifacts of the different builds.
* `-validate` - If specified, the specified image definition and configuration directory will be checked to ensure
the build can proceed, however the image will not actually be built.

## Testing Images

Expand Down
41 changes: 36 additions & 5 deletions cmd/eib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"log"
"os"
"path/filepath"
"strings"

"github.com/suse-edge/edge-image-builder/pkg/build"
"github.com/suse-edge/edge-image-builder/pkg/combustion"
"github.com/suse-edge/edge-image-builder/pkg/image"
"github.com/suse-edge/edge-image-builder/pkg/image/validation"
"github.com/suse-edge/edge-image-builder/pkg/kubernetes"
audit "github.com/suse-edge/edge-image-builder/pkg/log"
"github.com/suse-edge/edge-image-builder/pkg/network"
Expand All @@ -24,18 +26,21 @@ const (
argConfigFile = "config-file"
argConfigDir = "config-dir"
argBuildDir = "build-dir"
argValidate = "validate"
)

func processArgs() (*image.Context, error) {
var (
configFile string
configDir string
rootBuildDir string
validate bool
)

flag.StringVar(&configFile, argConfigFile, "", "name of the image configuration file")
flag.StringVar(&configDir, argConfigDir, "", "full path to the image configuration directory")
flag.StringVar(&rootBuildDir, argBuildDir, "", "full path to the directory to store build artifacts")
flag.BoolVar(&validate, argValidate, false, "if specified, the image definition will be validated but not built")
flag.Parse()

imageDefinition, err := parseImageDefinition(configFile, configDir)
Expand Down Expand Up @@ -66,6 +71,37 @@ func processArgs() (*image.Context, error) {
KubernetesArtefactDownloader: kubernetes.ArtefactDownloader{},
}

failedValidations := validation.ValidateDefinition(ctx)
if len(failedValidations) > 0 {
audit.Audit("Image definition validation found the following errors:")

failuresByComponent := map[string][]validation.FailedValidation{}
logMessageBuilder := strings.Builder{}

for _, fv := range failedValidations {
failuresByComponent[fv.Component] = append(failuresByComponent[fv.Component], fv)

logMessageBuilder.WriteString(fv.UserMessage)
if fv.Error != nil {
logMessageBuilder.WriteString(fv.Error.Error())
}
}

for componentName, failures := range failuresByComponent {
audit.Audit(fmt.Sprintf(" %s", componentName))
for _, cf := range failures {
audit.Audit(fmt.Sprintf(" %s", cf.UserMessage))
}
}

zap.S().Fatalf(logMessageBuilder.String())
}

if validate {
audit.Audit("The specified image definition is valid.")
os.Exit(0)
}

if !combustion.SkipRPMComponent(ctx) {
p, err := podman.New(buildDir)
if err != nil {
Expand Down Expand Up @@ -109,11 +145,6 @@ func parseImageDefinition(configFile string, configDir string) (*image.Definitio
return nil, fmt.Errorf("error parsing definition file \"%s\": %w", configFile, err)
}

err = image.ValidateDefinition(imageDefinition)
if err != nil {
return nil, fmt.Errorf("error validating definition file: %w", err)
}

return imageDefinition, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/image/validation/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
imageComponent = "image"
imageComponent = "Image"
)

func validateImage(ctx *image.Context) []FailedValidation {
Expand Down

0 comments on commit 70fd4bc

Please sign in to comment.