Skip to content

Commit

Permalink
Test grpcurl
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 committed Dec 3, 2024
1 parent 3175a0f commit 837bc3c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions pkg/autodiscover/autodiscover.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type DiscoveredTestData struct {
RoleBindings []rbacv1.RoleBinding // Contains all rolebindings from all namespaces
Roles []rbacv1.Role // Contains all roles from all namespaces
Services []*corev1.Service
AllServices []*corev1.Service
ServiceAccounts []*corev1.ServiceAccount
AllServiceAccounts []*corev1.ServiceAccount
Hpas []*scalingv1.HorizontalPodAutoscaler
Expand Down Expand Up @@ -279,6 +280,10 @@ func DoAutoDiscover(config *configuration.TestConfiguration) DiscoveredTestData
if err != nil {
log.Fatal("Cannot get list of services, err: %v", err)
}
data.AllServices, err = getServices(oc.K8sClient.CoreV1(), data.AllNamespaces, data.ServicesIgnoreList)
if err != nil {
log.Fatal("Cannot get list of all services, err: %v", err)
}
data.ServiceAccounts, err = getServiceAccounts(oc.K8sClient.CoreV1(), data.Namespaces)
if err != nil {
log.Fatal("Cannot get list of service accounts under test, err: %v", err)
Expand Down
13 changes: 11 additions & 2 deletions pkg/provider/catalogsources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"strconv"
"strings"

"github.com/Masterminds/semver"
olmv1Alpha "github.com/operator-framework/api/pkg/operators/v1alpha1"
Expand Down Expand Up @@ -48,12 +49,14 @@ func getCatalogSourceBundleCountFromProbeContainer(env *TestEnvironment, cs *olm
o := clientsholder.GetClientsHolder()

// Find the kubernetes service associated with the catalog source
for _, svc := range env.Services {
for _, svc := range env.AllServices {
// Skip if the service is not associated with the catalog source
if svc.Spec.Selector["olm.catalogSource"] != cs.Name {
continue
}

log.Info("Found service %q associated with catalog source %q.", svc.Name, cs.Name)

// Use a probe pod to get the bundle count
for _, probePod := range env.ProbePods {
ctx := clientsholder.NewContext(probePod.Namespace, probePod.Name, probePod.Spec.Containers[0].Name)
Expand All @@ -64,6 +67,10 @@ func getCatalogSourceBundleCountFromProbeContainer(env *TestEnvironment, cs *olm
continue
}

// Sanitize the command output
cmdValue = strings.TrimSpace(cmdValue)
cmdValue = strings.Trim(cmdValue, "\"")

// Parse the command output
bundleCount, err := strconv.Atoi(cmdValue)
if err != nil {
Expand All @@ -72,11 +79,13 @@ func getCatalogSourceBundleCountFromProbeContainer(env *TestEnvironment, cs *olm
}

// Try each probe pod until we get a valid bundle count (which should only be 1 probe pod)
log.Info("Found bundle count via grpcurl %d for catalog source %q.", bundleCount, cs.Name)
return bundleCount
}
}

return 0
log.Warn("Warning: No services found associated with catalog source %q.", cs.Name)
return -1
}

func getCatalogSourceBundleCountFromPackageManifests(env *TestEnvironment, cs *olmv1Alpha.CatalogSource) int {
Expand Down
2 changes: 2 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type TestEnvironment struct { // rename this with testTarget

HorizontalScaler []*scalingv1.HorizontalPodAutoscaler `json:"testHorizontalScaler"`
Services []*corev1.Service `json:"testServices"`
AllServices []*corev1.Service `json:"testAllServices"`
ServiceAccounts []*corev1.ServiceAccount `json:"testServiceAccounts"`
AllServiceAccounts []*corev1.ServiceAccount `json:"AllServiceAccounts"`
AllServiceAccountsMap map[string]*corev1.ServiceAccount
Expand Down Expand Up @@ -335,6 +336,7 @@ func buildTestEnvironment() { //nolint:funlen,gocyclo
env.RoleBindings = data.RoleBindings
env.Roles = data.Roles
env.Services = data.Services
env.AllServices = data.AllServices
env.NetworkPolicies = data.NetworkPolicies
for _, nsHelmChartReleases := range data.HelmChartReleases {
for _, helmChartRelease := range nsHelmChartReleases {
Expand Down
15 changes: 10 additions & 5 deletions tests/operator/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,17 @@ func testOperatorCatalogSourceBundleCount(check *checksdb.Check, env *provider.T
// The name and namespace match. Lookup the bundle count.
bundleCount := provider.GetCatalogSourceBundleCount(env, catalogSource)

if bundleCount > bundleCountLimit {
check.LogError("CatalogSource %q has more than "+bundleCountLimitStr+" ("+strconv.Itoa(bundleCount)+") referenced images", catalogSource.Name)
nonCompliantObjects = append(nonCompliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "CatalogSource has more than "+bundleCountLimitStr+" referenced images", false))
if bundleCount == -1 {
check.LogError("Failed to get bundle count for CatalogSource %q", catalogSource.Name)
nonCompliantObjects = append(nonCompliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "Failed to get bundle count", false))
} else {
check.LogInfo("CatalogSource %q has less than "+bundleCountLimitStr+" ("+strconv.Itoa(bundleCount)+") referenced images", catalogSource.Name)
compliantObjects = append(compliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "CatalogSource has less than "+bundleCountLimitStr+" referenced images", true))
if bundleCount > bundleCountLimit {
check.LogError("CatalogSource %q has more than "+bundleCountLimitStr+" ("+strconv.Itoa(bundleCount)+") referenced images", catalogSource.Name)
nonCompliantObjects = append(nonCompliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "CatalogSource has more than "+bundleCountLimitStr+" referenced images", false))
} else {
check.LogInfo("CatalogSource %q has less than "+bundleCountLimitStr+" ("+strconv.Itoa(bundleCount)+") referenced images", catalogSource.Name)
compliantObjects = append(compliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "CatalogSource has less than "+bundleCountLimitStr+" referenced images", true))
}
}

log.Debug("Adding catalog source %q to list of already reported", catalogSource.Name)
Expand Down

0 comments on commit 837bc3c

Please sign in to comment.