Skip to content

Commit

Permalink
Merge pull request #385 from denis-tingaikin/fix-multi-k8s-registry-i…
Browse files Browse the repository at this point in the history
…ssue

fix: multiple k8s-registries lead to NSE flapping
  • Loading branch information
edwarnicke authored Sep 7, 2022
2 parents 72783c8 + 0cd6089 commit 1adc517
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
26 changes: 19 additions & 7 deletions pkg/registry/etcd/ns_server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,6 +22,7 @@ import (
"context"
"errors"
"io"
"sync"
"time"

"github.com/golang/protobuf/ptypes/empty"
Expand All @@ -39,6 +42,7 @@ import (
type etcdNSRegistryServer struct {
chainContext context.Context
client versioned.Interface
versions sync.Map
ns string
}

Expand Down Expand Up @@ -84,6 +88,8 @@ func (n *etcdNSRegistryServer) Register(ctx context.Context, request *registry.N
apiResp.Spec.DeepCopyInto((*v1.NetworkServiceSpec)(request))
request.Name = apiResp.Name

n.versions.Store(apiResp.Spec.Name, apiResp.ResourceVersion)

return (*registry.NetworkService)(&apiResp.Spec), nil
}

Expand Down Expand Up @@ -168,13 +174,19 @@ func (n *etcdNSRegistryServer) Unregister(ctx context.Context, request *registry
if err != nil {
return nil, err
}
err = n.client.NetworkservicemeshV1().NetworkServices(n.ns).Delete(
ctx,
request.Name,
metav1.DeleteOptions{},
)
if err != nil {
return nil, err
if v, ok := n.versions.Load(request.Name); ok {
version := v.(string)
err = n.client.NetworkservicemeshV1().NetworkServices(n.ns).Delete(
ctx,
request.Name,
metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{
ResourceVersion: &version,
},
})
if err != nil {
return nil, err
}
}
return resp, nil
}
Expand Down
26 changes: 20 additions & 6 deletions pkg/registry/etcd/nse_server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,6 +22,7 @@ import (
"context"
"errors"
"io"
"sync"
"time"

"github.com/golang/protobuf/ptypes/empty"
Expand All @@ -39,6 +42,7 @@ import (
type etcdNSERegistryServer struct {
chainContext context.Context
client versioned.Interface
versions sync.Map
ns string
}

Expand Down Expand Up @@ -86,6 +90,8 @@ func (n *etcdNSERegistryServer) Register(ctx context.Context, request *registry.
return nil, err
}

n.versions.Store(apiResp.Spec.Name, apiResp.ResourceVersion)

return (*registry.NetworkServiceEndpoint)(&apiResp.Spec), nil
}

Expand Down Expand Up @@ -119,12 +125,20 @@ func (n *etcdNSERegistryServer) Unregister(ctx context.Context, request *registr
if err != nil {
return nil, err
}
err = n.client.NetworkservicemeshV1().NetworkServiceEndpoints(n.ns).Delete(
ctx,
request.Name,
metav1.DeleteOptions{})
if err != nil {
return nil, err

if v, ok := n.versions.Load(request.Name); ok {
version := v.(string)
err = n.client.NetworkservicemeshV1().NetworkServiceEndpoints(n.ns).Delete(
ctx,
request.Name,
metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{
ResourceVersion: &version,
},
})
if err != nil {
return nil, err
}
}
return resp, nil
}
Expand Down

0 comments on commit 1adc517

Please sign in to comment.