Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: alm-example parse check should not fail when the annotation if empty #215

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/validation/internal/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func validateExamplesAnnotations(csv *v1alpha1.ClusterServiceVersion) (errs []er

func validateJSON(value string) error {
var js json.RawMessage

if len(value) == 0 {
return nil
}

byteValue := []byte(value)
if err := json.Unmarshal(byteValue, &js); err != nil {
switch t := err.(type) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/validation/internal/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"io/ioutil"
"k8s.io/apimachinery/pkg/runtime/schema"
"path/filepath"
"testing"

Expand Down Expand Up @@ -80,6 +81,16 @@ func TestValidateCSV(t *testing.T) {
},
filepath.Join("testdata", "invalid.alm-examples.csv.yaml"),
},
{
validatorFuncTest{
description: "should not fail when alm-examples is not informed",
wantWarn: true,
errors: []errors.Error{
errors.WarnInvalidOperation("provided API should have an example annotation", schema.GroupVersionKind{Group: "etcd.database.coreos.com", Version: "v1beta2", Kind: "EtcdCluster"}),
},
},
filepath.Join("testdata", "correct.csv.empty.example.yaml"),
},
}
for _, c := range cases {
b, err := ioutil.ReadFile(c.csvPath)
Expand Down
61 changes: 61 additions & 0 deletions pkg/validation/internal/testdata/correct.csv.empty.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#! validate-crd: deploy/chart/templates/0000_30_02-clusterserviceversion.crd.yaml
#! parse-kind: ClusterServiceVersion
apiVersion: operators.coreos.com/v1alpha1
kind: ClusterServiceVersion
metadata:
name: etcdoperator.v0.9.0
namespace: placeholder
annotations:
"alm-examples": ""
spec:
version: 0.9.0
installModes:
- type: AllNamespaces
supported: true
install:
strategy: deployment
spec:
permissions:
- serviceAccountName: etcd-operator
rules:
- apiGroups:
- etcd.database.coreos.com
resources:
- etcdclusters
- etcdbackups
- etcdrestores
verbs:
- "*"
deployments:
- name: etcd-operator
spec:
replicas: 1
template:
metadata:
name: etcd-operator-alm-owned
labels:
name: etcd-operator-alm-owned
spec:
serviceAccountName: etcd-operator
containers:
- name: etcd-operator
command:
- etcd-operator
- --create-crd=false
image: quay.io/coreos/etcd-operator@sha256:db563baa8194fcfe39d1df744ed70024b0f1f9e9b55b5923c2f3a413c44dc6b8
env:
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
customresourcedefinitions:
owned:
- name: etcdclusters.etcd.database.coreos.com
version: v1beta2
kind: EtcdCluster