Skip to content

Commit 152e5d5

Browse files
author
Eric Stroczynski
authored
internal/generate/olm-catalog: package manifest generator (#2382)
* cmd/operator-sdk/olmcatalog: use package manifest generator * internal/generate/olm-catalog: implement package manifest generators, moving most of the code from internal/scaffold/olm-catalog verbatim
1 parent d7235f5 commit 152e5d5

22 files changed

+1053
-302
lines changed

cmd/operator-sdk/olmcatalog/gen-csv.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"io/ioutil"
2020
"path/filepath"
2121

22+
"github.com/operator-framework/operator-sdk/internal/generate/gen"
23+
gencatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog"
2224
"github.com/operator-framework/operator-sdk/internal/scaffold"
2325
"github.com/operator-framework/operator-sdk/internal/scaffold/input"
2426
catalog "github.com/operator-framework/operator-sdk/internal/scaffold/olm-catalog"
@@ -114,19 +116,20 @@ func genCSVFunc(cmd *cobra.Command, args []string) error {
114116
ConfigFilePath: csvConfigPath,
115117
OperatorName: operatorName,
116118
}
117-
err = s.Execute(cfg,
118-
csv,
119-
&catalog.PackageManifest{
120-
CSVVersion: csvVersion,
121-
Channel: csvChannel,
122-
ChannelIsDefault: defaultChannel,
123-
OperatorName: operatorName,
124-
},
125-
)
119+
err = s.Execute(cfg, csv)
126120
if err != nil {
127121
return fmt.Errorf("catalog scaffold failed: (%v)", err)
128122
}
129123

124+
gcfg := gen.Config{
125+
OperatorName: operatorName,
126+
OutputDir: filepath.Join(gencatalog.OLMCatalogDir, operatorName),
127+
}
128+
pkg := gencatalog.NewPackageManifest(gcfg, csvVersion, csvChannel, defaultChannel)
129+
if err := pkg.Generate(); err != nil {
130+
return fmt.Errorf("error generating package manifest: %v", err)
131+
}
132+
130133
// Write CRD's to the new or updated CSV package dir.
131134
if updateCRDs {
132135
input, err := csv.GetInput()

internal/generate/crd/crd_test.go

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
"io/ioutil"
1919
"math/rand"
2020
"os"
21+
"path"
2122
"path/filepath"
2223
"strconv"
23-
"strings"
2424
"testing"
2525

2626
gen "github.com/operator-framework/operator-sdk/internal/generate/gen"
@@ -30,25 +30,18 @@ import (
3030
)
3131

3232
const (
33-
testAPIVersion = "cache.example.com/v1alpha1"
34-
testKind = "Memcached"
33+
testGroup = "cache.example.com"
34+
testVersion = "v1alpha1"
35+
testKind = "Memcached"
36+
)
37+
38+
var (
39+
testDataDir = filepath.Join("..", "testdata")
40+
testGoDataDir = filepath.Join(testDataDir, "go")
41+
testAPIVersion = path.Join(testGroup, testVersion)
3542
)
3643

3744
func TestGenerate(t *testing.T) {
38-
tfDir := getTestFrameworkPath(t)
39-
// Must change directories since the test framework dir is a sub-module.
40-
wd, err := os.Getwd()
41-
if err != nil {
42-
t.Fatal(err)
43-
}
44-
defer func() {
45-
if err = os.Chdir(wd); err != nil {
46-
t.Fatal(err)
47-
}
48-
}()
49-
if err = os.Chdir(tfDir); err != nil {
50-
t.Fatal(err)
51-
}
5245
tmp, err := ioutil.TempDir("", "")
5346
if err != nil {
5447
t.Fatal(err)
@@ -69,11 +62,21 @@ func TestGenerate(t *testing.T) {
6962
}{
7063
{
7164
"Generate Go CRD",
72-
NewCRDGo(gen.Config{OutputDir: filepath.Join(tmp, strconv.Itoa(rand.Int()))}),
65+
NewCRDGo(gen.Config{
66+
Inputs: map[string]string{
67+
APIsDirKey: filepath.Join(testGoDataDir, scaffold.ApisDir),
68+
},
69+
OutputDir: filepath.Join(tmp, strconv.Itoa(rand.Int())),
70+
}),
7371
},
7472
{
7573
"Generate non-Go CRD",
76-
NewCRDNonGo(gen.Config{OutputDir: filepath.Join(tmp, strconv.Itoa(rand.Int()))}, *r),
74+
NewCRDNonGo(gen.Config{
75+
Inputs: map[string]string{
76+
APIsDirKey: filepath.Join(testGoDataDir, scaffold.ApisDir),
77+
},
78+
OutputDir: filepath.Join(tmp, strconv.Itoa(rand.Int())),
79+
}, *r),
7780
},
7881
}
7982

@@ -88,22 +91,11 @@ func TestGenerate(t *testing.T) {
8891
}
8992

9093
func TestCRDGo(t *testing.T) {
91-
tfDir := getTestFrameworkPath(t)
92-
// Must change directories since the test framework dir is a sub-module.
93-
wd, err := os.Getwd()
94-
if err != nil {
95-
t.Fatal(err)
96-
}
97-
defer func() {
98-
if err = os.Chdir(wd); err != nil {
99-
t.Fatal(err)
100-
}
101-
}()
102-
if err = os.Chdir(tfDir); err != nil {
103-
t.Fatal(err)
94+
cfg := gen.Config{
95+
Inputs: map[string]string{
96+
APIsDirKey: filepath.Join(testGoDataDir, scaffold.ApisDir),
97+
},
10498
}
105-
106-
cfg := gen.Config{}
10799
g := NewCRDGo(cfg)
108100
fileMap, err := g.(crdGenerator).generateGo()
109101
if err != nil {
@@ -121,9 +113,6 @@ func TestCRDGo(t *testing.T) {
121113
}
122114

123115
func TestCRDNonGo(t *testing.T) {
124-
tfDir := getTestFrameworkPath(t)
125-
tfCRDsDir := filepath.Join(tfDir, "deploy", "crds")
126-
127116
cases := []struct {
128117
description string
129118
apiVersion, kind string
@@ -133,11 +122,11 @@ func TestCRDNonGo(t *testing.T) {
133122
}{
134123
{
135124
"non-existent CRD with default structural schema",
136-
"cache.example.com/v1alpha1", "Memcached", filepath.Join("not", "exist"), crdNonGoDefaultExp, false,
125+
testAPIVersion, testKind, filepath.Join("not", "exist"), crdNonGoDefaultExp, false,
137126
},
138127
{
139128
"existing CRD with custom structural schema",
140-
"cache.example.com/v1alpha1", "Memcached", tfCRDsDir, crdCustomExp, false,
129+
testAPIVersion, testKind, filepath.Join(testGoDataDir, scaffold.CRDsDir), crdCustomExp, false,
141130
},
142131
}
143132

@@ -252,16 +241,3 @@ spec:
252241
served: true
253242
storage: true
254243
`
255-
256-
// getTestFrameworkPath constructs the path to the SDK's test-framework,
257-
// which containsa mock operator for testing, from the working directory path.
258-
func getTestFrameworkPath(t *testing.T) string {
259-
t.Helper()
260-
absPath, err := os.Getwd()
261-
if err != nil {
262-
t.Fatal(err)
263-
}
264-
absPath = absPath[:strings.Index(absPath, "internal")]
265-
tfDir := filepath.Join(absPath, "test", "test-framework")
266-
return tfDir
267-
}

internal/generate/olm-catalog/csv.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2020 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package olmcatalog
16+
17+
import (
18+
"path/filepath"
19+
20+
"github.com/operator-framework/operator-sdk/internal/scaffold"
21+
)
22+
23+
const (
24+
OLMCatalogDir = scaffold.DeployDir + string(filepath.Separator) + "olm-catalog"
25+
)
26+
27+
func getCSVName(name, version string) string {
28+
return name + ".v" + version
29+
}

0 commit comments

Comments
 (0)