Skip to content

Commit

Permalink
Merge pull request #253 from Mixaster995/enhancement/handle-all-names…
Browse files Browse the repository at this point in the history
…paces

Removed env provided namespace
  • Loading branch information
denis-tingaikin authored Oct 12, 2021
2 parents adf19e0 + f644d2a commit bb5bbe1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pkg/registry/etcd/ns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (n *etcdNSRegistryServer) Register(ctx context.Context, request *registry.N
)
if apierrors.IsAlreadyExists(err) {
var exist *v1.NetworkService
exist, err = n.client.NetworkservicemeshV1().NetworkServices(n.ns).Get(ctx, request.Name, metav1.GetOptions{})
exist, err = n.client.NetworkservicemeshV1().NetworkServices("").Get(ctx, request.Name, metav1.GetOptions{})
if err == nil {
exist.Spec = *(*v1.NetworkServiceSpec)(request)
apiResp, err = n.client.NetworkservicemeshV1().NetworkServices(n.ns).Update(ctx, exist, metav1.UpdateOptions{})
Expand All @@ -78,7 +78,7 @@ func (n *etcdNSRegistryServer) watch(query *registry.NetworkServiceQuery, s regi
var watchErr error
for watchErr == nil {
timeoutSeconds := int64(time.Minute / time.Second)
watcher, err := n.client.NetworkservicemeshV1().NetworkServices(n.ns).Watch(s.Context(), metav1.ListOptions{
watcher, err := n.client.NetworkservicemeshV1().NetworkServices("").Watch(s.Context(), metav1.ListOptions{
TimeoutSeconds: &timeoutSeconds,
})
if err != nil {
Expand Down Expand Up @@ -126,7 +126,7 @@ func (n *etcdNSRegistryServer) handleWatcher(
}

func (n *etcdNSRegistryServer) Find(query *registry.NetworkServiceQuery, s registry.NetworkServiceRegistry_FindServer) error {
list, err := n.client.NetworkservicemeshV1().NetworkServices(n.ns).List(s.Context(), metav1.ListOptions{})
list, err := n.client.NetworkservicemeshV1().NetworkServices("").List(s.Context(), metav1.ListOptions{})
if err != nil {
return err
}
Expand Down
34 changes: 30 additions & 4 deletions pkg/registry/etcd/ns_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ import (
)

func Test_NSReRegister(t *testing.T) {
s := etcd.NewNetworkServiceRegistryServer(context.Background(), "default", fake.NewSimpleClientset())
s := etcd.NewNetworkServiceRegistryServer(context.Background(), "", fake.NewSimpleClientset())
_, err := s.Register(context.Background(), &registry.NetworkService{Name: "ns-1"})
require.NoError(t, err)
_, err = s.Register(context.Background(), &registry.NetworkService{Name: "ns-1", Payload: "IP"})
require.NoError(t, err)
}

func Test_K8sNERegistry_ShouldMatchMetadataToName(t *testing.T) {
func Test_K8sNSRegistry_ShouldMatchMetadataToName(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

var myClientset = fake.NewSimpleClientset()

_, err := myClientset.NetworkservicemeshV1().NetworkServices("default").Create(ctx, &v1.NetworkService{
_, err := myClientset.NetworkservicemeshV1().NetworkServices("").Create(ctx, &v1.NetworkService{
ObjectMeta: metav1.ObjectMeta{
Name: "ns-1",
},
}, metav1.CreateOptions{})
require.NoError(t, err)

s := etcd.NewNetworkServiceRegistryServer(ctx, "default", myClientset)
s := etcd.NewNetworkServiceRegistryServer(ctx, "", myClientset)
c := adapters.NetworkServiceServerToClient(s)

stream, err := c.Find(ctx, &registry.NetworkServiceQuery{
Expand All @@ -68,3 +68,29 @@ func Test_K8sNERegistry_ShouldMatchMetadataToName(t *testing.T) {

require.Equal(t, "ns-1", nse.Name)
}

func Test_K8sNSRegistry_Find(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

var myClientset = fake.NewSimpleClientset()
_, err := myClientset.NetworkservicemeshV1().NetworkServices("some namespace").Create(ctx, &v1.NetworkService{
ObjectMeta: metav1.ObjectMeta{
Name: "ns-1",
},
}, metav1.CreateOptions{})
require.NoError(t, err)

c := adapters.NetworkServiceServerToClient(etcd.NewNetworkServiceRegistryServer(ctx, "", myClientset))
stream, err := c.Find(ctx, &registry.NetworkServiceQuery{
NetworkService: &registry.NetworkService{
Name: "ns-1",
},
})
require.NoError(t, err)

nse, err := stream.Recv()
require.NoError(t, err)

require.Equal(t, "ns-1", nse.Name)
}
6 changes: 3 additions & 3 deletions pkg/registry/etcd/nse_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (n *etcdNSERegistryServer) Register(ctx context.Context, request *registry.
)
if apierrors.IsAlreadyExists(err) {
var exist *v1.NetworkServiceEndpoint
exist, err = n.client.NetworkservicemeshV1().NetworkServiceEndpoints(n.ns).Get(ctx, request.Name, metav1.GetOptions{})
exist, err = n.client.NetworkservicemeshV1().NetworkServiceEndpoints("").Get(ctx, request.Name, metav1.GetOptions{})
if err == nil {
exist.Spec = *(*v1.NetworkServiceEndpointSpec)(request)
apiResp, err = n.client.NetworkservicemeshV1().NetworkServiceEndpoints(n.ns).Update(ctx, exist, metav1.UpdateOptions{})
Expand All @@ -77,7 +77,7 @@ func (n *etcdNSERegistryServer) Register(ctx context.Context, request *registry.
}

func (n *etcdNSERegistryServer) Find(query *registry.NetworkServiceEndpointQuery, s registry.NetworkServiceEndpointRegistry_FindServer) error {
list, err := n.client.NetworkservicemeshV1().NetworkServiceEndpoints(n.ns).List(s.Context(), metav1.ListOptions{})
list, err := n.client.NetworkservicemeshV1().NetworkServiceEndpoints("").List(s.Context(), metav1.ListOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func (n *etcdNSERegistryServer) watch(query *registry.NetworkServiceEndpointQuer
var watchErr error
for watchErr == nil {
timeoutSeconds := int64(time.Minute / time.Second)
watcher, err := n.client.NetworkservicemeshV1().NetworkServiceEndpoints(n.ns).Watch(s.Context(), metav1.ListOptions{
watcher, err := n.client.NetworkservicemeshV1().NetworkServiceEndpoints("").Watch(s.Context(), metav1.ListOptions{
TimeoutSeconds: &timeoutSeconds,
})
if err != nil {
Expand Down
28 changes: 27 additions & 1 deletion pkg/registry/etcd/nse_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func Test_NSEReRegister(t *testing.T) {
s := etcd.NewNetworkServiceEndpointRegistryServer(context.Background(), "default", fake.NewSimpleClientset())
s := etcd.NewNetworkServiceEndpointRegistryServer(context.Background(), "", fake.NewSimpleClientset())
_, err := s.Register(context.Background(), &registry.NetworkServiceEndpoint{Name: "nse-1"})
require.NoError(t, err)
_, err = s.Register(context.Background(), &registry.NetworkServiceEndpoint{Name: "nse-1", NetworkServiceNames: []string{"ns-1"}})
Expand Down Expand Up @@ -65,3 +65,29 @@ func Test_K8sNSERegistry_ShouldMatchMetadataToName(t *testing.T) {

require.Equal(t, "nse-1", nse.Name)
}

func Test_K8sNSERegistry_Find(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

var myClientset = fake.NewSimpleClientset()
_, err := myClientset.NetworkservicemeshV1().NetworkServiceEndpoints("some namespace").Create(ctx, &v1.NetworkServiceEndpoint{
ObjectMeta: metav1.ObjectMeta{
Name: "nse-1",
},
}, metav1.CreateOptions{})
require.NoError(t, err)

c := adapters.NetworkServiceEndpointServerToClient(etcd.NewNetworkServiceEndpointRegistryServer(ctx, "", myClientset))
stream, err := c.Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Name: "nse-1",
},
})
require.NoError(t, err)

nse, err := stream.Recv()
require.NoError(t, err)

require.Equal(t, "nse-1", nse.Name)
}

0 comments on commit bb5bbe1

Please sign in to comment.