Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bnshr committed Nov 25, 2024
1 parent e3fd824 commit 8b18241
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
25 changes: 24 additions & 1 deletion tests/operator/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package operator
import (
"strings"

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

Expand Down Expand Up @@ -50,11 +51,33 @@ func SplitCsv(csv string) CsvResult {
return result
}

func IsInstallModeSingleNamespace(installModes []v1alpha1.InstallMode) bool {
func isInstallModeSingleNamespace(installModes []v1alpha1.InstallMode) bool {
for i := 0; i < len(installModes); i++ {
if installModes[i].Type == v1alpha1.InstallModeTypeSingleNamespace {
return true
}
}
return false
}

func findOperatorGroup(name, namespace string, groups []*v1.OperatorGroup) *v1.OperatorGroup {
for _, group := range groups {
if group.Name == name && group.Namespace == namespace {
return group
}
}
return nil
}

func checkOperatorCompliance(csv *v1alpha1.ClusterServiceVersion, group *v1.OperatorGroup, operatorNamespace string, targetNamespaces []string) bool {
opGroupTargetNamespaces := group.Spec.TargetNamespaces
if isInstallModeSingleNamespace(csv.Spec.InstallModes) {
return len(opGroupTargetNamespaces) == 1 && len(targetNamespaces) == 1 && opGroupTargetNamespaces[0] == targetNamespaces[0]
}
for _, ns := range opGroupTargetNamespaces {
if ns == operatorNamespace {
return false
}
}
return true
}
68 changes: 23 additions & 45 deletions tests/operator/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package operator

import (
"fmt"
"strings"

"github.com/redhat-best-practices-for-k8s/certsuite/tests/common"
Expand All @@ -28,8 +29,6 @@ import (
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/provider"
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/testhelper"
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/versions"

v1 "github.com/operator-framework/api/pkg/operators/v1"
)

var (
Expand Down Expand Up @@ -146,54 +145,33 @@ func testOperatorInstallationInTenantNamespace(check *checksdb.Check,
csv := operator.Csv

operatorNamespace := csv.Annotations["olm.operatorNamespace"]
operatorGroupName := csv.Annotations["olm.operatorGroup"]

targetNamespacesStr := csv.Annotations["olm.targetNamespaces"]
operatorTargetNamespaces := strings.Split(targetNamespacesStr, ",")
check.LogInfo("operatorNamespace %s, targetNamespaces %v", operatorNamespace, operatorTargetNamespaces)
operatorGroup := findOperatorGroup(csv.Annotations["olm.operatorGroup"], operator.Namespace, env.OperatorGroups)

var operatorGroup *v1.OperatorGroup
for _, opGroup := range env.OperatorGroups {
if opGroup.Name == operatorGroupName && opGroup.Namespace == operator.Namespace {
operatorGroup = opGroup
break
}
}
check.LogInfo("operatorNamespace %s, targetNamespaces %v, operatorGroup %s", operatorNamespace,
operatorTargetNamespaces, operatorGroup.Name)

opGroupTargetNamespaces := operatorGroup.Spec.TargetNamespaces // array of strings

if IsInstallModeSingleNamespace(csv.Spec.InstallModes) {
// checks opgroup targetnamespace matches with csv targetnamespace
if len(opGroupTargetNamespaces) == 1 && len(operatorTargetNamespaces) == 1 {
if opGroupTargetNamespaces[0] == operatorTargetNamespaces[0] {
check.LogInfo("Operator %s with SingleInstallMode is installed in tenant namespace ", operator.Name)
compliantObjects = append(compliantObjects, testhelper.NewOperatorReportObject(operator.Namespace, operator.Name,
"Operator with SingleInstallMode is not installed in tenant namespace ", true).AddField(testhelper.OperatorName, operator.Name))
} else {
check.LogInfo("Operator %s with SingleInstallMode is not installed in tenant namespace ", operator.Name)
nonCompliantObjects = append(nonCompliantObjects, testhelper.NewOperatorReportObject(operator.Namespace, operator.Name,
"Operator with SingleInstallMode is not installed in tenant namespace ", false).AddField(testhelper.OperatorName, operator.Name))
}
}
} else {
// The operator must not be installed inside the targetNamespaces
var isOperatorInstalledInTargetNamespaces bool
for _, opGroupTargetNamespace := range opGroupTargetNamespaces {
if opGroupTargetNamespace == operatorNamespace {
isOperatorInstalledInTargetNamespaces = true
break
}
}
isCompliant := checkOperatorCompliance(csv, operatorGroup, operatorNamespace, operatorTargetNamespaces)
isSingleNamespace := isInstallModeSingleNamespace(csv.Spec.InstallModes)

if !isOperatorInstalledInTargetNamespaces {
check.LogInfo("Operator %s with non-SingleInstallMode is not installed in the tenant namespace ", operator.Name)
compliantObjects = append(compliantObjects, testhelper.NewOperatorReportObject(operator.Namespace, operator.Name,
"Operator with non-SingleInstallMode is not installed in tenant namespace ", true).AddField(testhelper.OperatorName, operator.Name))
} else {
check.LogInfo("Operator %s with non-SingleInstallMode is installed in the tenant namespace ", operator.Name)
nonCompliantObjects = append(nonCompliantObjects, testhelper.NewOperatorReportObject(operator.Namespace, operator.Name,
"Operator with non-SingleInstallMode is installed in tenant namespace ", false).AddField(testhelper.OperatorName, operator.Name))
}
message := "Operator with%s is %sinstalled in tenant namespace "
mode := "SingleNamespace InstallMode"
if !isSingleNamespace {
mode = "no SingleNamespace InstallMode"
}
status := "not "
if isCompliant {
status = ""
msg := fmt.Sprintf(message, mode, status)
check.LogInfo(msg)
compliantObjects = append(compliantObjects, testhelper.NewOperatorReportObject(operator.Namespace, operator.Name,
msg, true).AddField(testhelper.OperatorName, operator.Name))
} else {
msg := fmt.Sprintf(message, mode, status)
check.LogInfo(msg)
nonCompliantObjects = append(nonCompliantObjects, testhelper.NewOperatorReportObject(operator.Namespace, operator.Name,
msg, false).AddField(testhelper.OperatorName, operator.Name))
}
}

Expand Down

0 comments on commit 8b18241

Please sign in to comment.