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 cd3ecd0

Browse files
committedJun 3, 2020
[sdk-stamps] Modify test case for addition of stamps in crds
1 parent 2a26af3 commit cd3ecd0

File tree

10 files changed

+119
-53
lines changed

10 files changed

+119
-53
lines changed
 

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,15 @@ func getAnnotationsFilePath(outputDir, manifestsDir string) string {
381381
}
382382

383383
func getSDKMetricsContent() map[string]string {
384-
content := projutil.MakeMetricsLabels()
384+
content := projutil.MakeBundleMetricsLabels()
385385
return content.Data
386386
}
387387

388388
func writeMetricsToAnnotationsYaml(outputDir, manifestsDir string) error {
389389
annotationsFilePath := getAnnotationsFilePath(outputDir, manifestsDir)
390390

391391
// Write metrics annotations
392-
metricsContent := projutil.MakeMetricsLabels()
392+
metricsContent := projutil.MakeBundleMetricsLabels()
393393
err := registry.RewriteAnnotationsYaml(annotationsFilePath, manifestsDir, metricsContent.Data)
394394
if err != nil {
395395
return err

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

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

312-
if err = genutil.GenerateCRDNonGo(projectName, *resource, apiFlags.CrdVersion, projutil.OperatorTypeAnsible); err != nil {
312+
if err = genutil.GenerateCRDNonGo(projectName, *resource,
313+
apiFlags.CrdVersion, projutil.OperatorTypeAnsible); err != nil {
313314
return err
314315
}
315316

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,15 @@ func (b ClusterServiceVersion) GetBase() (base *v1alpha1.ClusterServiceVersion,
9292
return base, nil
9393
}
9494

95-
// addSDKstamps adds SDK stamps to CSV if they do not exist. It assumes that if sdk Builder
96-
// stamp is not present, then sdk stamps are also not populated in csv. Addition of Mediatype
97-
// stamp is skipped in csv.
95+
// addSDKstamps adds SDK stamps to the base if they do not exist. It assumes that if sdk Builder
96+
// stamp is not present, then sdk stamps are also not populated in csv.
9897
func addSDKstamps(csv *v1alpha1.ClusterServiceVersion) {
99-
if _, exist := csv.ObjectMeta.Annotations[projutil.Builder]; !exist {
100-
metricLabels := projutil.MakeMetricsLabels()
98+
if _, exist := csv.ObjectMeta.Annotations[projutil.OperatorBuilder]; !exist {
99+
metricLabels := projutil.MakeOperatorMetricLables()
101100
for label, value := range metricLabels.Data {
102-
if label != projutil.Mediatype {
103-
csv.ObjectMeta.Annotations[label] = value
104-
}
101+
csv.ObjectMeta.Annotations[label] = value
105102
}
106103
}
107-
108104
}
109105

110106
// setDefaults sets default values in b using b's existing values.

‎internal/generate/clusterserviceversion/clusterserviceversion_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ var (
6161
)
6262

6363
const (
64-
sdkBuilderTest = "operators.operatorframework.io.metrics.builder: operator-sdk-unknown"
65-
sdkLayoutTest = "operators.operatorframework.io.metrics.project_layout: unknown"
64+
testSDKbuilderStamp = "operators.operatorframework.io/builder: operator-sdk-unknown"
65+
testSDKlayoutStamp = "operators.operatorframework.io/project_layout: unknown"
6666
)
6767

6868
var (
@@ -162,8 +162,8 @@ var _ = Describe("Generating a ClusterServiceVersion", func() {
162162

163163
annotations := outputCSV.ObjectMeta.Annotations
164164
Expect(annotations).ToNot(BeNil())
165-
Expect(annotations).Should(HaveKey(projutil.Builder))
166-
Expect(annotations).Should(HaveKey(projutil.Layout))
165+
Expect(annotations).Should(HaveKey(projutil.OperatorBuilder))
166+
Expect(annotations).Should(HaveKey(projutil.OperatorLayout))
167167
})
168168
It("should write a ClusterServiceVersion manifest to a bundle file", func() {
169169
g = Generator{
@@ -482,6 +482,6 @@ func upgradeCSV(csv *v1alpha1.ClusterServiceVersion, name, version string) *v1al
482482
// cases do not generate a PROJECTFILE or an entire operator to get the version or layout
483483
// of SDK. Hence the values of those will appear "unknown".
484484
func removeSDKStampsFromCSVString(csv string) string {
485-
replacer := strings.NewReplacer(sdkBuilderTest, "", sdkLayoutTest, "")
485+
replacer := strings.NewReplacer(testSDKbuilderStamp, "", testSDKlayoutStamp, "")
486486
return replacer.Replace(csv)
487487
}

‎internal/generate/crd/crd.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,12 @@ func setSDKstamps(crd *apiextv1beta1.CustomResourceDefinition, operatorType stri
384384
}
385385

386386
func addStampsToAnnotations(mapAnnotations map[string]string, operatorType string) {
387-
metricLables := projutil.MakeMetricsLabels()
387+
metricLables := projutil.MakeOperatorMetricLables()
388388
for label, value := range metricLables.Data {
389-
if label != projutil.Mediatype {
390-
mapAnnotations[label] = value
391-
}
389+
mapAnnotations[label] = value
392390
}
393-
// Modifying operator type for ansible and helm legacy operators
391+
// Modifying operator type for ansible and helm legacy operators, as during
392+
// scaffolding the project directories are not written on disk
394393
if operatorType != projutil.OperatorTypeGo {
395394
projutil.SetSDKProjectLayout(operatorType, mapAnnotations)
396395
}

‎internal/generate/crd/crd_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ func TestCRDNonGo(t *testing.T) {
212212
Resource: *r,
213213
CRDVersion: c.crdVersion,
214214
IsOperatorGo: false,
215+
OperatorType: "unknown",
215216
}
216217
fileMap, err := g.generateNonGo()
217218
if err != nil {
@@ -231,6 +232,9 @@ func TestCRDNonGo(t *testing.T) {
231232
const crdNonGoDefaultExpV1beta1 = `apiVersion: apiextensions.k8s.io/v1beta1
232233
kind: CustomResourceDefinition
233234
metadata:
235+
annotations:
236+
operators.operatorframework.io/builder: operator-sdk-unknown
237+
operators.operatorframework.io/project_layout: unknown
234238
name: memcacheds.cache.example.com
235239
spec:
236240
group: cache.example.com
@@ -257,6 +261,9 @@ spec:
257261
const crdNonGoDefaultExpV1 = `apiVersion: apiextensions.k8s.io/v1
258262
kind: CustomResourceDefinition
259263
metadata:
264+
annotations:
265+
operators.operatorframework.io/builder: operator-sdk-unknown
266+
operators.operatorframework.io/project_layout: unknown
260267
name: memcacheds.cache.example.com
261268
spec:
262269
group: cache.example.com
@@ -283,6 +290,9 @@ spec:
283290
const crdCustomExpV1beta1 = `apiVersion: apiextensions.k8s.io/v1beta1
284291
kind: CustomResourceDefinition
285292
metadata:
293+
annotations:
294+
operators.operatorframework.io/builder: operator-sdk-unknown
295+
operators.operatorframework.io/project_layout: unknown
286296
name: memcacheds.cache.example.com
287297
spec:
288298
group: cache.example.com
@@ -344,6 +354,9 @@ spec:
344354
const crdCustomExpV1 = `apiVersion: apiextensions.k8s.io/v1
345355
kind: CustomResourceDefinition
346356
metadata:
357+
annotations:
358+
operators.operatorframework.io/builder: operator-sdk-unknown
359+
operators.operatorframework.io/project_layout: unknown
347360
name: memcacheds.cache.example.com
348361
spec:
349362
group: cache.example.com

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

+36-16
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,8 @@ func TestGoCSVUpgradeWithInputsToOutput(t *testing.T) {
175175

176176
// Read generated CSV from outputDir path
177177
csvOutputFile := filepath.Join(outputFromCSVDir, csvVersion, csvFileName)
178-
179178
outputCSV := getCSVFromFile(t, csvOutputFile)
180-
181179
removeSDKstampsFromCSVHelper(outputCSV)
182-
183180
expCSV := getCSVFromFile(t, expCsvFile)
184181

185182
assert.Equal(t, expCSV, outputCSV)
@@ -216,7 +213,7 @@ func TestGoCSVNew(t *testing.T) {
216213

217214
outputCSV, err := generateCSV(b)
218215
if err != nil {
219-
t.Errorf("error occurred %v", err)
216+
t.Errorf("Error occurred while generating CSV %v", err)
220217
}
221218
removeSDKstampsFromCSVHelper(outputCSV)
222219
assert.Equal(t, expCSV, outputCSV)
@@ -253,7 +250,7 @@ func TestGoCSVUpdate(t *testing.T) {
253250

254251
outputCSV, err := generateCSV(b)
255252
if err != nil {
256-
t.Errorf("error occurred %v", err)
253+
t.Errorf("Error occurred while generating CSV, %v", err)
257254
}
258255
removeSDKstampsFromCSVHelper(outputCSV)
259256
assert.Equal(t, expCSV, outputCSV)
@@ -290,7 +287,7 @@ func TestGoCSVUpgrade(t *testing.T) {
290287

291288
outputCSV, err := generateCSV(b)
292289
if err != nil {
293-
t.Errorf("error occurred %v", err)
290+
t.Errorf("Error occurred while generating CSV, %v", err)
294291
}
295292
removeSDKstampsFromCSVHelper(outputCSV)
296293
assert.Equal(t, expCSV, outputCSV)
@@ -327,7 +324,7 @@ func TestGoCSVNewManifests(t *testing.T) {
327324
expCSV := getCSVFromFile(t, filepath.Join(OLMCatalogDir, testProjectName, noUpdateDir, csvExpFile))
328325
outputCSV, err := generateCSV(b)
329326
if err != nil {
330-
t.Errorf("error occurred %v", err)
327+
t.Errorf("Error occurred while generating CSV, %v", err)
331328
}
332329
removeSDKstampsFromCSVHelper(outputCSV)
333330
assert.Equal(t, expCSV, outputCSV)
@@ -362,12 +359,12 @@ func TestGoCSVUpdateManifests(t *testing.T) {
362359
} else {
363360
expCSV := getCSVFromFile(t, filepath.Join(OLMCatalogDir, testProjectName, csvVersion, csvExpFile))
364361
if err != nil {
365-
t.Errorf("error occurred %v", err)
362+
t.Errorf("Error occurred while generating CSV, %v", err)
366363
}
367364

368365
outputCSV, err := generateCSV(b)
369366
if err != nil {
370-
t.Errorf("error occurred %v", err)
367+
t.Errorf("Error occurred while generating CSV, %v", err)
371368
}
372369
removeSDKstampsFromCSVHelper(outputCSV)
373370
assert.Equal(t, expCSV, outputCSV)
@@ -449,6 +446,31 @@ func TestGoCSVNewWithEmptyDeployDir(t *testing.T) {
449446
}
450447
}
451448

449+
func TestSDKStamps(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+
452474
func TestUpdateCSVVersion(t *testing.T) {
453475
cleanupFunc := chDirWithCleanup(t, testGoDataDir)
454476
defer cleanupFunc()
@@ -511,12 +533,12 @@ func getCSVFromFile(t *testing.T, path string) *olmapiv1alpha1.ClusterServiceVer
511533
csvpath := filepath.Join(path)
512534
b, err := ioutil.ReadFile(csvpath)
513535
if err != nil {
514-
t.Errorf("error reading manifest %s: %v", path, err)
536+
t.Errorf("Error reading manifest %s: %v", path, err)
515537
}
516538

517539
csv, err := generateCSV(b)
518540
if err != nil {
519-
t.Errorf("error generating csv %s: %v", path, err)
541+
t.Errorf("Error generating csv %s: %v", path, err)
520542
}
521543
return csv
522544
}
@@ -548,12 +570,10 @@ func generateCSV(input []byte) (*olmapiv1alpha1.ClusterServiceVersion, error) {
548570
// removeSDKstampsFromCSV removes the sdk stamps from CSV struct. Used for test cases where
549571
// we need to test the generated CSV with expected predefined CSV file on disk.
550572
func removeSDKstampsFromCSVHelper(csv *v1alpha1.ClusterServiceVersion) {
551-
if _, exist := csv.ObjectMeta.Annotations[projutil.Builder]; exist {
552-
metricLabels := projutil.MakeMetricsLabels()
573+
if _, exist := csv.ObjectMeta.Annotations[projutil.OperatorBuilder]; exist {
574+
metricLabels := projutil.MakeOperatorMetricLables()
553575
for label := range metricLabels.Data {
554-
if label != projutil.Mediatype {
555-
delete(csv.ObjectMeta.Annotations, label)
556-
}
576+
delete(csv.ObjectMeta.Annotations, label)
557577
}
558578
}
559579
}

‎internal/registry/validate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
2525
registrybundle "github.com/operator-framework/operator-registry/pkg/lib/bundle"
2626
log "github.com/sirupsen/logrus"
27-
"gopkg.in/yaml.v2"
2827
k8svalidation "k8s.io/apimachinery/pkg/api/validation"
2928
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3029
"k8s.io/apimachinery/pkg/util/validation/field"
30+
"sigs.k8s.io/yaml"
3131
)
3232

3333
const (

‎internal/util/projutil/projutil_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ var _ = Describe("Testing projutil helpers", func() {
9090
})
9191

9292
})
93+
94+
Describe("Test sdk_metrics_util helpers", func() {
95+
Describe("Testing SDK version", func() {
96+
It("should extract sdk version", func() {
97+
version := "v0.17.0-159-ge87627f4-dirty"
98+
output := parseVersion(version)
99+
Expect(output).To(Equal("v0.17.0"))
100+
})
101+
It("should extract sdk version", func() {
102+
version := "v0.18.0"
103+
output := parseVersion(version)
104+
Expect(output).To(Equal("v0.18.0"))
105+
})
106+
107+
})
108+
})
109+
93110
})
94111

95112
func TestMetadata(t *testing.T) {

‎internal/util/projutil/sdk_metrics_util.go

+34-14
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,54 @@
1515
package projutil
1616

1717
import (
18+
"regexp"
19+
1820
kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
1921
ver "github.com/operator-framework/operator-sdk/version"
2022
)
2123

2224
const (
23-
OperatorSDK = "operator-sdk"
24-
Mediatype = "operators.operatorframework.io.metrics.mediatype.v1"
25-
Builder = "operators.operatorframework.io.metrics.builder"
26-
Layout = "operators.operatorframework.io.metrics.project_layout"
27-
sdkMediatype = "metrics+v1"
28-
)
29-
30-
var (
31-
sdkBuilder = OperatorSDK + "-" + ver.GitVersion
25+
OperatorSDK = "operator-sdk"
26+
BundleMediatype = "operators.operatorframework.io.metrics.mediatype.v1"
27+
BundleSDKBuilder = "operators.operatorframework.io.metrics.builder"
28+
BundleSDKLayout = "operators.operatorframework.io.metrics.project_layout"
29+
sdkMediatype = "metrics+v1"
30+
OperatorBuilder = "operators.operatorframework.io/builder"
31+
OperatorLayout = "operators.operatorframework.io/project_layout"
3232
)
3333

3434
type MetricLabels struct {
3535
Data map[string]string
3636
}
3737

38-
func MakeMetricsLabels() MetricLabels {
38+
func MakeBundleMetricsLabels() MetricLabels {
3939
m := MetricLabels{
4040
Data: map[string]string{
41-
Mediatype: sdkMediatype,
42-
Builder: sdkBuilder,
43-
Layout: getSDKProjectLayout(),
41+
BundleMediatype: sdkMediatype,
42+
BundleSDKBuilder: getSDKProjectLayout(),
43+
BundleSDKLayout: getSDKProjectLayout(),
4444
},
4545
}
4646
return m
47+
}
48+
49+
func MakeOperatorMetricLables() MetricLabels {
50+
m := MetricLabels{
51+
Data: map[string]string{
52+
OperatorBuilder: getSDKProjectLayout(),
53+
OperatorLayout: getSDKProjectLayout(),
54+
},
55+
}
56+
return m
57+
}
58+
59+
func getSDKBuilder() string {
60+
return OperatorSDK + "-" + parseVersion(ver.GitVersion)
61+
}
4762

63+
func parseVersion(input string) string {
64+
re := regexp.MustCompile("v[0-9]*.[0-9]*.[0-9]*")
65+
return re.FindString(input)
4866
}
4967

5068
// getSDKProjectLayout returns the `layout` field in PROJECT file if it is a
@@ -60,6 +78,8 @@ func getSDKProjectLayout() string {
6078
return GetOperatorType()
6179
}
6280

81+
// SetSDKProjectLayout is a helper function to enable CRDs in Helm and Ansible
82+
// operators, to set operator layout value based on input scaffolding flag.
6383
func SetSDKProjectLayout(operatorType string, metricData map[string]string) {
64-
metricData[Layout] = operatorType
84+
metricData[OperatorLayout] = operatorType
6585
}

0 commit comments

Comments
 (0)
Please sign in to comment.