-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NETOBSERV-1790: Manage enrichment via "k8s.v1.cni.cncf.io/network-sta…
…tus" (#674) * Manage enrichment via "k8s.v1.cni.cncf.io/network-status" Manage enrichment by extracting pod IPs from the annotation "k8s.v1.cni.cncf.io/network-status", which is used (at least) by multus This allows to correlate Pods with their IPs on secondary interfaces * rely on mac address when possible * optional mac-input config * NETOBSERV-1799: avoid fmt.Sprintf * Use enriched namespace for infra/app layer Do not use IP lookup, since now some pods are indexed via MAC and not IPs * Use converToString insted of Sprintf (#701) * remove unecessary import --------- Co-authored-by: Julien Pinsonneau <[email protected]> Co-authored-by: Julien Pinsonneau <[email protected]>
- Loading branch information
1 parent
9145910
commit 72029f5
Showing
15 changed files
with
470 additions
and
194 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cni | ||
|
||
import ( | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type Plugin interface { | ||
GetNodeIPs(node *v1.Node) []string | ||
GetPodIPsAndMACs(pod *v1.Pod) ([]string, []string) | ||
} |
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,63 @@ | ||
package cni | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
log "github.com/sirupsen/logrus" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
statusAnnotation = "k8s.v1.cni.cncf.io/network-status" | ||
) | ||
|
||
type MultusPlugin struct { | ||
Plugin | ||
} | ||
|
||
func (m *MultusPlugin) GetNodeIPs(_ *v1.Node) []string { | ||
// No CNI-specific logic needed for pods | ||
return nil | ||
} | ||
|
||
func (m *MultusPlugin) GetPodIPsAndMACs(pod *v1.Pod) ([]string, []string) { | ||
// Cf https://k8snetworkplumbingwg.github.io/multus-cni/docs/quickstart.html#network-status-annotations | ||
ips, macs, err := extractNetStatusIPsAndMACs(pod.Annotations) | ||
if err != nil { | ||
// Log the error as Info, do not block other ips indexing | ||
log.Infof("failed to index IPs from network-status annotation: %v", err) | ||
} | ||
log.Tracef("GetPodIPsAndMACs found ips: %v macs: %v for pod %s", ips, macs, pod.Name) | ||
return ips, macs | ||
} | ||
|
||
type netStatItem struct { | ||
IPs []string `json:"ips"` | ||
MAC string `json:"mac"` | ||
} | ||
|
||
func extractNetStatusIPsAndMACs(annotations map[string]string) ([]string, []string, error) { | ||
if statusAnnotationJSON, ok := annotations[statusAnnotation]; ok { | ||
var ips, macs []string | ||
var networks []netStatItem | ||
err := json.Unmarshal([]byte(statusAnnotationJSON), &networks) | ||
if err == nil { | ||
for _, network := range networks { | ||
if len(network.IPs) > 0 { | ||
ips = append(ips, network.IPs...) | ||
} | ||
|
||
if len(network.MAC) > 0 { | ||
macs = append(macs, strings.ToUpper(network.MAC)) | ||
} | ||
} | ||
return ips, macs, nil | ||
} | ||
|
||
return nil, nil, fmt.Errorf("cannot read annotation %s: %w", statusAnnotation, err) | ||
} | ||
// Annotation not present => just ignore, no error | ||
return nil, nil, nil | ||
} |
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,50 @@ | ||
package cni | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExtractNetStatusIPs(t *testing.T) { | ||
// Annotation not found => no error, no ip | ||
ip, mac, err := extractNetStatusIPsAndMACs(map[string]string{}) | ||
require.NoError(t, err) | ||
require.Empty(t, ip) | ||
require.Empty(t, mac) | ||
|
||
// Annotation malformed => error, no ip | ||
ip, mac, err = extractNetStatusIPsAndMACs(map[string]string{ | ||
statusAnnotation: "whatever", | ||
}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "cannot read annotation") | ||
require.Empty(t, ip) | ||
require.Empty(t, mac) | ||
|
||
// Valid annotation => no error, ip | ||
ip, mac, err = extractNetStatusIPsAndMACs(map[string]string{ | ||
statusAnnotation: ` | ||
[{ | ||
"name": "cbr0", | ||
"ips": [ | ||
"10.244.1.73" | ||
], | ||
"default": true, | ||
"dns": {} | ||
},{ | ||
"name": "macvlan-conf", | ||
"interface": "net1", | ||
"ips": [ | ||
"192.168.1.205" | ||
], | ||
"mac": "86:1d:96:ff:55:0d", | ||
"dns": {} | ||
}] | ||
`, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, []string{"10.244.1.73", "192.168.1.205"}, ip) | ||
require.Equal(t, []string{"86:1D:96:FF:55:0D"}, mac) | ||
|
||
} |
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.