Skip to content

Commit ce85430

Browse files
hasbro17estroz
andcommitted
generate csv: replace config with CSV and package manifest generators
internal/generate/olm-catalog: implement CSV and package manifest generators, moving most of the code from internal/scaffold/olm-catalog verbatim. Also removed CSV config doc: remove CSVConfig documentation, update CLI docs Co-authored-by: Eric Stroczynski <[email protected]>
1 parent f9f0d18 commit ce85430

File tree

22 files changed

+875
-986
lines changed

22 files changed

+875
-986
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
- Add a new option to set the minimum log level that triggers stack trace generation in logs (`--zap-stacktrace-level`) ([#2319](https://github.com/operator-framework/operator-sdk/pull/2319))
66
- Added `pkg/status` with several new types and interfaces that can be used in `Status` structs to simplify handling of [status conditions](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties). ([#1143](https://github.com/operator-framework/operator-sdk/pull/1143))
77
- Added support for relative Ansible roles and playbooks paths in the Ansible operator's file. ([#2273](https://github.com/operator-framework/operator-sdk/pull/2273))
8+
- Added the [`generate csv --include`](doc/cli/operator-sdk_generate_csv.md#options) option to include files as input to the CSV generator in lieu of a config. ([#2249](https://github.com/operator-framework/operator-sdk/pull/2249))
89

910
### Changed
1011

1112
### Deprecated
1213

1314
### Removed
1415

16+
- **Breaking Change:** Removed CSV configuration file support (defaulting to deploy/olm-catalog/csv-config.yaml) in favor of including files as input to the generator using [`generate csv --include`](doc/cli/operator-sdk_generate_csv.md#options), defaulting to the `deploy/` directory. ([#2249](https://github.com/operator-framework/operator-sdk/pull/2249))
17+
1518
### Bug Fixes
1619

1720
- Fixed issue with Go dependencies caused by removed tag in `openshift/api` repository ([#2466](https://github.com/operator-framework/operator-sdk/issues/2466))

cmd/operator-sdk/bundle/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"path/filepath"
2222
"strings"
2323

24-
catalog "github.com/operator-framework/operator-sdk/internal/scaffold/olm-catalog"
24+
catalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog"
2525
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2626

2727
"github.com/operator-framework/operator-registry/pkg/lib/bundle"

cmd/operator-sdk/generate/csv.go

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ package generate
1717
import (
1818
"fmt"
1919
"io/ioutil"
20+
"os"
2021
"path/filepath"
2122

2223
"github.com/operator-framework/operator-sdk/internal/generate/gen"
2324
gencatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog"
2425
"github.com/operator-framework/operator-sdk/internal/scaffold"
25-
"github.com/operator-framework/operator-sdk/internal/scaffold/input"
26-
catalog "github.com/operator-framework/operator-sdk/internal/scaffold/olm-catalog"
2726
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
2827
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
2928
"github.com/operator-framework/operator-sdk/internal/util/projutil"
@@ -37,8 +36,9 @@ type csvCmd struct {
3736
csvVersion string
3837
csvChannel string
3938
fromVersion string
40-
csvConfigPath string
4139
operatorName string
40+
outputDir string
41+
includePaths []string
4242
updateCRDs bool
4343
defaultChannel bool
4444
}
@@ -53,9 +53,8 @@ for the operator. This file is used to publish the operator to the OLM Catalog.
5353
5454
A CSV semantic version is supplied via the --csv-version flag. If your operator
5555
has already generated a CSV manifest you want to use as a base, supply its
56-
version to --from-version. Otherwise the SDK will scaffold a new CSV manifest.
56+
version to --from-version. Otherwise the SDK will scaffold a new CSV manifest.`,
5757

58-
Configure CSV generation by writing a config file 'deploy/olm-catalog/csv-config.yaml`,
5958
RunE: func(cmd *cobra.Command, args []string) error {
6059
// The CSV generator assumes that the deploy and pkg directories are
6160
// present at runtime, so this command must be run in a project's root.
@@ -74,14 +73,20 @@ Configure CSV generation by writing a config file 'deploy/olm-catalog/csv-config
7473
},
7574
}
7675

77-
cmd.Flags().StringVar(&c.csvVersion, "csv-version", "", "Semantic version of the CSV")
76+
cmd.Flags().StringVar(&c.csvVersion, "csv-version", "",
77+
"Semantic version of the CSV")
7878
if err := cmd.MarkFlagRequired("csv-version"); err != nil {
7979
log.Fatalf("Failed to mark `csv-version` flag for `generate csv` subcommand as required: %v", err)
8080
}
8181
cmd.Flags().StringVar(&c.fromVersion, "from-version", "",
8282
"Semantic version of an existing CSV to use as a base")
83-
cmd.Flags().StringVar(&c.csvConfigPath, "csv-config", "",
84-
"Path to CSV config file. Defaults to deploy/olm-catalog/csv-config.yaml")
83+
cmd.Flags().StringSliceVar(&c.includePaths, "include", []string{scaffold.DeployDir},
84+
"Paths to include in CSV generation, ex. \"deploy/prod,deploy/test\". "+
85+
"If this flag is set and you want to enable default behavior, "+
86+
"you must include \"deploy/\" in the argument list")
87+
cmd.Flags().StringVar(&c.outputDir, "output-dir", scaffold.DeployDir,
88+
"Base directory to output generated CSV. The resulting CSV bundle directory"+
89+
"will be \"<output-dir>/olm-catalog/<operator-name>/<csv-version>\"")
8590
cmd.Flags().BoolVar(&c.updateCRDs, "update-crds", false,
8691
"Update CRD manifests in deploy/{operator-name}/{csv-version} the using latest API's")
8792
cmd.Flags().StringVar(&c.operatorName, "operator-name", "",
@@ -97,60 +102,42 @@ Configure CSV generation by writing a config file 'deploy/olm-catalog/csv-config
97102

98103
func (c csvCmd) run() error {
99104

100-
absProjectPath := projutil.MustGetwd()
101-
cfg := &input.Config{
102-
AbsProjectPath: absProjectPath,
103-
ProjectName: filepath.Base(absProjectPath),
104-
}
105-
if projutil.IsOperatorGo() {
106-
cfg.Repo = projutil.GetGoPkg()
107-
}
108-
109105
log.Infof("Generating CSV manifest version %s", c.csvVersion)
110106

111-
csvCfg, err := catalog.GetCSVConfig(c.csvConfigPath)
112-
if err != nil {
113-
return err
114-
}
115107
if c.operatorName == "" {
116-
// Use config operator name if not set by CLI, i.e. prefer CLI value over
117-
// config value.
118-
if c.operatorName = csvCfg.OperatorName; c.operatorName == "" {
119-
// Default to using project name if both are empty.
120-
c.operatorName = filepath.Base(absProjectPath)
121-
}
108+
c.operatorName = filepath.Base(projutil.MustGetwd())
122109
}
123-
124-
s := &scaffold.Scaffold{}
125-
csv := &catalog.CSV{
126-
CSVVersion: c.csvVersion,
127-
FromVersion: c.fromVersion,
128-
ConfigFilePath: c.csvConfigPath,
129-
OperatorName: c.operatorName,
130-
}
131-
err = s.Execute(cfg, csv)
132-
if err != nil {
133-
return fmt.Errorf("catalog scaffold failed: %v", err)
110+
cfg := gen.Config{
111+
OperatorName: c.operatorName,
112+
OutputDir: c.outputDir,
113+
Filters: gen.MakeFilters(c.includePaths...),
134114
}
135115

136-
gcfg := gen.Config{
137-
OperatorName: c.operatorName,
138-
OutputDir: filepath.Join(gencatalog.OLMCatalogDir, c.operatorName),
116+
csv := gencatalog.NewCSV(cfg, c.csvVersion, c.fromVersion)
117+
if err := csv.Generate(); err != nil {
118+
return fmt.Errorf("error generating CSV: %v", err)
139119
}
140-
pkg := gencatalog.NewPackageManifest(gcfg, c.csvVersion, c.csvChannel, c.defaultChannel)
120+
pkg := gencatalog.NewPackageManifest(cfg, c.csvVersion, c.csvChannel, c.defaultChannel)
141121
if err := pkg.Generate(); err != nil {
142122
return fmt.Errorf("error generating package manifest: %v", err)
143123
}
144124

145125
// Write CRD's to the new or updated CSV package dir.
146126
if c.updateCRDs {
147-
input, err := csv.GetInput()
127+
crdManifestSet, err := findCRDs(c.includePaths...)
148128
if err != nil {
149129
return err
150130
}
151-
err = writeCRDsToDir(csvCfg.CRDCRPaths, filepath.Dir(input.Path))
152-
if err != nil {
153-
return err
131+
baseDir := c.outputDir
132+
if baseDir == "" {
133+
baseDir = gencatalog.OLMCatalogDir
134+
}
135+
bundleDir := filepath.Join(baseDir, c.operatorName, c.csvVersion)
136+
for path, b := range crdManifestSet {
137+
path = filepath.Join(bundleDir, path)
138+
if err = ioutil.WriteFile(path, b, fileutil.DefaultFileMode); err != nil {
139+
return err
140+
}
154141
}
155142
}
156143

@@ -191,25 +178,41 @@ func validateVersion(version string) error {
191178
return nil
192179
}
193180

194-
func writeCRDsToDir(crdPaths []string, toDir string) error {
195-
for _, p := range crdPaths {
196-
b, err := ioutil.ReadFile(p)
197-
if err != nil {
198-
return err
199-
}
200-
typeMeta, err := k8sutil.GetTypeMetaFromBytes(b)
181+
// findCRDs searches directories and files in paths for CRD manifest paths,
182+
// returning a map of paths to file contents.
183+
func findCRDs(paths ...string) (map[string][]byte, error) {
184+
crdFileSet := map[string][]byte{}
185+
for _, path := range paths {
186+
info, err := os.Stat(path)
201187
if err != nil {
202-
return err
203-
}
204-
if typeMeta.Kind != "CustomResourceDefinition" {
205-
continue
188+
return nil, err
206189
}
207-
208-
path := filepath.Join(toDir, filepath.Base(p))
209-
err = ioutil.WriteFile(path, b, fileutil.DefaultFileMode)
210-
if err != nil {
211-
return err
190+
if info.IsDir() {
191+
subsetPaths, err := k8sutil.GetCRDManifestPaths(path)
192+
if err != nil {
193+
return nil, err
194+
}
195+
for _, crdPath := range subsetPaths {
196+
b, err := ioutil.ReadFile(crdPath)
197+
if err != nil {
198+
return nil, err
199+
}
200+
crdFileSet[filepath.Base(crdPath)] = b
201+
}
202+
} else {
203+
b, err := ioutil.ReadFile(path)
204+
if err != nil {
205+
return nil, err
206+
}
207+
typeMeta, err := k8sutil.GetTypeMetaFromBytes(b)
208+
if err != nil {
209+
log.Infof("Skipping non-manifest file %s", path)
210+
continue
211+
}
212+
if typeMeta.Kind == "CustomResourceDefinition" {
213+
crdFileSet[filepath.Base(path)] = b
214+
}
212215
}
213216
}
214-
return nil
217+
return crdFileSet, nil
215218
}

cmd/operator-sdk/run/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"fmt"
2020
"path/filepath"
2121

22+
olmcatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog"
2223
olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator"
23-
olmcatalog "github.com/operator-framework/operator-sdk/internal/scaffold/olm-catalog"
2424
k8sinternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
2525
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2626
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags"

doc/cli/operator-sdk_generate_csv.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ A CSV semantic version is supplied via the --csv-version flag. If your operator
1111
has already generated a CSV manifest you want to use as a base, supply its
1212
version to --from-version. Otherwise the SDK will scaffold a new CSV manifest.
1313

14-
Configure CSV generation by writing a config file 'deploy/olm-catalog/csv-config.yaml
15-
1614
```
1715
operator-sdk generate csv [flags]
1816
```
@@ -21,12 +19,13 @@ operator-sdk generate csv [flags]
2119

2220
```
2321
--csv-channel string Channel the CSV should be registered under in the package manifest
24-
--csv-config string Path to CSV config file. Defaults to deploy/olm-catalog/csv-config.yaml
2522
--csv-version string Semantic version of the CSV
2623
--default-channel Use the channel passed to --csv-channel as the package manifests' default channel. Only valid when --csv-channel is set
2724
--from-version string Semantic version of an existing CSV to use as a base
2825
-h, --help help for csv
26+
--include strings Paths to include in CSV generation, ex. "deploy/prod,deploy/test". If this flag is set and you want to enable default behavior, you must include "deploy/" in the argument list (default [deploy])
2927
--operator-name string Operator name to use while generating CSV
28+
--output-dir string Base directory to output generated CSV. The resulting CSV bundle directory will be "<output-dir>/olm-catalog/<operator-name>/<csv-version>" (default "deploy")
3029
--update-crds Update CRD manifests in deploy/{operator-name}/{csv-version} the using latest API's
3130
```
3231

doc/user/olm-catalog/generating-a-csv.md

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,20 @@ This document describes how to manage the following lifecycle for your Operator
1313
Operator SDK projects have an expected [project layout][doc-project-layout]. In particular, a few manifests are expected to be present in the `deploy` directory:
1414

1515
* Roles: `role.yaml`
16+
* ClusterRoles: `cluster_role.yaml`
1617
* Deployments: `operator.yaml`
1718
* Custom Resources (CR's): `crds/<full group>_<version>_<kind>_cr.yaml`
18-
* Custom Resource Definitions (CRD's): `crds/<full group>_<resource>_crd.yaml`.
19+
* CustomResourceDefinitions (CRD's): `crds/<full group>_<resource>_crd.yaml`.
1920

20-
`generate csv` reads these files and adds their data to a CSV in an alternate form.
21-
22-
The following example config containing default values should be copied and written to `deploy/olm-catalog/csv-config.yaml`:
23-
24-
```yaml
25-
crd-cr-paths:
26-
- deploy/crds
27-
operator-path: deploy/operator.yaml
28-
role-paths:
29-
- deploy/role.yaml
30-
```
31-
32-
Explanation of all config fields:
33-
34-
- `crd-cr-paths`: list of strings - a list of CRD and CR manifest file/directory paths. Defaults to `[deploy/crds]`.
35-
- `operator-path`: string - the operator `Deployment` manifest file path. Defaults to `deploy/operator.yaml`.
36-
- `role-paths`: list of strings - Role and ClusterRole manifest file paths. Defaults to `[deploy/role.yaml]`.
37-
- `operator-name`: string - the name used to create the CSV and manifest file names. Defaults to the project's name.
38-
39-
**Note**: The [design doc][doc-csv-design] has outdated field information which should not be referenced.
40-
41-
Fields in this config file can be modified to point towards alternate manifest locations, and passed to `generate csv --csv-config=<path>` to configure CSV generation. For example, if I have one set of production CR/CRD manifests under `deploy/crds/production`, and a set of test manifests under `deploy/crds/test`, and I only want to include production manifests in my CSV, I can set `crd-cr-paths: [deploy/crds/production]`. `generate csv` will then ignore `deploy/crds/test` when getting CR/CRD data.
21+
`generate csv` extracts manifests from files in `deploy/` by default that match the kinds above and adds them to the CSV. If your manifest files are not in `deploy/`, you can use the `--include=[list of paths]` option to instruct the command to extract manifests from files at those paths, ex. `--include="deploy/prod,deploy/test"`. Setting `--include` overrides default behavior; if you still want default behavior, you must append `deploy/` to the list of paths passed to `--include`.
4222

4323
## Versioning
4424

4525
CSV's are versioned in path, file name, and in their `metadata.name` field. For example, running `operator-sdk generate csv --csv-version 0.0.1` will generate a CSV at `deploy/olm-catalog/<operator-name>/0.0.1/<operator-name>.v0.0.1.clusterserviceversion.yaml`. A versioned directory such as `deploy/olm-catalog/<operator-name>/0.0.1` is known as a [*bundle*][doc-bundle]. Versions allow the OLM to upgrade or downgrade your Operator at runtime, i.e. in a cluster. A valid semantic version is required.
4626

4727
`generate csv` allows you to upgrade your CSV using the `--from-version` flag. If you have an existing CSV with version `0.0.1` and want to write a new version `0.0.2`, you can run `operator-sdk generate csv --csv-version 0.0.2 --from-version 0.0.1`. This will write a new CSV manifest to `deploy/olm-catalog/<operator-name>/0.0.2/<operator-name>.v0.0.2.clusterserviceversion.yaml` containing user-defined data from `0.0.1` and any modifications you've made to `roles.yaml`, `operator.yaml`, CR's, or CRD's.
4828

49-
The SDK can manage CRD's in your Operator bundle as well. You can pass the `--update-crds` flag to `generate csv` to add or update your CRD's in your bundle by copying manifests pointed to by `crd-cr-paths` in your config. CRD's in a bundle are not updated by default.
29+
The SDK can manage CRD's in your Operator bundle as well. You can pass the `--update-crds` flag to `generate csv` to add or update your CRD's in your bundle by copying manifests in `deploy/crds` to your bundle. CRD's in a bundle are not updated by default.
5030

5131
## First Generation
5232

internal/generate/crd/crd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestCRDNonGo(t *testing.T) {
151151
}
152152
cfg := gen.Config{
153153
Inputs: map[string]string{
154-
CRDsDirKey: c.crdsDir,
154+
CRDsDirKey: c.crdsDir
155155
},
156156
}
157157
g := NewCRDNonGo(cfg, *r)

0 commit comments

Comments
 (0)