Skip to content
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

added UI metadata and cache validation to opm validate #1360

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
25 changes: 16 additions & 9 deletions alpha/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,15 @@ func (c *Channel) Validate() error {

if len(c.Bundles) == 0 {
result.subErrors = append(result.subErrors, fmt.Errorf("channel must contain at least one bundle"))
}

if len(c.Bundles) > 0 {
if err := c.validateReplacesChain(); err != nil {
} else {
head, err := c.validateReplacesChain()
if err != nil {
result.subErrors = append(result.subErrors, err)
}

if head != nil && len(head.CsvJSON) == 0 && len(head.PropertiesP.CSVMetadatas) == 0 {
result.subErrors = append(result.subErrors, fmt.Errorf("channel head %q must include a %q property for the CSV or include the %q property", head.Name, property.TypeBundleObject, property.TypeCSVMetadata))
}
}

for name, b := range c.Bundles {
Expand Down Expand Up @@ -232,10 +235,10 @@ func (c *Channel) Validate() error {
// Non-skipped entries are defined as entries that are not skipped by any other entry in the channel.
// 3. There must be no cycles in the replaces chain.
// 4. The tail entry in the replaces chain is permitted to replace a non-existent entry.
func (c *Channel) validateReplacesChain() error {
func (c *Channel) validateReplacesChain() (*Bundle, error) {
head, err := c.Head()
if err != nil {
return err
return nil, err
}

allBundles := sets.NewString()
Expand All @@ -256,18 +259,18 @@ func (c *Channel) validateReplacesChain() error {
chainFrom[k] = append(chainFrom[k], cur.Replaces)
}
if replacesChainFromHead.Has(cur.Replaces) {
return fmt.Errorf("detected cycle in replaces chain of upgrade graph: %s", strings.Join(chainFrom[cur.Replaces], " -> "))
return nil, fmt.Errorf("detected cycle in replaces chain of upgrade graph: %s", strings.Join(chainFrom[cur.Replaces], " -> "))
}
replacesChainFromHead = replacesChainFromHead.Insert(cur.Replaces)
cur = c.Bundles[cur.Replaces]
}

strandedBundles := allBundles.Difference(replacesChainFromHead).Difference(skippedBundles).List()
if len(strandedBundles) > 0 {
return fmt.Errorf("channel contains one or more stranded bundles: %s", strings.Join(strandedBundles, ", "))
return nil, fmt.Errorf("channel contains one or more stranded bundles: %s", strings.Join(strandedBundles, ", "))
}

return nil
return head, nil
}

type Bundle struct {
Expand Down Expand Up @@ -331,6 +334,10 @@ func (b *Bundle) Validate() error {
result.subErrors = append(result.subErrors, fmt.Errorf("must be exactly one property with type %q", property.TypePackage))
}

if props != nil && len(props.CSVMetadatas) > 1 {
result.subErrors = append(result.subErrors, fmt.Errorf("must be at most one property with type %q", property.TypeCSVMetadata))
}

if b.Image == "" && len(b.Objects) == 0 {
result.subErrors = append(result.subErrors, errors.New("bundle image must be set"))
}
Expand Down
24 changes: 23 additions & 1 deletion cmd/opm/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/operator-framework/operator-registry/pkg/cache"
"github.com/operator-framework/operator-registry/pkg/lib/config"
)

func NewCmd() *cobra.Command {
logger := logrus.New()
var (
logger = logrus.New()
cacheDir string
)
validate := &cobra.Command{
Use: "validate <directory>",
Short: "Validate the declarative index config",
Expand All @@ -30,9 +34,27 @@ func NewCmd() *cobra.Command {
if err := config.Validate(c.Context(), os.DirFS(directory)); err != nil {
logger.Fatal(err)
}

if cacheDir != "" {
if err := func() error {
Copy link
Contributor

@grokspawn grokspawn Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any benefit over a local, unexported function?
like

func testCache(string cacheDir) error

store, err := cache.New(cacheDir)
if err != nil {
return err
}
defer store.Close()
if err := store.CheckIntegrity(c.Context(), os.DirFS(directory)); err != nil {
return err
}
return nil
}(); err != nil {
logger.Fatal(err)
}
}
return nil
},
}

validate.Flags().StringVar(&cacheDir, "alpha-with-cache", "", "path to cache directory to validate alongside catalog")

return validate
}
Loading