Skip to content

Commit 432c1d0

Browse files
[POA-3003] Handle deployment tags for daemonSet's apidump processes (#86)
Changes in this PR: * Changes in the deployment package's functions to accept the envVars map as the input * Use these functions while creating podArgs object in the kube run command * Add check in the apidump process to not call deployment.UpdateTags() function again if the process is started by the parent `kube run` command **Note:** Here, for `hostname`, we expect the `HOSTNAME` environment variable to be set, but it is required. It is set for our pods in Postman, but it is not set in a pod running in Minikube locally.
1 parent 7f7caa9 commit 432c1d0

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

apidump/apidump.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type DaemonsetArgs struct {
7777
StopChan <-chan error `json:"-"`
7878
APIKey string
7979
Environment string
80+
TraceTags tags.SingletonTags
8081
}
8182

8283
type Args struct {
@@ -374,19 +375,27 @@ func collectTraceTags(args *Args) map[tags.Key]string {
374375
// Set legacy deployment tags.
375376
traceTags[tags.XAkitaDeployment] = apispec.DefaultDeployment
376377
traceTags[tags.XAkitaSource] = tags.DeploymentSource
377-
deployment.UpdateTags(traceTags)
378+
379+
// Set tags from the daemonSet apidump process
380+
// Here we expect that the parent go routine has already set the tags
381+
// if DaemonsetArgs is set.
382+
if dArgs, exists := args.DaemonsetArgs.Get(); exists {
383+
for k, v := range dArgs.TraceTags {
384+
traceTags[k] = v
385+
}
386+
} else {
387+
deployment.UpdateTags(traceTags)
388+
hostname, err := os.Hostname()
389+
if err == nil {
390+
traceTags[tags.XInsightsHostname] = hostname
391+
}
392+
}
378393

379394
// Set source to user by default (if not CI or deployment)
380395
if _, ok := traceTags[tags.XAkitaSource]; !ok {
381396
traceTags[tags.XAkitaSource] = tags.UserSource
382397
}
383398

384-
// Set hostname tag
385-
hostname, err := os.Hostname()
386-
if err == nil {
387-
traceTags[tags.XInsightsHostname] = hostname
388-
}
389-
390399
printer.Debugln("trace tags:", traceTags)
391400
return traceTags
392401
}

cmd/internal/kube/daemonset/apidump_process.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (d *Daemonset) StartApiDumpProcess(podUID types.UID) error {
8787
StopChan: podArgs.StopChan,
8888
APIKey: podArgs.PodCreds.InsightsAPIKey,
8989
Environment: podArgs.PodCreds.InsightsEnvironment,
90+
TraceTags: podArgs.TraceTags,
9091
}),
9192
}
9293

cmd/internal/kube/daemonset/kube_events_worker.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package daemonset
33
import (
44
"github.com/akitasoftware/akita-libs/akid"
55
"github.com/pkg/errors"
6+
"github.com/postmanlabs/postman-insights-agent/deployment"
67
"github.com/postmanlabs/postman-insights-agent/printer"
78
coreV1 "k8s.io/api/core/v1"
89
)
@@ -179,6 +180,9 @@ func (d *Daemonset) inspectPodForEnvVars(pod coreV1.Pod, podArgs *PodArgs) error
179180
return requiredEnvVarMissingErr
180181
}
181182

183+
// Set the trace tags for apidump process from the pod info
184+
deployment.SetK8sTraceTags(pod, podArgs.TraceTags)
185+
182186
podArgs.ContainerUUID = containerUUID
183187
podArgs.InsightsProjectID = insightsProjectID
184188
podArgs.PodCreds = PodCreds{

cmd/internal/kube/daemonset/pod_args.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77

88
"github.com/akitasoftware/akita-libs/akid"
9+
"github.com/akitasoftware/akita-libs/tags"
910
"github.com/pkg/errors"
1011
)
1112

@@ -40,6 +41,7 @@ type PodCreds struct {
4041
type PodArgs struct {
4142
// apidump related fields
4243
InsightsProjectID akid.ServiceID
44+
TraceTags tags.SingletonTags
4345

4446
// Pod related fields
4547
PodName string
@@ -48,15 +50,16 @@ type PodArgs struct {
4850

4951
// for state management
5052
PodTrafficMonitorState PodTrafficMonitorState
51-
StateChangeMutex sync.Mutex
53+
StateChangeMutex sync.Mutex `json:"-"`
5254

5355
// send stop signal to apidump process
54-
StopChan chan error
56+
StopChan chan error `json:"-"`
5557
}
5658

5759
func NewPodArgs(podName string) *PodArgs {
5860
return &PodArgs{
59-
PodName: podName,
61+
TraceTags: tags.SingletonTags{},
62+
PodName: podName,
6063
// though 1 buffer size is enough, keeping 2 for safety
6164
StopChan: make(chan error, 2),
6265
}

deployment/deployment.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/akitasoftware/akita-libs/tags"
77
"github.com/postmanlabs/postman-insights-agent/printer"
8+
coreV1 "k8s.io/api/core/v1"
89
)
910

1011
// Internal type of the deployment, automatically discovered.
@@ -106,3 +107,14 @@ func UpdateTags(argsTags map[tags.Key]string) {
106107
argsTags[k] = v
107108
}
108109
}
110+
111+
// SetK8sTraceTags sets Kubernetes-specific tags in the trace tags.
112+
// This is used by daemonset to set tags for traces from a specific pod.
113+
func SetK8sTraceTags(pod coreV1.Pod, traceTags tags.SingletonTags) {
114+
traceTags[tags.XAkitaKubernetesNamespace] = pod.Namespace
115+
traceTags[tags.XAkitaKubernetesNode] = pod.Spec.NodeName
116+
traceTags[tags.XAkitaKubernetesPod] = pod.Name
117+
traceTags[tags.XAkitaKubernetesPodIP] = pod.Status.PodIP
118+
traceTags[tags.XAkitaKubernetesHostIP] = pod.Status.HostIP
119+
traceTags[tags.XInsightsHostname] = pod.Name
120+
}

0 commit comments

Comments
 (0)