|
| 1 | +// Copyright 2020 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package bundle |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "io/ioutil" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + |
| 23 | + "github.com/operator-framework/operator-registry/pkg/lib/bundle" |
| 24 | + log "github.com/sirupsen/logrus" |
| 25 | + "github.com/spf13/cobra" |
| 26 | +) |
| 27 | + |
| 28 | +// newValidateCmd returns a command that will validate an operator bundle image. |
| 29 | +func newValidateCmd() *cobra.Command { |
| 30 | + c := bundleCmd{} |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "validate", |
| 33 | + Short: "Validate an operator bundle image", |
| 34 | + Long: `The 'operator-sdk bundle validate' command will validate both content and |
| 35 | +format of an operator bundle image containing operator metadata and manifests. |
| 36 | +This command will exit with a non-zero exit code if any validation tests fail. |
| 37 | +
|
| 38 | +Note: the image being validated must exist in a remote registry, not just locally.`, |
| 39 | + Example: `The following command flow will generate test-operator bundle image manifests |
| 40 | +and validate that image: |
| 41 | +
|
| 42 | +$ cd ${HOME}/go/test-operator |
| 43 | +
|
| 44 | +# Generate manifests locally. |
| 45 | +$ operator-sdk bundle build --generate-only |
| 46 | +
|
| 47 | +# Modify the metadata and Dockerfile. |
| 48 | +$ cd ./deploy/olm-catalog/test-operator |
| 49 | +$ vim ./metadata/annotations.yaml |
| 50 | +$ vim ./Dockerfile |
| 51 | +
|
| 52 | +# Build and push the image using the docker CLI. |
| 53 | +$ docker build -t quay.io/example/test-operator:v0.1.0 . |
| 54 | +$ docker push quay.io/example/test-operator:v0.1.0 |
| 55 | +
|
| 56 | +# Ensure the image with modified metadata/Dockerfile is valid. |
| 57 | +$ operator-sdk bundle validate quay.io/example/test-operator:v0.1.0`, |
| 58 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 59 | + if len(args) != 1 { |
| 60 | + return errors.New("a bundle image tag is a required argument, ex. example.com/test-operator:v0.1.0") |
| 61 | + } |
| 62 | + c.imageTag = args[0] |
| 63 | + |
| 64 | + dir, err := ioutil.TempDir("", "bundle-") |
| 65 | + if err != nil { |
| 66 | + log.Fatal(err) |
| 67 | + } |
| 68 | + defer func() { |
| 69 | + if err = os.RemoveAll(dir); err != nil { |
| 70 | + log.Error(err.Error()) |
| 71 | + } |
| 72 | + }() |
| 73 | + logger := log.WithFields(log.Fields{ |
| 74 | + "container-tool": c.imageBuilder, |
| 75 | + "bundle-dir": dir, |
| 76 | + }) |
| 77 | + log.SetLevel(log.DebugLevel) |
| 78 | + val := bundle.NewImageValidator(c.imageBuilder, logger) |
| 79 | + if err = val.PullBundleImage(c.imageTag, dir); err != nil { |
| 80 | + log.Fatalf("Error to unpacking image: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + log.Info("Validating bundle image format and contents") |
| 84 | + |
| 85 | + if err = val.ValidateBundleFormat(dir); err != nil { |
| 86 | + log.Fatalf("Bundle format validation failed: %v", err) |
| 87 | + } |
| 88 | + manifestsDir := filepath.Join(dir, bundle.ManifestsDir) |
| 89 | + if err = val.ValidateBundleContent(manifestsDir); err != nil { |
| 90 | + log.Fatalf("Bundle content validation failed: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + log.Info("All validation tests have completed successfully") |
| 94 | + |
| 95 | + return nil |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + cmd.Flags().StringVarP(&c.imageBuilder, "image-builder", "b", "docker", |
| 100 | + "Tool to extract container images. One of: [docker, podman]") |
| 101 | + |
| 102 | + return cmd |
| 103 | +} |
0 commit comments