Skip to content

Commit d4c4637

Browse files
committed
refactor Package and CSV generators to use Inputs and OutputDir
1 parent 3cc87b4 commit d4c4637

File tree

8 files changed

+102
-89
lines changed

8 files changed

+102
-89
lines changed

cmd/operator-sdk/generate/csv.go

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type csvCmd struct {
3838
fromVersion string
3939
operatorName string
4040
outputDir string
41-
includePaths []string
41+
inputs map[string]string
4242
updateCRDs bool
4343
defaultChannel bool
4444
}
@@ -80,10 +80,18 @@ version to --from-version. Otherwise the SDK will scaffold a new CSV manifest.`,
8080
}
8181
cmd.Flags().StringVar(&c.fromVersion, "from-version", "",
8282
"Semantic version of an existing CSV to use as a base")
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")
83+
cmd.Flags().StringToStringVar(&c.inputs, "inputs",
84+
map[string]string{
85+
gencatalog.DeployDirKey: "deploy",
86+
gencatalog.APIsDirKey: "pkg/apis",
87+
},
88+
`Key value input paths used in CSV generation.
89+
Use this to set custom paths for operator manifests and API type definitions
90+
E.g: --inputs deploy=config/production,apis=pkg/myapp/apis
91+
Supported input keys:
92+
- deploy=<path to root directory for operator manifests (Deployment, RBAC, CRDs)>
93+
- apis=<path to root directory for API type defintions>
94+
`)
8795
cmd.Flags().StringVar(&c.outputDir, "output-dir", scaffold.DeployDir,
8896
"Base directory to output generated CSV. The resulting CSV bundle directory"+
8997
"will be \"<output-dir>/olm-catalog/<operator-name>/<csv-version>\"")
@@ -109,8 +117,8 @@ func (c csvCmd) run() error {
109117
}
110118
cfg := gen.Config{
111119
OperatorName: c.operatorName,
120+
Inputs: c.inputs,
112121
OutputDir: c.outputDir,
113-
Filters: gen.MakeFilters(c.includePaths...),
114122
}
115123

116124
csv := gencatalog.NewCSV(cfg, c.csvVersion, c.fromVersion)
@@ -124,19 +132,19 @@ func (c csvCmd) run() error {
124132

125133
// Write CRD's to the new or updated CSV package dir.
126134
if c.updateCRDs {
127-
crdManifestSet, err := findCRDs(c.includePaths...)
135+
// TODO: Any requirement to update CRD's from any place other
136+
// than the input "deploy" directory?
137+
// Can CRD manifests be present outside of the "deploy" directory?
138+
crdManifestSet, err := findCRDFileSet(c.inputs[gencatalog.DeployDirKey])
128139
if err != nil {
129-
return err
140+
return fmt.Errorf("failed to update CRD's: %v", err)
130141
}
131-
baseDir := c.outputDir
132-
if baseDir == "" {
133-
baseDir = gencatalog.OLMCatalogDir
134-
}
135-
bundleDir := filepath.Join(baseDir, c.operatorName, c.csvVersion)
142+
// TODO: This path should come from the CSV generator
143+
bundleDir := filepath.Join(c.outputDir, gencatalog.OLMCatalogChildDir, c.operatorName, c.csvVersion)
136144
for path, b := range crdManifestSet {
137145
path = filepath.Join(bundleDir, path)
138146
if err = ioutil.WriteFile(path, b, fileutil.DefaultFileMode); err != nil {
139-
return err
147+
return fmt.Errorf("failed to update CRD's: %v", err)
140148
}
141149
}
142150
}
@@ -178,41 +186,28 @@ func validateVersion(version string) error {
178186
return nil
179187
}
180188

181-
// findCRDs searches directories and files in paths for CRD manifest paths,
189+
// findCRDFileSet searches all directories and files in path for CRD manifests,
182190
// returning a map of paths to file contents.
183-
func findCRDs(paths ...string) (map[string][]byte, error) {
191+
func findCRDFileSet(path string) (map[string][]byte, error) {
184192
crdFileSet := map[string][]byte{}
185-
for _, path := range paths {
186-
info, err := os.Stat(path)
193+
info, err := os.Stat(path)
194+
if err != nil {
195+
return nil, err
196+
}
197+
if !info.IsDir() {
198+
return nil, fmt.Errorf("CRD's must be read from a directory. %s is a file", path)
199+
}
200+
201+
crdPaths, err := k8sutil.GetCRDManifestPaths(path)
202+
if err != nil {
203+
return nil, err
204+
}
205+
for _, crdPath := range crdPaths {
206+
b, err := ioutil.ReadFile(crdPath)
187207
if err != nil {
188208
return nil, err
189209
}
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-
}
215-
}
210+
crdFileSet[filepath.Base(crdPath)] = b
216211
}
217212
return crdFileSet, nil
218213
}

internal/generate/gen/config.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ type Config struct {
3131
// on-disk inputs are required. If not set, a default is used on a
3232
// per-generator basis.
3333
Inputs map[string]string
34-
// OutputDir is a dir in which to generate output files. If not set, a
35-
// default is used on a per-generator basis.
34+
// OutputDir is the root directory where the output files will be generated.
35+
// If not set, a default is used on a per-generator basis.
3636
OutputDir string
37-
// Filters is a set of functional filters for paths that a generator may
38-
// encounter while gathering data for generation. Filters provides
39-
// fine-grained control over Inputs, since often those paths are often
40-
// top-level directories.
41-
Filters FilterFuncs
4237
}
4338

4439
// FilterFuncs is a slice of filter funcs.

internal/generate/olm-catalog/csv.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ import (
4444
)
4545

4646
const (
47-
olmCatalogChildDir = "olm-catalog"
48-
OLMCatalogDir = scaffold.DeployDir + string(filepath.Separator) + olmCatalogChildDir
49-
CSVYamlFileExt = ".clusterserviceversion.yaml"
47+
OLMCatalogChildDir = "olm-catalog"
48+
// OLMCatalogDir is the default location for OLM catalog directory.
49+
OLMCatalogDir = scaffold.DeployDir + string(filepath.Separator) + OLMCatalogChildDir
50+
csvYamlFileExt = ".clusterserviceversion.yaml"
5051

51-
BundleDirKey = "bundle"
52+
// Input keys for CSV generator whose values are the filepaths for the respective input directories
53+
54+
// DEBUG: Why is this an input key? Users don't need to configure this.
55+
// bundleDirKey is for the location of the bundle manifests directory
56+
bundleDirKey = "bundle"
57+
// DeployDirKey is for the location of the operator manifests directory e.g "deploy/production"
5258
DeployDirKey = "deploy"
53-
APIsDirKey = "apis"
59+
// APIsDirKey is for the location of the API types directory e.g "pkg/apis"
60+
APIsDirKey = "apis"
5461
)
5562

5663
type csvGenerator struct {
@@ -59,7 +66,7 @@ type csvGenerator struct {
5966
csvVersion string
6067
// fromVersion is the CSV version from which to build a new CSV. A CSV
6168
// manifest with this version should exist at:
62-
// deploy/olm-catalog/{from_version}/operator-name.v{from_version}.{CSVYamlFileExt}
69+
// deploy/olm-catalog/{from_version}/operator-name.v{from_version}.{csvYamlFileExt}
6370
fromVersion string
6471
}
6572

@@ -79,23 +86,23 @@ func NewCSV(cfg gen.Config, csvVersion, fromVersion string) gen.Generator {
7986
g.Inputs[DeployDirKey] = scaffold.DeployDir
8087
} else {
8188
// Set to non-standard location.
82-
olmCatalogDir = filepath.Join(g.Inputs[DeployDirKey], olmCatalogChildDir)
89+
olmCatalogDir = filepath.Join(g.Inputs[DeployDirKey], OLMCatalogChildDir)
8390
}
84-
if bundle, ok := g.Inputs[BundleDirKey]; !ok || bundle == "" {
91+
if bundle, ok := g.Inputs[bundleDirKey]; !ok || bundle == "" {
8592
// Initialize so we don't have to check for key existence elsewhere.
86-
g.Inputs[BundleDirKey] = ""
93+
g.Inputs[bundleDirKey] = ""
8794
parentDir := filepath.Join(olmCatalogDir, g.OperatorName)
8895
if isBundleDirExist(parentDir, g.fromVersion) {
89-
g.Inputs[BundleDirKey] = filepath.Join(olmCatalogDir, g.OperatorName, g.fromVersion)
96+
g.Inputs[bundleDirKey] = filepath.Join(olmCatalogDir, g.OperatorName, g.fromVersion)
9097
} else if isBundleDirExist(parentDir, g.csvVersion) {
91-
g.Inputs[BundleDirKey] = filepath.Join(olmCatalogDir, g.OperatorName, g.csvVersion)
98+
g.Inputs[bundleDirKey] = filepath.Join(olmCatalogDir, g.OperatorName, g.csvVersion)
9299
}
93100
}
94101
if apisDir, ok := g.Inputs[APIsDirKey]; !ok || apisDir == "" {
95102
g.Inputs[APIsDirKey] = scaffold.ApisDir
96103
}
97104
if g.OutputDir == "" {
98-
g.OutputDir = filepath.Join(olmCatalogDir, g.OperatorName, g.csvVersion)
105+
g.OutputDir = scaffold.DeployDir
99106
}
100107
return g
101108
}
@@ -114,7 +121,7 @@ func getCSVName(name, version string) string {
114121
}
115122

116123
func getCSVFileName(name, version string) string {
117-
return getCSVName(strings.ToLower(name), version) + CSVYamlFileExt
124+
return getCSVName(strings.ToLower(name), version) + csvYamlFileExt
118125
}
119126

120127
// Generate allows a CSV to be written by marshalling
@@ -127,11 +134,14 @@ func (g csvGenerator) Generate() error {
127134
if len(fileMap) == 0 {
128135
return errors.New("error generating CSV manifest: no generated file found")
129136
}
130-
if err = os.MkdirAll(g.OutputDir, fileutil.DefaultDirFileMode); err != nil {
131-
return errors.Wrapf(err, "error mkdir %s", g.OutputDir)
137+
138+
csvOutputDir := filepath.Join(g.OutputDir, OLMCatalogChildDir, g.OperatorName, g.csvVersion)
139+
if err = os.MkdirAll(csvOutputDir, fileutil.DefaultDirFileMode); err != nil {
140+
return errors.Wrapf(err, "error mkdir %s", csvOutputDir)
132141
}
133142
for fileName, b := range fileMap {
134-
path := filepath.Join(g.OutputDir, fileName)
143+
path := filepath.Join(csvOutputDir, fileName)
144+
log.Debugf("CSV generator writing %s", path)
135145
if err = ioutil.WriteFile(path, b, fileutil.DefaultFileMode); err != nil {
136146
return err
137147
}
@@ -140,7 +150,7 @@ func (g csvGenerator) Generate() error {
140150
}
141151

142152
func (g csvGenerator) generate() (fileMap map[string][]byte, err error) {
143-
bundle := g.Inputs[BundleDirKey]
153+
bundle := g.Inputs[bundleDirKey]
144154
// Get current CSV to update, otherwise start with a fresh CSV.
145155
var csv *olmapiv1alpha1.ClusterServiceVersion
146156
if bundle != "" {
@@ -176,6 +186,7 @@ func (g csvGenerator) generate() (fileMap map[string][]byte, err error) {
176186
if err != nil {
177187
return nil, err
178188
}
189+
// TODO: Why is this a file map when we only generate one CSV file?
179190
fileMap = map[string][]byte{
180191
path: b,
181192
}
@@ -346,12 +357,11 @@ func (g csvGenerator) updateCSVFromManifests(csv *olmapiv1alpha1.ClusterServiceV
346357
kindManifestMap := map[schema.GroupVersionKind][][]byte{}
347358
crGVKSet := map[schema.GroupVersionKind]struct{}{}
348359
err = filepath.Walk(g.Inputs[DeployDirKey], func(path string, info os.FileInfo, werr error) error {
360+
// DEBUG: How is this walking to read deploy/crds/... manifests
361+
// if it's not walking directories?
349362
if werr != nil || info.IsDir() {
350363
return werr
351364
}
352-
if !g.Filters.SatisfiesAny(path) {
353-
return nil
354-
}
355365
b, err := ioutil.ReadFile(path)
356366
if err != nil {
357367
return err

internal/generate/olm-catalog/csv_go_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ func TestGoCSVGoNew(t *testing.T) {
7575

7676
cfg := gen.Config{
7777
OperatorName: testProjectName,
78-
Filters: gen.MakeFilters(scaffold.DeployDir),
7978
}
8079
g := NewCSV(cfg, csvVersion, "")
8180
fileMap, err := g.(csvGenerator).generate()
@@ -109,7 +108,6 @@ func TestGoCSVFromOld(t *testing.T) {
109108

110109
cfg := gen.Config{
111110
OperatorName: testProjectName,
112-
Filters: gen.MakeFilters(scaffold.DeployDir),
113111
}
114112
g := NewCSV(cfg, csvVersion, fromVersion)
115113
fileMap, err := g.(csvGenerator).generate()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func GetCRDDescriptionForGVK(apisDir string, gvk schema.GroupVersionKind) (olmap
5151
if strings.Contains(group, ".") {
5252
group = strings.Split(group, ".")[0]
5353
}
54+
// TODO(hasbro17): Lookup for API directory should not be configured to
55+
// <apisDir>/<gvk.group>/<gvk.Version>
56+
// Kubebuilder's layout is different.
5457
apiDir := filepath.Join(apisDir, group, gvk.Version)
5558
universe, err := getTypesFromDir(apiDir)
5659
if err != nil {

internal/generate/olm-catalog/package_manifest.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ import (
2626
"github.com/operator-framework/api/pkg/validation"
2727
"github.com/operator-framework/operator-registry/pkg/registry"
2828
"github.com/operator-framework/operator-sdk/internal/generate/gen"
29+
"github.com/operator-framework/operator-sdk/internal/scaffold"
2930
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
3031

3132
"github.com/ghodss/yaml"
3233
log "github.com/sirupsen/logrus"
3334
)
3435

3536
const (
36-
PackageManifestFileExt = ".package.yaml"
37+
packageManifestFileExt = ".package.yaml"
3738

38-
ManifestsDirKey = "manifests"
39+
// TODO: Is this an input that needs to be set via the csv generator inputs flag?
40+
manifestsDirKey = "manifests"
3941
)
4042

4143
type pkgGenerator struct {
@@ -68,21 +70,22 @@ func NewPackageManifest(cfg gen.Config, csvVersion, channel string, isDefault bo
6870
olmCatalogDir := OLMCatalogDir
6971
if deployDir, ok := g.Inputs[DeployDirKey]; ok && deployDir != "" {
7072
// Set to non-standard location.
71-
olmCatalogDir = filepath.Join(g.Inputs[DeployDirKey], olmCatalogChildDir)
73+
olmCatalogDir = filepath.Join(g.Inputs[DeployDirKey], OLMCatalogChildDir)
7274
}
7375

74-
if manifests, ok := g.Inputs[ManifestsDirKey]; !ok || manifests == "" {
75-
g.Inputs[ManifestsDirKey] = filepath.Join(olmCatalogDir, g.OperatorName)
76+
if manifests, ok := g.Inputs[manifestsDirKey]; !ok || manifests == "" {
77+
g.Inputs[manifestsDirKey] = filepath.Join(olmCatalogDir, g.OperatorName)
7678
}
79+
7780
if g.OutputDir == "" {
78-
g.OutputDir = filepath.Join(olmCatalogDir, g.OperatorName)
81+
g.OutputDir = scaffold.DeployDir
7982
}
8083
return g
8184
}
8285

8386
// getPkgFileName will return the name of the PackageManifestFile
8487
func getPkgFileName(operatorName string) string {
85-
return strings.ToLower(operatorName) + PackageManifestFileExt
88+
return strings.ToLower(operatorName) + packageManifestFileExt
8689
}
8790

8891
func (g pkgGenerator) Generate() error {
@@ -93,11 +96,13 @@ func (g pkgGenerator) Generate() error {
9396
if len(fileMap) == 0 {
9497
return errors.New("error generating package manifest: no generated file found")
9598
}
96-
if err = os.MkdirAll(g.OutputDir, fileutil.DefaultDirFileMode); err != nil {
97-
return fmt.Errorf("error mkdir %s: %v", g.OutputDir, err)
99+
pkgManifestOutputDir := filepath.Join(g.OutputDir, OLMCatalogChildDir, g.OperatorName)
100+
if err = os.MkdirAll(pkgManifestOutputDir, fileutil.DefaultDirFileMode); err != nil {
101+
return fmt.Errorf("error mkdir %s: %v", pkgManifestOutputDir, err)
98102
}
99103
for fileName, b := range fileMap {
100-
path := filepath.Join(g.OutputDir, fileName)
104+
path := filepath.Join(pkgManifestOutputDir, fileName)
105+
log.Debugf("Package manifest generator writing %s", path)
101106
if err = ioutil.WriteFile(path, b, fileutil.DefaultFileMode); err != nil {
102107
return err
103108
}
@@ -126,6 +131,7 @@ func (g pkgGenerator) generate() (map[string][]byte, error) {
126131
return nil, err
127132
}
128133

134+
// TODO: Why is this a file map?
129135
fileMap := map[string][]byte{
130136
g.fileName: b,
131137
}
@@ -136,7 +142,7 @@ func (g pkgGenerator) generate() (map[string][]byte, error) {
136142
// an existing one if found at the expected path.
137143
func (g pkgGenerator) buildPackageManifest() (registry.PackageManifest, error) {
138144
pkg := registry.PackageManifest{}
139-
path := filepath.Join(g.Inputs[ManifestsDirKey], g.fileName)
145+
path := filepath.Join(g.Inputs[manifestsDirKey], g.fileName)
140146
if _, err := os.Stat(path); err == nil {
141147
b, err := ioutil.ReadFile(path)
142148
if err != nil {

internal/generate/olm-catalog/package_manifest_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func newTestPackageManifestGenerator() gen.Generator {
3030
inputDir := filepath.Join(testGoDataDir, OLMCatalogDir, testProjectName)
3131
cfg := gen.Config{
3232
OperatorName: testProjectName,
33-
Inputs: map[string]string{ManifestsDirKey: inputDir},
33+
Inputs: map[string]string{manifestsDirKey: inputDir},
3434
}
3535
g := NewPackageManifest(cfg, csvVersion, "stable", true)
3636
return g

0 commit comments

Comments
 (0)