/
topology.go
177 lines (155 loc) · 5.33 KB
/
topology.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2021 Google LLC
//
// 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 v1beta1
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
topologyv1 "github.com/networkop/meshnet-cni/api/types/v1beta1"
)
// TopologyInterface provides access to the Topology CRD.
type TopologyInterface interface {
List(ctx context.Context, opts metav1.ListOptions) (*topologyv1.TopologyList, error)
Get(ctx context.Context, name string, options metav1.GetOptions) (*topologyv1.Topology, error)
Create(ctx context.Context, topology *topologyv1.Topology) (*topologyv1.Topology, error)
Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
Unstructured(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error)
Update(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*topologyv1.Topology, error)
}
// Interface is the clientset interface for topology.
type Interface interface {
Topology(namespace string) TopologyInterface
}
// Clientset is a client for the topology crds.
type Clientset struct {
dInterface dynamic.NamespaceableResourceInterface
restClient rest.Interface
}
var gvr = schema.GroupVersionResource{
Group: "networkop.co.uk",
Version: "v1beta1",
Resource: "topologies",
}
// NewForConfig returns a new Clientset based on c.
func NewForConfig(c *rest.Config) (*Clientset, error) {
config := *c
config.ContentConfig.GroupVersion = &schema.GroupVersion{Group: topologyv1.GroupName, Version: topologyv1.GroupVersion}
config.APIPath = "/apis"
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
config.UserAgent = rest.DefaultKubernetesUserAgent()
dClient, err := dynamic.NewForConfig(c)
if err != nil {
return nil, err
}
dInterface := dClient.Resource(gvr)
rClient, err := rest.RESTClientFor(&config)
if err != nil {
return nil, err
}
return &Clientset{
dInterface: dInterface,
restClient: rClient,
}, nil
}
func (c *Clientset) Topology(namespace string) TopologyInterface {
return &topologyClient{
dInterface: c.dInterface,
restClient: c.restClient,
ns: namespace,
}
}
type topologyClient struct {
dInterface dynamic.NamespaceableResourceInterface
restClient rest.Interface
ns string
}
func (t *topologyClient) List(ctx context.Context, opts metav1.ListOptions) (*topologyv1.TopologyList, error) {
result := topologyv1.TopologyList{}
err := t.restClient.
Get().
Namespace(t.ns).
Resource("topologies").
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)
return &result, err
}
func (t *topologyClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*topologyv1.Topology, error) {
result := topologyv1.Topology{}
err := t.restClient.
Get().
Namespace(t.ns).
Resource("topologies").
Name(name).
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)
return &result, err
}
func (t *topologyClient) Create(ctx context.Context, topology *topologyv1.Topology) (*topologyv1.Topology, error) {
result := topologyv1.Topology{}
err := t.restClient.
Post().
Namespace(t.ns).
Resource("topologies").
Body(topology).
Do(ctx).
Into(&result)
return &result, err
}
func (t *topologyClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
opts.Watch = true
return t.restClient.
Get().
Namespace(t.ns).
Resource("topologies").
VersionedParams(&opts, scheme.ParameterCodec).
Watch(ctx)
}
func (t *topologyClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return t.restClient.
Delete().
Namespace(t.ns).
Resource("topologies").
VersionedParams(&opts, scheme.ParameterCodec).
Name(name).
Do(ctx).
Error()
}
func (t *topologyClient) Update(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*topologyv1.Topology, error) {
result := topologyv1.Topology{}
obj, err := t.dInterface.Namespace(t.ns).UpdateStatus(ctx, obj, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &result)
if err != nil {
return nil, fmt.Errorf("failed to type assert return to topology")
}
return &result, nil
}
func (t *topologyClient) Unstructured(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
return t.dInterface.Namespace(t.ns).Get(ctx, name, opts, subresources...)
}
func init() {
topologyv1.AddToScheme(scheme.Scheme)
}