Skip to content

Commit 7ef9426

Browse files
authored
backport: pkg/k8sutil/k8sutil.go: Set kind and APIVersion (#1356)
Get method clears the TypeMeta.Kind and TypeMeta.APIVersion, when returning an object we want these to be set with the correct type and version respectively.
1 parent b9fbecb commit 7ef9426

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

pkg/k8sutil/k8sutil.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"strings"
2323

2424
corev1 "k8s.io/api/core/v1"
25-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
discovery "k8s.io/client-go/discovery"
2726
crclient "sigs.k8s.io/controller-runtime/pkg/client"
2827
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -99,19 +98,19 @@ func GetPod(ctx context.Context, client crclient.Client, ns string) (*corev1.Pod
9998

10099
log.V(1).Info("Found podname", "Pod.Name", podName)
101100

102-
pod := &corev1.Pod{
103-
TypeMeta: metav1.TypeMeta{
104-
APIVersion: "v1",
105-
Kind: "Pod",
106-
},
107-
}
101+
pod := &corev1.Pod{}
108102
key := crclient.ObjectKey{Namespace: ns, Name: podName}
109103
err := client.Get(ctx, key, pod)
110104
if err != nil {
111105
log.Error(err, "Failed to get Pod", "Pod.Namespace", ns, "Pod.Name", podName)
112106
return nil, err
113107
}
114108

109+
// .Get() clears the APIVersion and Kind,
110+
// so we need to set them before returning the object.
111+
pod.TypeMeta.APIVersion = "v1"
112+
pod.TypeMeta.Kind = "Pod"
113+
115114
log.V(1).Info("Found Pod", "Pod.Namespace", ns, "Pod.Name", pod.Name)
116115

117116
return pod, nil

0 commit comments

Comments
 (0)