@@ -26,15 +26,16 @@ import (
2626 "k8s.io/client-go/dynamic"
2727 "k8s.io/client-go/kubernetes/scheme"
2828 "k8s.io/client-go/rest"
29+ "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2930
3031 topologyv1 "github.com/networkop/meshnet-cni/api/types/v1beta1"
3132)
3233
3334// TopologyInterface provides access to the Topology CRD.
3435type TopologyInterface interface {
3536 List (ctx context.Context , opts metav1.ListOptions ) (* topologyv1.TopologyList , error )
36- Get (ctx context.Context , name string , options metav1.GetOptions ) (* topologyv1.Topology , error )
37- Create (ctx context.Context , topology * topologyv1.Topology ) (* topologyv1.Topology , error )
37+ Get (ctx context.Context , name string , opts metav1.GetOptions ) (* topologyv1.Topology , error )
38+ Create (ctx context.Context , topology * topologyv1.Topology , opts metav1. CreateOptions ) (* topologyv1.Topology , error )
3839 Delete (ctx context.Context , name string , opts metav1.DeleteOptions ) error
3940 Watch (ctx context.Context , opts metav1.ListOptions ) (watch.Interface , error )
4041 Unstructured (ctx context.Context , name string , opts metav1.GetOptions , subresources ... string ) (* unstructured.Unstructured , error )
@@ -49,19 +50,33 @@ type Interface interface {
4950// Clientset is a client for the topology crds.
5051type Clientset struct {
5152 dInterface dynamic.NamespaceableResourceInterface
52- restClient rest.Interface
5353}
5454
5555var gvr = schema.GroupVersionResource {
56- Group : "networkop.co.uk" ,
57- Version : "v1beta1" ,
56+ Group : topologyv1 . GroupName ,
57+ Version : topologyv1 . GroupVersion ,
5858 Resource : "topologies" ,
5959}
6060
61+ func GVR () schema.GroupVersionResource {
62+ return gvr
63+ }
64+
65+ var (
66+ groupVersion = & schema.GroupVersion {
67+ Group : topologyv1 .GroupName ,
68+ Version : topologyv1 .GroupVersion ,
69+ }
70+ )
71+
72+ func GV () * schema.GroupVersion {
73+ return groupVersion
74+ }
75+
6176// NewForConfig returns a new Clientset based on c.
6277func NewForConfig (c * rest.Config ) (* Clientset , error ) {
6378 config := * c
64- config .ContentConfig .GroupVersion = & schema. GroupVersion { Group : topologyv1 . GroupName , Version : topologyv1 . GroupVersion }
79+ config .ContentConfig .GroupVersion = groupVersion
6580 config .APIPath = "/apis"
6681 config .NegotiatedSerializer = scheme .Codecs .WithoutConversion ()
6782 config .UserAgent = rest .DefaultKubernetesUserAgent ()
@@ -70,108 +85,95 @@ func NewForConfig(c *rest.Config) (*Clientset, error) {
7085 return nil , err
7186 }
7287 dInterface := dClient .Resource (gvr )
73- rClient , err := rest .RESTClientFor (& config )
74- if err != nil {
75- return nil , err
76- }
77- return & Clientset {
78- dInterface : dInterface ,
79- restClient : rClient ,
80- }, nil
88+ return & Clientset {dInterface : dInterface }, nil
89+ }
90+
91+ // SetDynamicClient is only exposed for integration testing.
92+ func (c * Clientset ) SetDynamicClient (d dynamic.NamespaceableResourceInterface ) {
93+ c .dInterface = d
8194}
8295
8396func (c * Clientset ) Topology (namespace string ) TopologyInterface {
8497 return & topologyClient {
8598 dInterface : c .dInterface ,
86- restClient : c .restClient ,
8799 ns : namespace ,
88100 }
89101}
90102
91103type topologyClient struct {
92104 dInterface dynamic.NamespaceableResourceInterface
93- restClient rest.Interface
94105 ns string
95106}
96107
97108func (t * topologyClient ) List (ctx context.Context , opts metav1.ListOptions ) (* topologyv1.TopologyList , error ) {
109+ u , err := t .dInterface .Namespace (t .ns ).List (ctx , opts )
110+ if err != nil {
111+ return nil , err
112+ }
98113 result := topologyv1.TopologyList {}
99- err := t .restClient .
100- Get ().
101- Namespace (t .ns ).
102- Resource ("topologies" ).
103- VersionedParams (& opts , scheme .ParameterCodec ).
104- Do (ctx ).
105- Into (& result )
106-
107- return & result , err
114+ if err := runtime .DefaultUnstructuredConverter .FromUnstructured (u .UnstructuredContent (), & result ); err != nil {
115+ return nil , fmt .Errorf ("failed to type assert return to TopologyList: %w" , err )
116+ }
117+ return & result , nil
108118}
109119
110120func (t * topologyClient ) Get (ctx context.Context , name string , opts metav1.GetOptions ) (* topologyv1.Topology , error ) {
121+ u , err := t .dInterface .Namespace (t .ns ).Get (ctx , name , opts )
122+ if err != nil {
123+ return nil , err
124+ }
111125 result := topologyv1.Topology {}
112- err := t .restClient .
113- Get ().
114- Namespace (t .ns ).
115- Resource ("topologies" ).
116- Name (name ).
117- VersionedParams (& opts , scheme .ParameterCodec ).
118- Do (ctx ).
119- Into (& result )
120-
121- return & result , err
126+ if err := runtime .DefaultUnstructuredConverter .FromUnstructured (u .UnstructuredContent (), & result ); err != nil {
127+ return nil , fmt .Errorf ("failed to type assert return to Topology: %w" , err )
128+ }
129+ return & result , nil
122130}
123131
124- func (t * topologyClient ) Create (ctx context.Context , topology * topologyv1.Topology ) (* topologyv1.Topology , error ) {
132+ func (t * topologyClient ) Create (ctx context.Context , topology * topologyv1.Topology , opts metav1.CreateOptions ) (* topologyv1.Topology , error ) {
133+ gvk , err := apiutil .GVKForObject (topology , topologyv1 .Scheme )
134+ if err != nil {
135+ return nil , fmt .Errorf ("failed to get gvk for Topology: %w" , err )
136+ }
137+ topology .TypeMeta = metav1.TypeMeta {
138+ Kind : gvk .Kind ,
139+ APIVersion : gvk .GroupVersion ().String (),
140+ }
141+ obj , err := runtime .DefaultUnstructuredConverter .ToUnstructured (topology )
142+ if err != nil {
143+ return nil , fmt .Errorf ("failed to convert Topology to unstructured: %w" , err )
144+ }
145+ u , err := t .dInterface .Namespace (t .ns ).Create (ctx , & unstructured.Unstructured {Object : obj }, opts )
146+ if err != nil {
147+ return nil , err
148+ }
125149 result := topologyv1.Topology {}
126- err := t .restClient .
127- Post ().
128- Namespace (t .ns ).
129- Resource ("topologies" ).
130- Body (topology ).
131- Do (ctx ).
132- Into (& result )
133-
134- return & result , err
150+ if err := runtime .DefaultUnstructuredConverter .FromUnstructured (u .UnstructuredContent (), & result ); err != nil {
151+ return nil , fmt .Errorf ("failed to type assert return to Topology: %w" , err )
152+ }
153+ return & result , nil
135154}
136155
137156func (t * topologyClient ) Watch (ctx context.Context , opts metav1.ListOptions ) (watch.Interface , error ) {
138157 opts .Watch = true
139- return t .restClient .
140- Get ().
141- Namespace (t .ns ).
142- Resource ("topologies" ).
143- VersionedParams (& opts , scheme .ParameterCodec ).
144- Watch (ctx )
158+ return t .dInterface .Namespace (t .ns ).Watch (ctx , opts )
145159}
146160
147161func (t * topologyClient ) Delete (ctx context.Context , name string , opts metav1.DeleteOptions ) error {
148- return t .restClient .
149- Delete ().
150- Namespace (t .ns ).
151- Resource ("topologies" ).
152- VersionedParams (& opts , scheme .ParameterCodec ).
153- Name (name ).
154- Do (ctx ).
155- Error ()
162+ return t .dInterface .Namespace (t .ns ).Delete (ctx , name , opts )
156163}
157164
158165func (t * topologyClient ) Update (ctx context.Context , obj * unstructured.Unstructured , opts metav1.UpdateOptions ) (* topologyv1.Topology , error ) {
159- result := topologyv1.Topology {}
160166 obj , err := t .dInterface .Namespace (t .ns ).UpdateStatus (ctx , obj , metav1.UpdateOptions {})
161167 if err != nil {
162168 return nil , err
163169 }
164- err = runtime . DefaultUnstructuredConverter . FromUnstructured ( obj . UnstructuredContent (), & result )
165- if err != nil {
166- return nil , fmt .Errorf ("failed to type assert return to topology" )
170+ result := topologyv1. Topology {}
171+ if err := runtime . DefaultUnstructuredConverter . FromUnstructured ( obj . UnstructuredContent (), & result ); err != nil {
172+ return nil , fmt .Errorf ("failed to type assert return to Topology: %w" , err )
167173 }
168174 return & result , nil
169175}
170176
171177func (t * topologyClient ) Unstructured (ctx context.Context , name string , opts metav1.GetOptions , subresources ... string ) (* unstructured.Unstructured , error ) {
172178 return t .dInterface .Namespace (t .ns ).Get (ctx , name , opts , subresources ... )
173179}
174-
175- func init () {
176- topologyv1 .AddToScheme (scheme .Scheme )
177- }
0 commit comments