Skip to content

Commit 612305a

Browse files
authored
Get subscription channel from package manifest if it is not present (#2551)
* Get subscription channel from package manifest if it is not present * Addressing Gonzalo's comments, use official Package manifest object
1 parent ac7b2ea commit 612305a

File tree

9 files changed

+140
-31
lines changed

9 files changed

+140
-31
lines changed

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ require (
9595
github.com/gosuri/uitable v0.0.4 // indirect
9696
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
9797
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0 // indirect
98+
github.com/h2non/filetype v1.1.3 // indirect
99+
github.com/h2non/go-is-svg v0.0.0-20160927212452-35e8c4b0612c // indirect
98100
github.com/hashicorp/errwrap v1.1.0 // indirect
99101
github.com/hashicorp/go-multierror v1.1.1 // indirect
100102
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -128,9 +130,11 @@ require (
128130
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
129131
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
130132
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
133+
github.com/onsi/gomega v1.34.2 // indirect
131134
github.com/opencontainers/go-digest v1.0.0 // indirect
132135
github.com/opencontainers/image-spec v1.1.0 // indirect
133136
github.com/operator-framework/operator-manifest-tools v0.7.0 // indirect
137+
github.com/operator-framework/operator-registry v1.47.0 // indirect
134138
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
135139
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
136140
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
@@ -222,7 +226,7 @@ require (
222226
github.com/gorilla/websocket v1.5.3
223227
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.5
224228
github.com/manifoldco/promptui v0.9.0
225-
github.com/redhat-best-practices-for-k8s/oct v0.0.26
229+
github.com/redhat-best-practices-for-k8s/oct v0.0.27
226230
github.com/redhat-best-practices-for-k8s/privileged-daemonset v1.0.39
227231
github.com/redhat-openshift-ecosystem/openshift-preflight v0.0.0-20241021175030-e64988a27024
228232
github.com/robert-nix/ansihtml v1.0.1

go.sum

Lines changed: 75 additions & 23 deletions
Large diffs are not rendered by default.

internal/clientsholder/clientsholder.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
cncfNetworkAttachmentv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1"
3939
apiserverscheme "github.com/openshift/client-go/apiserver/clientset/versioned"
4040
ocpMachine "github.com/openshift/client-go/machineconfiguration/clientset/versioned"
41+
olmpkgclient "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/client/clientset/versioned/typed/operators/v1"
4142
appsv1 "k8s.io/api/apps/v1"
4243
scalingv1 "k8s.io/api/autoscaling/v1"
4344
corev1 "k8s.io/api/core/v1"
@@ -63,6 +64,7 @@ type ClientsHolder struct {
6364
ScalingClient scale.ScalesGetter
6465
APIExtClient apiextv1.Interface
6566
OlmClient olmClient.Interface
67+
OlmPkgClient olmpkgclient.OperatorsV1Interface
6668
OcpClient clientconfigv1.ConfigV1Interface
6769
K8sClient kubernetes.Interface
6870
K8sNetworkingClient networkingv1.NetworkingV1Interface
@@ -287,6 +289,10 @@ func newClientsHolder(filenames ...string) (*ClientsHolder, error) { //nolint:fu
287289
if err != nil {
288290
return nil, fmt.Errorf("cannot instantiate olm clientset: %s", err)
289291
}
292+
clientsHolder.OlmPkgClient, err = olmpkgclient.NewForConfig(clientsHolder.RestConfig)
293+
if err != nil {
294+
return nil, fmt.Errorf("cannot instantiate olm clientset: %s", err)
295+
}
290296
clientsHolder.K8sClient, err = kubernetes.NewForConfig(clientsHolder.RestConfig)
291297
if err != nil {
292298
return nil, fmt.Errorf("cannot instantiate k8sclient: %s", err)

pkg/autodiscover/autodiscover.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
configv1 "github.com/openshift/api/config/v1"
2828
clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
2929
olmv1Alpha "github.com/operator-framework/api/pkg/operators/v1alpha1"
30+
olmPkgv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
3031
"github.com/redhat-best-practices-for-k8s/certsuite/internal/clientsholder"
3132
"github.com/redhat-best-practices-for-k8s/certsuite/internal/log"
3233
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/compatibility"
@@ -73,6 +74,7 @@ type DiscoveredTestData struct {
7374
AllCsvs []*olmv1Alpha.ClusterServiceVersion
7475
AllInstallPlans []*olmv1Alpha.InstallPlan
7576
AllCatalogSources []*olmv1Alpha.CatalogSource
77+
AllPackageManifests []*olmPkgv1.PackageManifest
7678
Deployments []appsv1.Deployment
7779
StatefulSet []appsv1.StatefulSet
7880
PersistentVolumes []corev1.PersistentVolume
@@ -158,6 +160,7 @@ func DoAutoDiscover(config *configuration.TestConfiguration) DiscoveredTestData
158160
}
159161
data.AllInstallPlans = getAllInstallPlans(oc.OlmClient)
160162
data.AllCatalogSources = getAllCatalogSources(oc.OlmClient)
163+
data.AllPackageManifests = getAllPackageManifests(oc.OlmPkgClient)
161164
data.Namespaces = namespacesListToStringList(config.TargetNameSpaces)
162165
data.Pods, data.AllPods = findPodsByLabels(oc.K8sClient.CoreV1(), podsUnderTestLabelsObjects, data.Namespaces)
163166
data.AbnormalEvents = findAbnormalEvents(oc.K8sClient.CoreV1(), data.Namespaces)

pkg/autodiscover/autodiscover_operators.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import (
2525
clientOlm "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
2626
"github.com/redhat-best-practices-for-k8s/certsuite/internal/log"
2727
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/configuration"
28+
29+
olmpkgv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
30+
olmpkgclient "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/client/clientset/versioned/typed/operators/v1"
2831
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/stringhelper"
2932
"helm.sh/helm/v3/pkg/release"
3033
"k8s.io/apimachinery/pkg/api/errors"
@@ -202,3 +205,16 @@ func getAllCatalogSources(olmClient clientOlm.Interface) (out []*olmv1Alpha.Cata
202205
}
203206
return out
204207
}
208+
209+
// getAllPackageManifests is a helper function to get the all the PackageManifests in a cluster.
210+
func getAllPackageManifests(olmPkgClient olmpkgclient.OperatorsV1Interface) (out []*olmpkgv1.PackageManifest) {
211+
packageManifestsList, err := olmPkgClient.PackageManifests("").List(context.TODO(), metav1.ListOptions{})
212+
if err != nil {
213+
log.Error("Unable get Package Manifests in cluster, err: %v", err)
214+
return out
215+
}
216+
for index := range packageManifestsList.Items {
217+
out = append(out, &packageManifestsList.Items[index])
218+
}
219+
return out
220+
}

pkg/provider/operators.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/go-logr/stdr"
3131
olmv1 "github.com/operator-framework/api/pkg/operators/v1"
3232
olmv1Alpha "github.com/operator-framework/api/pkg/operators/v1alpha1"
33+
olmpkgv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
3334
"github.com/redhat-best-practices-for-k8s/certsuite/internal/clientsholder"
3435
"github.com/redhat-best-practices-for-k8s/certsuite/internal/log"
3536
"github.com/redhat-openshift-ecosystem/openshift-preflight/artifacts"
@@ -150,6 +151,7 @@ func getUniqueCsvListByName(csvs []*olmv1Alpha.ClusterServiceVersion) []*olmv1Al
150151

151152
func createOperators(csvs []*olmv1Alpha.ClusterServiceVersion,
152153
allSubscriptions []olmv1Alpha.Subscription,
154+
allPackageManifests []*olmpkgv1.PackageManifest,
153155
allInstallPlans []*olmv1Alpha.InstallPlan,
154156
allCatalogSources []*olmv1Alpha.CatalogSource,
155157
succeededRequired,
@@ -181,7 +183,7 @@ func createOperators(csvs []*olmv1Alpha.ClusterServiceVersion,
181183
op.PackageFromCsvName = packageAndVersion[0]
182184
op.Version = csv.Spec.Version.String()
183185
// Get at least one subscription and update the Operator object with it.
184-
if getAtLeastOneSubscription(op, csv, allSubscriptions) {
186+
if getAtLeastOneSubscription(op, csv, allSubscriptions, allPackageManifests) {
185187
targetNamespaces, err := getOperatorTargetNamespaces(op.SubscriptionNamespace)
186188
if err != nil {
187189
log.Error("Failed to get target namespaces for operator %s: %v", csv.Name, err)
@@ -200,7 +202,7 @@ func createOperators(csvs []*olmv1Alpha.ClusterServiceVersion,
200202
return operators
201203
}
202204

203-
func getAtLeastOneSubscription(op *Operator, csv *olmv1Alpha.ClusterServiceVersion, subscriptions []olmv1Alpha.Subscription) (atLeastOneSubscription bool) {
205+
func getAtLeastOneSubscription(op *Operator, csv *olmv1Alpha.ClusterServiceVersion, subscriptions []olmv1Alpha.Subscription, packageManifests []*olmpkgv1.PackageManifest) (atLeastOneSubscription bool) {
204206
atLeastOneSubscription = false
205207
for s := range subscriptions {
206208
subscription := &subscriptions[s]
@@ -214,11 +216,32 @@ func getAtLeastOneSubscription(op *Operator, csv *olmv1Alpha.ClusterServiceVersi
214216
op.Org = subscription.Spec.CatalogSource
215217
op.Channel = subscription.Spec.Channel
216218
atLeastOneSubscription = true
219+
220+
// If the channel is not present in the subscription, get the default channel from the package manifest
221+
if op.Channel == "" {
222+
aPackageManifest := getPackageManifestWithSubscription(subscription, packageManifests)
223+
if aPackageManifest != nil {
224+
op.Channel = aPackageManifest.Status.DefaultChannel
225+
} else {
226+
log.Error("Could not determine the default channel, this operator will always fail certification")
227+
}
228+
}
217229
break
218230
}
219231
return atLeastOneSubscription
220232
}
221233

234+
func getPackageManifestWithSubscription(subscription *olmv1Alpha.Subscription, packageManifests []*olmpkgv1.PackageManifest) *olmpkgv1.PackageManifest {
235+
for index := range packageManifests {
236+
if packageManifests[index].Status.PackageName == subscription.Spec.Package &&
237+
packageManifests[index].Namespace == subscription.Spec.CatalogSourceNamespace &&
238+
packageManifests[index].Status.CatalogSource == subscription.Spec.CatalogSource {
239+
return packageManifests[index]
240+
}
241+
}
242+
return nil
243+
}
244+
222245
func getAtLeastOneCsv(csv *olmv1Alpha.ClusterServiceVersion, installPlan *olmv1Alpha.InstallPlan) (atLeastOneCsv bool) {
223246
atLeastOneCsv = false
224247
for _, csvName := range installPlan.Spec.ClusterServiceVersionNames {

pkg/provider/operators_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/operator-framework/api/pkg/lib/version"
2424
olmv1 "github.com/operator-framework/api/pkg/operators/v1"
2525
olmv1Alpha "github.com/operator-framework/api/pkg/operators/v1alpha1"
26+
olmpkgv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
2627
"github.com/redhat-best-practices-for-k8s/certsuite/internal/clientsholder"
2728
"github.com/stretchr/testify/assert"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -296,7 +297,8 @@ func TestCreateOperators(t *testing.T) {
296297
_ = clientsholder.GetTestClientsHolder(nil)
297298
clientsholder.SetupFakeOlmClient(runtimeObjects)
298299

299-
ops := createOperators(tc.csvs, tc.subscriptions, tc.installPlan, tc.catalogSource, false, true)
300+
emptyManifests := []*olmpkgv1.PackageManifest{}
301+
ops := createOperators(tc.csvs, tc.subscriptions, emptyManifests, tc.installPlan, tc.catalogSource, false, true)
300302
assert.Equal(t, tc.expectedOperators, ops)
301303
}
302304
}

pkg/provider/provider.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
mcv1 "github.com/openshift/api/machineconfiguration/v1"
3030
olmv1 "github.com/operator-framework/api/pkg/operators/v1"
3131
olmv1Alpha "github.com/operator-framework/api/pkg/operators/v1alpha1"
32+
olmpkgv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
3233
"github.com/redhat-best-practices-for-k8s/certsuite/internal/clientsholder"
3334
"github.com/redhat-best-practices-for-k8s/certsuite/internal/log"
3435
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/autodiscover"
@@ -114,7 +115,8 @@ type TestEnvironment struct { // rename this with testTarget
114115
NetworkPolicies []networkingv1.NetworkPolicy
115116
AllInstallPlans []*olmv1Alpha.InstallPlan `json:"AllInstallPlans"`
116117
AllSubscriptions []olmv1Alpha.Subscription `json:"AllSubscriptions"`
117-
AllCatalogSources []*olmv1Alpha.CatalogSource `json:"-"`
118+
AllCatalogSources []*olmv1Alpha.CatalogSource `json:"AllCatalogSources"`
119+
AllPackageManifests []*olmpkgv1.PackageManifest `json:"AllPackageManifests"`
118120
OperatorGroups []*olmv1.OperatorGroup `json:"OperatorGroups"`
119121
IstioServiceMeshFound bool
120122
ValidProtocolNames []string
@@ -240,7 +242,8 @@ func buildTestEnvironment() { //nolint:funlen
240242
}
241243
env.AllSubscriptions = data.AllSubscriptions
242244
env.AllCatalogSources = data.AllCatalogSources
243-
env.AllOperators = createOperators(data.AllCsvs, data.AllSubscriptions, data.AllInstallPlans, data.AllCatalogSources, false, true)
245+
env.AllPackageManifests = data.AllPackageManifests
246+
env.AllOperators = createOperators(data.AllCsvs, data.AllSubscriptions, data.AllPackageManifests, data.AllInstallPlans, data.AllCatalogSources, false, true)
244247
env.AllOperatorsSummary = getSummaryAllOperators(env.AllOperators)
245248
env.AllCrds = data.AllCrds
246249
env.Namespaces = data.Namespaces
@@ -344,7 +347,7 @@ func buildTestEnvironment() { //nolint:funlen
344347
env.CollectorAppPassword = data.CollectorAppPassword
345348
env.CollectorAppEndpoint = data.CollectorAppEndpoint
346349

347-
operators := createOperators(data.Csvs, data.AllSubscriptions,
350+
operators := createOperators(data.Csvs, data.AllSubscriptions, data.AllPackageManifests,
348351
data.AllInstallPlans, data.AllCatalogSources, false, true)
349352
env.Operators = operators
350353
log.Info("Operators found: %d", len(env.Operators))

tests/certification/suite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func testAllOperatorCertified(check *checksdb.Check, env *provider.TestEnvironme
134134
}
135135
for _, operator := range operatorsUnderTest {
136136
check.LogInfo("Testing Operator %q", operator)
137-
isCertified := validator.IsOperatorCertified(operator.Name, ocpMinorVersion, operator.Channel)
137+
isCertified := validator.IsOperatorCertified(operator.Name, ocpMinorVersion)
138138
if !isCertified {
139139
check.LogError("Operator %q (channel %q) failed to be certified for OpenShift %s", operator.Name, operator.Channel, ocpMinorVersion)
140140
nonCompliantObjects = append(nonCompliantObjects, testhelper.NewOperatorReportObject(operator.Namespace, operator.Name, "Operator failed to be certified for OpenShift", false).

0 commit comments

Comments
 (0)