Skip to content

Commit

Permalink
Migrate packagemanifest code to eco-goinfra (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 authored Jan 24, 2025
1 parent 30cfb3f commit 1e0d69a
Show file tree
Hide file tree
Showing 40 changed files with 8,439 additions and 86 deletions.
104 changes: 18 additions & 86 deletions tests/globalhelper/packagemanifest.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,39 @@
package globalhelper

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
egiClients "github.com/openshift-kni/eco-goinfra/pkg/clients"
egiOLM "github.com/openshift-kni/eco-goinfra/pkg/olm"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// QueryPackageManifest queries the package manifest for the operator.
//
//nolint:gocognit
func QueryPackageManifestForVersion(operatorName, operatorNamespace, channel string) (string, error) {
gvr := schema.GroupVersionResource{
Group: "packages.operators.coreos.com",
Version: "v1",
Resource: "packagemanifests",
}

// Query the package manifest for the operator
pkgManifest, err := GetAPIClient().DynamicClient.Resource(gvr).Namespace("").List(context.TODO(), metav1.ListOptions{})
pkgManifest, err := egiOLM.ListPackageManifest(egiClients.New(""), operatorNamespace, client.ListOptions{})

if err != nil {
return "", err
}

for _, item := range pkgManifest.Items {
if item.GetName() == operatorName {
// check if defaultChannel exists
if _, ok := item.Object["status"].(map[string]interface{})["defaultChannel"]; !ok {
continue
}

// type assertion
if _, ok := item.Object["status"].(map[string]interface{})["defaultChannel"].(string); !ok {
continue
}

//nolint:forcetypeassert
fmt.Printf("Comparing %s with %s\n", item.Object["status"].(map[string]interface{})["defaultChannel"].(string), channel)
for _, item := range pkgManifest {
if item.Object.GetName() == operatorName {
fmt.Printf("Comparing %s with %s\n", item.Object.Status.DefaultChannel, channel)

//nolint:forcetypeassert
if item.Object["status"].(map[string]interface{})["defaultChannel"].(string) != channel {
// skip if the default channel is not the one we are looking for
if item.Object.Status.DefaultChannel != channel {
continue
}

channelsObj, _, err := unstructured.NestedSlice(item.Object, "status", "channels")
for _, chanObj := range item.Object.Status.Channels {
fmt.Printf("Comparing name %s with channel %s\n", chanObj.Name, channel)

if err != nil {
return "", err
}

for _, chanObj := range channelsObj {
// verify the name of the channel is the same as the one we are looking for
if _, ok := chanObj.(map[string]interface{})["name"]; !ok {
continue
}

// type assertion
if _, ok := chanObj.(map[string]interface{})["name"].(string); !ok {
// skip if the name of the channel is not the one we are looking for
if chanObj.Name != channel {
continue
}

//nolint:forcetypeassert
fmt.Printf("Comparing name %s with channel %s\n", chanObj.(map[string]interface{})["name"].(string), channel)

// skip this channel because it does not match the one we are looking for
//nolint:forcetypeassert
if chanObj.(map[string]interface{})["name"].(string) != channel {
continue
}

// check if version exists
if _, ok := chanObj.(map[string]interface{})["currentCSVDesc"].(map[string]interface{})["version"]; !ok {
continue
}

// type assertion
if _, ok := chanObj.(map[string]interface{})["currentCSVDesc"].(map[string]interface{})["version"].(string); !ok {
continue
}

//nolint:forcetypeassert
return chanObj.(map[string]interface{})["currentCSVDesc"].(map[string]interface{})["version"].(string), nil
return chanObj.CurrentCSVDesc.Version.String(), nil
}
}
}
Expand All @@ -92,14 +42,7 @@ func QueryPackageManifestForVersion(operatorName, operatorNamespace, channel str
}

func QueryPackageManifestForDefaultChannel(operatorName, operatorNamespace string) (string, error) {
gvr := schema.GroupVersionResource{
Group: "packages.operators.coreos.com",
Version: "v1",
Resource: "packagemanifests",
}

// Query the package manifest for the operator
pkgManifest, err := GetAPIClient().DynamicClient.Resource(gvr).Namespace("").List(context.TODO(), metav1.ListOptions{})
pkgManifest, err := egiOLM.ListPackageManifest(egiClients.New(""), operatorNamespace, client.ListOptions{})

if err != nil {
return "", err
Expand All @@ -108,20 +51,9 @@ func QueryPackageManifestForDefaultChannel(operatorName, operatorNamespace strin
// Example of how to get the default channel
// oc get packagemanifest cluster-logging -o json | jq .status.defaultChannel

for _, item := range pkgManifest.Items {
if item.GetName() == operatorName {
// check if defaultChannel exists
if _, ok := item.Object["status"].(map[string]interface{})["defaultChannel"]; !ok {
continue
}

// type assertion
if _, ok := item.Object["status"].(map[string]interface{})["defaultChannel"].(string); !ok {
continue
}

//nolint:forcetypeassert
return item.Object["status"].(map[string]interface{})["defaultChannel"].(string), nil
for _, item := range pkgManifest {
if item.Object.GetName() == operatorName {
return item.Object.Status.DefaultChannel, nil
}
}

Expand Down
Loading

0 comments on commit 1e0d69a

Please sign in to comment.