Refactor extraction of ingress pod details
This commit is contained in:
parent
b482b5dd32
commit
1389cc0e80
9 changed files with 99 additions and 68 deletions
|
|
@ -75,16 +75,22 @@ func GetNodeIPOrName(kubeClient clientset.Interface, name string, useInternalIP
|
|||
return defaultOrInternalIP
|
||||
}
|
||||
|
||||
// IngressNGINXPod hold information about the ingress-nginx pod
|
||||
var IngressNGINXPod *apiv1.Pod
|
||||
var (
|
||||
// IngressPodDetails hold information about the ingress-nginx pod
|
||||
IngressPodDetails *PodInfo
|
||||
|
||||
selectorLabelKeys = []string{
|
||||
"app.kubernetes.io/component",
|
||||
"app.kubernetes.io/instance",
|
||||
"app.kubernetes.io/name",
|
||||
}
|
||||
)
|
||||
|
||||
// PodInfo contains runtime information about the pod running the Ingres controller
|
||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||
type PodInfo struct {
|
||||
Name string
|
||||
Namespace string
|
||||
// Labels selectors of the running pod
|
||||
// This is used to search for other Ingress controller pods
|
||||
Labels map[string]string
|
||||
metav1.TypeMeta
|
||||
metav1.ObjectMeta
|
||||
}
|
||||
|
||||
// GetIngressPod load the ingress-nginx pod
|
||||
|
|
@ -96,28 +102,31 @@ func GetIngressPod(kubeClient clientset.Interface) error {
|
|||
return fmt.Errorf("unable to get POD information (missing POD_NAME or POD_NAMESPACE environment variable")
|
||||
}
|
||||
|
||||
IngressNGINXPod, _ = kubeClient.CoreV1().Pods(podNs).Get(context.TODO(), podName, metav1.GetOptions{})
|
||||
if IngressNGINXPod == nil {
|
||||
return fmt.Errorf("unable to get POD information")
|
||||
pod, err := kubeClient.CoreV1().Pods(podNs).Get(context.TODO(), podName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get POD information: %v", err)
|
||||
}
|
||||
|
||||
labels := map[string]string{}
|
||||
for _, key := range selectorLabelKeys {
|
||||
value, ok := pod.GetLabels()[key]
|
||||
if !ok {
|
||||
return fmt.Errorf("label %v is missing. Please do not remove", key)
|
||||
}
|
||||
|
||||
labels[key] = value
|
||||
}
|
||||
|
||||
IngressPodDetails = &PodInfo{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"},
|
||||
}
|
||||
|
||||
pod.ObjectMeta.DeepCopyInto(&IngressPodDetails.ObjectMeta)
|
||||
IngressPodDetails.SetLabels(labels)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPodDetails returns runtime information about the pod:
|
||||
// name, namespace and IP of the node where it is running
|
||||
func GetPodDetails() (*PodInfo, error) {
|
||||
if IngressNGINXPod == nil {
|
||||
return nil, fmt.Errorf("no ingress-nginx pod details available")
|
||||
}
|
||||
|
||||
return &PodInfo{
|
||||
Name: IngressNGINXPod.Name,
|
||||
Namespace: IngressNGINXPod.Namespace,
|
||||
Labels: IngressNGINXPod.GetLabels(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// MetaNamespaceKey knows how to make keys for API objects which implement meta.Interface.
|
||||
func MetaNamespaceKey(obj interface{}) string {
|
||||
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||
|
|
|
|||
|
|
@ -214,36 +214,36 @@ func TestGetNodeIP(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGetPodDetails(t *testing.T) {
|
||||
func TestGetIngressPod(t *testing.T) {
|
||||
// POD_NAME & POD_NAMESPACE not exist
|
||||
os.Setenv("POD_NAME", "")
|
||||
os.Setenv("POD_NAMESPACE", "")
|
||||
err1 := GetIngressPod(testclient.NewSimpleClientset())
|
||||
if err1 == nil {
|
||||
err := GetIngressPod(testclient.NewSimpleClientset())
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but returned nil")
|
||||
}
|
||||
|
||||
// POD_NAME not exist
|
||||
os.Setenv("POD_NAME", "")
|
||||
os.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
|
||||
err2 := GetIngressPod(testclient.NewSimpleClientset())
|
||||
if err2 == nil {
|
||||
err = GetIngressPod(testclient.NewSimpleClientset())
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but returned nil")
|
||||
}
|
||||
|
||||
// POD_NAMESPACE not exist
|
||||
os.Setenv("POD_NAME", "testpod")
|
||||
os.Setenv("POD_NAMESPACE", "")
|
||||
err3 := GetIngressPod(testclient.NewSimpleClientset())
|
||||
if err3 == nil {
|
||||
err = GetIngressPod(testclient.NewSimpleClientset())
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but returned nil")
|
||||
}
|
||||
|
||||
// POD not exist
|
||||
os.Setenv("POD_NAME", "testpod")
|
||||
os.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
|
||||
err4 := GetIngressPod(testclient.NewSimpleClientset())
|
||||
if err4 == nil {
|
||||
err = GetIngressPod(testclient.NewSimpleClientset())
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but returned nil")
|
||||
}
|
||||
|
||||
|
|
@ -254,8 +254,11 @@ func TestGetPodDetails(t *testing.T) {
|
|||
Name: "testpod",
|
||||
Namespace: apiv1.NamespaceDefault,
|
||||
Labels: map[string]string{
|
||||
"first": "first_label",
|
||||
"second": "second_label",
|
||||
"first": "first_label",
|
||||
"second": "second_label",
|
||||
"app.kubernetes.io/component": "controller",
|
||||
"app.kubernetes.io/instance": "ingress-nginx",
|
||||
"app.kubernetes.io/name": "ingress-nginx",
|
||||
},
|
||||
},
|
||||
}}},
|
||||
|
|
@ -273,9 +276,9 @@ func TestGetPodDetails(t *testing.T) {
|
|||
},
|
||||
}}})
|
||||
|
||||
err5 := GetIngressPod(fkClient)
|
||||
if err5 != nil {
|
||||
t.Errorf("expected a PodInfo but returned error")
|
||||
err = GetIngressPod(fkClient)
|
||||
if err != nil {
|
||||
t.Errorf("expected a PodInfo but returned error: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
36
internal/k8s/zz_generated.deepcopy.go
Normal file
36
internal/k8s/zz_generated.deepcopy.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// +build !ignore_autogenerated
|
||||
|
||||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
Licensed 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.
|
||||
*/
|
||||
|
||||
// Code generated by deepcopy-gen. DO NOT EDIT.
|
||||
|
||||
package k8s
|
||||
|
||||
import "k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
func (in *PodInfo) DeepCopyInto(out *PodInfo) {
|
||||
out.TypeMeta = in.TypeMeta
|
||||
out.ObjectMeta = in.ObjectMeta
|
||||
}
|
||||
|
||||
// DeepCopyObject returns a generically typed copy of an object
|
||||
func (in *PodInfo) DeepCopyObject() runtime.Object {
|
||||
out := PodInfo{}
|
||||
in.DeepCopyInto(&out)
|
||||
|
||||
return &out
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue