Repository: clusternet/clusternet
Pull request #227 (2021-12-30 08:27:22)

add support "app.kubernetes.io/component" to find apiserver pod
<!--  Thanks for sending a pull request!

-->

#### What type of PR is this?
kind/enhancement

#### What this PR does / why we need it:
support using `component=kube-apiserver`, or `app.kubernetes.io/component=kube-apiserver` to find apiserver pod

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes # https://github.com/clusternet/clusternet/issues/223

#### Special notes for your reviewer:


dixudx: @fangyuchen86 Please rebase your codes with latest main branch.
codecov-commenter: # [Codecov](https://codecov.io/gh/clusternet/clusternet/pull/227?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) Report
> Merging [#227](https://codecov.io/gh/clusternet/clusternet/pull/227?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) (092b0a6) into [main](https://codecov.io/gh/clusternet/clusternet/commit/fd045f2f565663b5f827e5148db56b895e8978f8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) (fd045f2) will **increase** coverage by `0.21%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/clusternet/clusternet/pull/227/graphs/tree.svg?width=650&height=150&src=pr&token=4O6PTR0RJN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet)](https://codecov.io/gh/clusternet/clusternet/pull/227?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet)

```diff
@@            Coverage Diff             @@
##             main     #227      +/-   ##
==========================================
+ Coverage   10.26%   10.48%   +0.21%     
==========================================
  Files          39       39              
  Lines        4237     4254      +17     
==========================================
+ Hits          435      446      +11     
- Misses       3771     3775       +4     
- Partials       31       33       +2     
```


| [Impacted Files](https://codecov.io/gh/clusternet/clusternet/pull/227?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) | Coverage Δ | |
|---|---|---|
| [...ernet/clusternet/pkg/hub/apiserver/shadow/utils.go](https://codecov.io/gh/clusternet/clusternet/pull/227/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet#diff-c3JjL2dpdGh1Yi5jb20vY2x1c3Rlcm5ldC9jbHVzdGVybmV0L3BrZy9odWIvYXBpc2VydmVyL3NoYWRvdy91dGlscy5nbw==) | `89.28% <0.00%> (-10.72%)` | :arrow_down: |
| [.../pkg/controllers/clusters/clusterstatus/network.go](https://codecov.io/gh/clusternet/clusternet/pull/227/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet#diff-c3JjL2dpdGh1Yi5jb20vY2x1c3Rlcm5ldC9jbHVzdGVybmV0L3BrZy9jb250cm9sbGVycy9jbHVzdGVycy9jbHVzdGVyc3RhdHVzL25ldHdvcmsuZ28=) | `38.94% <0.00%> (+9.46%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/clusternet/clusternet/pull/227?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/clusternet/clusternet/pull/227?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet). Last update [fd045f2...092b0a6](https://codecov.io/gh/clusternet/clusternet/pull/227?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet).


Patch:
diff --git a/pkg/controllers/clusters/clusterstatus/network.go b/pkg/controllers/clusters/clusterstatus/network.go
index 813bd12..c05d334 100644
--- a/pkg/controllers/clusters/clusterstatus/network.go
+++ b/pkg/controllers/clusters/clusterstatus/network.go
@@ -91,9 +91,13 @@ func findPodIPRangeFromNodeSpec(nodeLister corev1Lister.NodeLister) string {
 // findPodCommandParameter returns the pod container command parameter for the given pod.
 // copied from submariner.io/submariner-operator/pkg/discovery/network/pods.go
 func findPodCommandParameter(podLister corev1Lister.PodLister, labelSelectorValue, parameter string) string {
-	pod, err := findPod(podLister, "component", labelSelectorValue)
+	pod := findPodWithComponentLabel(podLister, labelSelectorValue)
+
+	if pod == nil {
+		pod = findPodWithK8sComponentLabel(podLister, labelSelectorValue)
+	}
 
-	if err != nil || pod == nil {
+	if pod == nil {
 		return ""
 	}
 	for _, container := range pod.Spec.Containers {
@@ -108,6 +112,24 @@ func findPodCommandParameter(podLister corev1Lister.PodLister, labelSelectorValu
 	return ""
 }
 
+func findPodWithComponentLabel(podLister corev1Lister.PodLister, labelSelectorValue string) *corev1.Pod {
+	pod, err := findPod(podLister, "component", labelSelectorValue)
+
+	if err != nil {
+		return nil
+	}
+	return pod
+}
+
+func findPodWithK8sComponentLabel(podLister corev1Lister.PodLister, labelSelectorValue string) *corev1.Pod {
+	pod, err := findPod(podLister, "app.kubernetes.io/component", labelSelectorValue)
+
+	if err != nil {
+		return nil
+	}
+	return pod
+}
+
 // findPod returns the pods filter by the given labelSelector.
 // copied from submariner.io/submariner-operator/pkg/discovery/network/pods.go
 func findPod(podLister corev1Lister.PodLister, labelSelectorKey, labelSelectorValue string) (*corev1.Pod, error) {


Test patch:
diff --git a/pkg/controllers/clusters/clusterstatus/network_test.go b/pkg/controllers/clusters/clusterstatus/network_test.go
index 3903e24..8bb8f69 100644
--- a/pkg/controllers/clusters/clusterstatus/network_test.go
+++ b/pkg/controllers/clusters/clusterstatus/network_test.go
@@ -52,6 +52,46 @@ func TestFindPodCommandParameterFromArgs(t *testing.T) {
 
 }
 
+func TestFindPodCommandParameterFromArgsWithK8sLabel(t *testing.T) {
+	podArgs := BuildPod("kube-apiserver", "app.kubernetes.io/component", "kube-apiserver")
+	podArgs.Spec.Containers = []corev1.Container{
+		{
+			Args: []string{"--service-cluster-ip-range=1.2.3.4"},
+		},
+	}
+
+	kubeClient := fake.NewSimpleClientset()
+	kubeFactory := informers.NewSharedInformerFactory(kubeClient, 0)
+	podInformer := kubeFactory.Core().V1().Pods()
+
+	err := podInformer.Informer().GetIndexer().Add(podArgs)
+	if err != nil {
+		t.Errorf("Add pod err: %v", err)
+	}
+
+	podArgsLister := podInformer.Lister()
+
+	tests := []struct {
+		description string
+		podLister   corelisters.PodLister
+		expectedIP  string
+	}{
+		{
+			description: "service-cluster-ip-range is in args",
+			podLister:   podArgsLister,
+			expectedIP:  "1.2.3.4",
+		},
+	}
+
+	for _, test := range tests {
+		clusterIPRange := findPodCommandParameter(test.podLister, "kube-apiserver", "--service-cluster-ip-range")
+		if clusterIPRange != test.expectedIP {
+			t.Errorf("get clusteriprange is %v", clusterIPRange)
+		}
+	}
+
+}
+
 func TestFindPodCommandParameterFromCommand(t *testing.T) {
 	podCommand := BuildPod("kube-apiserver", "component", "kube-apiserver")
 	podCommand.Spec.Containers = []corev1.Container{
@@ -86,6 +126,40 @@ func TestFindPodCommandParameterFromCommand(t *testing.T) {
 	}
 }
 
+func TestFindPodCommandParameterFromCommandWithK8sLabel(t *testing.T) {
+	podCommand := BuildPod("kube-apiserver", "app.kubernetes.io/component", "kube-apiserver")
+	podCommand.Spec.Containers = []corev1.Container{
+		{
+			Command: []string{"--service-cluster-ip-range=1.2.3.4"},
+		},
+	}
+
+	store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{"namespace": cache.MetaNamespaceIndexFunc})
+	lister := corelisters.NewPodLister(store)
+	if err := store.Add(podCommand); err != nil {
+		t.Errorf("add pod err %v", err)
+	}
+
+	tests := []struct {
+		description string
+		podLister   corelisters.PodLister
+		expectedIP  string
+	}{
+		{
+			description: "service-cluster-ip-range is in command",
+			podLister:   lister,
+			expectedIP:  "1.2.3.4",
+		},
+	}
+
+	for _, test := range tests {
+		clusterIPRange := findPodCommandParameter(test.podLister, "kube-apiserver", "--service-cluster-ip-range")
+		if clusterIPRange != test.expectedIP {
+			t.Errorf("get clusteriprange is %v", clusterIPRange)
+		}
+	}
+}
+
 func BuildPod(name, labelKey, lableValue string) *corev1.Pod {
 	pod := &corev1.Pod{
 		TypeMeta: metav1.TypeMeta{Kind: "Pod"},