Skip to content

Commit d8d5ec2

Browse files
committedOct 23, 2019
Clean up library code, unit tests and documentation details
1. Move bundle funcs to /pkg as a bundle package 2. ix some minor errors in unit test. 3. Modify bundle doc to reflect recent changes + operator-sdk information + Folder structure 4. Add bundle command to opm binary as hidden commands + The bundle commands are only available for internal development use as a part of opm binary. 5. Move bundle doc under /docs/design. Signed-off-by: Vu Dinh <[email protected]>
1 parent 21c6e83 commit d8d5ec2

File tree

11 files changed

+209
-180
lines changed

11 files changed

+209
-180
lines changed
 

‎cmd/operator-cli/main.go

-35
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package bundle
22

33
import (
4-
"fmt"
5-
"os"
6-
"os/exec"
7-
"path"
8-
4+
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
95
log "github.com/sirupsen/logrus"
106
"github.com/spf13/cobra"
117
)
@@ -21,7 +17,7 @@ func newBundleBuildCmd() *cobra.Command {
2117
bundleBuildCmd := &cobra.Command{
2218
Use: "build",
2319
Short: "Build operator bundle image",
24-
Long: `The operator-cli bundle build command will generate operator
20+
Long: `The opm bundle build command will generate operator
2521
bundle metadata if needed and build bundle image with operator manifest
2622
and metadata.
2723
@@ -33,7 +29,7 @@ func newBundleBuildCmd() *cobra.Command {
3329
After the build process is completed, a container image would be built
3430
locally in docker and available to push to a container registry.
3531
36-
$ operator-cli bundle build -dir /test/0.0.1/ -t quay.io/example/operator:v0.0.1
32+
$ opm bundle build -dir /test/0.0.1/ -t quay.io/example/operator:v0.0.1
3733
3834
Note: Bundle image is not runnable.
3935
`,
@@ -55,54 +51,11 @@ func newBundleBuildCmd() *cobra.Command {
5551
return bundleBuildCmd
5652
}
5753

58-
// Create build command to build bundle manifests image
59-
func buildBundleImage(directory, imageTag, imageBuilder string) (*exec.Cmd, error) {
60-
var args []string
61-
62-
dockerfilePath := path.Join(directory, dockerFile)
63-
64-
switch imageBuilder {
65-
case "docker", "podman":
66-
args = append(args, "build", "-f", dockerfilePath, "-t", imageTag, ".")
67-
case "buildah":
68-
args = append(args, "bud", "--format=docker", "-f", dockerfilePath, "-t", imageTag, ".")
69-
default:
70-
return nil, fmt.Errorf("%s is not supported image builder", imageBuilder)
71-
}
72-
73-
return exec.Command(imageBuilder, args...), nil
74-
}
75-
76-
func executeCommand(cmd *exec.Cmd) error {
77-
cmd.Stdout = os.Stdout
78-
cmd.Stderr = os.Stderr
79-
80-
log.Debugf("Running %#v", cmd.Args)
81-
82-
if err := cmd.Run(); err != nil {
83-
return fmt.Errorf("Failed to exec %#v: %v", cmd.Args, err)
84-
}
85-
86-
return nil
87-
}
88-
8954
func buildFunc(cmd *cobra.Command, args []string) error {
90-
// Generate annotations.yaml and Dockerfile
91-
err := generateFunc(cmd, args)
92-
if err != nil {
93-
return err
94-
}
95-
96-
// Build bundle image
97-
log.Info("Building bundle image")
98-
buildCmd, err := buildBundleImage(path.Dir(path.Clean(dirBuildArgs)), tagBuildArgs, imageBuilderArgs)
55+
err := bundle.BuildFunc(dirBuildArgs, tagBuildArgs, imageBuilderArgs)
9956
if err != nil {
10057
return err
10158
}
10259

103-
if err := executeCommand(buildCmd); err != nil {
104-
return err
105-
}
106-
10760
return nil
10861
}

‎cmd/operator-cli/bundle/cmd.go ‎cmd/opm/bundle/cmd.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66

77
func NewCmd() *cobra.Command {
88
runCmd := &cobra.Command{
9-
Use: "bundle",
10-
Short: "Operator bundle commands",
11-
Long: `Generate operator bundle metadata and build bundle image.`,
9+
Hidden: true,
10+
Use: "bundle",
11+
Short: "Operator bundle commands",
12+
Long: `Generate operator bundle metadata and build bundle image.`,
1213
}
1314

1415
runCmd.AddCommand(newBundleGenerateCmd())

‎cmd/opm/bundle/generate.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package bundle
2+
3+
import (
4+
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
5+
log "github.com/sirupsen/logrus"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
const (
10+
defaultPermission = 0644
11+
registryV1Type = "registry+v1"
12+
plainType = "plain"
13+
helmType = "helm"
14+
manifestsMetadata = "manifests+metadata"
15+
annotationsFile = "annotations.yaml"
16+
dockerFile = "Dockerfile"
17+
resourcesLabel = "operators.operatorframework.io.bundle.resources"
18+
mediatypeLabel = "operators.operatorframework.io.bundle.mediatype"
19+
)
20+
21+
type AnnotationMetadata struct {
22+
Annotations AnnotationType `yaml:"annotations"`
23+
}
24+
25+
type AnnotationType struct {
26+
Resources string `yaml:"operators.operatorframework.io.bundle.resources"`
27+
MediaType string `yaml:"operators.operatorframework.io.bundle.mediatype"`
28+
}
29+
30+
// newBundleGenerateCmd returns a command that will generate operator bundle
31+
// annotations.yaml metadata
32+
func newBundleGenerateCmd() *cobra.Command {
33+
bundleGenerateCmd := &cobra.Command{
34+
Use: "generate",
35+
Short: "Generate operator bundle metadata and Dockerfile",
36+
Long: `The opm buindle generate command will generate operator
37+
bundle metadata if needed and a Dockerfile to build Operator bundle image.
38+
39+
$ opm bundle generate -d /test/0.0.1/
40+
`,
41+
RunE: generateFunc,
42+
}
43+
44+
bundleGenerateCmd.Flags().StringVarP(&dirBuildArgs, "directory", "d", "", "The directory where bundle manifests are located.")
45+
if err := bundleGenerateCmd.MarkFlagRequired("directory"); err != nil {
46+
log.Fatalf("Failed to mark `directory` flag for `generate` subcommand as required")
47+
}
48+
49+
return bundleGenerateCmd
50+
}
51+
52+
func generateFunc(cmd *cobra.Command, args []string) error {
53+
err := bundle.GenerateFunc(dirBuildArgs)
54+
if err != nil {
55+
return err
56+
}
57+
58+
return nil
59+
}

‎cmd/opm/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/sirupsen/logrus"
55
"github.com/spf13/cobra"
66

7+
"github.com/operator-framework/operator-registry/cmd/opm/bundle"
78
"github.com/operator-framework/operator-registry/cmd/opm/registry"
89
)
910

@@ -15,6 +16,7 @@ func main() {
1516
}
1617

1718
rootCmd.AddCommand(registry.NewOpmRegistryCmd())
19+
rootCmd.AddCommand(bundle.NewCmd())
1820

1921
if err := rootCmd.Execute(); err != nil {
2022
logrus.Panic(err.Error())
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Operator Bundle
22

3-
An `Operator Bundle` is a container image that stores Kubernetes manifests and metadata associated with an operator. A bundle is meant to present a specific version of an operator.
3+
An `Operator Bundle` is a container image that stores Kubernetes manifests and metadata associated with an operator. A bundle is meant to present a *specific* version of an operator.
44

55
## Operator Bundle Overview
66

@@ -13,7 +13,7 @@ The operator manifests refers to a set of Kubernetes manifest(s) the defines the
1313
* API(s) provided and required.
1414
* Related images.
1515

16-
An `Operator Bundle` is built as scratch (non-runnable) container image that contains operator manifests and specific metadata in designated directories inside the image. Then, it can be pushed and pulled from an OCI-compliant container registry. Ultimately, an operator bundle will be used by [Operator Registry](https://github.com/operator-framework/operator-registry) and [Operator-Lifecycle-Manager (OLM)](https://github.com/operator-framework/operator-lifecycle-manager) to install an operator in OLM-enabled clusters.
16+
An `Operator Bundle` is built as a scratch (non-runnable) container image that contains operator manifests and specific metadata in designated directories inside the image. Then, it can be pushed and pulled from an OCI-compliant container registry. Ultimately, an operator bundle will be used by [Operator Registry](https://github.com/operator-framework/operator-registry) and [Operator-Lifecycle-Manager (OLM)](https://github.com/operator-framework/operator-lifecycle-manager) to install an operator in OLM-enabled clusters.
1717

1818
### Bundle Annotations
1919

@@ -34,19 +34,19 @@ annotations:
3434
```
3535
3636
*Notes:*
37-
* In case of a mismatch, the `annotations.yaml` file is authoritative because on-cluster operator-registry that relies on these annotations has access to the yaml file only.
37+
* In case of a mismatch, the `annotations.yaml` file is authoritative because the on-cluster operator-registry that relies on these annotations has access to the yaml file only.
3838
* The potential use case for the `LABELS` is - an external off-cluster tool can inspect the image to check the type of a given bundle image without downloading the content.
3939

4040
This example uses [Operator Registry Manifests](https://github.com/operator-framework/operator-registry#manifest-format) format to build an operator bundle image. The source directory of an operator registry bundle has the following layout.
4141
```
4242
$ tree test
4343
test
44-
├── 0.1.0
45-
│   ├── testbackup.crd.yaml
46-
│   ├── testcluster.crd.yaml
47-
│   ├── testoperator.v0.1.0.clusterserviceversion.yaml
48-
│   └── testrestore.crd.yaml
49-
└── annotations.yaml
44+
├── testbackup.crd.yaml
45+
├── testcluster.crd.yaml
46+
├── testoperator.v0.1.0.clusterserviceversion.yaml
47+
├── testrestore.crd.yaml
48+
└── metadata
49+
└── annotations.yaml
5050
```
5151
5252
### Bundle Dockerfile
@@ -60,7 +60,7 @@ FROM scratch
6060
LABEL operators.operatorframework.io.bundle.resources=manifests+metadata
6161
LABEL operators.operatorframework.io.bundle.mediatype=registry+v1
6262

63-
ADD test/0.1.0 /manifests
63+
ADD test/*.yaml /manifests
6464
ADD test/annotations.yaml /metadata/annotations.yaml
6565
```
6666
@@ -77,68 +77,69 @@ $ tree
7777
└── annotations.yaml
7878
```
7979

80-
## Operator Bundle Command-Line Tool
80+
## Operator Bundle Commands
8181

82-
A CLI tool is available to generate Bundle annotations and Dockerfile based on provided operator manifests.
82+
Operator SDK CLI is available to generate Bundle annotations and Dockerfile based on provided operator manifests.
8383

84-
### Bundle CLI
84+
### Operator SDK CLI
8585

86-
In order to build `operator-cli` CLI tool, follow these steps:
86+
In order to use Operator SDK CLI, follow the operator-SDK installation instruction:
8787

88-
1. Clone [Operator-Lifecycle-Manager (OLM)](https://github.com/operator-framework/operator-lifecycle-manager) repository.
89-
2. Build `operator-cli` binary:
90-
```bash
91-
$ go build ./cmd/operator-cli/
92-
```
88+
1. Install the [Operator SDK CLI](https://github.com/operator-framework/operator-sdk/blob/master/doc/user/install-operator-sdk.md)
9389

9490
Now, a binary named `operator-cli` is available in OLM's directory to use.
9591
```bash
96-
$ ./operator-cli
97-
Generate operator bundle metadata and build bundle image.
92+
$ ./operator-sdk
93+
An SDK for building operators with ease
9894

9995
Usage:
100-
bundle [command]
96+
operator-sdk [command]
10197

10298
Available Commands:
103-
build Build operator bundle image
104-
generate Generate operator bundle metadata and Dockerfile
99+
bundle Operator bundle commands
105100

106101
Flags:
107-
-h, --help help for bundle
102+
-h, --help help for operator-sdk
103+
--verbose Enable verbose logging
108104

109-
Use " bundle [command] --help" for more information about a command.
105+
Use "operator-sdk [command] --help" for more information about a command.
110106
```
111107

112108
### Generate Bundle Annotations and DockerFile
113109

114-
Using `operator-cli` CLI, bundle annotations can be generated from provided operator manifests. The command for `generate` task is:
110+
Using `operator-sdk` CLI, bundle annotations can be generated from provided operator manifests. The command for `generate` task is:
115111
```bash
116-
$ ./operator-cli bundle generate --directory /test/0.0.1/
112+
$ ./operator-sdk bundle generate --directory /test/
117113
```
118-
The `--directory` or `-d` specifies the directory where the operator manifests are located. The `annotations.yaml` and `Dockerfile` are generated in the same directory where the manifests folder is located (not where the YAML manifests are located). For example:
114+
The `--directory` or `-d` specifies the directory where the operator manifests are located. The `Dockerfile` is generated in the same directory where the YAML manifests are located while the `annotations.yaml` file is located in a folder named `metadata`. For example:
119115
```bash
120116
$ tree test
121117
test
122-
├── 0.0.1
123-
│   ├── testbackup.crd.yaml
124-
│   ├── testcluster.crd.yaml
125-
│   ├── testoperator.v0.1.0.clusterserviceversion.yaml
126-
│   └── testrestore.crd.yaml
127-
── annotations.yaml
118+
├── testbackup.crd.yaml
119+
├── testcluster.crd.yaml
120+
├── testoperator.v0.1.0.clusterserviceversion.yaml
121+
├── testrestore.crd.yaml
122+
── metadata
123+
│   └── annotations.yaml
128124
└── Dockerfile
129125
```
130126

131-
Note: If there are `annotations.yaml` and `Dockerfile` existing in the directory, they will be overwritten.
127+
*Notes:*
128+
* If there are `annotations.yaml` and `Dockerfile` existing in the directory, they will be overwritten.
132129

133130
### Build Bundle Image
134131

135132
Operator bundle image can be built from provided operator manifests using `build` command:
136133
```bash
137-
$ ./operator-cli bundle build --directory /test/0.0.1/ --tag quay.io/coreos/test-operator.v0.0.1:latest
134+
$ ./operator-sdk bundle build --directory /test/0.1.0/ --tag quay.io/coreos/test-operator.v0.1.0:latest
138135
```
139136
The `--directory` or `-d` specifies the directory where the operator manifests 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.
140137

141138
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:
142139
```bash
143-
$ ./operator-cli bundle build --directory /test/0.0.1/ --tag quay.io/coreos/test-operator.v0.0.1:latest --image-builder podman
140+
$ ./operator-sdk bundle build --directory /test/0.1.0/ --tag quay.io/coreos/test-operator.v0.1.0:latest --image-builder podman
144141
```
142+
143+
*Notes:*
144+
* If there are `annotations.yaml` and `Dockerfile` existing in the directory, they will be overwritten.
145+
* The directory where the operator manifests are located must must be inside the context of the build which in this case is inside the directory where you run the command.

0 commit comments

Comments
 (0)
Please sign in to comment.