Skip to content

Commit 9abdc1f

Browse files
committed
validate olm.bundle.object properties for channel heads
ensure at a valid csv on olm.bundle.properties to ensure channel is discoverable in packagemanifests and the web console Signed-off-by: Ankita Thomas <[email protected]>
1 parent dde7292 commit 9abdc1f

File tree

5 files changed

+399
-3
lines changed

5 files changed

+399
-3
lines changed

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ build/registry:
5757
build/olm:
5858
$(MAKE) $(PSM_CMD) $(OLM_CMDS) $(COLLECT_PROFILES_CMD)
5959

60+
# TODO: complete build command
6061
$(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)'"
6162
$(OPM):
62-
go build $(version_flags) $(GO_BUILD_OPTS) $(GO_BUILD_TAGS) -o $@ $(REGISTRY_PKG)/cmd/$(notdir $@)
63+
go build $(version_flags) $(GO_BUILD_OPTS) $(GO_BUILD_TAGS) -o $@ $(ROOT_PKG)/cmd/$(notdir $@)
6364

6465
$(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)'"
6566
$(REGISTRY_CMDS):
@@ -79,8 +80,8 @@ $(COLLECT_PROFILES_CMD): FORCE
7980
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)'
8081
cross:
8182
ifeq ($(shell go env GOARCH),amd64)
82-
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
83-
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
83+
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
84+
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
8485
endif
8586

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

106107
unit/registry:
107108
$(MAKE) unit WHAT=operator-registry
109+
go test $(ROOT_DIR)/pkg/validate/...
108110

109111
unit/api:
110112
$(MAKE) unit WHAT=api TARGET_NAME=test

cmd/opm/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
utilerrors "k8s.io/apimachinery/pkg/util/errors"
8+
9+
"github.com/operator-framework/operator-registry/cmd/opm/root"
10+
registrylib "github.com/operator-framework/operator-registry/pkg/registry"
11+
12+
"github.com/openshift/operator-framework-olm/cmd/opm/validate"
13+
)
14+
15+
func main() {
16+
override := map[string]*cobra.Command{"validate <directory>": validate.NewCmd()}
17+
cmd := root.NewCmd()
18+
for _, c := range cmd.Commands() {
19+
if newCmd, ok := override[c.Use]; ok {
20+
cmd.RemoveCommand(c)
21+
cmd.AddCommand(newCmd)
22+
}
23+
}
24+
25+
if err := cmd.Execute(); err != nil {
26+
agg, ok := err.(utilerrors.Aggregate)
27+
if !ok {
28+
os.Exit(1)
29+
}
30+
for _, e := range agg.Errors() {
31+
if _, ok := e.(registrylib.BundleImageAlreadyAddedErr); ok {
32+
os.Exit(2)
33+
}
34+
if _, ok := e.(registrylib.PackageVersionAlreadyAddedErr); ok {
35+
os.Exit(3)
36+
}
37+
}
38+
os.Exit(1)
39+
}
40+
}

cmd/opm/validate/validate.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package validate
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/sirupsen/logrus"
8+
"github.com/spf13/cobra"
9+
10+
dsvalidate "github.com/openshift/operator-framework-olm/pkg/validate"
11+
"github.com/operator-framework/operator-registry/cmd/opm/validate"
12+
)
13+
14+
func NewCmd() *cobra.Command {
15+
logger := logrus.New()
16+
validateCmd := validate.NewCmd()
17+
validateFn := validateCmd.RunE
18+
validateCmd.RunE = func(c *cobra.Command, args []string) error {
19+
if err := validateFn(c, args); err != nil {
20+
logger.Fatal(err)
21+
}
22+
23+
directory := args[0]
24+
s, err := os.Stat(directory)
25+
if err != nil {
26+
return err
27+
}
28+
if !s.IsDir() {
29+
return fmt.Errorf("%q is not a directory", directory)
30+
}
31+
32+
if err := dsvalidate.Validate(os.DirFS(directory)); err != nil {
33+
logger.Fatal(err)
34+
}
35+
return nil
36+
}
37+
38+
return validateCmd
39+
}

pkg/validate/validate.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package validate
2+
3+
import (
4+
"fmt"
5+
"io/fs"
6+
7+
"k8s.io/apimachinery/pkg/util/json"
8+
9+
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
10+
"github.com/operator-framework/operator-registry/alpha/declcfg"
11+
"github.com/operator-framework/operator-registry/alpha/model"
12+
"github.com/operator-framework/operator-registry/pkg/api"
13+
)
14+
15+
func Validate(root fs.FS) error {
16+
// Load config files and convert them to declcfg objects
17+
cfg, err := declcfg.LoadFS(root)
18+
if err != nil {
19+
return err
20+
}
21+
// Validate the config using model validation:
22+
// This will convert declcfg objects to intermediate model objects that are
23+
// also used for serve and add commands. The conversion process will run
24+
// validation for the model objects and ensure they are valid.
25+
mdl, err := declcfg.ConvertToModel(*cfg)
26+
if err != nil {
27+
return err
28+
}
29+
30+
if err = validatePackageManifest(mdl); err != nil {
31+
return err
32+
}
33+
return nil
34+
}
35+
36+
func validatePackageManifest(mdl model.Model) error {
37+
for _, pkg := range mdl {
38+
for _, channel := range pkg.Channels {
39+
head, err := channel.Head()
40+
if err != nil {
41+
return err
42+
}
43+
44+
if len(head.CsvJSON) == 0 {
45+
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)
46+
}
47+
bundle, err := api.ConvertModelBundleToAPIBundle(*head)
48+
if err != nil {
49+
return err
50+
}
51+
52+
csv := operatorsv1alpha1.ClusterServiceVersion{}
53+
err = json.Unmarshal([]byte(bundle.GetCsvJson()), &csv)
54+
if err != nil {
55+
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)
56+
}
57+
}
58+
}
59+
return nil
60+
}

0 commit comments

Comments
 (0)