Skip to content
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

delete pkg/hubble/ipcache and GetIPIdentity func from ipcache #11652

Merged
merged 1 commit into from May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 0 additions & 26 deletions pkg/hubble/ipcache/ipcache.go

This file was deleted.

9 changes: 6 additions & 3 deletions pkg/hubble/parser/getters/getters.go
Expand Up @@ -20,7 +20,7 @@ import (
flowpb "github.com/cilium/cilium/api/v1/flow"
"github.com/cilium/cilium/api/v1/models"
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/cilium/pkg/hubble/ipcache"
"github.com/cilium/cilium/pkg/ipcache"
)

// DNSGetter ...
Expand All @@ -44,8 +44,11 @@ type IdentityGetter interface {

// IPGetter fetches per-IP metadata
type IPGetter interface {
// GetIPIdentity fetches information known about a remote IP.
GetIPIdentity(ip net.IP) (identity ipcache.IPIdentity, ok bool)
// GetK8sMetadata returns Kubernetes metadata for the given IP address.
GetK8sMetadata(ip string) *ipcache.K8sMetadata
// LookupByIP returns the corresponding security identity that endpoint IP
// maps to as well as if the corresponding entry exists.
LookupByIP(ip string) (ipcache.Identity, bool)
}

// ServiceGetter fetches service metadata.
Expand Down
8 changes: 4 additions & 4 deletions pkg/hubble/parser/seven/parser.go
Expand Up @@ -121,11 +121,11 @@ func (p *Parser) Decode(payload *pb.Payload, decoded *pb.Flow) error {
destinationNames = p.dnsGetter.GetNamesOf(uint32(sourceEndpoint.ID), destinationIP)
}
if p.ipGetter != nil {
if id, ok := p.ipGetter.GetIPIdentity(sourceIP); ok {
sourceNamespace, sourcePod = id.Namespace, id.PodName
if meta := p.ipGetter.GetK8sMetadata(sourceIP.String()); meta != nil {
sourceNamespace, sourcePod = meta.Namespace, meta.PodName
}
if id, ok := p.ipGetter.GetIPIdentity(destinationIP); ok {
destinationNamespace, destinationPod = id.Namespace, id.PodName
if meta := p.ipGetter.GetK8sMetadata(destinationIP.String()); meta != nil {
destinationNamespace, destinationPod = meta.Namespace, meta.PodName
}
}

Expand Down
14 changes: 6 additions & 8 deletions pkg/hubble/parser/seven/parser_test.go
Expand Up @@ -28,9 +28,8 @@ import (
"time"

pb "github.com/cilium/cilium/api/v1/flow"
"github.com/cilium/cilium/pkg/hubble/ipcache"
"github.com/cilium/cilium/pkg/hubble/testutils"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/proxy/accesslog"
"github.com/cilium/cilium/pkg/u8proto"
Expand Down Expand Up @@ -124,15 +123,14 @@ func TestDecodeL7HTTPRecord(t *testing.T) {
},
}
IPGetter := &testutils.FakeIPGetter{
OnGetIPIdentity: func(ip net.IP) (id ipcache.IPIdentity, ok bool) {
if ip.Equal(net.ParseIP(fakeDestinationEndpoint.IPv4)) {
return ipcache.IPIdentity{
Identity: identity.NumericIdentity(fakeDestinationEndpoint.Identity),
OnGetK8sMetadata: func(ip string) *ipcache.K8sMetadata {
if ip == fakeDestinationEndpoint.IPv4 {
return &ipcache.K8sMetadata{
Namespace: "default",
PodName: "pod-1234",
}, true
}
}
return
return nil
},
}
serviceGetter := &testutils.FakeServiceGetter{
Expand Down
8 changes: 5 additions & 3 deletions pkg/hubble/parser/threefour/parser.go
Expand Up @@ -255,9 +255,11 @@ func (p *Parser) resolveEndpoint(ip net.IP, securityIdentity uint32) *pb.Endpoin
// for remote endpoints, assemble the information via ip and identity
var namespace, podName string
if p.ipGetter != nil {
if ipIdentity, ok := p.ipGetter.GetIPIdentity(ip); ok {
securityIdentity = uint32(ipIdentity.Identity)
namespace, podName = ipIdentity.Namespace, ipIdentity.PodName
if ipIdentity, ok := p.ipGetter.LookupByIP(ip.String()); ok {
securityIdentity = uint32(ipIdentity.ID)
}
if meta := p.ipGetter.GetK8sMetadata(ip.String()); meta != nil {
namespace, podName = meta.Namespace, meta.PodName
}
}
var labels []string
Expand Down
23 changes: 18 additions & 5 deletions pkg/hubble/parser/threefour/parser_test.go
Expand Up @@ -29,11 +29,12 @@ import (
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/byteorder"
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/cilium/pkg/hubble/ipcache"
"github.com/cilium/cilium/pkg/hubble/testutils"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/monitor"
"github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/source"

"github.com/golang/protobuf/ptypes/timestamp"
"github.com/google/gopacket"
Expand Down Expand Up @@ -85,12 +86,24 @@ func TestL34Decode(t *testing.T) {
},
}
ipGetter := &testutils.FakeIPGetter{
OnGetIPIdentity: func(ip net.IP) (identity ipcache.IPIdentity, ok bool) {
OnGetK8sMetadata: func(ip string) *ipcache.K8sMetadata {
if ip == "192.168.33.11" {
return &ipcache.K8sMetadata{
Namespace: "remote",
PodName: "pod-192.168.33.11",
}
}
return nil
},
OnLookupByIP: func(ip string) (ipcache.Identity, bool) {
// pretend IP belongs to a pod on a remote node
if ip.Equal(net.ParseIP("192.168.33.11")) {
return ipcache.IPIdentity{Namespace: "remote", PodName: "pod-192.168.33.11"}, true
if ip == "192.168.33.11" {
return ipcache.Identity{
ID: 1234,
Source: source.Unspec,
}, true
}
return
return ipcache.Identity{}, false
},
}
serviceGetter := &testutils.FakeServiceGetter{
Expand Down
30 changes: 21 additions & 9 deletions pkg/hubble/testutils/fake.go
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/cilium/cilium/api/v1/models"
observerpb "github.com/cilium/cilium/api/v1/observer"
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/cilium/pkg/hubble/ipcache"
"github.com/cilium/cilium/pkg/ipcache"

"github.com/golang/protobuf/ptypes/timestamp"
)
Expand Down Expand Up @@ -103,21 +103,33 @@ var NoopEndpointGetter = FakeEndpointGetter{

// FakeIPGetter is used for unit tests that needs IPGetter.
type FakeIPGetter struct {
OnGetIPIdentity func(ip net.IP) (id ipcache.IPIdentity, ok bool)
OnGetK8sMetadata func(ip string) *ipcache.K8sMetadata
OnLookupByIP func(ip string) (ipcache.Identity, bool)
}

// GetIPIdentity implements FakeIPGetter.GetIPIdentity.
func (f *FakeIPGetter) GetIPIdentity(ip net.IP) (id ipcache.IPIdentity, ok bool) {
if f.OnGetIPIdentity != nil {
return f.OnGetIPIdentity(ip)
// GetK8sMetadata implements FakeIPGetter.GetK8sMetadata.
func (f *FakeIPGetter) GetK8sMetadata(ip string) *ipcache.K8sMetadata {
if f.OnGetK8sMetadata != nil {
return f.OnGetK8sMetadata(ip)
}
panic("OnGetIPIdentity not set")
panic("OnGetK8sMetadata not set")
}

// LookupByIP implements FakeIPGetter.LookupByIP.
func (f *FakeIPGetter) LookupByIP(ip string) (ipcache.Identity, bool) {
if f.OnLookupByIP != nil {
return f.OnLookupByIP(ip)
}
panic("OnLookupByIP not set")
}

// NoopIPGetter always returns an empty response.
var NoopIPGetter = FakeIPGetter{
OnGetIPIdentity: func(ip net.IP) (id ipcache.IPIdentity, ok bool) {
return ipcache.IPIdentity{}, false
OnGetK8sMetadata: func(ip string) *ipcache.K8sMetadata {
return nil
},
OnLookupByIP: func(ip string) (ipcache.Identity, bool) {
return ipcache.Identity{}, false
},
}

Expand Down
24 changes: 0 additions & 24 deletions pkg/ipcache/ipcache.go
Expand Up @@ -17,7 +17,6 @@ package ipcache
import (
"net"

hubbleIPCache "github.com/cilium/cilium/pkg/hubble/ipcache"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
Expand Down Expand Up @@ -558,26 +557,3 @@ func (m *K8sMetadata) Equal(o *K8sMetadata) bool {
}
return m.Namespace == o.Namespace && m.PodName == o.PodName
}

// GetIPIdentity returns the IP identity of the given IP address. Hubble uses this function to populate
// fields like namespace and pod name for remote endpoints. If the K8s metadata is unavailable, it sets
// the Identity field for the IP identity.
//
// - IPGetter: https://github.com/cilium/hubble/blob/04ab72591faca62a305ce0715108876167182e04/pkg/parser/getters/getters.go#L46
func (ipc *IPCache) GetIPIdentity(ip net.IP) (hubbleIPCache.IPIdentity, bool) {
ipIdentity, ok := ipc.LookupByIP(ip.String())
if !ok {
return hubbleIPCache.IPIdentity{}, false
}
meta := ipc.GetK8sMetadata(ip.String())
if meta == nil {
return hubbleIPCache.IPIdentity{
Identity: ipIdentity.ID,
}, true
}
return hubbleIPCache.IPIdentity{
Identity: ipIdentity.ID,
Namespace: meta.Namespace,
PodName: meta.PodName,
}, true
}