Skip to content

Commit a01de38

Browse files
committed
Add example, long help text, and rename getTypesFromDirRecursive
1 parent 4aa680b commit a01de38

File tree

6 files changed

+184
-45
lines changed

6 files changed

+184
-45
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Added
44

5+
- Added the [`generate csv --deploy-dir --apis-dir --crd-dir`](doc/cli/operator-sdk_generate_csv.md#options) flags to allow configuring input locations for operator manifests and API types directories to the CSV generator in lieu of a config. See the CLI reference doc or `generate csv -h` help text for more details. ([#2511](https://github.com/operator-framework/operator-sdk/pull/2511))
6+
- Added the [`generate csv --output-dir`](doc/cli/operator-sdk_generate_csv.md#options) flag to allow configuring the output location for the catalog directory. ([#2511](https://github.com/operator-framework/operator-sdk/pull/2511))
57
- The flag `--watch-namespace` and `--operator-namespace` was added to `operator-sdk run --local`, `operator-sdk test --local` and `operator-sdk cleanup` commands in order to replace the flag `--namespace` which was deprecated.([#2617](https://github.com/operator-framework/operator-sdk/pull/2617))
68
- The methods `ctx.GetOperatorNamespace()` and `ctx.GetWatchNamespace()` was added `pkg/test` in order to replace `ctx.GetNamespace()` which is deprecated. ([#2617](https://github.com/operator-framework/operator-sdk/pull/2617))
79
- The `--crd-version` flag was added to the `new`, `add api`, `add crd`, and `generate crds` commands so that users can opt-in to `v1` CRDs. ([#2684](https://github.com/operator-framework/operator-sdk/pull/2684))
@@ -29,6 +31,7 @@
2931

3032
- **Breaking Change:** remove `pkg/restmapper` which was deprecated in `v0.14.0`. Projects that use this package must switch to the `DynamicRESTMapper` implementation in [controller-runtime](https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/client/apiutil#NewDynamicRESTMapper). ([#2544](https://github.com/operator-framework/operator-sdk/pull/2544))
3133
- **Breaking Change:** remove deprecated `operator-sdk generate openapi` subcommand. ([#2740](https://github.com/operator-framework/operator-sdk/pull/2740))
34+
- **Breaking Change:** Removed CSV configuration file support (defaulting to deploy/olm-catalog/csv-config.yaml) in favor of specifying inputs to the generator via [`generate csv --deploy-dir --apis-dir --crd-dir`](doc/cli/operator-sdk_generate_csv.md#options), and configuring output locations via [`generate csv --output-dir`](doc/cli/operator-sdk_generate_csv.md#options). ([#2511](https://github.com/operator-framework/operator-sdk/pull/2511))
3235

3336
### Bug Fixes
3437

cmd/operator-sdk/generate/csv.go

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,88 @@ for the operator. This file is used to publish the operator to the OLM Catalog.
5555
5656
A CSV semantic version is supplied via the --csv-version flag. If your operator
5757
has already generated a CSV manifest you want to use as a base, supply its
58-
version to --from-version. Otherwise the SDK will scaffold a new CSV manifest.`,
58+
version to --from-version. Otherwise the SDK will scaffold a new CSV manifest.
59+
60+
CSV input flags:
61+
--deploy-dir: The CSV file contents will be generated from the operator manifests
62+
present in this directory.
63+
64+
--apis-dir: The CSV annotation comments will be parsed from the Go types under this path to
65+
fill out metadata for owned APIs in spec.customresourcedefinitions.owned.
66+
67+
--crd-dir: The CRD manifests are updated from this path to the CSV bundle directory.
68+
Note: The CSV generator only uses this to copy the CRD manifests.
69+
The CSV contents for spec.customresourcedefinitions.owned will still be generated
70+
from the CRD manifests in the deploy directory specified by --deploy-dir.
71+
If unset, it defaults to the same value as --deploy-dir.
72+
73+
`,
5974
Example: `
60-
TODO
61-
`,
75+
##### Generate CSV from default input paths #####
76+
$ tree pkg/apis/ deploy/
77+
pkg/apis/
78+
├── ...
79+
└── cache
80+
├── group.go
81+
└── v1alpha1
82+
├── ...
83+
└── memcached_types.go
84+
deploy/
85+
├── crds
86+
│   ├── cache.example.com_memcacheds_crd.yaml
87+
│   └── cache.example.com_v1alpha1_memcached_cr.yaml
88+
├── operator.yaml
89+
├── role.yaml
90+
├── role_binding.yaml
91+
└── service_account.yaml
92+
93+
$ operator-sdk generate csv --csv-version=0.0.1 --update-crds
94+
INFO[0000] Generating CSV manifest version 0.0.1
95+
...
96+
97+
$ tree deploy/
98+
deploy/
99+
...
100+
├── olm-catalog
101+
│   └── memcached-operator
102+
│   ├── 0.0.1
103+
│   │   ├── cache.example.com_memcacheds_crd.yaml
104+
│   │   └── memcached-operator.v0.0.1.clusterserviceversion.yaml
105+
│   └── memcached-operator.package.yaml
106+
...
107+
108+
109+
110+
##### Generate CSV from custom input paths #####
111+
$ operator-sdk generate csv --csv-version=0.0.1 --update-crds \
112+
--deploy-dir=config --apis-dir=api --output-dir=production
113+
INFO[0000] Generating CSV manifest version 0.0.1
114+
...
115+
116+
$ tree config/ api/ production/
117+
config/
118+
├── crds
119+
│   ├── cache.example.com_memcacheds_crd.yaml
120+
│   └── cache.example.com_v1alpha1_memcached_cr.yaml
121+
├── operator.yaml
122+
├── role.yaml
123+
├── role_binding.yaml
124+
└── service_account.yaml
125+
api/
126+
├── ...
127+
└── cache
128+
├── group.go
129+
└── v1alpha1
130+
├── ...
131+
└── memcached_types.go
132+
production/
133+
└── olm-catalog
134+
└── memcached-operator
135+
├── 0.0.1
136+
│   ├── cache.example.com_memcacheds_crd.yaml
137+
│   └── memcached-operator.v0.0.1.clusterserviceversion.yaml
138+
└── memcached-operator.package.yaml
139+
`,
62140

63141
RunE: func(cmd *cobra.Command, args []string) error {
64142
if len(args) != 0 {
@@ -67,6 +145,12 @@ TODO
67145
if err := c.validate(); err != nil {
68146
return fmt.Errorf("error validating command flags: %v", err)
69147
}
148+
149+
if err := projutil.CheckProjectRoot(); err != nil {
150+
log.Warn("Could not detect project root. Ensure that this command " +
151+
"runs from the project root directory.")
152+
}
153+
70154
// Default for crd dir if unset
71155
if c.crdDir == "" {
72156
c.crdDir = c.deployDir
@@ -87,22 +171,11 @@ TODO
87171
"Semantic version of an existing CSV to use as a base")
88172

89173
cmd.Flags().StringVar(&c.deployDir, "deploy-dir", "deploy",
90-
`Project relative path to root directory for operator manifests (Deployment, RBAC, CRDs).
91-
The CSV file contents will be generated from the manifests present in this directory.
92-
`)
174+
`Project relative path to root directory for operator manifests (Deployment, RBAC, CRDs)`)
93175
cmd.Flags().StringVar(&c.apisDir, "apis-dir", filepath.Join("pkg", "apis"),
94-
`Project relative path to root directory for API type defintions.
95-
The CSV annotation comments will be parsed from the Go types under this path to
96-
fill out metadata for owned APIs in spec.customresourcedefinitions.owned.
97-
`)
176+
`Project relative path to root directory for API type defintions`)
98177
cmd.Flags().StringVar(&c.crdDir, "crd-dir", "",
99-
`Project relative path to root directory for for CRD manifests.
100-
Used when --update-crds is set to copy over CRD manifests to the CSV bundle directory.
101-
Note: The CSV generator only uses this to copy the CRD manifests.
102-
The CSV contents for spec.customresourcedefinitions.owned will still be updated
103-
from the CRD manifests in the deploy directory specified by --deploy-dir.
104-
If unset, it defaults to the same value as --deploy-dir.
105-
`)
178+
`Project relative path to root directory for for CRD manifests`)
106179

107180
cmd.Flags().StringVar(&c.outputDir, "output-dir", scaffold.DeployDir,
108181
"Base directory to output generated CSV. The resulting CSV bundle directory"+

doc/cli/operator-sdk_generate_csv.md

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ 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+
CSV input flags:
15+
--deploy-dir: The CSV file contents will be generated from the operator manifests
16+
present in this directory.
17+
18+
--apis-dir: The CSV annotation comments will be parsed from the Go types under this path to
19+
fill out metadata for owned APIs in spec.customresourcedefinitions.owned.
20+
21+
--crd-dir: The CRD manifests are updated from this path to the CSV bundle directory.
22+
Note: The CSV generator only uses this to copy the CRD manifests.
23+
The CSV contents for spec.customresourcedefinitions.owned will still be generated
24+
from the CRD manifests in the deploy directory specified by --deploy-dir.
25+
If unset, it defaults to the same value as --deploy-dir.
26+
27+
28+
1429
```
1530
operator-sdk generate csv [flags]
1631
```
@@ -19,30 +34,82 @@ operator-sdk generate csv [flags]
1934

2035
```
2136
22-
TODO
23-
37+
##### Generate CSV from default input paths #####
38+
$ tree pkg/apis/ deploy/
39+
pkg/apis/
40+
├── ...
41+
└── cache
42+
├── group.go
43+
└── v1alpha1
44+
├── ...
45+
└── memcached_types.go
46+
deploy/
47+
├── crds
48+
│   ├── cache.example.com_memcacheds_crd.yaml
49+
│   └── cache.example.com_v1alpha1_memcached_cr.yaml
50+
├── operator.yaml
51+
├── role.yaml
52+
├── role_binding.yaml
53+
└── service_account.yaml
54+
55+
$ operator-sdk generate csv --csv-version=0.0.1 --update-crds
56+
INFO[0000] Generating CSV manifest version 0.0.1
57+
...
58+
59+
$ tree deploy/
60+
deploy/
61+
...
62+
├── olm-catalog
63+
│   └── memcached-operator
64+
│   ├── 0.0.1
65+
│   │   ├── cache.example.com_memcacheds_crd.yaml
66+
│   │   └── memcached-operator.v0.0.1.clusterserviceversion.yaml
67+
│   └── memcached-operator.package.yaml
68+
...
69+
70+
71+
72+
##### Generate CSV from custom input paths #####
73+
$ operator-sdk generate csv --csv-version=0.0.1 --update-crds \
74+
--deploy-dir=config --apis-dir=api --output-dir=production
75+
INFO[0000] Generating CSV manifest version 0.0.1
76+
...
77+
78+
$ tree config/ api/ production/
79+
config/
80+
├── crds
81+
│   ├── cache.example.com_memcacheds_crd.yaml
82+
│   └── cache.example.com_v1alpha1_memcached_cr.yaml
83+
├── operator.yaml
84+
├── role.yaml
85+
├── role_binding.yaml
86+
└── service_account.yaml
87+
api/
88+
├── ...
89+
└── cache
90+
├── group.go
91+
└── v1alpha1
92+
├── ...
93+
└── memcached_types.go
94+
production/
95+
└── olm-catalog
96+
└── memcached-operator
97+
├── 0.0.1
98+
│   ├── cache.example.com_memcacheds_crd.yaml
99+
│   └── memcached-operator.v0.0.1.clusterserviceversion.yaml
100+
└── memcached-operator.package.yaml
101+
24102
```
25103

26104
### Options
27105

28106
```
29-
--apis-dir string Project relative path to root directory for API type defintions.
30-
The CSV annotation comments will be parsed from the Go types under this path to
31-
fill out metadata for owned APIs in spec.customresourcedefinitions.owned.
32-
(default "pkg/apis")
33-
--crd-dir string Project relative path to root directory for for CRD manifests.
34-
Used when --update-crds is set to copy over CRD manifests to the CSV bundle directory.
35-
Note: The CSV generator only uses this to copy the CRD manifests.
36-
The CSV contents for spec.customresourcedefinitions.owned will still be updated
37-
from the CRD manifests in the deploy directory specified by --deploy-dir.
38-
If unset, it defaults to the same value as --deploy-dir.
39-
107+
--apis-dir string Project relative path to root directory for API type defintions (default "pkg/apis")
108+
--crd-dir string Project relative path to root directory for for CRD manifests
40109
--csv-channel string Channel the CSV should be registered under in the package manifest
41110
--csv-version string Semantic version of the CSV
42111
--default-channel Use the channel passed to --csv-channel as the package manifests' default channel. Only valid when --csv-channel is set
43-
--deploy-dir string Project relative path to root directory for operator manifests (Deployment, RBAC, CRDs).
44-
The CSV file contents will be generated from the manifests present in this directory.
45-
(default "deploy")
112+
--deploy-dir string Project relative path to root directory for operator manifests (Deployment, RBAC, CRDs) (default "deploy")
46113
--from-version string Semantic version of an existing CSV to use as a base
47114
-h, --help help for csv
48115
--operator-name string Operator name to use while generating CSV

internal/generate/olm-catalog/descriptor/descriptor.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func GetCRDDescriptionForGVK(apisDir string, gvk schema.GroupVersionKind) (olmap
7272
var pkgTypes []*types.Type
7373
if expectedPkgPath != "" {
7474
// Look for the pkg types at the expected single or multi group import path
75-
universe, err := getTypesFromDirRecursive(expectedPkgPath)
75+
universe, err := getPkgsFromDirRecursive(expectedPkgPath)
7676
if err != nil {
7777
return olmapiv1alpha1.CRDDescription{}, err
7878
}
@@ -88,7 +88,7 @@ func GetCRDDescriptionForGVK(apisDir string, gvk schema.GroupVersionKind) (olmap
8888
// root apisDir has no .go files.
8989
// Workaround for this is to have a doc.go file in the package.
9090
// Move away from using gengo in the future if possible.
91-
universe, err := getTypesFromDirRecursive(apisDir)
91+
universe, err := getPkgsFromDirRecursive(apisDir)
9292
if err != nil {
9393
return olmapiv1alpha1.CRDDescription{}, err
9494
}
@@ -189,9 +189,9 @@ func getExpectedPkgLayout(apisDir, group, version string) (expectedPkgPath strin
189189
return "", nil
190190
}
191191

192-
// getTypesFromDir gets all Go types from dir and recursively its sub directories.
192+
// getPkgsFromDirRecursive gets all Go types from dir and recursively its sub directories.
193193
// dir must be the project relative path to the pkg directory
194-
func getTypesFromDirRecursive(dir string) (types.Universe, error) {
194+
func getPkgsFromDirRecursive(dir string) (types.Universe, error) {
195195
if _, err := os.Stat(dir); err != nil {
196196
return nil, err
197197
}
@@ -217,7 +217,6 @@ func getTypesFromDirRecursive(dir string) (types.Universe, error) {
217217
}
218218

219219
// getTypesForPkgPath find the pkg with the given path in universe
220-
// Note that pkg path must be relative to the project root directory.
221220
func getTypesForPkgPath(pkgPath string, universe types.Universe) (pkgTypes []*types.Type, err error) {
222221
var pkg *types.Package
223222
for _, upkg := range universe {

internal/generate/olm-catalog/descriptor/descriptor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestGetKindTypeForAPI(t *testing.T) {
128128

129129
var pkgTypes []*types.Type
130130
if st.isExpectedLayout {
131-
universe, err := getTypesFromDirRecursive(expectedPkgPath)
131+
universe, err := getPkgsFromDirRecursive(expectedPkgPath)
132132
if err != nil {
133133
t.Fatalf("Failed to get universe of types from API root directory (%s): %v)", st.apisDir, err)
134134
}
@@ -138,7 +138,7 @@ func TestGetKindTypeForAPI(t *testing.T) {
138138
expectedPkgPath, st.apisDir, err)
139139
}
140140
} else {
141-
universe, err := getTypesFromDirRecursive(st.apisDir)
141+
universe, err := getPkgsFromDirRecursive(st.apisDir)
142142
if err != nil {
143143
t.Fatalf("Failed to get universe of types from API root directory (%s): %v)", st.apisDir, err)
144144
}

internal/util/projutil/project_util.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func MustInProjectRoot() {
7777

7878
// CheckProjectRoot checks if the current dir is the project root, and returns
7979
// an error if not.
80+
// TODO(hasbro17): Change this to check for go.mod
81+
// "build/Dockerfile" may not be present in all projects
8082
func CheckProjectRoot() error {
8183
// If the current directory has a "build/Dockerfile", then it is safe to say
8284
// we are at the project root.
@@ -190,11 +192,6 @@ func GetOperatorType() OperatorType {
190192
return OperatorTypeUnknown
191193
}
192194

193-
func hasGoPkgFile() bool {
194-
_, err := os.Stat("go.mod")
195-
return err == nil
196-
}
197-
198195
func IsOperatorGo() bool {
199196
_, err := os.Stat(mainFile)
200197
return err == nil

0 commit comments

Comments
 (0)