Skip to content

Commit

Permalink
Add some validation at the rpm level
Browse files Browse the repository at this point in the history
These fields are required to build an rpm.
When the fields are not there rpmbuild gives an error about the fields,
but those field names are different so its confusing to the user.

This checks those fields early so we can give a better error message.

Signed-off-by: Brian Goff <[email protected]>
  • Loading branch information
cpuguy83 committed Feb 26, 2024
1 parent 62d4740 commit 5f2e250
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions frontend/mariner2/handle_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func tdnfCacheMountWithPrefix(prefix string) llb.RunOption {
}

func handleRPM(ctx context.Context, client gwclient.Client, spec *dalec.Spec) (gwclient.Reference, *image.Image, error) {
if err := rpm.ValidateSpec(spec); err != nil {
return nil, nil, fmt.Errorf("rpm: invalid spec: %w", err)
}

pg := dalec.ProgressGroup("Building mariner2 rpm: " + spec.Name)
sOpt, err := frontend.SourceOptFromClient(ctx, client)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions frontend/rpm/handle_buildroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func BuildrootHandler(target string) frontend.BuildFunc {

// SpecToBuildrootLLB converts a dalec.Spec to an rpm buildroot
func SpecToBuildrootLLB(spec *dalec.Spec, target string, sOpt dalec.SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error) {
if err := ValidateSpec(spec); err != nil {
return llb.Scratch(), fmt.Errorf("invalid spec: %w", err)
}
opts = append(opts, dalec.ProgressGroup("Create RPM buildroot"))
sources, err := Dalec2SourcesLLB(spec, sOpt, opts...)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions frontend/rpm/handle_rpm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package rpm

import (
"errors"
"fmt"

"github.com/Azure/dalec"
"github.com/moby/buildkit/client/llb"
)
Expand Down Expand Up @@ -35,3 +38,26 @@ func Build(topDir, workerImg llb.State, specPath string, opts ...llb.Constraints
).
AddMount("/build/out", llb.Scratch())
}

var errMissingRequiredField = errors.New("missing required field")

// ValidateSpec makes sure all the necessary fields are present in the spec to make rpmbuild work
// This validation is specific to rpmbuild.
func ValidateSpec(spec *dalec.Spec) (out error) {
if spec.Name == "" {
out = errors.Join(out, fmt.Errorf("%w: name", errMissingRequiredField))
}
if spec.Version == "" {
out = errors.Join(out, fmt.Errorf("%w: version", errMissingRequiredField))
}
if spec.Revision == "" {
out = errors.Join(out, fmt.Errorf("%w: revision", errMissingRequiredField))
}
if spec.Description == "" {
out = errors.Join(out, fmt.Errorf("%w: description", errMissingRequiredField))
}
if spec.Website == "" {
out = errors.Join(out, fmt.Errorf("%w: website", errMissingRequiredField))
}
return out
}
3 changes: 3 additions & 0 deletions frontend/rpm/handle_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func SpecHandler(target string) frontend.BuildFunc {
}

func Dalec2SpecLLB(spec *dalec.Spec, in llb.State, target, dir string, opts ...llb.ConstraintsOpt) (llb.State, error) {
if err := ValidateSpec(spec); err != nil {
return llb.Scratch(), fmt.Errorf("invalid spec: %w", err)
}
opts = append(opts, dalec.ProgressGroup("Generate RPM spec"))
buf := bytes.NewBuffer(nil)
info, _ := debug.ReadBuildInfo()
Expand Down

0 comments on commit 5f2e250

Please sign in to comment.