Skip to content

Commit

Permalink
add workload name and workload kind to slim api and hubble api
Browse files Browse the repository at this point in the history
Signed-off-by: Sugang Li <[email protected]>
  • Loading branch information
sugangli authored and aanm committed Jul 2, 2021
1 parent 2641808 commit d1dcf95
Show file tree
Hide file tree
Showing 15 changed files with 1,381 additions and 851 deletions.
1,651 changes: 867 additions & 784 deletions api/v1/flow/flow.pb.go

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions api/v1/flow/flow.pb.json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions api/v1/flow/flow.proto
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ message Endpoint {
// labels in `foo=bar` format.
repeated string labels = 4;
string pod_name = 5;
repeated Workload workloads = 6;
}

message Workload {
string name = 1;
string kind = 2;
}

message TCP {
Expand Down
1 change: 1 addition & 0 deletions api/v1/observer/observer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/hubble/api/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v1

import (
"github.com/cilium/cilium/pkg/identity"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
)

// EndpointInfo defines readable fields of a Cilium endpoint.
Expand All @@ -26,4 +27,5 @@ type EndpointInfo interface {
GetK8sPodName() string
GetK8sNamespace() string
GetLabels() []string
GetPod() *slim_corev1.Pod
}
10 changes: 9 additions & 1 deletion pkg/hubble/parser/threefour/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,21 @@ func (p *Parser) resolveEndpoint(ip net.IP, datapathSecurityIdentity uint32) *pb
if p.endpointGetter != nil {
if ep, ok := p.endpointGetter.GetEndpointInfo(ip); ok {
epIdentity := resolveIdentityConflict(ep.GetIdentity())
return &pb.Endpoint{
e := &pb.Endpoint{
ID: uint32(ep.GetID()),
Identity: epIdentity,
Namespace: ep.GetK8sNamespace(),
Labels: sortAndFilterLabels(p.log, ep.GetLabels(), epIdentity),
PodName: ep.GetK8sPodName(),
}
if pod := ep.GetPod(); pod != nil {
olen := len(pod.GetOwnerReferences())
e.Workloads = make([]*pb.Workload, olen)
for index, owner := range pod.GetOwnerReferences() {
e.Workloads[index] = &pb.Workload{Kind: owner.Kind, Name: owner.Name}
}
}
return e
}
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/hubble/parser/threefour/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"github.com/cilium/cilium/pkg/hubble/testutils"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/ipcache"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
slim_metav1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/meta/v1"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/monitor"
"github.com/cilium/cilium/pkg/monitor/api"
Expand Down Expand Up @@ -79,6 +81,16 @@ func TestL34Decode(t *testing.T) {
Identity: 5678,
PodName: "pod-10.16.236.178",
PodNamespace: "default",
Pod: &slim_corev1.Pod{
ObjectMeta: slim_metav1.ObjectMeta{
OwnerReferences: []slim_metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "pod",
},
},
},
},
}, true
}
return nil, false
Expand Down
7 changes: 7 additions & 0 deletions pkg/hubble/testutils/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
poolTypes "github.com/cilium/cilium/pkg/hubble/relay/pool/types"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/ipcache"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/policy"

Expand Down Expand Up @@ -419,6 +420,7 @@ type FakeEndpointInfo struct {
PodName string
PodNamespace string
Labels []string
Pod *slim_corev1.Pod

PolicyMap map[policy.Key]labels.LabelArrayList
PolicyRevision uint64
Expand Down Expand Up @@ -449,6 +451,11 @@ func (e *FakeEndpointInfo) GetLabels() []string {
return e.Labels
}

// GetPod return the pod object of the endpoint.
func (e *FakeEndpointInfo) GetPod() *slim_corev1.Pod {
return e.Pod
}

func (e *FakeEndpointInfo) GetRealizedPolicyRuleLabelsForKey(key policy.Key) (
derivedFrom labels.LabelArrayList,
revision uint64,
Expand Down
389 changes: 326 additions & 63 deletions pkg/k8s/slim/k8s/apis/meta/v1/generated.pb.go

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions pkg/k8s/slim/k8s/apis/meta/v1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions pkg/k8s/slim/k8s/apis/meta/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,31 @@ func SingleObject(meta ObjectMeta) metav1.ListOptions {
ResourceVersion: meta.ResourceVersion,
}
}

// FullOwnerReferences converts slim OwnerReferences to original OwnerReferences
func FullOwnerReferences(references []OwnerReference) []metav1.OwnerReference {

var fullRefs []metav1.OwnerReference
for _, ref := range references {
full := metav1.OwnerReference{
Name: ref.Name,
Kind: ref.Kind,
}
fullRefs = append(fullRefs, full)
}
return fullRefs
}

// SlimOwnerReferences converts original OwnerReferences to slim OwnerReferences
func SlimOwnerReferences(references []metav1.OwnerReference) []OwnerReference {

var slimRefs []OwnerReference
for _, ref := range references {
slim := OwnerReference{
Name: ref.Name,
Kind: ref.Kind,
}
slimRefs = append(slimRefs, slim)
}
return slimRefs
}
8 changes: 5 additions & 3 deletions pkg/k8s/slim/k8s/apis/meta/v1/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ func (meta *ObjectMeta) GetAnnotations() map[string]string { return m
func (meta *ObjectMeta) SetAnnotations(annotations map[string]string) { meta.Annotations = annotations }
func (meta *ObjectMeta) GetFinalizers() []string { panic("not implemented") }
func (meta *ObjectMeta) SetFinalizers(_ []string) { panic("not implemented") }
func (meta *ObjectMeta) GetOwnerReferences() []metav1.OwnerReference { panic("not implemented") }
func (meta *ObjectMeta) SetOwnerReferences(_ []metav1.OwnerReference) {
panic("not implemented")
func (meta *ObjectMeta) GetOwnerReferences() []metav1.OwnerReference {
return FullOwnerReferences(meta.OwnerReferences)
}
func (meta *ObjectMeta) SetOwnerReferences(references []metav1.OwnerReference) {
meta.OwnerReferences = SlimOwnerReferences(references)
}
func (meta *ObjectMeta) GetClusterName() string { panic("not implemented") }
func (meta *ObjectMeta) SetClusterName(_ string) { panic("not implemented") }
Expand Down
22 changes: 22 additions & 0 deletions pkg/k8s/slim/k8s/apis/meta/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ type ObjectMeta struct {
// More info: http://kubernetes.io/docs/user-guide/annotations
// +optional
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`

// List of objects depended by this object. If ALL objects in the list have
// been deleted, this object will be garbage collected. If this object is managed by a controller,
// then an entry in this list will point to this controller, with the controller field set to true.
// There cannot be more than one managing controller.
// +optional
// +patchMergeKey=uid
// +patchStrategy=merge
OwnerReferences []OwnerReference `json:"ownerReferences,omitempty" patchStrategy:"merge" patchMergeKey:"uid" protobuf:"bytes,13,rep,name=ownerReferences"`
}

const (
Expand All @@ -159,6 +168,19 @@ const (
NamespacePublic = "kube-public"
)

// OwnerReference contains enough information to let you identify an owning
// object. An owning object must be in the same namespace as the dependent, or
// be cluster-scoped, so there is no namespace field.
// +structType=atomic
type OwnerReference struct {
// Kind of the referent.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
Kind string `json:"kind" protobuf:"bytes,1,opt,name=kind"`
// Name of the referent.
// More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name" protobuf:"bytes,3,opt,name=name"`
}

// Note:
// There are two different styles of label selectors used in versioned types:
// an older style which is represented as just a string in versioned types, and a
Expand Down
21 changes: 21 additions & 0 deletions pkg/k8s/slim/k8s/apis/meta/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions pkg/k8s/slim/k8s/apis/meta/v1/zz_generated.deepequal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1dcf95

Please sign in to comment.