2 * Copyright 2018 Intel Corporation, Inc
3 * Copyright © 2021 Samsung Electronics
4 * Copyright © 2021 Orange
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
23 "k8s.io/apimachinery/pkg/runtime/schema"
25 pkgerrors "github.com/pkg/errors"
28 // QueryStatus is what is returned when status is queried for an instance
29 type QueryStatus struct {
30 ResourceCount int32 `json:"resourceCount"`
31 ResourcesStatus []ResourceStatus `json:"resourcesStatus"`
34 // QueryManager is an interface exposes the instantiation functionality
35 type QueryManager interface {
36 Query(namespace, cloudRegion, apiVersion, kind, name, labels, id string) (QueryStatus, error)
39 // QueryClient implements the InstanceManager interface
40 // It will also be used to maintain some localized state
41 type QueryClient struct {
46 // NewQueryClient returns an instance of the QueryClient
47 // which implements the InstanceManager
48 func NewQueryClient() *QueryClient {
55 // Query returns state of instance's filtered resources
56 func (v *QueryClient) Query(namespace, cloudRegion, apiVersion, kind, name, labels, id string) (QueryStatus, error) {
58 //Read the status from the DD
60 k8sClient := KubernetesClient{}
61 err := k8sClient.Init(cloudRegion, id)
63 return QueryStatus{}, pkgerrors.Wrap(err, "Getting CloudRegion Information")
66 var resourcesStatus []ResourceStatus
68 resList, err := k8sClient.queryResources(apiVersion, kind, labels, namespace)
70 return QueryStatus{}, pkgerrors.Wrap(err, "Querying Resources")
72 // If user specifies both label and name, we want to pick up only single resource from these matching label
74 //Assigning 0-length, because we may actually not find matching name
75 resourcesStatus = make([]ResourceStatus, 0)
76 for _, res := range resList {
78 resourcesStatus = append(resourcesStatus, res)
83 resourcesStatus = resList
85 } else if name != "" {
86 resIdentifier := helm.KubernetesResource{
88 GVK: schema.FromAPIVersionAndKind(apiVersion, kind),
90 res, err := k8sClient.GetResourceStatus(resIdentifier, namespace)
92 return QueryStatus{}, pkgerrors.Wrap(err, "Querying Resource")
94 resourcesStatus = []ResourceStatus{res}
96 resList, err := k8sClient.queryResources(apiVersion, kind, labels, namespace)
98 return QueryStatus{}, pkgerrors.Wrap(err, "Querying Resources")
100 resourcesStatus = resList
104 ResourceCount: int32(len(resourcesStatus)),
105 ResourcesStatus: resourcesStatus,