Skip to content

Commit

Permalink
Merge pull request #131 from dejanzele/feat/metadata-labels-annotations
Browse files Browse the repository at this point in the history
feat: add support for GetLabels and GetAnnotations in metadata
  • Loading branch information
k8s-ci-robot authored Nov 13, 2024
2 parents b1ba800 + 37184ee commit 05231fc
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/advanced/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ func (t testPod) GetResourceVersion() string {
return "v1"
}

func (t testPod) GetLabels() map[string]string {
return map[string]string{}
}

func (t testPod) GetAnnotations() map[string]string {
return map[string]string{}
}

func (t testPod) Spec() *protoapi.PodSpec {
nodeName := t.nodeName
return &protoapi.PodSpec{NodeName: &nodeName}
Expand Down
8 changes: 8 additions & 0 deletions examples/nodenumber/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ func (t testPod) GetResourceVersion() string {
return "v1"
}

func (t testPod) GetLabels() map[string]string {
return map[string]string{}
}

func (t testPod) GetAnnotations() map[string]string {
return map[string]string{}
}

func (t testPod) Spec() *protoapi.PodSpec {
nodeName := t.nodeName
return &protoapi.PodSpec{NodeName: &nodeName}
Expand Down
2 changes: 2 additions & 0 deletions guest/api/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Metadata interface {
GetName() string
GetNamespace() string
GetResourceVersion() string
GetLabels() map[string]string
GetAnnotations() map[string]string
}

type Node interface {
Expand Down
8 changes: 8 additions & 0 deletions guest/handle/eventrecorder/internal/eventrecorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ func (podSmall) GetKind() string {
func (podSmall) GetResourceVersion() string {
return "v1"
}

func (podSmall) GetLabels() map[string]string {
return map[string]string{}
}

func (podSmall) GetAnnotations() map[string]string {
return map[string]string{}
}
16 changes: 16 additions & 0 deletions guest/internal/prefilter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func (pod) GetApiVersion() string {
return "v1"
}

func (pod) GetLabels() map[string]string {
return internalproto.GetLabels(lazyPod())
}

func (pod) GetAnnotations() map[string]string {
return internalproto.GetAnnotations(lazyPod())
}

func (pod) Spec() *protoapi.PodSpec {
return lazyPod().Spec
}
Expand Down Expand Up @@ -185,6 +193,14 @@ func (n *nodeInfo) GetResourceVersion() string {
return n.lazyNode().GetResourceVersion()
}

func (n *nodeInfo) GetLabels() map[string]string {
return n.lazyNode().GetLabels()
}

func (n *nodeInfo) GetAnnotations() map[string]string {
return n.lazyNode().GetAnnotations()
}

func (n *nodeInfo) Node() proto.Node {
return n.lazyNode()
}
Expand Down
30 changes: 30 additions & 0 deletions guest/internal/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,34 @@ func GetResourceVersion[O object](o O) string {
return ""
}

func GetLabels[O object](o O) map[string]string {
if md := o.GetMetadata(); md != nil {
return md.Labels
}
return nil
}

func GetAnnotations[O object](o O) map[string]string {
if md := o.GetMetadata(); md != nil {
return md.Annotations
}
return nil
}

var _ proto.Node = (*Node)(nil)

type Node struct {
Msg *protoapi.Node
}

func (o *Node) GetLabels() map[string]string {
return GetLabels(o.Msg)
}

func (o *Node) GetAnnotations() map[string]string {
return GetAnnotations(o.Msg)
}

func (o *Node) GetName() string {
return GetName(o.Msg)
}
Expand Down Expand Up @@ -98,6 +120,14 @@ type Pod struct {
Msg *protoapi.Pod
}

func (o *Pod) GetLabels() map[string]string {
return GetLabels(o.Msg)
}

func (o *Pod) GetAnnotations() map[string]string {
return GetAnnotations(o.Msg)
}

func (o *Pod) GetName() string {
return GetName(o.Msg)
}
Expand Down
17 changes: 17 additions & 0 deletions guest/klog/api/k8s_references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var _ proto.Metadata = &testMetadata{}

type testMetadata struct {
Name, Namespace, UID, ResourceVersion string
Labels, Annotations map[string]string
}

func (t *testMetadata) GetName() string {
Expand All @@ -46,6 +47,14 @@ func (t *testMetadata) GetResourceVersion() string {
return t.ResourceVersion
}

func (t *testMetadata) GetLabels() map[string]string {
return t.Labels
}

func (t *testMetadata) GetAnnotations() map[string]string {
return t.Annotations
}

var _ proto.Metadata = &panicMetadata{}

type panicMetadata struct {
Expand All @@ -68,6 +77,14 @@ func (panicMetadata) GetResourceVersion() string {
panic("unexpected")
}

func (panicMetadata) GetLabels() map[string]string {
panic("unexpected")
}

func (panicMetadata) GetAnnotations() map[string]string {
panic("unexpected")
}

func TestKObj(t *testing.T) {
tests := []struct {
name string
Expand Down
8 changes: 8 additions & 0 deletions guest/klog/internal/klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,11 @@ func (podSmall) GetUid() string {
func (podSmall) GetResourceVersion() string {
return "resource-version"
}

func (podSmall) GetLabels() map[string]string {
return map[string]string{}
}

func (podSmall) GetAnnotations() map[string]string {
return map[string]string{}
}
8 changes: 8 additions & 0 deletions guest/prefilterextensions/prefilterextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ func (p *podInfo) GetUid() string {
return p.lazyPod().GetUid()
}

func (p *podInfo) GetLabels() map[string]string {
return p.lazyPod().GetLabels()
}

func (p *podInfo) GetAnnotations() map[string]string {
return p.lazyPod().GetAnnotations()
}

func (p *podInfo) Pod() proto.Pod {
return p.lazyPod()
}
Expand Down
9 changes: 9 additions & 0 deletions internal/e2e/guest/guest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var _ proto.Metadata = &testMetadata{}

type testMetadata struct {
Name, Namespace, UID, ResourceVersion string
Labels, Annotations map[string]string
}

func (t *testMetadata) GetName() string {
Expand All @@ -63,6 +64,14 @@ func (t *testMetadata) GetResourceVersion() string {
return t.ResourceVersion
}

func (t *testMetadata) GetLabels() map[string]string {
return t.Labels
}

func (t *testMetadata) GetAnnotations() map[string]string {
return t.Annotations
}

type stringerFunc func() string

func (s stringerFunc) String() string {
Expand Down

0 comments on commit 05231fc

Please sign in to comment.