Skip to content

Commit

Permalink
add l2 advertisment to metallb add clientset and tests (openconfig#211)
Browse files Browse the repository at this point in the history
* add l2 advertisment to metallb add clientset and tests

* add test check
  • Loading branch information
marcushines authored Sep 14, 2022
1 parent f83ba16 commit 81cebfa
Show file tree
Hide file tree
Showing 8 changed files with 660 additions and 131 deletions.
136 changes: 46 additions & 90 deletions api/metallb/clientset/v1beta1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package v1beta1

import (
"context"
"fmt"

metallbv1 "go.universe.tf/metallb/api/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -26,10 +25,9 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

// IPAddressPoolInterface provides access to the AddressPool CRD.
// IPAddressPoolInterface provides access to the IPAddressPool CRD.
type IPAddressPoolInterface interface {
List(ctx context.Context, opts metav1.ListOptions) (*metallbv1.IPAddressPoolList, error)
Get(ctx context.Context, name string, opts metav1.GetOptions) (*metallbv1.IPAddressPool, error)
Expand All @@ -40,26 +38,54 @@ type IPAddressPoolInterface interface {
Update(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*metallbv1.IPAddressPool, error)
}

// L2AdvertisementInterface provides access to the L2Advertisement CRD.
type L2AdvertisementInterface interface {
List(ctx context.Context, opts metav1.ListOptions) (*metallbv1.L2AdvertisementList, error)
Get(ctx context.Context, name string, opts metav1.GetOptions) (*metallbv1.L2Advertisement, error)
Create(ctx context.Context, pool *metallbv1.L2Advertisement, opts metav1.CreateOptions) (*metallbv1.L2Advertisement, 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) (*metallbv1.L2Advertisement, error)
}

// Interface is the clientset interface for Metallb.
type Interface interface {
IPAddressPool(namespace string) IPAddressPoolInterface
L2Advertisement(namespace string) L2AdvertisementInterface
}

// Clientset is a client for the metallb crds.
type Clientset struct {
dInterface dynamic.NamespaceableResourceInterface
dClient dynamic.Interface
}

var gvrIPAddressPool = schema.GroupVersionResource{
Group: groupVersion.Group,
Version: groupVersion.Version,
Resource: "ipaddresspools",
func (c *Clientset) Metallb() Interface {
return c
}

var (
gvrIPAddressPool = schema.GroupVersionResource{
Group: groupVersion.Group,
Version: groupVersion.Version,
Resource: "ipaddresspools",
}

gvrL2Advertisment = schema.GroupVersionResource{
Group: groupVersion.Group,
Version: groupVersion.Version,
Resource: "l2advertisements",
}
)

func IPAddressPoolGVR() schema.GroupVersionResource {
return gvrIPAddressPool
}

func L2AdvertisementGVR() schema.GroupVersionResource {
return gvrL2Advertisment
}

var (
groupVersion = metallbv1.GroupVersion
Scheme = runtime.NewScheme()
Expand All @@ -76,8 +102,8 @@ func GV() *schema.GroupVersion {
return &groupVersion
}

// NewAddressPoolForConfig returns a new Clientset based on c.
func NewAddressPoolForConfig(c *rest.Config) (*Clientset, error) {
// NewForConfig returns a new Clientset based on c.
func NewForConfig(c *rest.Config) (*Clientset, error) {
config := *c
config.ContentConfig.GroupVersion = &groupVersion
config.APIPath = "/apis"
Expand All @@ -87,96 +113,26 @@ func NewAddressPoolForConfig(c *rest.Config) (*Clientset, error) {
if err != nil {
return nil, err
}
dInterface := dClient.Resource(gvrIPAddressPool)
return &Clientset{dInterface: dInterface}, nil
return &Clientset{
dClient: dClient,
}, nil
}

// SetDynamicClient is only exposed for integration testing.
func (c *Clientset) SetDynamicClient(d dynamic.NamespaceableResourceInterface) {
c.dInterface = d
func (c *Clientset) SetDynamicClient(d dynamic.Interface) {
c.dClient = d
}

func (c *Clientset) IPAddressPool(namespace string) IPAddressPoolInterface {
return &addressPoolClient{
dInterface: c.dInterface,
dInterface: c.dClient.Resource(gvrIPAddressPool),
ns: namespace,
}
}

type addressPoolClient struct {
dInterface dynamic.NamespaceableResourceInterface
ns string
}

func (a *addressPoolClient) List(ctx context.Context, opts metav1.ListOptions) (*metallbv1.IPAddressPoolList, error) {
u, err := a.dInterface.Namespace(a.ns).List(ctx, opts)
if err != nil {
return nil, err
}
result := metallbv1.IPAddressPoolList{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &result); err != nil {
return nil, fmt.Errorf("failed to type assert return to IPAddressPoolList: %w", err)
}
return &result, nil
}

func (a *addressPoolClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*metallbv1.IPAddressPool, error) {
u, err := a.dInterface.Namespace(a.ns).Get(ctx, name, opts)
if err != nil {
return nil, err
}
result := metallbv1.IPAddressPool{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &result); err != nil {
return nil, fmt.Errorf("failed to type assert return to IPAddressPool: %w", err)
}
return &result, nil
}

func (a *addressPoolClient) Create(ctx context.Context, pool *metallbv1.IPAddressPool, opts metav1.CreateOptions) (*metallbv1.IPAddressPool, error) {
gvk, err := apiutil.GVKForObject(pool, Scheme)
if err != nil {
return nil, fmt.Errorf("failed to get gvk for AddressPool: %w", err)
}
pool.TypeMeta = metav1.TypeMeta{
Kind: gvk.Kind,
APIVersion: gvk.GroupVersion().String(),
}
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pool)
if err != nil {
return nil, fmt.Errorf("failed to convert IPAddressPool to unstructured: %w", err)
}
u, err := a.dInterface.Namespace(a.ns).Create(ctx, &unstructured.Unstructured{Object: obj}, opts)
if err != nil {
return nil, err
}
result := metallbv1.IPAddressPool{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &result); err != nil {
return nil, fmt.Errorf("failed to type assert return to AddressPool: %w", err)
}
return &result, nil
}

func (a *addressPoolClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
opts.Watch = true
return a.dInterface.Namespace(a.ns).Watch(ctx, opts)
}

func (a *addressPoolClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return a.dInterface.Namespace(a.ns).Delete(ctx, name, opts)
}

func (a *addressPoolClient) Update(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*metallbv1.IPAddressPool, error) {
obj, err := a.dInterface.Namespace(a.ns).UpdateStatus(ctx, obj, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
result := metallbv1.IPAddressPool{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &result); err != nil {
return nil, fmt.Errorf("failed to type assert return to AddressPool: %w", err)
func (c *Clientset) L2Advertisement(namespace string) L2AdvertisementInterface {
return &l2AdvertisementClient{
dInterface: c.dClient.Resource(gvrL2Advertisment),
ns: namespace,
}
return &result, nil
}

func (a *addressPoolClient) Unstructured(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
return a.dInterface.Namespace(a.ns).Get(ctx, name, opts, subresources...)
}
4 changes: 2 additions & 2 deletions api/metallb/clientset/v1beta1/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
)

func NewSimpleClientset(objects ...runtime.Object) (*metallbv1client.Clientset, error) {
cs, err := metallbv1client.NewAddressPoolForConfig(&rest.Config{})
cs, err := metallbv1client.NewForConfig(&rest.Config{})
if err != nil {
return nil, err
}
c := dfake.NewSimpleDynamicClient(metallbv1client.Scheme, objects...)
cs.SetDynamicClient(c.Resource(metallbv1client.IPAddressPoolGVR()))
cs.SetDynamicClient(c)
return cs, nil
}
105 changes: 105 additions & 0 deletions api/metallb/clientset/v1beta1/ipaddresspool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2022 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"

metallbv1 "go.universe.tf/metallb/api/v1beta1"
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/watch"
"k8s.io/client-go/dynamic"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

type addressPoolClient struct {
dInterface dynamic.NamespaceableResourceInterface
ns string
}

func (a *addressPoolClient) List(ctx context.Context, opts metav1.ListOptions) (*metallbv1.IPAddressPoolList, error) {
u, err := a.dInterface.Namespace(a.ns).List(ctx, opts)
if err != nil {
return nil, err
}
result := metallbv1.IPAddressPoolList{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &result); err != nil {
return nil, fmt.Errorf("failed to type assert return to IPAddressPoolList: %w", err)
}
return &result, nil
}

func (a *addressPoolClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*metallbv1.IPAddressPool, error) {
u, err := a.dInterface.Namespace(a.ns).Get(ctx, name, opts)
if err != nil {
return nil, err
}
result := metallbv1.IPAddressPool{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &result); err != nil {
return nil, fmt.Errorf("failed to type assert return to IPAddressPool: %w", err)
}
return &result, nil
}

func (a *addressPoolClient) Create(ctx context.Context, pool *metallbv1.IPAddressPool, opts metav1.CreateOptions) (*metallbv1.IPAddressPool, error) {
gvk, err := apiutil.GVKForObject(pool, Scheme)
if err != nil {
return nil, fmt.Errorf("failed to get gvk for AddressPool: %w", err)
}
pool.TypeMeta = metav1.TypeMeta{
Kind: gvk.Kind,
APIVersion: gvk.GroupVersion().String(),
}
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pool)
if err != nil {
return nil, fmt.Errorf("failed to convert IPAddressPool to unstructured: %w", err)
}
u, err := a.dInterface.Namespace(a.ns).Create(ctx, &unstructured.Unstructured{Object: obj}, opts)
if err != nil {
return nil, err
}
result := metallbv1.IPAddressPool{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &result); err != nil {
return nil, fmt.Errorf("failed to type assert return to AddressPool: %w", err)
}
return &result, nil
}

func (a *addressPoolClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
opts.Watch = true
return a.dInterface.Namespace(a.ns).Watch(ctx, opts)
}

func (a *addressPoolClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return a.dInterface.Namespace(a.ns).Delete(ctx, name, opts)
}

func (a *addressPoolClient) Update(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*metallbv1.IPAddressPool, error) {
obj, err := a.dInterface.Namespace(a.ns).UpdateStatus(ctx, obj, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
result := metallbv1.IPAddressPool{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &result); err != nil {
return nil, fmt.Errorf("failed to type assert return to AddressPool: %w", err)
}
return &result, nil
}

func (a *addressPoolClient) Unstructured(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
return a.dInterface.Namespace(a.ns).Get(ctx, name, opts, subresources...)
}
Loading

0 comments on commit 81cebfa

Please sign in to comment.