You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@skywalking.apache.org by wu...@apache.org on 2022/09/14 14:14:46 UTC

[skywalking-rover] branch main updated: Enhance the render context for kubernetes process (#52)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-rover.git


The following commit(s) were added to refs/heads/main by this push:
     new 48fb8ce  Enhance the render context for kubernetes process (#52)
48fb8ce is described below

commit 48fb8ced2e4aaf505777c6eac2587d799ab9c2bd
Author: mrproliu <74...@qq.com>
AuthorDate: Wed Sep 14 22:14:41 2022 +0800

    Enhance the render context for kubernetes process (#52)
---
 CHANGES.md                                         | 11 ++++++++
 .../configuration/process_discovery/kubernetes.md  |  4 ++-
 pkg/process/finders/base/funcs.go                  | 29 ++++++++++++++++++++++
 pkg/process/finders/base/template.go               |  2 +-
 pkg/process/finders/kubernetes/container.go        | 10 ++++++++
 pkg/process/finders/kubernetes/template.go         | 24 ++++++++++++++++++
 scripts/release/create_bin_release.sh              |  2 +-
 scripts/release/create_source_release.sh           |  2 +-
 8 files changed, 80 insertions(+), 4 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index f752ecd..33553b5 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -2,6 +2,17 @@ Changes by Version
 ==================
 Release Notes.
 
+0.4.0
+------------------
+#### Features
+* Enhancing the render context for the Kubernetes process.
+
+#### Bug Fixes
+
+#### Issues and PR
+- All issues are [here](https://github.com/apache/skywalking/milestone/154?closed=1)
+- All and pull requests are [here](https://github.com/apache/skywalking-rover/milestone/4?closed=1)
+
 0.3.0
 ------------------
 #### Features
diff --git a/docs/en/setup/configuration/process_discovery/kubernetes.md b/docs/en/setup/configuration/process_discovery/kubernetes.md
index eede8c8..87fd93a 100644
--- a/docs/en/setup/configuration/process_discovery/kubernetes.md
+++ b/docs/en/setup/configuration/process_discovery/kubernetes.md
@@ -82,6 +82,7 @@ The information on the current pod.
 | 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. |
 
 #### Container
 
@@ -90,4 +91,5 @@ 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. |
\ No newline at end of file
+| 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/base/funcs.go b/pkg/process/finders/base/funcs.go
new file mode 100644
index 0000000..8bbd939
--- /dev/null
+++ b/pkg/process/finders/base/funcs.go
@@ -0,0 +1,29 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package base
+
+var customFunctions = map[string]interface{}{
+	"equalsOrDefault": equalsOrDefault,
+}
+
+func equalsOrDefault(eq, ifSame, needs string) string {
+	if needs == eq {
+		return ifSame
+	}
+	return needs
+}
diff --git a/pkg/process/finders/base/template.go b/pkg/process/finders/base/template.go
index 2aee7c4..8dd3402 100644
--- a/pkg/process/finders/base/template.go
+++ b/pkg/process/finders/base/template.go
@@ -37,7 +37,7 @@ type TemplateBuilder struct {
 }
 
 func NewTemplateBuilder(name, content string) (*TemplateBuilder, error) {
-	tmpl, err := template.New(name).Parse(content)
+	tmpl, err := template.New(name).Funcs(customFunctions).Parse(content)
 	if err != nil {
 		return nil, fmt.Errorf("failed to parse template for %s, content: %s. reason: %v", name, content, err)
 	}
diff --git a/pkg/process/finders/kubernetes/container.go b/pkg/process/finders/kubernetes/container.go
index 94dabb3..da8ace1 100644
--- a/pkg/process/finders/kubernetes/container.go
+++ b/pkg/process/finders/kubernetes/container.go
@@ -123,6 +123,16 @@ func (c *PodContainer) FindOwner(ctx context.Context, kindName string, k8sConfig
 	return nil, nil
 }
 
+func (c *PodContainer) FindContainerFromSamePod(name string) *PodContainer {
+	cs := AnalyzeContainers(c.Pod, c.registry)
+	for _, co := range cs {
+		if co.ContainerSpec.Name == name {
+			return co
+		}
+	}
+	return nil
+}
+
 func (c *PodContainer) parseRestClientFromAPIVersion(conf *rest.Config, apiVersion string) (rest.Interface, error) {
 	groupVersion, err := schema.ParseGroupVersion(apiVersion)
 	if err != nil {
diff --git a/pkg/process/finders/kubernetes/template.go b/pkg/process/finders/kubernetes/template.go
index dc82baa..f77c409 100644
--- a/pkg/process/finders/kubernetes/template.go
+++ b/pkg/process/finders/kubernetes/template.go
@@ -99,6 +99,14 @@ func (p *TemplatePod) Node() string {
 	return p.pc.Pod.Spec.NodeName
 }
 
+func (p *TemplatePod) FindContainer(name string) (*TemplateContainer, error) {
+	container := p.pc.FindContainerFromSamePod(name)
+	if container == nil {
+		return nil, fmt.Errorf("could not found the container")
+	}
+	return &TemplateContainer{pc: container}, nil
+}
+
 func (p *TemplatePod) LabelValue(names string) (string, error) {
 	namesArray := strings.Split(names, ",")
 	for _, name := range namesArray {
@@ -173,3 +181,19 @@ func (c *TemplateContainer) Name() string {
 func (c *TemplateContainer) ID() string {
 	return c.pc.ContainerStatus.ContainerID
 }
+
+func (c *TemplateContainer) EnvValue(names string) (string, error) {
+	namesArray := strings.Split(names, ",")
+	for _, e := range c.pc.ContainerSpec.Env {
+		for _, needName := range namesArray {
+			if e.Name == needName {
+				return e.Value, nil
+			}
+		}
+	}
+	actualNames := make([]string, 0)
+	for _, e := range c.pc.ContainerSpec.Env {
+		actualNames = append(actualNames, e.Name)
+	}
+	return "", fmt.Errorf("could not found matches environment, want names: %v, actual names: %v", namesArray, actualNames)
+}
diff --git a/scripts/release/create_bin_release.sh b/scripts/release/create_bin_release.sh
index 6a9401d..7330e08 100644
--- a/scripts/release/create_bin_release.sh
+++ b/scripts/release/create_bin_release.sh
@@ -44,7 +44,7 @@ PRODUCT_NAME=${PRODUCT_NAME}-${VERSION}
 rm -rf ${PRODUCT_NAME}
 mkdir ${PRODUCT_NAME}
 
-git clone https://github.com/mrproliu/skywalking-rover.git ./${PRODUCT_NAME}
+git clone https://github.com/apache/skywalking-rover.git ./${PRODUCT_NAME}
 cd ${PRODUCT_NAME}
 
 TAG_EXIST=`git tag -l ${TAG_NAME} | wc -l`
diff --git a/scripts/release/create_source_release.sh b/scripts/release/create_source_release.sh
index dd5e959..c0c4ab5 100644
--- a/scripts/release/create_source_release.sh
+++ b/scripts/release/create_source_release.sh
@@ -43,7 +43,7 @@ PRODUCT_NAME=${PRODUCT_NAME}-${VERSION}
 rm -rf ${PRODUCT_NAME}
 mkdir ${PRODUCT_NAME}
 
-git clone https://github.com/mrproliu/skywalking-rover.git ./${PRODUCT_NAME}
+git clone https://github.com/apache/skywalking-rover.git ./${PRODUCT_NAME}
 cd ${PRODUCT_NAME}
 
 TAG_EXIST=`git tag -l ${TAG_NAME} | wc -l`