iface.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package ncutils
  2. import (
  3. "github.com/gravitl/netmaker/models"
  4. )
  5. func IfaceDelta(currentNode *models.Node, newNode *models.Node) bool {
  6. // single comparison statements
  7. if newNode.Endpoint != currentNode.Endpoint ||
  8. newNode.LocalAddress != currentNode.LocalAddress ||
  9. newNode.PublicKey != currentNode.PublicKey ||
  10. newNode.Address != currentNode.Address ||
  11. newNode.IsEgressGateway != currentNode.IsEgressGateway ||
  12. newNode.IsIngressGateway != currentNode.IsIngressGateway ||
  13. newNode.IsRelay != currentNode.IsRelay ||
  14. newNode.UDPHolePunch != currentNode.UDPHolePunch ||
  15. newNode.IsPending != currentNode.IsPending ||
  16. newNode.PersistentKeepalive != currentNode.PersistentKeepalive ||
  17. len(newNode.ExcludedAddrs) != len(currentNode.ExcludedAddrs) ||
  18. len(newNode.AllowedIPs) != len(currentNode.AllowedIPs) {
  19. return true
  20. }
  21. // multi-comparison statements
  22. if newNode.IsDualStack == "yes" {
  23. if newNode.Address6 != currentNode.Address6 {
  24. return true
  25. }
  26. }
  27. if newNode.IsEgressGateway == "yes" {
  28. if len(currentNode.EgressGatewayRanges) != len(newNode.EgressGatewayRanges) {
  29. return true
  30. }
  31. for _, address := range newNode.EgressGatewayRanges {
  32. if !StringSliceContains(currentNode.EgressGatewayRanges, address) {
  33. return true
  34. }
  35. }
  36. }
  37. if newNode.IsRelay == "yes" {
  38. if len(currentNode.RelayAddrs) != len(newNode.RelayAddrs) {
  39. return true
  40. }
  41. for _, address := range newNode.RelayAddrs {
  42. if !StringSliceContains(currentNode.RelayAddrs, address) {
  43. return true
  44. }
  45. }
  46. }
  47. for _, address := range newNode.AllowedIPs {
  48. if !StringSliceContains(currentNode.AllowedIPs, address) {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. // StringSliceContains - sees if a string slice contains a string element
  55. func StringSliceContains(slice []string, item string) bool {
  56. for _, s := range slice {
  57. if s == item {
  58. return true
  59. }
  60. }
  61. return false
  62. }