pkg/k8sutil/k8sutil.go: Set kind and APIVersion
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.
@@ -22,7 +22,6 @@ import (
"strings"
corev1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
discovery "k8s.io/client-go/discovery"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -99,12 +98,7 @@ func GetPod(ctx context.Context, client crclient.Client, ns string) (*corev1.Pod
log.V(1).Info("Found podname", "Pod.Name", podName)
- pod := &corev1.Pod{
- TypeMeta: metav1.TypeMeta{
- APIVersion: "v1",
- Kind: "Pod",
- },
- }
+ pod := &corev1.Pod{}
key := crclient.ObjectKey{Namespace: ns, Name: podName}
err := client.Get(ctx, key, pod)
if err != nil {
@@ -112,6 +106,11 @@ func GetPod(ctx context.Context, client crclient.Client, ns string) (*corev1.Pod
return nil, err
}
+ // .Get() clears the APIVersion and Kind,
+ // so we need to set them before returning the object.
+ pod.TypeMeta.APIVersion = "v1"
+ pod.TypeMeta.Kind = "Pod"
+
log.V(1).Info("Found Pod", "Pod.Namespace", ns, "Pod.Name", pod.Name)
return pod, nil
