Skip to content

Commit

Permalink
Merge pull request #316 from samira-barouti/bugfix-yaml-manifests
Browse files Browse the repository at this point in the history
Ensure DeployableByOLM outputs the artifact in yaml manifest format
  • Loading branch information
bcrochet authored Oct 28, 2021
2 parents 4b64346 + b88be80 commit 5f89532
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions certification/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ var ErrRemoveOSPIDFromProjectID = errors.New("please remove leading character os
var ErrRemoveSpecialCharFromProjectID = errors.New("please remove all special characters from project id")
var ErrPullRequestURL = errors.New("please enter a valid url: including scheme, host, and path to pull request")
var ErrIndexImageUndefined = errors.New("no environment variable PFLT_INDEXIMAGE could be found")
var ErrUnsupportedGoType = errors.New("go type unsupported")
61 changes: 51 additions & 10 deletions certification/internal/policy/operator/deployable_by_olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import (
"strings"
"time"

operatorv1 "github.com/operator-framework/api/pkg/operators/v1"
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"

"github.com/spf13/viper"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/redhat-openshift-ecosystem/openshift-preflight/certification"
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/artifacts"
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/errors"
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/internal/cli"

log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
kubeErr "k8s.io/apimachinery/pkg/api/errors"
)

Expand Down Expand Up @@ -332,25 +336,25 @@ func (p *DeployableByOlmCheck) cleanUp(operatorData OperatorData) {
if err != nil {
log.Error("unable to retrieve the subscription")
}
p.writeToFile(subs, operatorData.App, "subscription")
p.writeToFile(subs)

cs, err := p.OpenshiftEngine.GetCatalogSource(operatorData.App, cli.OpenshiftOptions{Namespace: operatorData.InstallNamespace})
if err != nil {
log.Error("unable to retrieve the catalogsource")
}
p.writeToFile(cs, operatorData.App, "catalogsource")
p.writeToFile(cs)

og, err := p.OpenshiftEngine.GetOperatorGroup(operatorData.App, cli.OpenshiftOptions{Namespace: operatorData.InstallNamespace})
if err != nil {
log.Error("unable to retrieve the operatorgroup")
}
p.writeToFile(og, operatorData.App, "operatorgroup")
p.writeToFile(og)

ns, err := p.OpenshiftEngine.GetNamespace(operatorData.InstallNamespace)
if err != nil {
log.Error("unable to retrieve the namespace")
}
p.writeToFile(ns, operatorData.InstallNamespace, "namespace")
p.writeToFile(ns)

log.Trace("Deleting the resources created by Check")
p.OpenshiftEngine.DeleteSubscription(operatorData.App, cli.OpenshiftOptions{Namespace: operatorData.InstallNamespace})
Expand All @@ -375,15 +379,52 @@ func (p *DeployableByOlmCheck) cleanUp(operatorData OperatorData) {
p.OpenshiftEngine.DeleteNamespace(operatorData.InstallNamespace, cli.OpenshiftOptions{})
}

func (p *DeployableByOlmCheck) writeToFile(data interface{}, resource string, resourceType string) error {
yamlData, err := yaml.Marshal(data)
func (p *DeployableByOlmCheck) writeToFile(data interface{}) error {
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(data)
if err != nil {
log.Error("unable to convert the object to unstructured.Unstructured: ", err)
return err
}

u := &unstructured.Unstructured{Object: obj}

switch data.(type) {
case *operatorv1alpha1.CatalogSource:
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "operators.coreos.com",
Kind: "CatalogSource",
Version: "v1alpha1",
})
case *operatorv1.OperatorGroup:
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "operators.coreos.com",
Kind: "OperatorGroup",
Version: "v1",
})
case *operatorv1alpha1.Subscription:
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "operators.coreos.com",
Kind: "Subscription",
Version: "v1alpha1",
})
case *corev1.Namespace:
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Kind: "Namespace",
Version: "v1",
})
default:
return errors.ErrUnsupportedGoType
}

jsonManifest, err := u.MarshalJSON()
if err != nil {
log.Error("unable to serialize the data")
log.Error("unable to marshal to json: ", err)
return err
}

filename := fmt.Sprintf("%s-%s.yaml", resource, resourceType)
if _, err := artifacts.WriteFile(filename, string(yamlData)); err != nil {
filename := fmt.Sprintf("%s-%s.json", u.GetName(), u.GetKind())
if _, err := artifacts.WriteFile(filename, string(jsonManifest)); err != nil {
log.Error("failed to write the k8s object to the file", err)
return err
}
Expand Down
19 changes: 19 additions & 0 deletions certification/internal/policy/operator/operator_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"
)

func TestOperator(t *testing.T) {
Expand Down Expand Up @@ -136,6 +137,9 @@ func (foe FakeOpenshiftEngine) GetOperatorGroup(name string, opts cli.OpenshiftO
Spec: operatorv1.OperatorGroupSpec{
TargetNamespaces: []string{"test-ns"},
},
Status: operatorv1.OperatorGroupStatus{
LastUpdated: &metav1.Time{Time: time.Now()},
},
}, nil
}

Expand Down Expand Up @@ -210,6 +214,10 @@ func (foe FakeOpenshiftEngine) DeleteCatalogSource(name string, opts cli.Openshi

func (foe FakeOpenshiftEngine) GetCatalogSource(name string, opts cli.OpenshiftOptions) (*operatorv1alpha1.CatalogSource, error) {
return &operatorv1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cs",
Namespace: "test-ns",
},
Spec: operatorv1alpha1.CatalogSourceSpec{
SourceType: operatorv1alpha1.SourceTypeGrpc,
Image: "indexImageUri",
Expand Down Expand Up @@ -347,11 +355,18 @@ func (foe BadOpenshiftEngine) GetOperatorGroup(name string, opts cli.OpenshiftOp
Spec: operatorv1.OperatorGroupSpec{
TargetNamespaces: []string{"test-ns"},
},
Status: operatorv1.OperatorGroupStatus{
LastUpdated: &metav1.Time{Time: time.Now()},
},
}, nil
}

func (foe BadOpenshiftEngine) CreateCatalogSource(data cli.CatalogSourceData, opts cli.OpenshiftOptions) (*operatorv1alpha1.CatalogSource, error) {
return &operatorv1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cs",
Namespace: "test-ns",
},
Spec: operatorv1alpha1.CatalogSourceSpec{
SourceType: operatorv1alpha1.SourceTypeGrpc,
Image: "indexImageUri",
Expand All @@ -365,6 +380,10 @@ func (foe BadOpenshiftEngine) DeleteCatalogSource(name string, opts cli.Openshif

func (foe BadOpenshiftEngine) GetCatalogSource(name string, opts cli.OpenshiftOptions) (*operatorv1alpha1.CatalogSource, error) {
return &operatorv1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cs",
Namespace: "test-ns",
},
Spec: operatorv1alpha1.CatalogSourceSpec{
SourceType: operatorv1alpha1.SourceTypeGrpc,
Image: "indexImageUri",
Expand Down

0 comments on commit 5f89532

Please sign in to comment.