From 441fa0bf9816d280444e03622f3004148de27929 Mon Sep 17 00:00:00 2001 From: Mrproliu <741550557@qq.com> Date: Wed, 23 Aug 2023 12:16:20 +0800 Subject: [PATCH 1/2] Adding `LabelValueWithDefault` function to obtain information from pod --- CHANGES.md | 1 + .../process_discovery/kubernetes.md | 77 ++++++++++--------- pkg/process/finders/kubernetes/template.go | 11 +++ 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e66497d6..a5d7237c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ Release Notes. ------------------ #### Features * Enhance compatibility when profiling with SSL. +* Adding `LabelValueWithDefault` function to obtain information from pod. #### Bug Fixes diff --git a/docs/en/setup/configuration/process_discovery/kubernetes.md b/docs/en/setup/configuration/process_discovery/kubernetes.md index 87fd93a6..e8358917 100644 --- a/docs/en/setup/configuration/process_discovery/kubernetes.md +++ b/docs/en/setup/configuration/process_discovery/kubernetes.md @@ -6,18 +6,18 @@ After finding the process, it would collect the metadata of the process when the ## Configuration -| Name | Default | Environment Key | Description | -|------|---------|-----------------|-------------| -| process_discovery.kubernetes.active | false | ROVER_PROCESS_DISCOVERY_KUBERNETES_ACTIVE | Is active the kubernetes process discovery. | -| process_discovery.kubernetes.node_name | | ROVER_PROCESS_DISCOVERY_KUBERNETES_NODE_NAME | Current deployed node name, it could be inject by `spec.nodeName`. | -| process_discovery.kubernetes.namespaces | | ROVER_PROCESS_DISCOVERY_KUBERNETES_NAMESPACES | Including pod by namespaces, if empty means including all namespaces. Multiple namespaces split by ",". | -| process_discovery.kubernetes.analyzers | | | Declare how to build the process. The istio and k8s resources are active by default. | -| process_discovery.kubernetes.analyzers.active | | | Set is active analyzer. | -| process_discovery.kubernetes.analyzers.filters | | | Define which process is match to current process builder. | -| process_discovery.kubernetes.analyzers.service_name | | | The Service Name of the process entity. | -| process_discovery.kubernetes.analyzers.instance_name | | | The Service Instance Name of the process entity, by default, the instance name is the host IP v4 address from "en0" net interface. | -| process_discovery.kubernetes.analyzers.process_name | | | The Process Name of the process entity, by default, the process name is the executable name of the process. | -| process_discovery.kubernetes.analyzers.labels | | | The Process Labels, used to aggregate similar process from service entity. Multiple labels split by ",". | +| Name | Default | Environment Key | Description | +|------------------------------------------------------|---------|-----------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------| +| process_discovery.kubernetes.active | false | ROVER_PROCESS_DISCOVERY_KUBERNETES_ACTIVE | Is active the kubernetes process discovery. | +| process_discovery.kubernetes.node_name | | ROVER_PROCESS_DISCOVERY_KUBERNETES_NODE_NAME | Current deployed node name, it could be inject by `spec.nodeName`. | +| process_discovery.kubernetes.namespaces | | ROVER_PROCESS_DISCOVERY_KUBERNETES_NAMESPACES | Including pod by namespaces, if empty means including all namespaces. Multiple namespaces split by ",". | +| process_discovery.kubernetes.analyzers | | | Declare how to build the process. The istio and k8s resources are active by default. | +| process_discovery.kubernetes.analyzers.active | | | Set is active analyzer. | +| process_discovery.kubernetes.analyzers.filters | | | Define which process is match to current process builder. | +| process_discovery.kubernetes.analyzers.service_name | | | The Service Name of the process entity. | +| process_discovery.kubernetes.analyzers.instance_name | | | The Service Instance Name of the process entity, by default, the instance name is the host IP v4 address from "en0" net interface. | +| process_discovery.kubernetes.analyzers.process_name | | | The Process Name of the process entity, by default, the process name is the executable name of the process. | +| process_discovery.kubernetes.analyzers.labels | | | The Process Labels, used to aggregate similar process from service entity. Multiple labels split by ",". | ## Process Analyze @@ -39,24 +39,24 @@ Is the same with the [process context in scanner](./scanner.md#process), but doe Provide current pod information and judgments. -| Name | Argument | Example | Description | -|------|--------- |-----------|-------------| -| Name | None | `eq .Pod.Name "test-pod-name"` | The name of the current pod. The example shows the pod name is equal to `test-pod-name`. | -| Namespace | None | `eq .Pod.Namespace "test-namesapce"` | The name of the current pod namespace. The example shows the pod namespace name is equal to `test-namespace`. | -| Node | None | `eq .Pod.Node "test-node"` | The name of the node deployed. The example shows the pod node name is equal to `test-node`. | -| LabelValue | KeyNames | `eq .Pod.LavelValue "a,b" "v"` | The label value of the label keys, If provide multiple keys, if any key has value, then don't need to get other values. The example shows the pod has anyone `a` or `b` label key, and the value matches to `v`. | -| ServiceName | None | `eq .Pod.ServiceName "test-service"` | The service name of the pod. The example shows current pods matched service name is `test-service`. | -| HasContainer | Container name | `.Pod.HasContainer "istio-proxy"` | The pod has the appointed container name. | -| LabelSelector | selector | `.Pod.LabelSelector` | The pod is matches the label selector. For more details, please read the [official documentation](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors). | -| HasServiceName | None | `.Pod.HasServiceName` | The pod has the matched service. | +| Name | Argument | Example | Description | +|----------------|----------------|--------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Name | None | `eq .Pod.Name "test-pod-name"` | The name of the current pod. The example shows the pod name is equal to `test-pod-name`. | +| Namespace | None | `eq .Pod.Namespace "test-namesapce"` | The name of the current pod namespace. The example shows the pod namespace name is equal to `test-namespace`. | +| Node | None | `eq .Pod.Node "test-node"` | The name of the node deployed. The example shows the pod node name is equal to `test-node`. | +| LabelValue | KeyNames | `eq .Pod.LavelValue "a,b" "v"` | The label value of the label keys, If provide multiple keys, if any key has value, then don't need to get other values. The example shows the pod has anyone `a` or `b` label key, and the value matches to `v`. | +| ServiceName | None | `eq .Pod.ServiceName "test-service"` | The service name of the pod. The example shows current pods matched service name is `test-service`. | +| HasContainer | Container name | `.Pod.HasContainer "istio-proxy"` | The pod has the appointed container name. | +| LabelSelector | selector | `.Pod.LabelSelector` | The pod is matches the label selector. For more details, please read the [official documentation](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors). | +| HasServiceName | None | `.Pod.HasServiceName` | The pod has the matched service. | #### Container Context Provide current container(under the pod) information. -| Name | Argument | Example | Description | -|------|--------- |-----------|-------------| -| Name | None | `eq .Container.Name "istio-proxy"`| The name of the current container under the pod. The examples show the container name is equal to `istio-proxy`. | +| Name | Argument | Example | Description | +|-------|----------|-------------------------------------|------------------------------------------------------------------------------------------------------------------| +| Name | None | `eq .Container.Name "istio-proxy"` | The name of the current container under the pod. The examples show the container name is equal to `istio-proxy`. | ### Entity The entity including `layer`, `serviceName`, `instanceName`, `processName` and `labels` properties. @@ -75,21 +75,22 @@ Same with the [process context in the scanner](./scanner.md#process). The information on the current pod. -| Name | Argument | Example | Description | -|------|--------- |-----------|-------------| -| Name | None | `{{.Pod.Name}}` | The name of current pod. | -| Namespace | None | `{{.Pod.Namespace}}` | The name of current pod namespace. | -| Node | None | `{{.Pod.Node}}` | The name of the node deployed. | -| LabelValue | KeyNames | `{{.Pod.LavelValue "a,b"}}` | The label value of the label keys, If provide multiple keys, if any key has value, then don't need to get other values. | -| ServiceName | None | `{{.Pod.ServiceName}}` | The service name of the pod. If the pod hasn't matched service, then return an empty string. | -| FindContainer | ContainerName | `{{.Pod.FindContainer "test"}}` | Find the Container context by container name. | +| Name | Argument | Example | Description | +|-----------------------|-------------------|--------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Name | None | `{{.Pod.Name}}` | The name of current pod. | +| Namespace | None | `{{.Pod.Namespace}}` | The name of current pod namespace. | +| Node | None | `{{.Pod.Node}}` | The name of the node deployed. | +| LabelValue | KeyNames | `{{.Pod.LavelValue "a,b"}}` | The label value of the label keys, If provide multiple keys, if any key has value, then don't need to get other values. | +| LabelValueWithDefault | KeyNames, Default | `{{.Pod.LavelValueWithDefault "a,b" "v"}}` | The label value of the label keys, If provide multiple keys, if any key has value, then don't need to get other values. If all keys don't have value, then return the default value. | +| ServiceName | None | `{{.Pod.ServiceName}}` | The service name of the pod. If the pod hasn't matched service, then return an empty string. | +| FindContainer | ContainerName | `{{.Pod.FindContainer "test"}}` | Find the Container context by container name. | #### Container The information of the current container under the pod. -| Name | Argument | Example | Description | -|------|--------- |-----------|-------------| -| Name | None | `{{.Container.Name}}`| The name of the current container under the pod. | -| ID | None | `{{.Container.ID}}`| The id of the current container under the pod. | -| EnvValue | KeyNames | `{{.Container.EnvValue "a,b"}}`| The environment value of the first non-value key in the provided candidates(Iterate from left to right). | \ No newline at end of file +| Name | Argument | Example | Description | +|----------|----------|-------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------| +| Name | None | `{{.Container.Name}}` The name of the current container under the pod. | | +| ID | None | `{{.Container.ID}}` | The id of the current container under the pod. | +| EnvValue | KeyNames | `{{.Container.EnvValue "a,b"}}` | The environment value of the first non-value key in the provided candidates(Iterate from left to right). | \ No newline at end of file diff --git a/pkg/process/finders/kubernetes/template.go b/pkg/process/finders/kubernetes/template.go index f77c4094..473d0010 100644 --- a/pkg/process/finders/kubernetes/template.go +++ b/pkg/process/finders/kubernetes/template.go @@ -122,6 +122,17 @@ func (p *TemplatePod) LabelValue(names string) (string, error) { return "", fmt.Errorf("could not found matches label, want names: %v, actual names: %v", namesArray, actualNames) } +func (p *TemplatePod) LabelValueWithDefault(names, def string) (string, error) { + namesArray := strings.Split(names, ",") + for _, name := range namesArray { + val := p.pc.Pod.Labels[name] + if val != "" { + return val, nil + } + } + return def, nil +} + func (p *TemplatePod) ServiceName() string { return p.pc.ServiceName() } From 674bd28c9d146e329a6f757a778b8a63213f658d Mon Sep 17 00:00:00 2001 From: Mrproliu <741550557@qq.com> Date: Wed, 23 Aug 2023 13:13:03 +0800 Subject: [PATCH 2/2] update function --- CHANGES.md | 2 +- configs/rover_configs.yaml | 4 ++-- .../process_discovery/kubernetes.md | 17 ++++++++--------- pkg/process/finders/kubernetes/template.go | 17 +---------------- .../task/network/base/rover_configs.yaml | 4 ++-- .../profiling/task/network/envoy/rover.yaml | 4 ++-- 6 files changed, 16 insertions(+), 32 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a5d7237c..dfb6eca8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,7 +6,7 @@ Release Notes. ------------------ #### Features * Enhance compatibility when profiling with SSL. -* Adding `LabelValueWithDefault` function to obtain information from pod. +* Update `LabelValue` obtain pod information function to add default value parameter. #### Bug Fixes diff --git a/configs/rover_configs.yaml b/configs/rover_configs.yaml index f9af6c9a..90132d53 100644 --- a/configs/rover_configs.yaml +++ b/configs/rover_configs.yaml @@ -83,7 +83,7 @@ process_discovery: - ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_HAS_ENVOY:.Pod.HasContainer "istio-proxy"} - ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_CONTAINER_IS_ENVOY:eq .Container.Name "istio-proxy"} layer: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_LAYER:MESH_DP} - service_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_SERVICE_NAME:{{.Pod.Namespace}}::{{.Pod.LabelValue "service.istio.io/canonical-name,app.kubernetes.io/name,app" }}} + service_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_SERVICE_NAME:{{.Pod.Namespace}}::{{.Pod.LabelValue "service.istio.io/canonical-name,app.kubernetes.io/name,app" ""}}} instance_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_INSTANCE_NAME:{{.Pod.Name}}} process_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_PROCESS_NAME:{{.Process.ExeName}}} labels: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_LABELS:mesh-envoy} @@ -92,7 +92,7 @@ process_discovery: - ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_HAS_ENVOY:.Pod.HasContainer "istio-proxy"} - ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_CONTAINER_NOT_ENVOY:ne .Container.Name "istio-proxy"} layer: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_LAYER:MESH} - service_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_SERVICE_NAME:{{.Pod.Namespace}}::{{.Pod.LabelValue "service.istio.io/canonical-name,app.kubernetes.io/name,app" }}} + service_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_SERVICE_NAME:{{.Pod.Namespace}}::{{.Pod.LabelValue "service.istio.io/canonical-name,app.kubernetes.io/name,app" ""}}} instance_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_INSTANCE_NAME:{{.Pod.Name}}} process_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_PROCESS_NAME:{{.Process.ExeName}}} labels: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_LABELS:mesh-application} diff --git a/docs/en/setup/configuration/process_discovery/kubernetes.md b/docs/en/setup/configuration/process_discovery/kubernetes.md index e8358917..26404273 100644 --- a/docs/en/setup/configuration/process_discovery/kubernetes.md +++ b/docs/en/setup/configuration/process_discovery/kubernetes.md @@ -75,15 +75,14 @@ Same with the [process context in the scanner](./scanner.md#process). The information on the current pod. -| Name | Argument | Example | Description | -|-----------------------|-------------------|--------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| Name | None | `{{.Pod.Name}}` | The name of current pod. | -| Namespace | None | `{{.Pod.Namespace}}` | The name of current pod namespace. | -| Node | None | `{{.Pod.Node}}` | The name of the node deployed. | -| LabelValue | KeyNames | `{{.Pod.LavelValue "a,b"}}` | The label value of the label keys, If provide multiple keys, if any key has value, then don't need to get other values. | -| LabelValueWithDefault | KeyNames, Default | `{{.Pod.LavelValueWithDefault "a,b" "v"}}` | The label value of the label keys, If provide multiple keys, if any key has value, then don't need to get other values. If all keys don't have value, then return the default value. | -| ServiceName | None | `{{.Pod.ServiceName}}` | The service name of the pod. If the pod hasn't matched service, then return an empty string. | -| FindContainer | ContainerName | `{{.Pod.FindContainer "test"}}` | Find the Container context by container name. | +| Name | Argument | Example | Description | +|---------------|-------------------|-----------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Name | None | `{{.Pod.Name}}` | The name of current pod. | +| Namespace | None | `{{.Pod.Namespace}}` | The name of current pod namespace. | +| Node | None | `{{.Pod.Node}}` | The name of the node deployed. | +| LabelValue | KeyNames, Default | `{{.Pod.LabelValue "a,b" "v"}}` | The label value of the label keys, If provide multiple keys, if any key has value, then don't need to get other values. If all keys don't have value, then return the default value. | +| ServiceName | None | `{{.Pod.ServiceName}}` | The service name of the pod. If the pod hasn't matched service, then return an empty string. | +| FindContainer | ContainerName | `{{.Pod.FindContainer "test"}}` | Find the Container context by container name. | #### Container diff --git a/pkg/process/finders/kubernetes/template.go b/pkg/process/finders/kubernetes/template.go index 473d0010..d54e3c57 100644 --- a/pkg/process/finders/kubernetes/template.go +++ b/pkg/process/finders/kubernetes/template.go @@ -107,22 +107,7 @@ func (p *TemplatePod) FindContainer(name string) (*TemplateContainer, error) { return &TemplateContainer{pc: container}, nil } -func (p *TemplatePod) LabelValue(names string) (string, error) { - namesArray := strings.Split(names, ",") - for _, name := range namesArray { - val := p.pc.Pod.Labels[name] - if val != "" { - return val, nil - } - } - actualNames := make([]string, 0) - for k := range p.pc.Pod.Labels { - actualNames = append(actualNames, k) - } - return "", fmt.Errorf("could not found matches label, want names: %v, actual names: %v", namesArray, actualNames) -} - -func (p *TemplatePod) LabelValueWithDefault(names, def string) (string, error) { +func (p *TemplatePod) LabelValue(names, def string) (string, error) { namesArray := strings.Split(names, ",") for _, name := range namesArray { val := p.pc.Pod.Labels[name] diff --git a/test/e2e/cases/profiling/task/network/base/rover_configs.yaml b/test/e2e/cases/profiling/task/network/base/rover_configs.yaml index 4bbf8cb9..1f0def52 100644 --- a/test/e2e/cases/profiling/task/network/base/rover_configs.yaml +++ b/test/e2e/cases/profiling/task/network/base/rover_configs.yaml @@ -97,7 +97,7 @@ process_discovery: - ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_HAS_ENVOY:.Pod.HasContainer "istio-proxy"} - ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_CONTAINER_IS_ENVOY:eq .Container.Name "istio-proxy"} layer: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_LAYER:MESH_DP} - service_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_SERVICE_NAME:{{.Pod.Namespace}}::{{.Pod.LabelValue "service.istio.io/canonical-name,app.kubernetes.io/name,app" }}} + service_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_SERVICE_NAME:{{.Pod.Namespace}}::{{.Pod.LabelValue "service.istio.io/canonical-name,app.kubernetes.io/name,app" ""}}} instance_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_INSTANCE_NAME:{{.Pod.Name}}} process_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_PROCESS_NAME:{{.Process.ExeName}}} labels: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_LABELS:mesh-envoy} @@ -106,7 +106,7 @@ process_discovery: - ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_HAS_ENVOY:.Pod.HasContainer "istio-proxy"} - ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_CONTAINER_NOT_ENVOY:ne .Container.Name "istio-proxy"} layer: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_LAYER:MESH} - service_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_SERVICE_NAME:{{.Pod.Namespace}}::{{.Pod.LabelValue "service.istio.io/canonical-name,app.kubernetes.io/name,app" }}} + service_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_SERVICE_NAME:{{.Pod.Namespace}}::{{.Pod.LabelValue "service.istio.io/canonical-name,app.kubernetes.io/name,app" ""}}} instance_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_INSTANCE_NAME:{{.Pod.Name}}} process_name: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_PROCESS_NAME:{{.Process.ExeName}}} labels: ${ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_LABELS:mesh-application} diff --git a/test/e2e/cases/profiling/task/network/envoy/rover.yaml b/test/e2e/cases/profiling/task/network/envoy/rover.yaml index 90fa0dec..de729373 100644 --- a/test/e2e/cases/profiling/task/network/envoy/rover.yaml +++ b/test/e2e/cases/profiling/task/network/envoy/rover.yaml @@ -90,9 +90,9 @@ spec: - name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_PROCESS_NAME value: "{{.Process.ExeNameInCommandLine}}" - name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_ENVOY_INSTANCE_NAME - value: "{{.Pod.LabelValue \"service.istio.io/canonical-name,app.kubernetes.io/name,app\" }}" + value: "{{.Pod.LabelValue \"service.istio.io/canonical-name,app.kubernetes.io/name,app\" \"\"}}" - name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_INSTANCE_NAME - value: "{{.Pod.LabelValue \"service.istio.io/canonical-name,app.kubernetes.io/name,app\" }}" + value: "{{.Pod.LabelValue \"service.istio.io/canonical-name,app.kubernetes.io/name,app\" \"\"}}" hostPID: true hostNetwork: true dnsPolicy: ClusterFirstWithHostNet