Skip to content

Commit e50cfdd

Browse files
Vu Dinhdinhxuanvu
authored andcommitted
feat(validate): Add opm validate cmd to validate config files
Validate declarative config (JSON) files in a given directory. The validation is using the validation library from declcfg, model and property packages. Signed-off-by: Vu Dinh <[email protected]>
1 parent 3bc6bf5 commit e50cfdd

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

cmd/opm/root/cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/operator-framework/operator-registry/cmd/opm/index"
99
"github.com/operator-framework/operator-registry/cmd/opm/registry"
1010
"github.com/operator-framework/operator-registry/cmd/opm/serve"
11+
"github.com/operator-framework/operator-registry/cmd/opm/validate"
1112
"github.com/operator-framework/operator-registry/cmd/opm/version"
1213
)
1314

@@ -24,7 +25,7 @@ func NewCmd() *cobra.Command {
2425
},
2526
}
2627

27-
cmd.AddCommand(registry.NewOpmRegistryCmd(), alpha.NewCmd(), serve.NewCmd(), newAddCmd())
28+
cmd.AddCommand(registry.NewOpmRegistryCmd(), alpha.NewCmd(), serve.NewCmd(), newAddCmd(), validate.NewConfigValidateCmd())
2829
index.AddCommand(cmd)
2930
version.AddCommand(cmd)
3031

cmd/opm/validate/validate.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package validate
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/sirupsen/logrus"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/operator-framework/operator-registry/pkg/lib/config"
11+
)
12+
13+
func NewConfigValidateCmd() *cobra.Command {
14+
validate := &cobra.Command{
15+
Use: "validate <directory>",
16+
Short: "Validate the declarative index config",
17+
Long: "Validate the declarative config JSON file(s) in a given directory",
18+
Args: func(cmd *cobra.Command, args []string) error {
19+
return cobra.ExactArgs(1)(cmd, args)
20+
},
21+
RunE: configValidate,
22+
}
23+
24+
validate.Flags().BoolP("debug", "d", false, "enable debug log output")
25+
return validate
26+
}
27+
28+
func configValidate(cmd *cobra.Command, args []string) error {
29+
debug, err := cmd.Flags().GetBool("debug")
30+
if err != nil {
31+
return err
32+
}
33+
34+
logger := logrus.WithField("cmd", "validate")
35+
if debug {
36+
logger.Logger.SetLevel(logrus.DebugLevel)
37+
}
38+
39+
if _, err := os.Stat(args[0]); os.IsNotExist(err) {
40+
logger.Error(err.Error())
41+
}
42+
43+
err = config.ValidateConfig(args[0])
44+
if err != nil {
45+
logger.Error(err.Error())
46+
return fmt.Errorf("failed to validate config: %s", err)
47+
}
48+
49+
return nil
50+
}

pkg/lib/config/validate.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package config
2+
3+
import (
4+
"github.com/operator-framework/operator-registry/internal/declcfg"
5+
)
6+
7+
// ValidateConfig takes a directory containing the declarative config file(s)
8+
// 1. Validate if declarative config file(s) are valid based on specified schema
9+
// 2. Validate the `replaces` chains of the upgrade graph
10+
// Inputs:
11+
// directory: the directory where declarative config file(s) exist
12+
// Outputs:
13+
// error: a wrapped error that contains a list of error strings
14+
func ValidateConfig(directory string) error {
15+
// Load config files
16+
cfg, err := declcfg.LoadDir(directory)
17+
if err != nil {
18+
return err
19+
}
20+
// Validate the config using model validation
21+
_, err = declcfg.ConvertToModel(*cfg)
22+
if err != nil {
23+
return err
24+
}
25+
return nil
26+
}

0 commit comments

Comments
 (0)