Skip to content

Commit 1f920b9

Browse files
committedJan 16, 2020
Refactor bundle validation code to seperate format and contend validation
1. Seperate bundle format and content validation into 2 seperate functions 2. Remove operator-sdk reference from registry docs. Using `opm` reference instead. 3. Add bundle object validation to improve content validation Signed-off-by: Vu Dinh <[email protected]>
1 parent 7673f8e commit 1f920b9

32 files changed

+1224
-97
lines changed
 

‎cmd/opm/alpha/bundle/validate.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bundle
33
import (
44
"io/ioutil"
55
"os"
6+
"path/filepath"
67

78
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
89
log "github.com/sirupsen/logrus"
@@ -54,9 +55,14 @@ func validateFunc(cmd *cobra.Command, args []string) error {
5455
return err
5556
}
5657

57-
logger.Info("Unpacked image layers, validating bundle image contents")
58+
logger.Info("Unpacked image layers, validating bundle image format & contents")
5859

59-
err = imageValidator.ValidateBundle(dir)
60+
err = imageValidator.ValidateBundleFormat(dir)
61+
if err != nil {
62+
return err
63+
}
64+
65+
err = imageValidator.ValidateBundleFormat(filepath.Join(dir, bundle.ManifestsDir))
6066
if err != nil {
6167
return err
6268
}

‎docs/design/operator-bundle.md

+19-25
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,34 @@ $ tree
9191

9292
## Operator Bundle Commands
9393

94-
Operator SDK CLI is available to generate Bundle annotations and Dockerfile based on provided operator manifests.
94+
`opm` (Operator Package Manager) is a CLI tool to generate bundle annotations, build bundle manifests image, validate bundle manifests image and other functionalities. Please note that the `generate`, `build` and `validate` features of `opm` CLI are currently in alpha and only meant for development use.
9595

96-
### Operator SDK CLI
96+
### `opm` (Operator Package Manager)
9797

98-
In order to use Operator SDK CLI, follow the operator-SDK installation instruction:
98+
In order to use `opm` CLI, follow the `opm` build instruction:
9999

100-
1. Install the [Operator SDK CLI](https://github.com/operator-framework/operator-sdk/blob/master/doc/user/install-operator-sdk.md)
100+
1. Clone the operator registry repository:
101101

102-
Now, a binary named `operator-sdk` is available in OLM's directory to use.
103102
```bash
104-
$ ./operator-sdk
105-
An SDK for building operators with ease
106-
107-
Usage:
108-
operator-sdk [command]
103+
$ git clone https://github.com/operator-framework/operator-registry
104+
```
109105

110-
Available Commands:
111-
bundle Operator bundle commands
106+
2. Build `opm` binary using this command:
112107

113-
Flags:
114-
-h, --help help for operator-sdk
115-
--verbose Enable verbose logging
116-
117-
Use "operator-sdk [command] --help" for more information about a command.
108+
```bash
109+
$ go build ./cmd/opm/
118110
```
119111

112+
Now, a binary named `opm` is now built in current directory and ready to be used.
113+
120114
### Generate Bundle Annotations and DockerFile
121115
*Notes:*
122116
* If there are `annotations.yaml` and `Dockerfile` existing in the directory, they will be overwritten.
123117

124-
Using `operator-sdk` CLI, bundle annotations can be generated from provided operator manifests. The overall `bundle generate` command usage is:
118+
Using `opm` CLI, bundle annotations can be generated from provided operator manifests. The overall `bundle generate` command usage is:
125119
```bash
126120
Usage:
127-
operator-sdk bundle generate [flags]
121+
opm alpha bundle generate [flags]
128122

129123
Flags:
130124
-c, --channels string The list of channels that bundle image belongs to
@@ -141,7 +135,7 @@ The `--directory/-d`, `--channels/-c`, `--package/-p` are required flags while `
141135

142136
The command for `generate` task is:
143137
```bash
144-
$ ./operator-sdk bundle generate --directory /test --package test-operator \
138+
$ ./opm alpha bundle generate --directory /test --package test-operator \
145139
--channels stable,beta --default stable
146140
```
147141

@@ -173,7 +167,7 @@ $ docker build -f /path/to/Dockerfile -t quay.io/test/test-operator:latest /path
173167
Operator bundle image can be built from provided operator manifests using `build` command (see *Notes* below). The overall `bundle build` command usage is:
174168
```bash
175169
Usage:
176-
operator-SDK bundle build [flags]
170+
opm alpha bundle build [flags]
177171

178172
Flags:
179173
-c, --channels string The list of channels that bundle image belongs to
@@ -192,15 +186,15 @@ Flags:
192186
193187
The command for `build` task is:
194188
```bash
195-
$ ./operator-sdk bundle build --directory /test --tag quay.io/coreos/test-operator.v0.1.0:latest \
189+
$ ./opm alpha bundle build --directory /test --tag quay.io/coreos/test-operator.v0.1.0:latest \
196190
--package test-operator --channels stable,beta --default stable
197191
```
198192
199193
The `--directory` or `-d` specifies the directory where the operator manifests for a specific version are located. The `--tag` or `-t` specifies the image tag that you want the operator bundle image to have. By using `build` command, the `annotations.yaml` and `Dockerfile` are automatically generated in the background.
200194
201195
The default image builder is `Docker`. However, ` Buildah` and `Podman` are also supported. An image builder can specified via `--image-builder` or `-b` optional tag in `build` command. For example:
202196
```bash
203-
$ ./operator-sdk bundle build --directory /test/0.1.0/ --tag quay.io/coreos/test-operator.v0.1.0:latest \
197+
$ ./opm alpha bundle build --directory /test/0.1.0/ --tag quay.io/coreos/test-operator.v0.1.0:latest \
204198
--image-builder podman --package test-operator --channels stable,beta --default stable
205199
```
206200
@@ -215,7 +209,7 @@ The `--package` or `-p` is the name of package fo the operator such as `etcd` wh
215209
Operator bundle image can validate bundle image that is publicly available in an image registry using `validate` command (see *Notes* below). The overall `bundle validate` command usage is:
216210
```bash
217211
Usage:
218-
operator-SDK bundle validate [flags]
212+
opm alpha bundle validate [flags]
219213
220214
Flags:
221215
-t, --tag string The name of the bundle image will be built
@@ -225,7 +219,7 @@ Flags:
225219
226220
The command for `validate` task is:
227221
```bash
228-
$ ./operator-sdk bundle build --tag quay.io/coreos/test-operator.v0.1.0:latest --image-builder docker
222+
$ ./opm alpha bundle build --tag quay.io/coreos/test-operator.v0.1.0:latest --image-builder docker
229223
```
230224
231225
The `validate` command will first extract the contents of the bundle image into a temporary directory after it pulls the image from its image registry. Then, it will validate the format of bundle image to ensure manifests and metadata are located in their appropriate directories (`/manifests/` for bundle manifests files such as CSV and `/metadata/` for metadata files such as `annotations.yaml`). Also, it will validate the information in `annotations.yaml` to confirm that metadata is matching the provided data. For example, the provided media type in annotations.yaml just matches the actual media type is provided in the bundle image.

‎pkg/lib/bundle/errors.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@ import (
66
)
77

88
// ValidationError is an imlementation of the Error type
9-
// that defines a set of errors when validating the bundle
9+
// that defines a list of errors when validating the bundle
1010
type ValidationError struct {
11-
AnnotationErrors []error
12-
FormatErrors []error
11+
Errors []error
1312
}
1413

1514
func (v ValidationError) Error() string {
1615
var errs []string
17-
for _, err := range v.AnnotationErrors {
18-
errs = append(errs, err.Error())
19-
}
20-
for _, err := range v.FormatErrors {
16+
for _, err := range v.Errors {
2117
errs = append(errs, err.Error())
2218
}
2319
return fmt.Sprintf("Bundle validation errors: %s",
2420
strings.Join(errs, ","))
2521
}
2622

27-
func NewValidationError(annotationErrs, formatErrs []error) ValidationError {
23+
func NewValidationError(errs []error) ValidationError {
2824
return ValidationError{
29-
AnnotationErrors: annotationErrs,
30-
FormatErrors: formatErrs,
25+
Errors: errs,
3126
}
3227
}

‎pkg/lib/bundle/interfaces.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ type BundleImageValidator interface {
1414
PullBundleImage(imageTag string, directory string) error
1515
// Validate bundle takes a directory containing the contents of a bundle image
1616
// and validates that the format is correct
17-
ValidateBundle(directory string) error
17+
ValidateBundleFormat(directory string) error
18+
// Validate bundle takes a directory containing the contents of a bundle image
19+
// and validates that the content is correct
20+
ValidateBundleContent(directory string) error
1821
}
1922

2023
// NewImageValidator is a constructor that returns an ImageValidator
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: etcdbackups.etcd.database.coreos.com
5+
spec:
6+
group: etcd.database.coreos.com
7+
names:
8+
kind: EtcdBackup
9+
listKind: EtcdBackupList
10+
plural: etcdbackups
11+
singular: etcdbackup
12+
scope: Namespaced
13+
version: v1beta2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: etcdclusters.etcd.database.coreos.com
5+
spec:
6+
group: etcd.database.coreos.com
7+
names:
8+
kind: EtcdCluster
9+
listKind: EtcdClusterList
10+
plural: etcdclusters
11+
shortNames:
12+
- etcdclus
13+
- etcd
14+
singular: etcdcluster
15+
scope: Namespaced
16+
version: v1beta2

0 commit comments

Comments
 (0)
Please sign in to comment.