Skip to content

Commit e8ed9cd

Browse files
camilamacedo86ankitathomas
authored andcommitted
(fix) : Fix check to verify OCP label versions when it is =v4.x (openshift#148)
Upstream-repository: api Upstream-commit: e4b9266c693b1443c33fff99aabd49c07232e6ba
1 parent 4441e74 commit e8ed9cd

File tree

5 files changed

+179
-135
lines changed

5 files changed

+179
-135
lines changed

staging/api/pkg/validation/internal/community.go

+73-66
Original file line numberDiff line numberDiff line change
@@ -239,75 +239,71 @@ func validateImageFile(checks CommunityOperatorChecks, deprecatedAPImsg string)
239239
}
240240

241241
value := strings.Split(line[i], "=")
242-
indexRange := value[1]
243-
doubleCote := "\""
244-
singleCote := "'"
245-
indexRange = strings.ReplaceAll(indexRange, singleCote, "")
246-
indexRange = strings.ReplaceAll(indexRange, doubleCote, "")
242+
// It means that the OCP label is =OCP version
243+
if len(value) > 2 && len(value[2]) > 0 {
244+
version := cleanStringToGetTheVersionToParse(value[2])
245+
verParsed, err := semver.ParseTolerant(version)
246+
if err != nil {
247+
checks.errs = append(checks.errs, fmt.Errorf("unable to parse the value (%s) on (%s)",
248+
version, ocpLabelindex))
249+
return checks
250+
}
251+
252+
if verParsed.GE(semVerOCPV1beta1Unsupported) {
253+
checks.errs = append(checks.errs, fmt.Errorf("this bundle is using APIs which were "+
254+
"deprecated and removed in v1.22. "+
255+
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22. "+
256+
"Migrate the API(s) for "+
257+
"%s or provide compatible version(s) by using the %s annotation in "+
258+
"`metadata/annotations.yaml` to ensure that the index image will be geneared "+
259+
"with its label. (e.g. LABEL %s='4.6-4.8')",
260+
deprecatedAPImsg,
261+
ocpLabelindex,
262+
ocpLabelindex))
263+
return checks
264+
}
265+
return checks
266+
}
267+
indexRange := cleanStringToGetTheVersionToParse(value[1])
247268
if len(indexRange) > 1 {
248269
// if has the = then, the value needs to be < 4.9
249-
if strings.Contains(indexRange, "=") {
250-
version := strings.Split(indexRange, "=")[1]
251-
verParsed, err := semver.ParseTolerant(version)
252-
if err != nil {
253-
checks.errs = append(checks.errs, fmt.Errorf("unable to parse the value (%s) on (%s)",
254-
version, ocpLabelindex))
255-
return checks
256-
}
257-
258-
if verParsed.GE(semVerOCPV1beta1Unsupported) {
259-
checks.errs = append(checks.errs, fmt.Errorf("this bundle is using APIs which were "+
260-
"deprecated and removed in v1.22. "+
261-
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22. "+
262-
"Migrate the API(s) for "+
263-
"%s or provide compatible version(s) by using the %s annotation in "+
264-
"`metadata/annotations.yaml` to ensure that the index image will be geneared "+
265-
"with its label. (e.g. LABEL %s='4.6-4.8')",
266-
deprecatedAPImsg,
267-
ocpLabelindex,
268-
ocpLabelindex))
269-
return checks
270-
}
271-
} else {
272-
// if not has not the = then the value needs contains - value less < 4.9
273-
if !strings.Contains(indexRange, "-") {
274-
checks.errs = append(checks.errs, fmt.Errorf("this bundle is using APIs which were "+
275-
"deprecated and removed in v1.22. "+
276-
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22 "+
277-
"The %s allows to distribute it on >= %s. Migrate the API(s) for "+
278-
"%s or provide compatible version(s) by using the %s annotation in "+
279-
"`metadata/annotations.yaml` to ensure that the index image will be generated "+
280-
"with its label. (e.g. LABEL %s='4.6-4.8')",
281-
indexRange,
282-
ocpVerV1beta1Unsupported,
283-
deprecatedAPImsg,
284-
ocpLabelindex,
285-
ocpLabelindex))
286-
return checks
287-
}
288-
289-
version := strings.Split(indexRange, "-")[1]
290-
verParsed, err := semver.ParseTolerant(version)
291-
if err != nil {
292-
checks.errs = append(checks.errs, fmt.Errorf("unable to parse the value (%s) on (%s)",
293-
version, ocpLabelindex))
294-
return checks
295-
}
296-
297-
if verParsed.GE(semVerOCPV1beta1Unsupported) {
298-
checks.errs = append(checks.errs, fmt.Errorf("this bundle is using APIs which were "+
299-
"deprecated and removed in v1.22. "+
300-
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22. "+
301-
"Upgrade the APIs from "+
302-
"for %s or provide compatible distribution version(s) by using the %s "+
303-
"annotation in `metadata/annotations.yaml` to ensure that the index image will "+
304-
"be generated with its label. (e.g. LABEL %s='4.6-4.8')",
305-
deprecatedAPImsg,
306-
ocpLabelindex,
307-
ocpLabelindex))
308-
return checks
309-
}
270+
// if not has not the = then the value needs contains - value less < 4.9
271+
if !strings.Contains(indexRange, "-") {
272+
checks.errs = append(checks.errs, fmt.Errorf("this bundle is using APIs which were "+
273+
"deprecated and removed in v1.22. "+
274+
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22 "+
275+
"The %s allows to distribute it on >= %s. Migrate the API(s) for "+
276+
"%s or provide compatible version(s) by using the %s annotation in "+
277+
"`metadata/annotations.yaml` to ensure that the index image will be generated "+
278+
"with its label. (e.g. LABEL %s='4.6-4.8')",
279+
indexRange,
280+
ocpVerV1beta1Unsupported,
281+
deprecatedAPImsg,
282+
ocpLabelindex,
283+
ocpLabelindex))
284+
return checks
285+
}
286+
287+
version := strings.Split(indexRange, "-")[1]
288+
verParsed, err := semver.ParseTolerant(version)
289+
if err != nil {
290+
checks.errs = append(checks.errs, fmt.Errorf("unable to parse the value (%s) on (%s)",
291+
version, ocpLabelindex))
292+
return checks
293+
}
310294

295+
if verParsed.GE(semVerOCPV1beta1Unsupported) {
296+
checks.errs = append(checks.errs, fmt.Errorf("this bundle is using APIs which were "+
297+
"deprecated and removed in v1.22. "+
298+
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22. "+
299+
"Upgrade the APIs from "+
300+
"for %s or provide compatible distribution version(s) by using the %s "+
301+
"annotation in `metadata/annotations.yaml` to ensure that the index image will "+
302+
"be generated with its label. (e.g. LABEL %s='4.6-4.8')",
303+
deprecatedAPImsg,
304+
ocpLabelindex,
305+
ocpLabelindex))
306+
return checks
311307
}
312308
} else {
313309
checks.errs = append(checks.errs, fmt.Errorf("unable to get the range informed on %s",
@@ -328,3 +324,14 @@ func validateImageFile(checks CommunityOperatorChecks, deprecatedAPImsg string)
328324
}
329325
return checks
330326
}
327+
328+
// cleanStringToGetTheVersionToParse will remove the expected characters for
329+
// we are able to parse the version informed.
330+
func cleanStringToGetTheVersionToParse(value string) string {
331+
doubleQuote := "\""
332+
singleQuote := "'"
333+
value = strings.ReplaceAll(value, singleQuote, "")
334+
value = strings.ReplaceAll(value, doubleQuote, "")
335+
value = strings.ReplaceAll(value, "v", "")
336+
return value
337+
}

staging/api/pkg/validation/internal/community_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ func Test_communityValidator(t *testing.T) {
129129
},
130130
},
131131
},
132+
{
133+
name: "should pass when the olm annotation and index label are set with a " +
134+
"value =v4.8 and has deprecated apis",
135+
wantError: false,
136+
args: args{
137+
bundleDir: "./testdata/valid_bundle_v1beta1",
138+
imageIndexPath: "./testdata/dockerfile/valid_bundle_4_8.Dockerfile",
139+
annotations: map[string]string{
140+
"olm.properties": fmt.Sprintf(`[{"type": "olm.maxOpenShiftVersion", "value": "4.8"}]`),
141+
},
142+
},
143+
},
132144
}
133145

134146
for _, tt := range tests {

staging/api/pkg/validation/internal/operatorhub_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ func TestValidateHubDeprecatedAPIS(t *testing.T) {
397397
minKubeVersion: "",
398398
directory: "./testdata/valid_bundle_v1beta1",
399399
},
400-
wantError: true,
401-
errStrings: []string{"this bundle is using APIs which were deprecated and removed in v1.22. " +
400+
wantError: true,
401+
errStrings: []string{"this bundle is using APIs which were deprecated and removed in v1.22. " +
402402
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22. " +
403403
"Migrate the API(s) for CRD: ([\"etcdbackups.etcd.database.coreos.com\"" +
404404
" \"etcdclusters.etcd.database.coreos.com\" \"etcdrestores.etcd.database.coreos.com\"])"},
@@ -423,7 +423,7 @@ func TestValidateHubDeprecatedAPIS(t *testing.T) {
423423
},
424424
wantError: true,
425425
wantWarning: true,
426-
errStrings: []string{"unable to use csv.Spec.MinKubeVersion to verify the CRD/Webhook apis because it " +
426+
errStrings: []string{"unable to use csv.Spec.MinKubeVersion to verify the CRD/Webhook apis because it " +
427427
"has an invalid value: invalid"},
428428
warnStrings: []string{"this bundle is using APIs which were deprecated and removed in v1.22. " +
429429
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22. " +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM scratch
2+
3+
# Core bundle labels.
4+
LABEL operators.operatorframework.io.bundle.mediatype.v1=registry+v1
5+
LABEL operators.operatorframework.io.bundle.manifests.v1=manifests/
6+
LABEL operators.operatorframework.io.bundle.metadata.v1=metadata/
7+
LABEL operators.operatorframework.io.bundle.package.v1=memcached-operator
8+
LABEL operators.operatorframework.io.bundle.channels.v1=alpha
9+
LABEL com.redhat.openshift.versions="=v4.8"
10+
11+
# Labels for testing.
12+
LABEL operators.operatorframework.io.test.mediatype.v1=scorecard+v1
13+
LABEL operators.operatorframework.io.test.config.v1=tests/scorecard/
14+
15+
# Copy files to locations specified by labels.
16+
COPY bundle/manifests /manifests/
17+
COPY bundle/metadata /metadata/
18+
COPY bundle/tests/scorecard /tests/scorecard/

vendor/github.com/operator-framework/api/pkg/validation/internal/community.go

+73-66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)