-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect interface changes and start/stop new/removed lldp discovery #73
Open
majst01
wants to merge
15
commits into
master
Choose a base branch
from
detect-interface-changes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−257
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9a27bbc
Detect interface changes and start/stop new/removed lldp discovery
majst01 01076a3
Slight refactor
majst01 5ed1eb2
Use sync.Map instead
majst01 6f83470
better impl
majst01 39ae993
Also re-register switch on port changes
majst01 392065b
Fix
majst01 8ead97a
update
majst01 f307c7a
less verbose logging
majst01 66c767a
fix
majst01 b364077
shorter intervall
majst01 6262d6b
Fix
majst01 2b128eb
Update all deps
majst01 ae45920
Omg
majst01 0bfe8b5
check only once
majst01 7bc9d26
no other interfaces than swp*
majst01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"strings" | ||
"time" | ||
|
||
"github.com/metal-stack/go-lldpd/pkg/lldp" | ||
) | ||
|
||
const ( | ||
detectChangesInterval = time.Minute | ||
) | ||
|
||
func (c *Core) DetectInterfaceChanges(ctx context.Context, discoveryResultChan chan lldp.DiscoveryResult) { | ||
ticker := time.NewTicker(detectChangesInterval) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
c.log.Info("checking for port changes") | ||
ifs, err := net.Interfaces() | ||
if err != nil { | ||
c.log.Errorw("unable to gather interfaces, ignoring", "error", err) | ||
continue | ||
} | ||
actualInterfaces := []string{} | ||
for _, iface := range ifs { | ||
// consider only switch port interfaces | ||
if !strings.HasPrefix(iface.Name, "swp") { | ||
continue | ||
} | ||
actualInterfaces = append(actualInterfaces, iface.Name) | ||
} | ||
existingInterfaces := []string{} | ||
c.interfaces.Range(func(key, value any) bool { | ||
existingInterfaces = append(existingInterfaces, key.(string)) | ||
return true | ||
}) | ||
|
||
addedInterfaces, removedInterfaces := difference(existingInterfaces, actualInterfaces) | ||
|
||
if len(addedInterfaces) == 0 && len(removedInterfaces) == 0 { | ||
c.log.Info("no port changes detected") | ||
continue | ||
} else { | ||
c.log.Infow("switch interfaces changed, re-register switch", "added", addedInterfaces, "removed", removedInterfaces) | ||
c.RegisterSwitch() | ||
} | ||
|
||
for _, i := range removedInterfaces { | ||
c.log.Infow("remove lldp discovery for", "interfaces", i) | ||
c.stopLLDPDiscovery(i) | ||
} | ||
for _, i := range addedInterfaces { | ||
iface, err := net.InterfaceByName(i) | ||
if err != nil { | ||
c.log.Errorw("unable to get interface by name", "interface", i, "error", err) | ||
continue | ||
} | ||
c.log.Infow("add lldp discovery for", "interfaces", *iface) | ||
c.startLLDPDiscovery(ctx, discoveryResultChan, *iface) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/exp/slices" | ||
) | ||
|
||
func Test_difference(t *testing.T) { | ||
type args struct { | ||
} | ||
tests := []struct { | ||
name string | ||
old []string | ||
new []string | ||
wantAdded []string | ||
wantRemoved []string | ||
}{ | ||
{ | ||
name: "equal", | ||
old: []string{"a", "b", "c"}, | ||
new: []string{"a", "b", "c"}, | ||
wantAdded: []string{}, | ||
wantRemoved: []string{}, | ||
}, | ||
{ | ||
name: "one added", | ||
old: []string{"a", "b", "c"}, | ||
new: []string{"a", "b", "d", "c"}, | ||
wantAdded: []string{"d"}, | ||
wantRemoved: []string{}, | ||
}, | ||
{ | ||
name: "one removed", | ||
old: []string{"a", "b", "d", "c"}, | ||
new: []string{"a", "b", "c"}, | ||
wantAdded: []string{}, | ||
wantRemoved: []string{"d"}, | ||
}, | ||
{ | ||
name: "more added and removed", | ||
old: []string{"a", "x", "b", "d", "c"}, | ||
new: []string{"a", "b", "c", "z", "j"}, | ||
wantAdded: []string{"z", "j"}, | ||
wantRemoved: []string{"x", "d"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotAdded, gotRemoved := difference(tt.old, tt.new) | ||
if !slices.Equal(gotAdded, tt.wantAdded) { | ||
t.Errorf("difference() gotAdded = %v, want %v", gotAdded, tt.wantAdded) | ||
} | ||
if !slices.Equal(gotRemoved, tt.wantRemoved) { | ||
t.Errorf("difference() gotRemoved = %v, want %v", gotRemoved, tt.wantRemoved) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In cmd/internal/core/detect-changes.go line 32 the interface is checked for "swp" prefix.
If the same check gets kept in cmd/internal/core/phone-home.go then behavious is consistent across callers (both only call this function for "swp" interfaces) and the duplicate check here could be dropped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is a bit too spread, will think of a solution, maybe factoring interface listing to a separate func (which also detects switch os in the future, SONiC hast "Ethernet.." )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the PR #68 for a possible solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes