-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathretriever.go
111 lines (93 loc) · 3.13 KB
/
retriever.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
*
* Copyright © 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package retriever
import (
"errors"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
log "github.com/sirupsen/logrus"
)
// MetadataRetrieverClient is the interface for retrieving metadata.
type MetadataRetrieverClient interface {
GetPVCLabels(context.Context, *GetPVCLabelsRequest) (*GetPVCLabelsResponse, error)
}
// GetPVCLabelsRequest defines API request type
type GetPVCLabelsRequest struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
NameSpace string `protobuf:"bytes,1,opt,name=namespace,proto3" json:"namespace,omitempty"`
}
// GetPVCLabelsResponse defines API response type
type GetPVCLabelsResponse struct {
Parameters map[string]string `protobuf:"bytes,4,rep,name=parameters,proto3" json:"parameters,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
}
// MetadataRetrieverClientType holds client connection and timeout
type MetadataRetrieverClientType struct {
conn *grpc.ClientConn
timeout time.Duration
}
// NewMetadataRetrieverClient returns csiclient
func NewMetadataRetrieverClient(conn *grpc.ClientConn, timeout time.Duration) *MetadataRetrieverClientType {
return &MetadataRetrieverClientType{
conn: conn,
timeout: timeout,
}
}
// GetPVCLabels gets the PVC labels and returns it
func (s *MetadataRetrieverClientType) GetPVCLabels(
ctx context.Context,
req *GetPVCLabelsRequest) (
*GetPVCLabelsResponse, error,
) {
log.Infof("Get PVC labels for %s in namespace %s", req.Name, req.NameSpace)
if req.Name == "" {
return nil, errors.New(
"PVC Name cannot be empty")
}
// TODO: config and clientset to be moved to BeforeServe()
config, err := rest.InClusterConfig()
if err != nil {
log.Error("Error getting cluster config: ", err)
return nil, err
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Error("Error creating clientset: ", err)
return nil, err
}
pvcClient := clientset.CoreV1().PersistentVolumeClaims(req.NameSpace)
if pvcClient == nil {
log.Error("Error getting PVC client: ", err)
return nil, err
}
pvc, err := pvcClient.Get(ctx, req.Name, metav1.GetOptions{})
if err != nil {
log.Error("Error retrieving PVC info: ", err)
return nil, err
}
parameters := make(map[string]string)
for k, v := range pvc.Labels {
parameters[k] = v
}
resp := &GetPVCLabelsResponse{
Parameters: parameters,
}
return resp, err
}