-
Notifications
You must be signed in to change notification settings - Fork 362
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
Look up Pods by name to fetch labels in Flow Aggregator #4942
Conversation
pkg/flowaggregator/flowaggregator.go
Outdated
@@ -482,18 +485,11 @@ func (fa *flowAggregator) fillK8sMetadata(key ipfixintermediate.FlowKey, record | |||
} | |||
} | |||
|
|||
func (fa *flowAggregator) fetchPodLabels(podAddress string) string { | |||
pods, err := fa.podInformer.Informer().GetIndexer().ByIndex(podInfoIndex, podAddress) | |||
func (fa *flowAggregator) fetchPodLabels(podName string, podNamespace string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: make namespace come first in parameter list
pkg/flowaggregator/flowaggregator.go
Outdated
func (fa *flowAggregator) fetchPodLabels(podAddress string) string { | ||
pods, err := fa.podInformer.Informer().GetIndexer().ByIndex(podInfoIndex, podAddress) | ||
func (fa *flowAggregator) fetchPodLabels(podName string, podNamespace string) string { | ||
pod, err := fa.podLister.Pods(podNamespace).Get(podName) | ||
if err != nil { | ||
klog.Warning(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the functions you update, could you make sure all the klog
calls are changed to use structured logging
In this case, use klog.InfoS
or klog.ErrorS
as you deem appropriate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, updated.
pkg/flowaggregator/flowaggregator.go
Outdated
podLabelString = fa.fetchPodLabels(key.DestinationAddress) | ||
|
||
destinationPodLabelString := "" | ||
if destinationPodName, _, exist := record.GetInfoElementWithValue("destinationPodName"); exist { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's clean up the code to avoid duplication between source & destination Pods (using a helper function, and maybe with an early return if some field is missing, to avoid too much if
nesting)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I add a new parameter side
to the fillPodLabels
to avoid introducing a new helper function. Please let me know if you think a separate function looks better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit message:
we change to lookup Pods by name and namespace
lookup -> looking up
Namespace
ad0d047
to
babc0c7
Compare
Thanks, updated. |
babc0c7
to
8b22b91
Compare
8b22b91
to
d548e3e
Compare
pkg/flowaggregator/flowaggregator.go
Outdated
if err != nil { | ||
klog.Warningf("Add sourcePodLabels InfoElementWithValue failed: %v", err) | ||
} | ||
func (fa *flowAggregator) fillPodLabels(record ipfixentities.Record, side string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, it would be better to keep func (fa *flowAggregator) fillPodLabels(record ipfixentities.Record)
and introduce func (fa *flowAggregator) fillPodLabelsForSide(record ipfixentities.Record, podNamespaceIE, podNameIE, podLabelsIE string) error
, so that the code looks like this:
func (fa *flowAggregator) fillPodLabels(record ipfixentities.Record) {
if err := fillPodLabelsForSide(record, "sourcePodNamespace", "sourcePodName", "sourcePodLabels"); err != nil {
klog.ErrorS(err, "Error when filling pod labels", "side", "source")
}
if err := fillPodLabelsForSide(record, "destinationPodNamespace", "destinationPodName", "destinationPodLabels"); err != nil {
klog.ErrorS(err, "Error when filling pod labels", "side", "destination")
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it looks better in this way, thanks for the advice!
d548e3e
to
938f4ff
Compare
pkg/flowaggregator/flowaggregator.go
Outdated
if podName, _, exist := record.GetInfoElementWithValue(podNameIEName); exist { | ||
podNameString := podName.GetStringValue() | ||
if podNamespace, _, exist := record.GetInfoElementWithValue(podNamespaceIEName); exist { | ||
podNamespaceString := podNamespace.GetStringValue() | ||
if podNameString != "" && podNamespaceString != "" { | ||
podLabelsString = fa.fetchPodLabels(podNamespaceString, podNameString) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the Go convention is to use ok
(instead of exist
) for that type of variable. The convention is followed in pkg/flowaggregator/flowrecord/record.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks.
Currently Flow Aggregator looks up Pods by IP address to fetch labels, it is very possible to obtain incorrect Pods when Pod turnover quickly. In this commit, we change to looking up Pods by Pod Name and Namespace to fetch labels in Flow Aggregator. Signed-off-by: Yongming Ding <dyongming@vmware.com>
938f4ff
to
a1949bf
Compare
/test-all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/test-conformance |
Currently Flow Aggregator looks up Pods by IP address to fetch labels, it is very possible to obtain incorrect Pods when Pod turnover is high. In this commit, we instead to look up Pods by Pod Name and Namespace to fetch labels in Flow Aggregator. Signed-off-by: Yongming Ding <dyongming@vmware.com>
Currently Flow Aggregator looks up Pods by IP address to fetch labels, it is very possible to obtain incorrect Pods when Pod turnover quickly.
In this commit, we change to looking up Pods by Pod Name and Namespace to fetch labels in Flow Aggregator.