-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
lease.go
118 lines (98 loc) · 3.61 KB
/
lease.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
112
113
114
115
116
117
118
/*
Copyright 2022 The KubeEdge Authors.
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 client
import (
"encoding/json"
"fmt"
"reflect"
coordinationv1 "k8s.io/api/coordination/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/kubeedge/beehive/pkg/core/model"
"github.com/kubeedge/kubeedge/edge/pkg/common/message"
"github.com/kubeedge/kubeedge/edge/pkg/common/modules"
)
// LeasesGetter to get lease interface
type LeasesGetter interface {
Leases(namespace string) LeasesInterface
}
// LeasesInterface is interface for client leases
type LeasesInterface interface {
Create(lease *coordinationv1.Lease) (*coordinationv1.Lease, error)
Update(lease *coordinationv1.Lease) (*coordinationv1.Lease, error)
Get(name string) (*coordinationv1.Lease, error)
}
type leases struct {
namespace string
send SendInterface
}
func newLeases(namespace string, s SendInterface) *leases {
return &leases{
send: s,
namespace: namespace,
}
}
// LeaseResp represents lease response from the api-server
type LeaseResp struct {
Object *coordinationv1.Lease
Err apierrors.StatusError
}
func (c *leases) Create(lease *coordinationv1.Lease) (*coordinationv1.Lease, error) {
resource := fmt.Sprintf("%s/%s/%s", c.namespace, model.ResourceTypeLease, lease.Name)
leaseMsg := message.BuildMsg(modules.MetaGroup, "", modules.EdgedModuleName, resource, model.InsertOperation, lease)
resp, err := c.send.SendSync(leaseMsg)
if err != nil {
return nil, fmt.Errorf("create lease failed, err: %v", err)
}
content, err := resp.GetContentData()
if err != nil {
return nil, fmt.Errorf("parse message to lease failed, err: %v", err)
}
return handleLeaseResp(content)
}
func (c *leases) Update(lease *coordinationv1.Lease) (*coordinationv1.Lease, error) {
resource := fmt.Sprintf("%s/%s/%s", c.namespace, model.ResourceTypeLease, lease.Name)
leaseMsg := message.BuildMsg(modules.MetaGroup, "", modules.EdgedModuleName, resource, model.UpdateOperation, lease)
resp, err := c.send.SendSync(leaseMsg)
if err != nil {
return nil, fmt.Errorf("update lease failed, err: %v", err)
}
content, err := resp.GetContentData()
if err != nil {
return nil, fmt.Errorf("parse message to lease failed, err: %v", err)
}
return handleLeaseResp(content)
}
func (c *leases) Get(name string) (*coordinationv1.Lease, error) {
resource := fmt.Sprintf("%s/%s/%s", c.namespace, model.ResourceTypeLease, name)
leaseMsg := message.BuildMsg(modules.MetaGroup, "", modules.EdgedModuleName, resource, model.QueryOperation, nil)
resp, err := c.send.SendSync(leaseMsg)
if err != nil {
return nil, fmt.Errorf("query lease failed, err: %v", err)
}
content, err := resp.GetContentData()
if err != nil {
return nil, fmt.Errorf("parse message to lease failed, err: %v", err)
}
return handleLeaseResp(content)
}
func handleLeaseResp(content []byte) (*coordinationv1.Lease, error) {
var leaseResp LeaseResp
err := json.Unmarshal(content, &leaseResp)
if err != nil {
return nil, fmt.Errorf("unmarshal message to lease failed, err: %v", err)
}
if reflect.DeepEqual(leaseResp.Err, apierrors.StatusError{}) {
return leaseResp.Object, nil
}
return leaseResp.Object, &leaseResp.Err
}