Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3ee2066

Browse files
committedJun 24, 2020
[sdk-stamps] Remove addition of sdk labels from crd
1 parent c08b01d commit 3ee2066

File tree

19 files changed

+136
-280
lines changed

19 files changed

+136
-280
lines changed
 
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
entries:
2-
- description: Add sdk annotations to bundle resources (CRDs, CSVs, `annotations.yaml` and `bundle.dockerfile`).
2+
- description: Add sdk annotations to bundle resources (CSVs, `annotations.yaml` and `bundle.dockerfile`).
33
kind: "addition"

‎cmd/operator-sdk/add/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func doAnsibleAPIScaffold() error {
197197
if err != nil {
198198
return fmt.Errorf("new ansible api scaffold failed: %v", err)
199199
}
200-
if err = genutil.GenerateCRDNonGo("", *r, apiFlags.CrdVersion, projutil.OperatorTypeAnsible); err != nil {
200+
if err = genutil.GenerateCRDNonGo("", *r, apiFlags.CrdVersion); err != nil {
201201
return err
202202
}
203203

‎cmd/operator-sdk/bundle/create.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (c bundleCreateCmd) runGenerate() error {
243243
rootDir = filepath.Dir(c.directory)
244244
}
245245

246-
if err = rewriteBundleImageContents(rootDir); err != nil {
246+
if err = RewriteBundleImageContents(rootDir); err != nil {
247247
return err
248248
}
249249
return nil
@@ -341,19 +341,19 @@ func copyScorecardConfig() error {
341341
}
342342

343343
func addLabelsToDockerfile(filename string, metricAnnotation map[string]string) error {
344-
var sdkMetricContent string
344+
var sdkMetricContent strings.Builder
345345
for key, value := range metricAnnotation {
346-
sdkMetricContent += fmt.Sprintf("LABEL %s=%s\n", key, value)
346+
sdkMetricContent.WriteString(fmt.Sprintf("LABEL %s=%s\n", key, value))
347347
}
348348

349-
err := projutil.RewriteFileContents(filename, "LABEL", sdkMetricContent)
349+
err := projutil.RewriteFileContents(filename, "LABEL", sdkMetricContent.String())
350350
if err != nil {
351351
return fmt.Errorf("error rewriting dockerfile with metric labels, %v", err)
352352
}
353353
return nil
354354
}
355355

356-
func rewriteBundleImageContents(rootDir string) error {
356+
func RewriteBundleImageContents(rootDir string) error {
357357
metricLabels := projutil.MakeBundleMetricsLabels()
358358

359359
// write metric labels to bundle.Dockerfile
@@ -366,7 +366,7 @@ func rewriteBundleImageContents(rootDir string) error {
366366
return fmt.Errorf("error writing metric labels to annotations.yaml: %v", err)
367367
}
368368

369-
// CopyScorecardConfig to bundle.Dockerfile
369+
// Add a COPY for the scorecard config to bundle.Dockerfile.
370370
if err := copyScorecardConfig(); err != nil {
371371
return fmt.Errorf("error copying scorecardConfig to bundle image, %v", err)
372372
}

‎cmd/operator-sdk/generate/bundle/bundle.go

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
2525
"sigs.k8s.io/kubebuilder/pkg/model/config"
2626

27+
genbundle "github.com/operator-framework/operator-sdk/cmd/operator-sdk/bundle"
2728
genutil "github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate/internal"
2829
gencsv "github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion"
2930
"github.com/operator-framework/operator-sdk/internal/generate/collector"
@@ -259,5 +260,15 @@ func (c bundleCmd) generateMetadata(manifestsDir, outputDir string) error {
259260
if err != nil {
260261
return fmt.Errorf("error generating bundle metadata: %v", err)
261262
}
263+
if c.overwrite {
264+
rootDir := outputDir
265+
if rootDir == "" {
266+
rootDir = filepath.Dir(manifestsDir)
267+
}
268+
269+
if err = genbundle.RewriteBundleImageContents(rootDir); err != nil {
270+
return err
271+
}
272+
}
262273
return nil
263274
}

‎cmd/operator-sdk/new/cmd.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ func doAnsibleScaffold() error {
268268
return fmt.Errorf("new ansible scaffold failed: %v", err)
269269
}
270270

271-
if err = genutil.GenerateCRDNonGo(projectName, *resource,
272-
apiFlags.CrdVersion, projutil.OperatorTypeAnsible); err != nil {
271+
if err = genutil.GenerateCRDNonGo(projectName, *resource, apiFlags.CrdVersion); err != nil {
273272
return err
274273
}
275274

‎hack/generate/test-framework/gen-test-framework.sh

-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,3 @@ go mod tidy
2222
# Run gen commands
2323
../../build/operator-sdk generate k8s
2424
../../build/operator-sdk generate crds
25-
26-
# Remove sdk labels/annotations from crds
27-
sed -i "/annotations/d" deploy/crds/cache.example.com_memcacheds_crd.yaml
28-
sed -i "/operators.operatorframework.io/d" deploy/crds/cache.example.com_memcacheds_crd.yaml
29-
sed -i "/annotations/d" deploy/crds/cache.example.com_memcachedrs_crd.yaml
30-
sed -i "/operators.operatorframework.io/d" deploy/crds/cache.example.com_memcachedrs_crd.yaml

‎internal/generate/clusterserviceversion/bases/clusterserviceversion.go

-19
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ func (b ClusterServiceVersion) GetBase() (base *v1alpha1.ClusterServiceVersion,
7070
base = b.makeNewBase()
7171
}
7272

73-
// Add sdk labels to csv
74-
setSDKAnnotations(base)
75-
7673
// Interactively fill in UI metadata.
7774
if b.Interactive {
7875
meta := &uiMetadata{}
@@ -92,22 +89,6 @@ func (b ClusterServiceVersion) GetBase() (base *v1alpha1.ClusterServiceVersion,
9289
return base, nil
9390
}
9491

95-
// setSDKAnnotations adds SDK metric labels to the base if they do not exist. It assumes that
96-
// if sdk Builder annotation is not present, then all sdk labels are also not populated in csv.
97-
func setSDKAnnotations(csv *v1alpha1.ClusterServiceVersion) {
98-
annotations := csv.GetAnnotations()
99-
if annotations == nil {
100-
annotations = make(map[string]string)
101-
}
102-
103-
if _, exist := annotations[projutil.OperatorBuilder]; !exist {
104-
for key, value := range projutil.MakeOperatorMetricLabels() {
105-
annotations[key] = value
106-
}
107-
}
108-
csv.SetAnnotations(annotations)
109-
}
110-
11192
// setDefaults sets default values in b using b's existing values.
11293
func (b *ClusterServiceVersion) setDefaults() {
11394
if b.DisplayName == "" {

‎internal/generate/clusterserviceversion/clusterserviceversion.go

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
"github.com/blang/semver"
24+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
2425
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2526
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
2627
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -160,13 +161,29 @@ func (g *Generator) Generate(cfg *config.Config, opts ...Option) (err error) {
160161
return err
161162
}
162163

164+
// Add sdk labels to csv
165+
setSDKAnnotations(csv)
166+
163167
w, err := g.getWriter()
164168
if err != nil {
165169
return err
166170
}
167171
return genutil.WriteObject(w, csv)
168172
}
169173

174+
// setSDKAnnotations adds SDK metric labels to the base if they do not exist.
175+
func setSDKAnnotations(csv *v1alpha1.ClusterServiceVersion) {
176+
annotations := csv.GetAnnotations()
177+
if annotations == nil {
178+
annotations = make(map[string]string)
179+
}
180+
181+
for key, value := range projutil.MakeOperatorMetricLabels() {
182+
annotations[key] = value
183+
}
184+
csv.SetAnnotations(annotations)
185+
}
186+
170187
// LegacyOption is a function that modifies a Generator for legacy project layouts.
171188
type LegacyOption Option
172189

@@ -204,6 +221,9 @@ func (g *Generator) GenerateLegacy(opts ...LegacyOption) (err error) {
204221
return err
205222
}
206223

224+
// Add sdk labels to csv
225+
setSDKAnnotations(csv)
226+
207227
w, err := g.getWriter()
208228
if err != nil {
209229
return err

‎internal/generate/clusterserviceversion/clusterserviceversion_test.go

+28-50
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,8 @@ var _ = Describe("Generating a ClusterServiceVersion", func() {
141141
}
142142
Expect(g.Generate(cfg, opts...)).ToNot(HaveOccurred())
143143
outputFile := filepath.Join(tmp, "bases", makeCSVFileName(operatorName))
144-
outputFileContents := removeSDKLabelsFromCSVString(string(readFileHelper(outputFile)))
145144
Expect(outputFile).To(BeAnExistingFile())
146-
Expect(outputFileContents).To(MatchYAML(baseCSVUIMetaStr))
145+
Expect(readFileHelper(outputFile)).To(MatchYAML(baseCSVUIMetaStr))
147146
})
148147
It("should have sdk labels in annotations", func() {
149148
g = Generator{
@@ -160,7 +159,7 @@ var _ = Describe("Generating a ClusterServiceVersion", func() {
160159
Expect(err).ToNot(HaveOccurred())
161160
Expect(outputFile).To(BeAnExistingFile())
162161

163-
annotations := outputCSV.ObjectMeta.Annotations
162+
annotations := outputCSV.GetAnnotations()
164163
Expect(annotations).ToNot(BeNil())
165164
Expect(annotations).Should(HaveKey(projutil.OperatorBuilder))
166165
Expect(annotations).Should(HaveKey(projutil.OperatorLayout))
@@ -178,9 +177,8 @@ var _ = Describe("Generating a ClusterServiceVersion", func() {
178177
}
179178
Expect(g.Generate(cfg, opts...)).ToNot(HaveOccurred())
180179
outputFile := filepath.Join(tmp, bundle.ManifestsDir, makeCSVFileName(operatorName))
181-
outputFileContents := removeSDKLabelsFromCSVString(string(readFileHelper(outputFile)))
182180
Expect(outputFile).To(BeAnExistingFile())
183-
Expect(outputFileContents).To(MatchYAML(newCSVStr))
181+
Expect(readFileHelper(outputFile)).To(MatchYAML(newCSVStr))
184182
})
185183
It("should write a ClusterServiceVersion manifest to a package file", func() {
186184
g = Generator{
@@ -196,7 +194,7 @@ var _ = Describe("Generating a ClusterServiceVersion", func() {
196194
Expect(g.Generate(cfg, opts...)).ToNot(HaveOccurred())
197195
outputFile := filepath.Join(tmp, g.Version, makeCSVFileName(operatorName))
198196
Expect(outputFile).To(BeAnExistingFile())
199-
Expect(string(readFileHelper(outputFile))).To(MatchYAML(newCSVStr))
197+
Expect(readFileHelper(outputFile)).To(MatchYAML(newCSVStr))
200198
})
201199

202200
It("should write a ClusterServiceVersion manifest to a legacy bundle file", func() {
@@ -212,9 +210,8 @@ var _ = Describe("Generating a ClusterServiceVersion", func() {
212210
}
213211
Expect(g.GenerateLegacy(opts...)).ToNot(HaveOccurred())
214212
outputFile := filepath.Join(tmp, bundle.ManifestsDir, makeCSVFileName(operatorName))
215-
outputFileContents := removeSDKLabelsFromCSVString(string(readFileHelper(outputFile)))
216213
Expect(outputFile).To(BeAnExistingFile())
217-
Expect(outputFileContents).To(MatchYAML(newCSVStr))
214+
Expect(readFileHelper(outputFile)).To(MatchYAML(newCSVStr))
218215
})
219216
It("should write a ClusterServiceVersion manifest as a legacy package file", func() {
220217
g = Generator{
@@ -280,46 +277,28 @@ var _ = Describe("Generating a ClusterServiceVersion", func() {
280277
})
281278
})
282279

283-
Context("to create a new", func() {
284-
285-
Context("bundle base", func() {
286-
It("should return the default base object", func() {
287-
g = Generator{
288-
OperatorName: operatorName,
289-
OperatorType: operatorType,
290-
config: cfg,
291-
getBase: makeBaseGetter(baseCSV),
292-
}
293-
csv, err := g.generate()
294-
Expect(err).ToNot(HaveOccurred())
295-
Expect(csv).To(Equal(baseCSV))
296-
})
297-
It("should return a base object with customresourcedefinitions", func() {
298-
g = Generator{
299-
OperatorName: operatorName,
300-
OperatorType: operatorType,
301-
config: cfg,
302-
getBase: makeBaseGetter(baseCSVUIMeta),
303-
}
304-
csv, err := g.generate()
305-
Expect(err).ToNot(HaveOccurred())
306-
Expect(csv).To(Equal(baseCSVUIMeta))
307-
})
280+
Context("to create a new base ClusterServiceVersion", func() {
281+
It("should return the default base object", func() {
282+
g = Generator{
283+
OperatorName: operatorName,
284+
OperatorType: operatorType,
285+
config: cfg,
286+
getBase: makeBaseGetter(baseCSV),
287+
}
288+
csv, err := g.generate()
289+
Expect(err).ToNot(HaveOccurred())
290+
Expect(csv).To(Equal(baseCSV))
308291
})
309-
Context("bundle", func() {
310-
It("should return the expected object", func() {
311-
g = Generator{
312-
OperatorName: operatorName,
313-
OperatorType: operatorType,
314-
Version: version,
315-
Collector: col,
316-
config: cfg,
317-
getBase: makeBaseGetter(baseCSVUIMeta),
318-
}
319-
csv, err := g.generate()
320-
Expect(err).ToNot(HaveOccurred())
321-
Expect(csv).To(Equal(newCSV))
322-
})
292+
It("should return a base object with customresourcedefinitions", func() {
293+
g = Generator{
294+
OperatorName: operatorName,
295+
OperatorType: operatorType,
296+
config: cfg,
297+
getBase: makeBaseGetter(baseCSVUIMeta),
298+
}
299+
csv, err := g.generate()
300+
Expect(err).ToNot(HaveOccurred())
301+
Expect(csv).To(Equal(baseCSVUIMeta))
323302
})
324303
})
325304

@@ -380,7 +359,6 @@ var _ = Describe("Generating a ClusterServiceVersion", func() {
380359
Expect(csv).To(Equal(upgradeCSV(newCSV, g.OperatorName, g.Version)))
381360
})
382361
})
383-
384362
})
385363

386364
})
@@ -445,10 +423,10 @@ func initTestCSVsHelper() {
445423
ExpectWithOffset(1, err).ToNot(HaveOccurred())
446424
}
447425

448-
func readFileHelper(path string) []byte {
426+
func readFileHelper(path string) string {
449427
b, err := ioutil.ReadFile(path)
450428
ExpectWithOffset(1, err).ToNot(HaveOccurred())
451-
return b
429+
return removeSDKLabelsFromCSVString(string(b))
452430
}
453431

454432
func modifyCSVDepImageHelper(tag string) func(csv *v1alpha1.ClusterServiceVersion) {

‎internal/generate/crd/crd.go

+3-26
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
"github.com/operator-framework/operator-sdk/internal/scaffold"
4141
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
4242
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
43-
"github.com/operator-framework/operator-sdk/internal/util/projutil"
4443
)
4544

4645
const DefaultCRDVersion = "v1"
@@ -68,8 +67,6 @@ type Generator struct {
6867
// ApisDir is for the location of the API types directory e.g "pkg/apis"
6968
// The CSV annotation comments will be parsed from the types under this path.
7069
ApisDir string
71-
// OperatorType refers to the type of operator (go/ansible/helm)
72-
OperatorType string
7370
}
7471

7572
func (g Generator) validate() error {
@@ -188,8 +185,9 @@ func (g Generator) generateGo() (map[string][]byte, error) {
188185
// just remove it.
189186
annotations := crd.GetAnnotations()
190187
delete(annotations, "controller-gen.kubebuilder.io/version")
191-
// Add sdk related labels to the cached annotations
192-
g.addSDKLabelsToAnnotations(annotations)
188+
if len(annotations) == 0 {
189+
annotations = nil
190+
}
193191
crd.SetAnnotations(annotations)
194192
b, err := k8sutil.GetObjectBytes(&crd, yaml.Marshal)
195193
if err != nil {
@@ -275,7 +273,6 @@ func (g Generator) generateNonGo() (map[string][]byte, error) {
275273

276274
sort.Sort(k8sutil.CRDVersions(crd.Spec.Versions))
277275
setCRDStorageVersion(crd)
278-
g.setSDKLabels(crd)
279276
if err := checkCRDVersions(crd); err != nil {
280277
return nil, fmt.Errorf("invalid version in CRD %s: %w", crd.GetName(), err)
281278
}
@@ -374,26 +371,6 @@ func setCRDStorageVersion(crd *apiextv1beta1.CustomResourceDefinition) {
374371
crd.Spec.Versions[0].Storage = true
375372
}
376373

377-
func (g Generator) setSDKLabels(crd *apiextv1beta1.CustomResourceDefinition) {
378-
annotations := crd.GetAnnotations()
379-
if annotations == nil {
380-
annotations = make(map[string]string)
381-
}
382-
g.addSDKLabelsToAnnotations(annotations)
383-
crd.SetAnnotations(annotations)
384-
}
385-
386-
func (g Generator) addSDKLabelsToAnnotations(mapAnnotations map[string]string) {
387-
for label, value := range projutil.MakeOperatorMetricLabels() {
388-
mapAnnotations[label] = value
389-
}
390-
// Modifying operator type for ansible and helm legacy operators, as during
391-
// scaffolding the project directories are not written on disk
392-
if g.OperatorType != projutil.OperatorTypeGo {
393-
mapAnnotations[projutil.OperatorLayout] = g.OperatorType
394-
}
395-
}
396-
397374
// checkCRDVersions ensures version(s) generated for a CRD are in valid format.
398375
// From the Kubernetes CRD docs:
399376
//

‎internal/generate/crd/crd_test.go

-17
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,9 @@ func TestGenerate(t *testing.T) {
132132
}
133133

134134
func TestCRDGo(t *testing.T) {
135-
// Setting the OperatorType to be "unknown", so that it is propagated in the
136-
// sdk labels. The project layout cannot be inferred here as the entire
137-
// project scaffolding is not done.
138135
g := Generator{
139136
IsOperatorGo: true,
140137
ApisDir: filepath.Join(testGoDataDir, scaffold.ApisDir),
141-
OperatorType: "unknown",
142138
}
143139

144140
r, err := scaffold.NewResource(testAPIVersion, testKind)
@@ -216,7 +212,6 @@ func TestCRDNonGo(t *testing.T) {
216212
Resource: *r,
217213
CRDVersion: c.crdVersion,
218214
IsOperatorGo: false,
219-
OperatorType: "unknown",
220215
}
221216
fileMap, err := g.generateNonGo()
222217
if err != nil {
@@ -236,9 +231,6 @@ func TestCRDNonGo(t *testing.T) {
236231
const crdNonGoDefaultExpV1beta1 = `apiVersion: apiextensions.k8s.io/v1beta1
237232
kind: CustomResourceDefinition
238233
metadata:
239-
annotations:
240-
operators.operatorframework.io/builder: operator-sdk-unknown
241-
operators.operatorframework.io/project_layout: unknown
242234
name: memcacheds.cache.example.com
243235
spec:
244236
group: cache.example.com
@@ -265,9 +257,6 @@ spec:
265257
const crdNonGoDefaultExpV1 = `apiVersion: apiextensions.k8s.io/v1
266258
kind: CustomResourceDefinition
267259
metadata:
268-
annotations:
269-
operators.operatorframework.io/builder: operator-sdk-unknown
270-
operators.operatorframework.io/project_layout: unknown
271260
name: memcacheds.cache.example.com
272261
spec:
273262
group: cache.example.com
@@ -294,9 +283,6 @@ spec:
294283
const crdCustomExpV1beta1 = `apiVersion: apiextensions.k8s.io/v1beta1
295284
kind: CustomResourceDefinition
296285
metadata:
297-
annotations:
298-
operators.operatorframework.io/builder: operator-sdk-unknown
299-
operators.operatorframework.io/project_layout: unknown
300286
name: memcacheds.cache.example.com
301287
spec:
302288
group: cache.example.com
@@ -358,9 +344,6 @@ spec:
358344
const crdCustomExpV1 = `apiVersion: apiextensions.k8s.io/v1
359345
kind: CustomResourceDefinition
360346
metadata:
361-
annotations:
362-
operators.operatorframework.io/builder: operator-sdk-unknown
363-
operators.operatorframework.io/project_layout: unknown
364347
name: memcacheds.cache.example.com
365348
spec:
366349
group: cache.example.com

‎internal/generate/olm-catalog/csv_go_test.go

+29-133
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,21 @@
1515
package olmcatalog
1616

1717
import (
18-
"bytes"
19-
"fmt"
2018
"io/ioutil"
2119
"os"
2220
"os/exec"
2321
"path/filepath"
2422
"testing"
2523

26-
"github.com/blang/semver"
27-
"github.com/operator-framework/api/pkg/operators/v1alpha1"
28-
olmapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
29-
log "github.com/sirupsen/logrus"
30-
"github.com/stretchr/testify/assert"
31-
"sigs.k8s.io/yaml"
32-
3324
"github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases"
3425
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
3526
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
3627
"github.com/operator-framework/operator-sdk/internal/util/projutil"
28+
29+
"github.com/blang/semver"
30+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
31+
"github.com/stretchr/testify/assert"
32+
"sigs.k8s.io/yaml"
3733
)
3834

3935
const (
@@ -81,6 +77,14 @@ func mkTempDirWithCleanup(t *testing.T, prefix string) (dir string, f func()) {
8177
return
8278
}
8379

80+
func readFile(t *testing.T, path string) []byte {
81+
b, err := ioutil.ReadFile(path)
82+
if err != nil {
83+
t.Fatalf("Failed to read testdata file: %v", err)
84+
}
85+
return b
86+
}
87+
8488
// TODO: Change to table driven subtests to test out different Inputs/Output for the generator
8589
func TestGoCSVNewWithInputsToOutput(t *testing.T) {
8690
// Change directory to project root so the test cases can form the correct pkg imports
@@ -115,15 +119,13 @@ func TestGoCSVNewWithInputsToOutput(t *testing.T) {
115119

116120
// Read expected CSV
117121
expBundleDir := filepath.Join("expected-catalog", OLMCatalogChildDir, testProjectName, csvVersion)
122+
csvExp := string(readFile(t, filepath.Join(expBundleDir, csvFileName)))
118123

119124
// Read generated CSV from outputDir path
120125
outputBundleDir := filepath.Join(outputDir, OLMCatalogChildDir, testProjectName, csvVersion)
126+
csvOutput := string(readFile(t, filepath.Join(outputBundleDir, csvFileName)))
121127

122-
outputCSV := getCSVFromFile(t, filepath.Join(outputBundleDir, csvFileName))
123-
removeSDKLabelsFromCSVHelper(outputCSV)
124-
125-
expCSV := getCSVFromFile(t, filepath.Join(expBundleDir, csvFileName))
126-
assert.Equal(t, expCSV, outputCSV)
128+
assert.Equal(t, csvExp, csvOutput)
127129
}
128130

129131
func TestGoCSVUpgradeWithInputsToOutput(t *testing.T) {
@@ -172,14 +174,13 @@ func TestGoCSVUpgradeWithInputsToOutput(t *testing.T) {
172174

173175
// Read expected CSV
174176
expCsvFile := filepath.Join(expCatalogDir, testProjectName, csvVersion, csvFileName)
177+
csvExp := string(readFile(t, expCsvFile))
175178

176179
// Read generated CSV from outputDir path
177180
csvOutputFile := filepath.Join(outputFromCSVDir, csvVersion, csvFileName)
178-
outputCSV := getCSVFromFile(t, csvOutputFile)
179-
removeSDKLabelsFromCSVHelper(outputCSV)
180-
expCSV := getCSVFromFile(t, expCsvFile)
181+
csvOutput := string(readFile(t, csvOutputFile))
181182

182-
assert.Equal(t, expCSV, outputCSV)
183+
assert.Equal(t, csvExp, csvOutput)
183184
}
184185

185186
func TestGoCSVNew(t *testing.T) {
@@ -206,17 +207,11 @@ func TestGoCSVNew(t *testing.T) {
206207
}
207208

208209
csvExpFile := getCSVFileNameLegacy(testProjectName, csvVersion)
210+
csvExpBytes := readFile(t, filepath.Join(OLMCatalogDir, testProjectName, noUpdateDir, csvExpFile))
209211
if b, ok := fileMap[csvExpFile]; !ok {
210212
t.Errorf("Failed to generate CSV for version %s", csvVersion)
211213
} else {
212-
expCSV := getCSVFromFile(t, filepath.Join(OLMCatalogDir, testProjectName, noUpdateDir, csvExpFile))
213-
214-
outputCSV, err := generateCSV(b)
215-
if err != nil {
216-
t.Errorf("Error occurred while generating CSV %v", err)
217-
}
218-
removeSDKLabelsFromCSVHelper(outputCSV)
219-
assert.Equal(t, expCSV, outputCSV)
214+
assert.Equal(t, string(csvExpBytes), string(b))
220215
}
221216
}
222217

@@ -243,17 +238,11 @@ func TestGoCSVUpdate(t *testing.T) {
243238
}
244239

245240
csvExpFile := getCSVFileNameLegacy(testProjectName, csvVersion)
241+
csvExpBytes := readFile(t, filepath.Join(OLMCatalogDir, testProjectName, csvVersion, csvExpFile))
246242
if b, ok := fileMap[csvExpFile]; !ok {
247243
t.Errorf("Failed to generate CSV for version %s", csvVersion)
248244
} else {
249-
expCSV := getCSVFromFile(t, filepath.Join(OLMCatalogDir, testProjectName, csvVersion, csvExpFile))
250-
251-
outputCSV, err := generateCSV(b)
252-
if err != nil {
253-
t.Errorf("Error occurred while generating CSV, %v", err)
254-
}
255-
removeSDKLabelsFromCSVHelper(outputCSV)
256-
assert.Equal(t, expCSV, outputCSV)
245+
assert.Equal(t, string(csvExpBytes), string(b))
257246
}
258247
}
259248

@@ -280,17 +269,11 @@ func TestGoCSVUpgrade(t *testing.T) {
280269
}
281270

282271
csvExpFile := getCSVFileNameLegacy(testProjectName, csvVersion)
272+
csvExpBytes := readFile(t, filepath.Join(OLMCatalogDir, testProjectName, csvVersion, csvExpFile))
283273
if b, ok := fileMap[csvExpFile]; !ok {
284274
t.Errorf("Failed to generate CSV for version %s", csvVersion)
285275
} else {
286-
expCSV := getCSVFromFile(t, filepath.Join(OLMCatalogDir, testProjectName, csvVersion, csvExpFile))
287-
288-
outputCSV, err := generateCSV(b)
289-
if err != nil {
290-
t.Errorf("Error occurred while generating CSV, %v", err)
291-
}
292-
removeSDKLabelsFromCSVHelper(outputCSV)
293-
assert.Equal(t, expCSV, outputCSV)
276+
assert.Equal(t, string(csvExpBytes), string(b))
294277
}
295278
}
296279

@@ -318,16 +301,11 @@ func TestGoCSVNewManifests(t *testing.T) {
318301
}
319302

320303
csvExpFile := getCSVFileNameLegacy(testProjectName, csvVersion)
304+
csvExpBytes := readFile(t, filepath.Join(OLMCatalogDir, testProjectName, noUpdateDir, csvExpFile))
321305
if b, ok := fileMap[getCSVFileName(testProjectName)]; !ok {
322306
t.Errorf("Failed to generate CSV for version %s", csvVersion)
323307
} else {
324-
expCSV := getCSVFromFile(t, filepath.Join(OLMCatalogDir, testProjectName, noUpdateDir, csvExpFile))
325-
outputCSV, err := generateCSV(b)
326-
if err != nil {
327-
t.Errorf("Error occurred while generating CSV, %v", err)
328-
}
329-
removeSDKLabelsFromCSVHelper(outputCSV)
330-
assert.Equal(t, expCSV, outputCSV)
308+
assert.Equal(t, string(csvExpBytes), string(b))
331309
}
332310
}
333311

@@ -354,20 +332,11 @@ func TestGoCSVUpdateManifests(t *testing.T) {
354332
}
355333

356334
csvExpFile := getCSVFileNameLegacy(testProjectName, csvVersion)
335+
csvExpBytes := readFile(t, filepath.Join(OLMCatalogDir, testProjectName, csvVersion, csvExpFile))
357336
if b, ok := fileMap[getCSVFileName(testProjectName)]; !ok {
358337
t.Errorf("Failed to generate CSV for version %s", csvVersion)
359338
} else {
360-
expCSV := getCSVFromFile(t, filepath.Join(OLMCatalogDir, testProjectName, csvVersion, csvExpFile))
361-
if err != nil {
362-
t.Errorf("Error occurred while generating CSV, %v", err)
363-
}
364-
365-
outputCSV, err := generateCSV(b)
366-
if err != nil {
367-
t.Errorf("Error occurred while generating CSV, %v", err)
368-
}
369-
removeSDKLabelsFromCSVHelper(outputCSV)
370-
assert.Equal(t, expCSV, outputCSV)
339+
assert.Equal(t, string(csvExpBytes), string(b))
371340
}
372341
}
373342

@@ -446,31 +415,6 @@ func TestGoCSVNewWithEmptyDeployDir(t *testing.T) {
446415
}
447416
}
448417

449-
func TestSDKLabels(t *testing.T) {
450-
cleanupFunc := chDirWithCleanup(t, testGoDataDir)
451-
defer cleanupFunc()
452-
453-
b := bases.ClusterServiceVersion{
454-
OperatorName: testProjectName,
455-
OperatorType: projutil.OperatorTypeGo,
456-
}
457-
458-
// check if base CSV has the required stamp values
459-
csv, err := b.GetBase()
460-
if err != nil {
461-
t.Fatal(err)
462-
}
463-
464-
annotations := csv.ObjectMeta.Annotations
465-
if _, ok := annotations[projutil.OperatorBuilder]; !ok {
466-
t.Errorf("CSV base does not contain SDK stamp %s", projutil.OperatorBuilder)
467-
}
468-
469-
if _, ok := annotations[projutil.OperatorLayout]; !ok {
470-
t.Errorf("CSV base does not contain SDK stamp %s", projutil.OperatorLayout)
471-
}
472-
}
473-
474418
func TestUpdateCSVVersion(t *testing.T) {
475419
cleanupFunc := chDirWithCleanup(t, testGoDataDir)
476420
defer cleanupFunc()
@@ -528,51 +472,3 @@ func TestUpdateCSVVersion(t *testing.T) {
528472
t.Errorf("Wanted csv replaces %s, got %s", wantedReplaces, csv.Spec.Replaces)
529473
}
530474
}
531-
532-
func getCSVFromFile(t *testing.T, path string) *olmapiv1alpha1.ClusterServiceVersion {
533-
csvpath := filepath.Join(path)
534-
b, err := ioutil.ReadFile(csvpath)
535-
if err != nil {
536-
t.Errorf("Error reading manifest %s: %v", path, err)
537-
}
538-
539-
csv, err := generateCSV(b)
540-
if err != nil {
541-
t.Errorf("Error generating csv %s: %v", path, err)
542-
}
543-
return csv
544-
}
545-
546-
func generateCSV(input []byte) (*olmapiv1alpha1.ClusterServiceVersion, error) {
547-
scanner := k8sutil.NewYAMLScanner(bytes.NewBuffer(input))
548-
for scanner.Scan() {
549-
manifest := scanner.Bytes()
550-
typeMeta, err := k8sutil.GetTypeMetaFromBytes(manifest)
551-
if err != nil {
552-
log.Debugf("Skipping non-Object manifest : %v", err)
553-
continue
554-
}
555-
if typeMeta.Kind == olmapiv1alpha1.ClusterServiceVersionKind {
556-
csv := &olmapiv1alpha1.ClusterServiceVersion{}
557-
if err := yaml.Unmarshal(manifest, csv); err != nil {
558-
return nil, fmt.Errorf("error unmarshalling ClusterServiceVersion from manifest: %v", err)
559-
}
560-
return csv, nil
561-
}
562-
}
563-
if err := scanner.Err(); err != nil {
564-
return nil, fmt.Errorf("error scanning manifest %v", err)
565-
}
566-
return nil, fmt.Errorf("error getting manifest")
567-
568-
}
569-
570-
// removeSDKLabelsFromCSVHelper removes the sdk labels from CSV struct. Used for test cases where
571-
// we need to test the generated CSV with expected predefined CSV file on disk.
572-
func removeSDKLabelsFromCSVHelper(csv *v1alpha1.ClusterServiceVersion) {
573-
if _, exist := csv.ObjectMeta.Annotations[projutil.OperatorBuilder]; exist {
574-
for label := range projutil.MakeOperatorMetricLabels() {
575-
delete(csv.ObjectMeta.Annotations, label)
576-
}
577-
}
578-
}

‎internal/genutil/crds.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ func CRDGen(crdVersion string) error {
4444
}
4545

4646
// GenerateCRDNonGo generates CRDs for Non-Go APIs(Eg., Ansible,Helm)
47-
func GenerateCRDNonGo(projectName string, resource scaffold.Resource, crdVersion, operatorType string) error {
47+
func GenerateCRDNonGo(projectName string, resource scaffold.Resource, crdVersion string) error {
4848
crdsDir := filepath.Join(projectName, scaffold.CRDsDir)
4949
crd := gencrd.Generator{
5050
CRDsDir: crdsDir,
5151
OutputDir: crdsDir,
5252
CRDVersion: crdVersion,
5353
Resource: resource,
5454
IsOperatorGo: false,
55-
OperatorType: operatorType,
5655
}
5756
if err := crd.Generate(); err != nil {
5857
return fmt.Errorf("error generating CRD for %s: %w", resource, err)

‎internal/registry/validate.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package registry
1717
import (
1818
"fmt"
1919
"io/ioutil"
20+
"os"
2021

2122
apimanifests "github.com/operator-framework/api/pkg/manifests"
2223
apivalidation "github.com/operator-framework/api/pkg/validation"
@@ -175,7 +176,12 @@ func writeAnnotationFile(filename string, annotation *registrybundle.AnnotationM
175176
return err
176177
}
177178

178-
err = ioutil.WriteFile(filename, []byte(file), 0644)
179+
mode := os.FileMode(0666)
180+
if info, err := os.Stat(filename); err == nil {
181+
mode = info.Mode()
182+
}
183+
184+
err = ioutil.WriteFile(filename, []byte(file), mode)
179185
if err != nil {
180186
return fmt.Errorf("error writing modified contents to annotations file, %v", err)
181187
}

‎internal/scaffold/helm/api.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/operator-framework/operator-sdk/internal/genutil"
2727
"github.com/operator-framework/operator-sdk/internal/scaffold"
2828
"github.com/operator-framework/operator-sdk/internal/scaffold/input"
29-
"github.com/operator-framework/operator-sdk/internal/util/projutil"
3029
"github.com/operator-framework/operator-sdk/pkg/helm/watches"
3130
)
3231

@@ -63,7 +62,7 @@ func API(cfg input.Config, createOpts CreateChartOptions) error {
6362
if err != nil {
6463
log.Fatalf("API scaffold failed: %v", err)
6564
}
66-
if err = genutil.GenerateCRDNonGo("", *r, createOpts.CRDVersion, projutil.OperatorTypeHelm); err != nil {
65+
if err = genutil.GenerateCRDNonGo("", *r, createOpts.CRDVersion); err != nil {
6766
return err
6867
}
6968

‎internal/scaffold/helm/init.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/operator-framework/operator-sdk/internal/genutil"
2727
"github.com/operator-framework/operator-sdk/internal/scaffold"
2828
"github.com/operator-framework/operator-sdk/internal/scaffold/input"
29-
"github.com/operator-framework/operator-sdk/internal/util/projutil"
3029
"github.com/operator-framework/operator-sdk/pkg/helm/watches"
3130
)
3231

@@ -79,7 +78,7 @@ func Init(cfg input.Config, createOpts CreateChartOptions) error {
7978
}
8079

8180
// nolint:staticcheck
82-
if err = genutil.GenerateCRDNonGo("", *resource, createOpts.CRDVersion, projutil.OperatorTypeHelm); err != nil {
81+
if err = genutil.GenerateCRDNonGo("", *resource, createOpts.CRDVersion); err != nil {
8382
return err
8483
}
8584

‎internal/util/projutil/projutil_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,5 @@ var _ = Describe("Testing projutil helpers", func() {
9494

9595
func TestMetadata(t *testing.T) {
9696
RegisterFailHandler(Fail)
97-
RunSpecs(t, "Helpers suite")
97+
RunSpecs(t, "Projutil Helpers suite")
9898
}

‎internal/util/projutil/sdk_stamps_util.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@ package projutil
1616

1717
import (
1818
"regexp"
19-
"strings"
2019

2120
kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
2221
ver "github.com/operator-framework/operator-sdk/version"
22+
log "github.com/sirupsen/logrus"
2323
)
2424

2525
const (
26-
OperatorBuilder = "operators.operatorframework.io/builder"
27-
OperatorLayout = "operators.operatorframework.io/project_layout"
28-
bundleMediaType = "operators.operatorframework.io.metrics.mediatype.v1"
29-
bundleBuilder = "operators.operatorframework.io.metrics.builder"
30-
bundleLayout = "operators.operatorframework.io.metrics.project_layout"
26+
OperatorBuilder = "operators.operatorframework.io/builder"
27+
OperatorLayout = "operators.operatorframework.io/project_layout"
28+
bundleMediaType = "operators.operatorframework.io.metrics.mediatype.v1"
29+
bundleBuilder = "operators.operatorframework.io.metrics.builder"
30+
bundleLayout = "operators.operatorframework.io.metrics.project_layout"
31+
metricsMediatype = "metrics+v1"
3132
)
3233

3334
// MakeBundleMetricsLabels returns the SDK metric labels which will be added
3435
// to bundle resources like bundle.Dockerfile and annotations.yaml.
3536
func MakeBundleMetricsLabels() map[string]string {
3637
return map[string]string{
37-
bundleMediaType: "metrics+v1",
38+
bundleMediaType: metricsMediatype,
3839
bundleBuilder: getSDKBuilder(),
3940
bundleLayout: getSDKProjectLayout(),
4041
}
@@ -59,19 +60,27 @@ func parseVersion(input string) string {
5960
if version == "" {
6061
return "unknown"
6162
}
62-
if strings.Contains(input, "dirty") {
63+
64+
if checkIfUnreleased(input) {
6365
version = version + "+git"
6466
}
6567
return version
6668
}
6769

70+
// checkIfUnreleased returns true if sdk was not built from released version.
71+
func checkIfUnreleased(input string) bool {
72+
re := regexp.MustCompile(`v[0-9]+\.[0-9]+\.[0-9]+-.+`)
73+
return re.MatchString(input)
74+
}
75+
6876
// getSDKProjectLayout returns the `layout` field in PROJECT file if it is a
6977
// Kubebuilder scaffolded project, or else returns the kind of operator.
7078
func getSDKProjectLayout() string {
7179
if kbutil.HasProjectFile() {
7280
cfg, err := kbutil.ReadConfig()
7381
if err != nil {
74-
return err.Error()
82+
log.Debugf("Error reading config: %v", err)
83+
return "unknown"
7584
}
7685
return cfg.Layout
7786
}

‎internal/util/projutil/sdk_stamps_util_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
. "github.com/onsi/gomega"
2020
)
2121

22-
var _ = Describe("Testing sdk labels helper function", func() {
23-
Describe("Testing SDK version", func() {
22+
var _ = Describe("SDK Label helper functions", func() {
23+
Describe("parseVersion", func() {
2424
It("should extract sdk version", func() {
2525
version := "v0.17.0-159-ge87627f4-dirty"
2626
output := parseVersion(version)
@@ -31,6 +31,11 @@ var _ = Describe("Testing sdk labels helper function", func() {
3131
output := parseVersion(version)
3232
Expect(output).To(Equal("v0.18.0"))
3333
})
34+
It("should extract sdk version", func() {
35+
version := "v0.18.0-ge87627f4"
36+
output := parseVersion(version)
37+
Expect(output).To(Equal("v0.18.0+git"))
38+
})
3439

3540
})
3641
})

0 commit comments

Comments
 (0)
Please sign in to comment.