-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathvalidate.go
114 lines (92 loc) · 4.1 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package bundle
import (
"fmt"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/operator-framework/operator-registry/pkg/containertools"
"github.com/operator-framework/operator-registry/pkg/image"
"github.com/operator-framework/operator-registry/pkg/image/containersimageregistry"
"github.com/operator-framework/operator-registry/pkg/image/execregistry"
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
)
var (
optional string
)
func newBundleValidateCmd() *cobra.Command {
bundleValidateCmd := &cobra.Command{
Use: "validate",
Short: "Validate bundle image",
Long: `The "opm alpha bundle validate" command will validate a bundle image
from a remote source to determine if its format and content information are accurate.
Required validators. These validators will run by default on every invocation of the command.
* CSV validator - validates the CSV name and replaces fields.
* CRD validator - validates the CRDs OpenAPI V3 schema.
* Bundle validator - validates the bundle format and annotations.yaml file as well as the optional dependencies.yaml file.
Optional validators. These validators are disabled by default and can be enabled via the --optional-validators flag.
* Operatorhub validator - performs operatorhub.io validation. To validate a bundle using custom categories use with the OPERATOR_BUNDLE_CATEGORIES environmental variable to point to a json-encoded categories file.
* Bundle objects validator - performs validation on resources like PodDisruptionBudgets and PriorityClasses.
See https://olm.operatorframework.io/docs/tasks/validate-package/#validation for more info.
Note that this subcommand is deprecated and will be removed in a future release. Migrate to operator-sdk bundle validate.`,
Example: `$ opm alpha bundle validate --tag quay.io/test/test-operator:latest --image-builder docker`,
RunE: validateFunc,
Args: cobra.NoArgs,
Deprecated: "This subcommand is deprecated and will be removed in a future release. Migrate to operator-sdk bundle validate",
}
bundleValidateCmd.Flags().StringVarP(&tag, "tag", "t", "",
"The path of a registry to pull from, image name and its tag that present the bundle image (e.g. quay.io/test/test-operator:latest)")
if err := bundleValidateCmd.MarkFlagRequired("tag"); err != nil {
log.Fatalf("Failed to mark `tag` flag for `validate` subcommand as required")
}
bundleValidateCmd.Flags().StringVarP(&containerTool, "image-builder", "b", "docker", "Tool used to pull and unpack bundle images. One of: [none, docker, podman]")
bundleValidateCmd.Flags().StringVarP(&optional, "optional-validators", "o", "", "Specifies optional validations to be run. One or more of: [operatorhub, bundle-objects]")
return bundleValidateCmd
}
func validateFunc(cmd *cobra.Command, _ []string) error {
logger := log.WithFields(log.Fields{"container-tool": containerTool})
log.SetLevel(log.DebugLevel)
var (
registry image.Registry
err error
)
tool := containertools.NewContainerTool(containerTool, containertools.NoneTool)
switch tool {
case containertools.PodmanTool, containertools.DockerTool:
registry, err = execregistry.NewRegistry(tool, logger)
case containertools.NoneTool:
registry, err = containersimageregistry.NewDefault()
default:
err = fmt.Errorf("unrecognized container-tool option: %s", containerTool)
}
if err != nil {
return err
}
imageValidator := bundle.NewImageValidator(registry, logger, optional)
dir, err := os.MkdirTemp("", "bundle-")
logger.Infof("Create a temp directory at %s", dir)
if err != nil {
return err
}
defer func() {
err := os.RemoveAll(dir)
if err != nil {
logger.Error(err.Error())
}
}()
err = imageValidator.PullBundleImage(tag, dir)
if err != nil {
return err
}
logger.Info("Unpacked image layers, validating bundle image format & contents")
err = imageValidator.ValidateBundleFormat(dir)
if err != nil {
return err
}
err = imageValidator.ValidateBundleContent(filepath.Join(dir, bundle.ManifestsDir))
if err != nil {
return err
}
logger.Info("All validation tests have been completed successfully")
return nil
}