Skip to content

Commit e531b91

Browse files
author
Eric Stroczynski
authored
cmd/operator-sdk: add bundle validate command (#2411)
* cmd/operator-sdk/bundle: the 'bundle validate' command is a CLI wrapper for operator-registry bundle content and format validators * go.mod,go.sum: pin operator-registry commit containing validation functions * doc/cli: regenerate * CHANGELOG.md: add bundle validate addition * test/test-framework/go.sum: revendor
1 parent b24e4d3 commit e531b91

File tree

8 files changed

+171
-8
lines changed

8 files changed

+171
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Added the [`cleanup`](./doc/cli/operator-sdk_cleanup.md) subcommand and [`run --olm`](./doc/cli/operator-sdk_run.md) to manage deployment/deletion of operators. These commands currently interact with OLM via an in-cluster registry-server created using an operator's on-disk manifests and managed by `operator-sdk`. ([#2402](https://github.com/operator-framework/operator-sdk/pull/2402), [#2441](https://github.com/operator-framework/operator-sdk/pull/2441))
66
- Added [`bundle create`](./doc/cli/operator-sdk_bundle_create.md) which builds, and optionally generates metadata for, [operator bundle images](https://github.com/openshift/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md). ([#2076](https://github.com/operator-framework/operator-sdk/pull/2076), [#2438](https://github.com/operator-framework/operator-sdk/pull/2438))
7+
- Added [`bundle validate`](./doc/cli/operator-sdk_bundle_validate.md) which validates [operator bundle images](https://github.com/openshift/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md). ([#2411](https://github.com/operator-framework/operator-sdk/pull/2411))
78

89
### Changed
910

cmd/operator-sdk/bundle/cmd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ More information on operator bundle images and metadata:
3333
https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-bundle.md#docker`,
3434
}
3535

36-
cmd.AddCommand(newCreateCmd())
36+
cmd.AddCommand(
37+
newCreateCmd(),
38+
newValidateCmd(),
39+
)
3740
return cmd
3841
}
3942

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

doc/cli/operator-sdk_bundle.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ https://github.com/openshift/enhancements/blob/master/enhancements/olm/operator-
2020

2121
* [operator-sdk](operator-sdk.md) - An SDK for building operators with ease
2222
* [operator-sdk bundle create](operator-sdk_bundle_create.md) - Create an operator bundle image
23+
* [operator-sdk bundle validate](operator-sdk_bundle_validate.md) - Validate an operator bundle image
2324

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## operator-sdk bundle validate
2+
3+
Validate an operator bundle image
4+
5+
### Synopsis
6+
7+
The 'operator-sdk bundle validate' command will validate both content and
8+
format of an operator bundle image containing operator metadata and manifests.
9+
This command will exit with a non-zero exit code if any validation tests fail.
10+
11+
Note: the image being validated must exist in a remote registry, not just locally.
12+
13+
```
14+
operator-sdk bundle validate [flags]
15+
```
16+
17+
### Examples
18+
19+
```
20+
The following command flow will generate test-operator bundle image manifests
21+
and validate that image:
22+
23+
$ cd ${HOME}/go/test-operator
24+
25+
# Generate manifests locally.
26+
$ operator-sdk bundle build --generate-only
27+
28+
# Modify the metadata and Dockerfile.
29+
$ cd ./deploy/olm-catalog/test-operator
30+
$ vim ./metadata/annotations.yaml
31+
$ vim ./Dockerfile
32+
33+
# Build and push the image using the docker CLI.
34+
$ docker build -t quay.io/example/test-operator:v0.1.0 .
35+
$ docker push quay.io/example/test-operator:v0.1.0
36+
37+
# Ensure the image with modified metadata/Dockerfile is valid.
38+
$ operator-sdk bundle validate quay.io/example/test-operator:v0.1.0
39+
```
40+
41+
### Options
42+
43+
```
44+
-h, --help help for validate
45+
-b, --image-builder string Tool to extract container images. One of: [docker, podman] (default "docker")
46+
```
47+
48+
### SEE ALSO
49+
50+
* [operator-sdk bundle](operator-sdk_bundle.md) - Work with operator bundle metadata and bundle images
51+

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ require (
2424
github.com/mattn/go-isatty v0.0.8
2525
github.com/mitchellh/go-homedir v1.1.0
2626
github.com/mitchellh/mapstructure v1.1.2
27-
github.com/operator-framework/api v0.0.0-20191127212340-9066a6e95573
27+
github.com/operator-framework/api v0.0.0-20200120235816-80fd2f1a09c9
2828
github.com/operator-framework/operator-lifecycle-manager v0.0.0-20191115003340-16619cd27fa5
29-
github.com/operator-framework/operator-registry v1.5.3
29+
github.com/operator-framework/operator-registry v1.5.7-0.20200121213444-d8e2ec52c19a
3030
github.com/pborman/uuid v1.2.0
3131
github.com/pkg/errors v0.8.1
3232
github.com/prometheus/client_golang v1.2.1

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsO
525525
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
526526
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
527527
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.1/go.mod h1:F9YacGpnZbLQMzuPI0rR6op21YvNu/RjL705LJJpM3k=
528+
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
528529
github.com/mesos/mesos-go v0.0.9/go.mod h1:kPYCMQ9gsOXVAle1OsoY4I1+9kPu8GHkf88aV59fDr4=
529530
github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9VvG35NY=
530531
github.com/miekg/dns v0.0.0-20181005163659-0d29b283ac0f/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
@@ -597,19 +598,20 @@ github.com/openshift/origin v0.0.0-20160503220234-8f127d736703/go.mod h1:0Rox5r9
597598
github.com/openshift/prom-label-proxy v0.1.1-0.20191016113035-b8153a7f39f1/go.mod h1:p5MuxzsYP1JPsNGwtjtcgRHHlGziCJJfztff91nNixw=
598599
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
599600
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
600-
github.com/operator-framework/api v0.0.0-20191127212340-9066a6e95573 h1:bMO43IWWPM3HCGIiuM/GyXjtSJWsrhOlzUpZMesVUw0=
601-
github.com/operator-framework/api v0.0.0-20191127212340-9066a6e95573/go.mod h1:S5IdlJvmKkF84K2tBvsrqJbI2FVy03P88R75snpRxJo=
601+
github.com/operator-framework/api v0.0.0-20200120235816-80fd2f1a09c9 h1:HfxMEPJ0djo/RNfrmli3kI2oKS6IeuIZWu1Q5Rewt/o=
602+
github.com/operator-framework/api v0.0.0-20200120235816-80fd2f1a09c9/go.mod h1:S5IdlJvmKkF84K2tBvsrqJbI2FVy03P88R75snpRxJo=
602603
github.com/operator-framework/operator-lifecycle-manager v0.0.0-20191115003340-16619cd27fa5 h1:rjaihxY50c5C+kbQIK4s36R8zxByATYrgRbua4eiG6o=
603604
github.com/operator-framework/operator-lifecycle-manager v0.0.0-20191115003340-16619cd27fa5/go.mod h1:zL34MNy92LPutBH5gQK+gGhtgTUlZZX03I2G12vWHF4=
604-
github.com/operator-framework/operator-registry v1.5.1 h1:8ruUOG6IBDVTAXYWKsv6hwr4yv/0SFPFPAYGCpcv97E=
605605
github.com/operator-framework/operator-registry v1.5.1/go.mod h1:agrQlkWOo1q8U1SAaLSS2WQ+Z9vswNT2M2HFib9iuLY=
606-
github.com/operator-framework/operator-registry v1.5.3 h1:az83WDwgB+tHsmVn+tFq72yQBbaUAye8e4+KkDQmzLs=
607606
github.com/operator-framework/operator-registry v1.5.3/go.mod h1:agrQlkWOo1q8U1SAaLSS2WQ+Z9vswNT2M2HFib9iuLY=
607+
github.com/operator-framework/operator-registry v1.5.7-0.20200121213444-d8e2ec52c19a h1:+Kxyr2Vp1PaPAF2yzrxLu0NcxbX9O5W+mHP6w+wQ5w8=
608+
github.com/operator-framework/operator-registry v1.5.7-0.20200121213444-d8e2ec52c19a/go.mod h1:ekexcV4O8YMxdQuPb+Xco7MHfVmRIq7Jvj5e6NU7dHI=
608609
github.com/otiai10/copy v1.0.1 h1:gtBjD8aq4nychvRZ2CyJvFWAw0aja+VHazDdruZKGZA=
609610
github.com/otiai10/copy v1.0.1/go.mod h1:8bMCJrAqOtN/d9oyh5HR7HhLQMvcGMpGdwRDYsfOCHc=
610611
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
611612
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776 h1:o59bHXu8Ejas8Kq6pjoVJQ9/neN66SM8AKh6wI42BBs=
612613
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776/go.mod h1:3HNVkVOU7vZeFXocWuvtcS0XSFLcf2XUSDHkq9t1jU4=
614+
github.com/otiai10/mint v1.2.3 h1:PsrRBmrxR68kyNu6YlqYHbNlItc5vOkuS6LBEsNttVA=
613615
github.com/otiai10/mint v1.2.3/go.mod h1:YnfyPNhBvnY8bW4SGQHCs/aAFhkgySlMZbrF5U0bOVw=
614616
github.com/otiai10/mint v1.2.4 h1:DxYL0itZyPaR5Z9HILdxSoHx+gNs6Yx+neOGS3IVUk0=
615617
github.com/otiai10/mint v1.2.4/go.mod h1:d+b7n/0R3tdyUYYylALXpWQ/kTN+QobSq/4SRGBkR3M=

test/test-framework/go.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsO
456456
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
457457
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
458458
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.1/go.mod h1:F9YacGpnZbLQMzuPI0rR6op21YvNu/RjL705LJJpM3k=
459+
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
459460
github.com/mesos/mesos-go v0.0.9/go.mod h1:kPYCMQ9gsOXVAle1OsoY4I1+9kPu8GHkf88aV59fDr4=
460461
github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9VvG35NY=
461462
github.com/miekg/dns v0.0.0-20181005163659-0d29b283ac0f/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
@@ -519,10 +520,11 @@ github.com/openshift/origin v0.0.0-20160503220234-8f127d736703/go.mod h1:0Rox5r9
519520
github.com/openshift/prom-label-proxy v0.1.1-0.20191016113035-b8153a7f39f1/go.mod h1:p5MuxzsYP1JPsNGwtjtcgRHHlGziCJJfztff91nNixw=
520521
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
521522
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
522-
github.com/operator-framework/api v0.0.0-20191127212340-9066a6e95573/go.mod h1:S5IdlJvmKkF84K2tBvsrqJbI2FVy03P88R75snpRxJo=
523+
github.com/operator-framework/api v0.0.0-20200120235816-80fd2f1a09c9/go.mod h1:S5IdlJvmKkF84K2tBvsrqJbI2FVy03P88R75snpRxJo=
523524
github.com/operator-framework/operator-lifecycle-manager v0.0.0-20191115003340-16619cd27fa5/go.mod h1:zL34MNy92LPutBH5gQK+gGhtgTUlZZX03I2G12vWHF4=
524525
github.com/operator-framework/operator-registry v1.5.1/go.mod h1:agrQlkWOo1q8U1SAaLSS2WQ+Z9vswNT2M2HFib9iuLY=
525526
github.com/operator-framework/operator-registry v1.5.3/go.mod h1:agrQlkWOo1q8U1SAaLSS2WQ+Z9vswNT2M2HFib9iuLY=
527+
github.com/operator-framework/operator-registry v1.5.7-0.20200121213444-d8e2ec52c19a/go.mod h1:ekexcV4O8YMxdQuPb+Xco7MHfVmRIq7Jvj5e6NU7dHI=
526528
github.com/otiai10/copy v1.0.1/go.mod h1:8bMCJrAqOtN/d9oyh5HR7HhLQMvcGMpGdwRDYsfOCHc=
527529
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
528530
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776/go.mod h1:3HNVkVOU7vZeFXocWuvtcS0XSFLcf2XUSDHkq9t1jU4=

0 commit comments

Comments
 (0)