Skip to content

Commit 31757cd

Browse files
Merge pull request #188 from acmenezes/refactor_decouple
Refactor decouple
2 parents 96948d8 + 8d227c2 commit 31757cd

File tree

10 files changed

+243
-294
lines changed

10 files changed

+243
-294
lines changed

cmd/check.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88

99
"github.com/opdev/opcap/internal/capability"
10-
"github.com/opdev/opcap/internal/logger"
1110
"github.com/opdev/opcap/internal/operator"
1211

1312
pkgserverv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
@@ -45,7 +44,7 @@ Flags:
4544
return types.Error{Msg: "Unable to create OpCap client."}
4645
}
4746
var packageManifestList pkgserverv1.PackageManifestList
48-
err = psc.ListPackageManifests(context.TODO(), &packageManifestList, checkflags)
47+
err = psc.ListPackageManifests(context.TODO(), &packageManifestList, checkflags.CatalogSource, checkflags.Packages)
4948
if err != nil {
5049
return types.Error{Msg: "Unable to list PackageManifests.\n" + err.Error()}
5150
}
@@ -64,17 +63,27 @@ Flags:
6463
return nil
6564
},
6665
Run: func(cmd *cobra.Command, args []string) {
67-
// Build auditor by catalog
68-
auditor, err := capability.BuildAuditorByCatalog(checkflags)
69-
if err != nil {
70-
logger.Sugar.Fatal("Unable to build auditor")
66+
capAuditor := &capability.CapAuditor{
67+
AuditPlan: checkflags.AuditPlan,
68+
CatalogSource: checkflags.CatalogSource,
69+
CatalogSourceNamespace: checkflags.CatalogSourceNamespace,
70+
Packages: checkflags.Packages,
7171
}
72+
7273
// run all dynamically built audits in the auditor workqueue
73-
auditor.RunAudits()
74+
capAuditor.RunAudits()
7475
},
7576
}
7677

77-
var checkflags operator.OperatorCheckOptions
78+
type CheckCommandFlags struct {
79+
AuditPlan []string `json:"auditPlan"`
80+
CatalogSource string `json:"catalogsource"`
81+
CatalogSourceNamespace string `json:"catalogsourcenamespace"`
82+
ListPackages bool `json:"listPackages"`
83+
Packages []string `json:"packages"`
84+
}
85+
86+
var checkflags CheckCommandFlags
7887

7988
func init() {
8089

@@ -87,8 +96,7 @@ func init() {
8796
"specifies the catalogsource to test against")
8897
flags.StringVar(&checkflags.CatalogSourceNamespace, "catalogsourcenamespace", "openshift-marketplace",
8998
"specifies the namespace where the catalogsource exists")
90-
flags.StringSliceVar(&checkflags.AuditPlan, "auditplan", defaultAuditPlan, "audit plan is the ordered list of operator test functions to be called during a capability audit.")
99+
flags.StringSliceVar(&checkflags.AuditPlan, "audit-plan", defaultAuditPlan, "audit plan is the ordered list of operator test functions to be called during a capability audit.")
91100
flags.BoolVar(&checkflags.ListPackages, "list-packages", false, "list packages in the catalog")
92-
flags.StringSliceVar(&checkflags.FilterPackages, "filter-packages", []string{}, "a list of package(s) which limits audits and/or other flag(s) output")
93-
flags.BoolVar(&checkflags.AllInstallModes, "all-installmodes", false, "when set, all install modes supported by an operator will be tested")
101+
flags.StringSliceVar(&checkflags.Packages, "packages", []string{}, "a list of package(s) which limits audits and/or other flag(s) output")
94102
}

internal/capability/audit.go

+24-36
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,66 @@ import (
1010
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
1111
)
1212

13-
// Audit defines all the methods used to run a full audit Plan against a single operator
14-
// All new capability tests should be added to this interface and used with a CapAudit
15-
// instance and as part of an auditPlan
16-
type Audit interface {
17-
OperatorInstall() error
18-
OperandInstall() error
19-
OperandCleanUp() error
20-
OperatorCleanUp() error
21-
Report() error
22-
}
23-
2413
// CapAudit is an implementation of the Audit interface
25-
type CapAudit struct {
14+
type capAudit struct {
2615

2716
// client has access to all operator methods
28-
Client operator.Client
17+
client operator.Client
2918

3019
// OpenShift Cluster Version under test
31-
OcpVersion string
20+
ocpVersion string
3221

3322
// namespace is the ns where the operator will be installed
34-
Namespace string
23+
namespace string
3524

3625
// operatorGroupData contains information to create operator groups
37-
OperatorGroupData operator.OperatorGroupData
26+
operatorGroupData operator.OperatorGroupData
3827

3928
// subscription holds the data to install an operator via OLM
40-
Subscription operator.SubscriptionData
29+
subscription operator.SubscriptionData
4130

4231
// Cluster CSV for current operator under test
43-
Csv operatorv1alpha1.ClusterServiceVersion
32+
csv operatorv1alpha1.ClusterServiceVersion
4433

4534
// How much time to wait for a CSV before timeout
46-
CsvWaitTime time.Duration
35+
csvWaitTime time.Duration
4736

4837
// If the given CSV timed out on install
49-
CsvTimeout bool
38+
csvTimeout bool
5039

5140
// auditPlan is a list of functions to be run in sequence in a given audit
5241
// all of them must be an implemented method of CapAudit and must be part
5342
// of the Audit interface
54-
AuditPlan []string
43+
auditPlan []string
5544

5645
// CustomResources stores CR manifests to deploy operands
57-
CustomResources []map[string]interface{}
46+
customResources []map[string]interface{}
5847

5948
// Operands stores a list of unstructured custom resources that were created at the API level
6049
// This data is used for further analysis on statuses, conditions and other patterns
61-
Operands []unstructured.Unstructured
50+
operands []unstructured.Unstructured
6251
}
6352

64-
func newCapAudit(c operator.Client, subscription operator.SubscriptionData, auditPlan []string) (CapAudit, error) {
53+
func newCapAudit(c operator.Client, subscription operator.SubscriptionData, auditPlan []string) (capAudit, error) {
6554

66-
// TODO: optimize performance by re-using the namespace for the same operator
67-
ns := strings.Join([]string{"opcap", strings.ReplaceAll(subscription.Package, ".", "-"), strings.ToLower(string(subscription.InstallModeType))}, "-")
55+
ns := strings.Join([]string{"opcap", strings.ReplaceAll(subscription.Package, ".", "-")}, "-")
6856
operatorGroupName := strings.Join([]string{subscription.Name, subscription.Channel, "group"}, "-")
6957

7058
ocpVersion, err := c.GetOpenShiftVersion()
7159
if err != nil {
7260
logger.Debugw("Couldn't get OpenShift version for testing", "Err:", err)
73-
return CapAudit{}, err
61+
return capAudit{}, err
7462
}
7563

76-
return CapAudit{
77-
Client: c,
78-
OcpVersion: ocpVersion,
79-
Namespace: ns,
80-
OperatorGroupData: newOperatorGroupData(operatorGroupName, getTargetNamespaces(subscription, ns)),
81-
Subscription: subscription,
82-
CsvWaitTime: time.Minute,
83-
CsvTimeout: false,
84-
AuditPlan: auditPlan,
64+
return capAudit{
65+
client: c,
66+
ocpVersion: ocpVersion,
67+
namespace: ns,
68+
operatorGroupData: newOperatorGroupData(operatorGroupName, getTargetNamespaces(subscription, ns)),
69+
subscription: subscription,
70+
csvWaitTime: time.Minute,
71+
csvTimeout: false,
72+
auditPlan: auditPlan,
8573
}, nil
8674
}
8775

internal/capability/auditor.go

+24-27
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,26 @@ import (
66
"github.com/opdev/opcap/internal/operator"
77
)
88

9-
// Auditor interface represents the object running capability audits against operators
10-
// It has methods to create a workqueue with all the package and audit requirements for a
11-
// particular audit run
12-
type Auditor interface {
13-
BuildWorkQueueByCatalog(opts operator.OperatorCheckOptions) error
14-
RunAudits() error
15-
}
16-
179
// capAuditor implements Auditor
18-
type capAuditor struct {
10+
type CapAuditor struct {
1911

20-
// Workqueue holds capAudits in a buffered channel in order to execute them
21-
WorkQueue chan CapAudit
22-
}
12+
// AuditPlan holds the tests that should be run during an audit
13+
AuditPlan []string
2314

24-
// BuildAuditorByCatalog creates a new Auditor with workqueue based on a selected catalog
25-
func BuildAuditorByCatalog(options operator.OperatorCheckOptions) (capAuditor, error) {
15+
// CatalogSource may be built-in OLM or custom
16+
CatalogSource string
17+
// CatalogSourceNamespace will be openshift-marketplace or custom
18+
CatalogSourceNamespace string
2619

27-
var auditor capAuditor
28-
err := auditor.BuildWorkQueueByCatalog(options)
29-
if err != nil {
30-
logger.Fatalf("Unable to build workqueue err := %s", err.Error())
31-
}
32-
return auditor, nil
20+
// Packages is a subset of packages to be tested from a catalogSource
21+
Packages []string
22+
23+
// WorkQueue holds capAudits in a buffered channel in order to execute them
24+
WorkQueue chan capAudit
3325
}
3426

3527
// BuildWorkQueueByCatalog fills in the auditor workqueue with all package information found in a specific catalog
36-
func (capAuditor *capAuditor) BuildWorkQueueByCatalog(options operator.OperatorCheckOptions) error {
28+
func (capAuditor *CapAuditor) buildWorkQueueByCatalog() error {
3729

3830
c, err := operator.NewOpCapClient()
3931
if err != nil {
@@ -43,20 +35,20 @@ func (capAuditor *capAuditor) BuildWorkQueueByCatalog(options operator.OperatorC
4335
}
4436

4537
// Getting subscription data form the package manifests available in the selected catalog
46-
subscriptions, err := c.GetSubscriptionData(options)
38+
subscriptions, err := c.GetSubscriptionData(capAuditor.CatalogSource, capAuditor.CatalogSourceNamespace, capAuditor.Packages)
4739
if err != nil {
48-
logger.Errorf("Error while getting bundles from CatalogSource %s: %w", options.CatalogSource, err)
40+
logger.Errorf("Error while getting bundles from CatalogSource %s: %w", capAuditor.CatalogSource, err)
4941
return err
5042
}
5143

5244
// build workqueue as buffered channel based subscriptionData list size
53-
capAuditor.WorkQueue = make(chan CapAudit, len(subscriptions))
45+
capAuditor.WorkQueue = make(chan capAudit, len(subscriptions))
5446
defer close(capAuditor.WorkQueue)
5547

5648
// add capAudits to the workqueue
5749
for _, subscription := range subscriptions {
5850

59-
capAudit, err := newCapAudit(c, subscription, options.AuditPlan)
51+
capAudit, err := newCapAudit(c, subscription, capAuditor.AuditPlan)
6052
if err != nil {
6153
logger.Debugf("Couldn't build capAudit for subscription %s", "Err:", err)
6254
return err
@@ -70,14 +62,19 @@ func (capAuditor *capAuditor) BuildWorkQueueByCatalog(options operator.OperatorC
7062
}
7163

7264
// RunAudits executes all selected functions in order for a given audit at a time
73-
func (capAuditor *capAuditor) RunAudits() error {
65+
func (capAuditor *CapAuditor) RunAudits() error {
66+
err := capAuditor.buildWorkQueueByCatalog()
67+
if err != nil {
68+
logger.Debugf("Unable to build workqueue err := %s", err.Error())
69+
return err
70+
}
7471

7572
// read workqueue for audits
7673
for audit := range capAuditor.WorkQueue {
7774

7875
// read a particular audit's auditPlan for functions
7976
// to be executed against operator
80-
for _, function := range audit.AuditPlan {
77+
for _, function := range audit.auditPlan {
8178

8279
// run function/method by name
8380
m := reflect.ValueOf(&audit).MethodByName(function)

internal/capability/operand_cleanup.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package capability
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/opdev/opcap/internal/operator"
87

@@ -14,11 +13,12 @@ import (
1413
)
1514

1615
// OperandCleanup removes the operand from the OCP cluster in the ca.namespace
17-
func (ca *CapAudit) OperandCleanUp() error {
18-
logger.Debugw("cleaningUp operand for operator", "package", ca.Subscription.Package, "channel", ca.Subscription.Channel, "installmode", ca.Subscription.InstallModeType)
16+
func (ca *capAudit) OperandCleanUp() error {
17+
logger.Debugw("cleaningUp operand for operator", "package", ca.subscription.Package, "channel", ca.subscription.Channel, "installmode",
18+
ca.subscription.InstallModeType)
1919

20-
if len(ca.CustomResources) > 0 {
21-
for _, cr := range ca.CustomResources {
20+
if len(ca.customResources) > 0 {
21+
for _, cr := range ca.customResources {
2222
obj := &unstructured.Unstructured{Object: cr}
2323

2424
// using dynamic client to create Unstructured objests in k8s
@@ -28,7 +28,7 @@ func (ca *CapAudit) OperandCleanUp() error {
2828
}
2929

3030
var crdList apiextensionsv1.CustomResourceDefinitionList
31-
err = ca.Client.ListCRDs(context.TODO(), &crdList)
31+
err = ca.client.ListCRDs(context.TODO(), &crdList)
3232
if err != nil {
3333
return err
3434
}
@@ -52,12 +52,12 @@ func (ca *CapAudit) OperandCleanUp() error {
5252
name := obj.Object["metadata"].(map[string]interface{})["name"].(string)
5353

5454
// check if CR exists, only then cleanup the operand
55-
crInstance, _ := client.Resource(gvr).Namespace(ca.Namespace).Get(context.TODO(), name, v1.GetOptions{})
55+
crInstance, _ := client.Resource(gvr).Namespace(ca.namespace).Get(context.TODO(), name, v1.GetOptions{})
5656
if crInstance != nil {
5757
// delete the resource using the dynamic client
58-
err = client.Resource(gvr).Namespace(ca.Namespace).Delete(context.TODO(), name, v1.DeleteOptions{})
58+
err = client.Resource(gvr).Namespace(ca.namespace).Delete(context.TODO(), name, v1.DeleteOptions{})
5959
if err != nil {
60-
fmt.Printf("failed operandCleanUp: %s package: %s error: %s\n", Resource, ca.Subscription.Package, err.Error())
60+
logger.Debugf("failed operandCleanUp: %s package: %s error: %s\n", Resource, ca.subscription.Package, err.Error())
6161
return err
6262
}
6363
}

0 commit comments

Comments
 (0)