-
Notifications
You must be signed in to change notification settings - Fork 13
/
packagemanifest.go
52 lines (41 loc) · 1.42 KB
/
packagemanifest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package globalhelper
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// QueryPackageManifest queries the package manifest for the operator.
func QueryPackageManifestForVersion(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{})
if err != nil {
return "", err
}
for _, item := range pkgManifest.Items {
if item.GetName() == operatorName {
channelsObj, _, err := unstructured.NestedSlice(item.Object, "status", "channels")
if err != nil {
return "", err
}
for _, channel := range channelsObj {
// check if version exists
if _, ok := channel.(map[string]interface{})["currentCSVDesc"].(map[string]interface{})["version"]; !ok {
continue
}
// type assertion
if _, ok := channel.(map[string]interface{})["currentCSVDesc"].(map[string]interface{})["version"].(string); !ok {
continue
}
//nolint:forcetypeassert
return channel.(map[string]interface{})["currentCSVDesc"].(map[string]interface{})["version"].(string), nil
}
}
}
return "not found", nil
}