1 package kclient
2
3 import (
4 "fmt"
5 "strings"
6
7 "github.com/redhat-developer/odo/pkg/api"
8
9 kerrors "k8s.io/apimachinery/pkg/api/errors"
10 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11 "k8s.io/apimachinery/pkg/runtime/schema"
12 "k8s.io/klog"
13 )
14
15 func (c *Client) GetRegistryList() ([]api.Registry, error) {
16 namespacedList, err := c.ListDynamicResources("", schema.GroupVersionResource{
17 Group: "registry.devfile.io",
18 Version: "v1alpha1",
19 Resource: "devfileregistrieslists",
20 }, "")
21 if err != nil {
22 if !kerrors.IsForbidden(err) && !kerrors.IsUnauthorized(err) {
23 return nil, err
24 } else {
25 klog.V(4).Infof("accessing %q is forbidden or unauthorized", "devfileregistrieslists")
26 }
27 }
28
29 clusterList, err := c.ListClusterWideDynamicResources(schema.GroupVersionResource{
30 Group: "registry.devfile.io",
31 Version: "v1alpha1",
32 Resource: "clusterdevfileregistrieslists",
33 }, "")
34 if err != nil {
35 if !kerrors.IsForbidden(err) && !kerrors.IsUnauthorized(err) {
36 return nil, err
37 } else {
38 klog.V(4).Infof("accessing %q is forbidden or unauthorized", "clusterdevfileregistrieslists")
39 }
40 }
41
42 result, err := addDevfileRegistries(namespacedList)
43 if err != nil {
44 return nil, err
45 }
46
47 clusterResult, err := addDevfileRegistries(clusterList)
48 if err != nil {
49 return nil, err
50 }
51
52 return append(result, clusterResult...), nil
53 }
54
55 func addDevfileRegistries(list *unstructured.UnstructuredList) ([]api.Registry, error) {
56 if list == nil {
57 return nil, nil
58 }
59
60 result := []api.Registry{}
61 for _, item := range list.Items {
62 vals, found, err := unstructured.NestedSlice(item.Object, "spec", "devfileRegistries")
63 if err != nil {
64 return nil, err
65 }
66 if !found {
67 continue
68 }
69 for _, val := range vals {
70 castedVal, ok := val.(map[string]interface{})
71 if !ok {
72 return nil, fmt.Errorf("unable to read resource %s/%s", item.GetKind(), item.GetName())
73 }
74 name, ok := castedVal["name"].(string)
75 if !ok {
76 return nil, fmt.Errorf("unable to read name in resource %s/%s", item.GetKind(), item.GetName())
77 }
78 url, ok := castedVal["url"].(string)
79 if !ok {
80 return nil, fmt.Errorf("unable to read url in resource %s/%s", item.GetKind(), item.GetName())
81 }
82
83 secure := true
84 skipTLSVerify, found := castedVal["skipTLSVerify"]
85 if found {
86 castedSkipTLSVerify, ok := skipTLSVerify.(bool)
87 if !ok {
88 return nil, fmt.Errorf("unable to read skipTLSVerify in resource %s/%s", item.GetKind(), item.GetName())
89 }
90 secure = !castedSkipTLSVerify
91 }
92
93 if strings.TrimSpace(url) == "" {
94 klog.V(4).Infof("ignored registry %q because url is blank", name)
95 continue
96 }
97
98 result = append(result, api.Registry{
99 Name: name,
100 URL: url,
101 Secure: secure,
102 })
103 }
104 }
105 return result, nil
106 }
107
View as plain text