diff options
author | 2021-02-25 14:17:19 +0000 | |
---|---|---|
committer | 2021-02-25 14:17:19 +0000 | |
commit | 965da8328566d58b2b38373f76aa1ab79c0b314e (patch) | |
tree | 21d98c7a7d73b9f8db7fb0b78f779601f71b4f17 /src/k8splugin/internal/app/client.go | |
parent | 2f09583725c8481ea3d7505ed2394c180af5ef03 (diff) | |
parent | 74dfd71d3628c52e63f66c079244638c675b2b9c (diff) |
Merge "Provide Query API for CNF Instances"
Diffstat (limited to 'src/k8splugin/internal/app/client.go')
-rw-r--r-- | src/k8splugin/internal/app/client.go | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/k8splugin/internal/app/client.go b/src/k8splugin/internal/app/client.go index 6762d1bc..f0edf8c9 100644 --- a/src/k8splugin/internal/app/client.go +++ b/src/k8splugin/internal/app/client.go @@ -1,6 +1,6 @@ /* Copyright 2018 Intel Corporation. -Copyright © 2020 Samsung Electronics +Copyright © 2021 Samsung Electronics Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -90,6 +90,39 @@ func (k *KubernetesClient) getPodsByLabel(namespace string) ([]ResourceStatus, e return resp, nil } +func (k *KubernetesClient) queryResources(apiVersion, kind, labelSelector, namespace string) ([]ResourceStatus, error) { + dynClient := k.GetDynamicClient() + mapper := k.GetMapper() + gvk := schema.FromAPIVersionAndKind(apiVersion, kind) + mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version) + if err != nil { + return nil, pkgerrors.Wrap(err, "Preparing mapper based on GVK") + } + + gvr := mapping.Resource + opts := metav1.ListOptions{ + LabelSelector: labelSelector, + } + var unstrList *unstructured.UnstructuredList + switch mapping.Scope.Name() { + case meta.RESTScopeNameNamespace: + unstrList, err = dynClient.Resource(gvr).Namespace(namespace).List(opts) + case meta.RESTScopeNameRoot: + unstrList, err = dynClient.Resource(gvr).List(opts) + default: + return nil, pkgerrors.New("Got an unknown RESTScopeName for mapping: " + gvk.String()) + } + if err != nil { + return nil, pkgerrors.Wrap(err, "Querying for resources") + } + + resp := make([]ResourceStatus, len(unstrList.Items)) + for _, unstr := range unstrList.Items { + resp = append(resp, ResourceStatus{unstr.GetName(), gvk, unstr}) + } + return resp, nil +} + // getResourcesStatus yields status of given generic resource func (k *KubernetesClient) getResourceStatus(res helm.KubernetesResource, namespace string) (ResourceStatus, error) { dynClient := k.GetDynamicClient() |