forked from projectcalico/libcalico-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathippool.go
127 lines (108 loc) · 3.39 KB
/
ippool.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
119
120
121
122
123
124
125
126
127
// Copyright (c) 2016-2017 Tigera, Inc. 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 resources
import (
"reflect"
"github.com/projectcalico/libcalico-go/lib/api"
"github.com/projectcalico/libcalico-go/lib/backend/k8s/custom"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/converter"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
const (
IPPoolResourceName = "IPPools"
IPPoolCRDName = "ippools.crd.projectcalico.org"
)
func NewIPPoolClient(c *kubernetes.Clientset, r *rest.RESTClient) K8sResourceClient {
return &customK8sResourceClient{
clientSet: c,
restClient: r,
name: IPPoolCRDName,
resource: IPPoolResourceName,
description: "Calico IP Pools",
k8sResourceType: reflect.TypeOf(custom.IPPool{}),
k8sListType: reflect.TypeOf(custom.IPPoolList{}),
converter: IPPoolConverter{},
}
}
// IPPoolConverter implements the K8sResourceConverter interface.
type IPPoolConverter struct {
converter.IPPoolConverter
}
func (_ IPPoolConverter) ListInterfaceToKey(l model.ListInterface) model.Key {
il := l.(model.IPPoolListOptions)
if il.CIDR.IP != nil {
return model.IPPoolKey{CIDR: il.CIDR}
}
return nil
}
func (_ IPPoolConverter) KeyToName(k model.Key) (string, error) {
return IPNetToResourceName(k.(model.IPPoolKey).CIDR), nil
}
func (_ IPPoolConverter) NameToKey(name string) (model.Key, error) {
cidr, err := ResourceNameToIPNet(name)
if err != nil {
return nil, err
}
return model.IPPoolKey{
CIDR: *cidr,
}, nil
}
func (i IPPoolConverter) ToKVPair(r CustomK8sResource) (*model.KVPair, error) {
// Since we are using the Calico API Spec definition to store the Calico
// IPPool, use the client conversion helper to convert API to KVP.
t := r.(*custom.IPPool)
_, err := ResourceNameToIPNet(t.Metadata.Name)
if err != nil {
return nil, err
}
ippool := api.IPPool{
Metadata: api.IPPoolMetadata{
CIDR: t.Spec.CIDR,
},
Spec: t.Spec.IPPoolSpec,
}
kvp, err := i.IPPoolConverter.ConvertAPIToKVPair(ippool)
if err != nil {
return nil, err
}
kvp.Revision = t.Metadata.ResourceVersion
return kvp, nil
}
func (i IPPoolConverter) FromKVPair(kvp *model.KVPair) (CustomK8sResource, error) {
// Since we are using the Calico API Spec definition to store the IPPool CRD Spec,
// we can use the client conversion helper to convert KVP to API.
r, err := i.IPPoolConverter.ConvertKVPairToAPI(kvp)
if err != nil {
return nil, err
}
crdName, err := i.KeyToName(kvp.Key)
if err != nil {
return nil, err
}
crd := custom.IPPool{
Metadata: metav1.ObjectMeta{
Name: crdName,
},
Spec: custom.IPPoolSpec{
IPPoolSpec: r.(*api.IPPool).Spec,
CIDR: r.(*api.IPPool).Metadata.CIDR,
},
}
if kvp.Revision != nil {
crd.Metadata.ResourceVersion = kvp.Revision.(string)
}
return &crd, nil
}