Skip to content

Bug 2008027: validate olm.bundle.object properties on channel heads #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ build/registry:
build/olm:
$(MAKE) $(PSM_CMD) $(OLM_CMDS) $(COLLECT_PROFILES_CMD)

# TODO: complete build command
$(OPM): version_flags=-ldflags "-X '$(REGISTRY_PKG)/cmd/opm/version.gitCommit=$(GIT_COMMIT)' -X '$(REGISTRY_PKG)/cmd/opm/version.opmVersion=$(OPM_VERSION)' -X '$(REGISTRY_PKG)/cmd/opm/version.buildDate=$(BUILD_DATE)'"
$(OPM):
go build $(version_flags) $(GO_BUILD_OPTS) $(GO_BUILD_TAGS) -o $@ $(REGISTRY_PKG)/cmd/$(notdir $@)
go build $(version_flags) $(GO_BUILD_OPTS) $(GO_BUILD_TAGS) -o $@ $(ROOT_PKG)/cmd/$(notdir $@)

$(REGISTRY_CMDS): version_flags=-ldflags "-X '$(REGISTRY_PKG)/cmd/opm/version.gitCommit=$(GIT_COMMIT)' -X '$(REGISTRY_PKG)/cmd/opm/version.opmVersion=$(OPM_VERSION)' -X '$(REGISTRY_PKG)/cmd/opm/version.buildDate=$(BUILD_DATE)'"
$(REGISTRY_CMDS):
Expand All @@ -79,8 +80,8 @@ $(COLLECT_PROFILES_CMD): FORCE
cross: version_flags=-X '$(REGISTRY_PKG)/cmd/opm/version.gitCommit=$(GIT_COMMIT)' -X '$(REGISTRY_PKG)/cmd/opm/version.opmVersion=$(OPM_VERSION)' -X '$(REGISTRY_PKG)/cmd/opm/version.buildDate=$(BUILD_DATE)'
cross:
ifeq ($(shell go env GOARCH),amd64)
GOOS=darwin CC=o64-clang CXX=o64-clang++ CGO_ENABLED=1 go build $(GO_BUILD_OPTS) $(GO_BUILD_TAGS) -o "bin/darwin-amd64-opm" --ldflags "-extld=o64-clang $(version_flags)" $(REGISTRY_PKG)/cmd/opm
GOOS=windows CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_ENABLED=1 go build $(GO_BUILD_OPTS) $(GO_BUILD_TAGS) -o "bin/windows-amd64-opm" --ldflags "-extld=x86_64-w64-mingw32-gcc $(version_flags)" -buildmode=exe $(REGISTRY_PKG)/cmd/opm
GOOS=darwin CC=o64-clang CXX=o64-clang++ CGO_ENABLED=1 go build $(GO_BUILD_OPTS) $(GO_BUILD_TAGS) -o "bin/darwin-amd64-opm" --ldflags "-extld=o64-clang $(version_flags)" $(ROOT_PKG)/cmd/opm
GOOS=windows CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_ENABLED=1 go build $(GO_BUILD_OPTS) $(GO_BUILD_TAGS) -o "bin/windows-amd64-opm" --ldflags "-extld=x86_64-w64-mingw32-gcc $(version_flags)" -buildmode=exe $(ROOT_PKG)/cmd/opm
endif

build/olm-container:
Expand All @@ -105,6 +106,7 @@ unit/olm: bin/kubebuilder

unit/registry:
$(MAKE) unit WHAT=operator-registry
go test $(ROOT_DIR)/pkg/validate/...

unit/api:
$(MAKE) unit WHAT=api TARGET_NAME=test
Expand Down
40 changes: 40 additions & 0 deletions cmd/opm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"os"

"github.com/spf13/cobra"
utilerrors "k8s.io/apimachinery/pkg/util/errors"

"github.com/operator-framework/operator-registry/cmd/opm/root"
registrylib "github.com/operator-framework/operator-registry/pkg/registry"

"github.com/openshift/operator-framework-olm/cmd/opm/validate"
)

func main() {
override := map[string]*cobra.Command{"validate <directory>": validate.NewCmd()}
cmd := root.NewCmd()
for _, c := range cmd.Commands() {
if newCmd, ok := override[c.Use]; ok {
cmd.RemoveCommand(c)
cmd.AddCommand(newCmd)
}
}

if err := cmd.Execute(); err != nil {
agg, ok := err.(utilerrors.Aggregate)
if !ok {
os.Exit(1)
}
for _, e := range agg.Errors() {
if _, ok := e.(registrylib.BundleImageAlreadyAddedErr); ok {
os.Exit(2)
}
if _, ok := e.(registrylib.PackageVersionAlreadyAddedErr); ok {
os.Exit(3)
}
}
os.Exit(1)
}
}
39 changes: 39 additions & 0 deletions cmd/opm/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package validate

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

dsvalidate "github.com/openshift/operator-framework-olm/pkg/validate"
"github.com/operator-framework/operator-registry/cmd/opm/validate"
)

func NewCmd() *cobra.Command {
logger := logrus.New()
validateCmd := validate.NewCmd()
validateFn := validateCmd.RunE
validateCmd.RunE = func(c *cobra.Command, args []string) error {
if err := validateFn(c, args); err != nil {
logger.Fatal(err)
}

directory := args[0]
s, err := os.Stat(directory)
if err != nil {
return err
}
if !s.IsDir() {
return fmt.Errorf("%q is not a directory", directory)
}

if err := dsvalidate.Validate(os.DirFS(directory)); err != nil {
logger.Fatal(err)
}
return nil
}

return validateCmd
}
60 changes: 60 additions & 0 deletions pkg/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package validate

import (
"fmt"
"io/fs"

"k8s.io/apimachinery/pkg/util/json"

operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/model"
"github.com/operator-framework/operator-registry/pkg/api"
)

func Validate(root fs.FS) error {
// Load config files and convert them to declcfg objects
cfg, err := declcfg.LoadFS(root)
if err != nil {
return err
}
// Validate the config using model validation:
// This will convert declcfg objects to intermediate model objects that are
// also used for serve and add commands. The conversion process will run
// validation for the model objects and ensure they are valid.
mdl, err := declcfg.ConvertToModel(*cfg)
if err != nil {
return err
}

if err = validatePackageManifest(mdl); err != nil {
return err
}
return nil
}

func validatePackageManifest(mdl model.Model) error {
for _, pkg := range mdl {
for _, channel := range pkg.Channels {
head, err := channel.Head()
if err != nil {
return err
}

if len(head.CsvJSON) == 0 {
return fmt.Errorf("missing head CSV on package %s, channel %s head %s: ensure valid csv under 'olm.bundle.object' properties", pkg.Name, channel.Name, head.Name)
}
bundle, err := api.ConvertModelBundleToAPIBundle(*head)
if err != nil {
return err
}

csv := operatorsv1alpha1.ClusterServiceVersion{}
err = json.Unmarshal([]byte(bundle.GetCsvJson()), &csv)
if err != nil {
return fmt.Errorf("invalid head CSV on package %s, channel %s head %s: failed to unmarshal any 'olm.bundle.object' property as CSV JSON: %v", pkg.Name, channel.Name, head.Name, err)
}
}
}
return nil
}
Loading