From ee6bac0734f2a93087d96f161f294fcaa008f394 Mon Sep 17 00:00:00 2001 From: Tomofumi Hayashi Date: Mon, 10 May 2021 22:48:22 +0900 Subject: [PATCH] Fix error case at update network status At the error case of coreClient.Pods(), pod variable may not have Name and Namespace, so keep it and use it query in this case. --- .../v1/zz_generated.deepcopy.go | 2 +- pkg/client/clientset/versioned/clientset.go | 2 +- pkg/client/clientset/versioned/doc.go | 2 +- .../versioned/fake/clientset_generated.go | 2 +- pkg/client/clientset/versioned/fake/doc.go | 2 +- .../clientset/versioned/fake/register.go | 2 +- pkg/client/clientset/versioned/scheme/doc.go | 2 +- .../clientset/versioned/scheme/register.go | 2 +- .../versioned/typed/k8s.cni.cncf.io/v1/doc.go | 2 +- .../typed/k8s.cni.cncf.io/v1/fake/doc.go | 2 +- .../v1/fake/fake_k8s.cni.cncf.io_client.go | 2 +- .../fake/fake_networkattachmentdefinition.go | 2 +- .../k8s.cni.cncf.io/v1/generated_expansion.go | 2 +- .../v1/k8s.cni.cncf.io_client.go | 2 +- .../v1/networkattachmentdefinition.go | 2 +- .../informers/externalversions/factory.go | 2 +- .../informers/externalversions/generic.go | 2 +- .../internalinterfaces/factory_interfaces.go | 2 +- .../k8s.cni.cncf.io/interface.go | 2 +- .../k8s.cni.cncf.io/v1/interface.go | 2 +- .../v1/networkattachmentdefinition.go | 2 +- .../k8s.cni.cncf.io/v1/expansion_generated.go | 2 +- .../v1/networkattachmentdefinition.go | 2 +- pkg/utils/cniconfig.go | 2 +- pkg/utils/cniconfig_test.go | 2 +- pkg/utils/net-attach-def.go | 35 ++++++++++--------- pkg/utils/net-attach-def_test.go | 4 ++- 27 files changed, 46 insertions(+), 43 deletions(-) diff --git a/pkg/apis/k8s.cni.cncf.io/v1/zz_generated.deepcopy.go b/pkg/apis/k8s.cni.cncf.io/v1/zz_generated.deepcopy.go index 80450b73d..1ceb1e6e4 100644 --- a/pkg/apis/k8s.cni.cncf.io/v1/zz_generated.deepcopy.go +++ b/pkg/apis/k8s.cni.cncf.io/v1/zz_generated.deepcopy.go @@ -1,7 +1,7 @@ // +build !ignore_autogenerated /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/clientset.go b/pkg/client/clientset/versioned/clientset.go index 2b86a7d12..f4238c549 100644 --- a/pkg/client/clientset/versioned/clientset.go +++ b/pkg/client/clientset/versioned/clientset.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/doc.go b/pkg/client/clientset/versioned/doc.go index 10f4419bd..22485f354 100644 --- a/pkg/client/clientset/versioned/doc.go +++ b/pkg/client/clientset/versioned/doc.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/fake/clientset_generated.go b/pkg/client/clientset/versioned/fake/clientset_generated.go index 672626c4d..344a0efa1 100644 --- a/pkg/client/clientset/versioned/fake/clientset_generated.go +++ b/pkg/client/clientset/versioned/fake/clientset_generated.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/fake/doc.go b/pkg/client/clientset/versioned/fake/doc.go index ff9007d25..c5afab287 100644 --- a/pkg/client/clientset/versioned/fake/doc.go +++ b/pkg/client/clientset/versioned/fake/doc.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/fake/register.go b/pkg/client/clientset/versioned/fake/register.go index 05d477740..98d4014d5 100644 --- a/pkg/client/clientset/versioned/fake/register.go +++ b/pkg/client/clientset/versioned/fake/register.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/scheme/doc.go b/pkg/client/clientset/versioned/scheme/doc.go index 25a11f92f..49f3510bf 100644 --- a/pkg/client/clientset/versioned/scheme/doc.go +++ b/pkg/client/clientset/versioned/scheme/doc.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/scheme/register.go b/pkg/client/clientset/versioned/scheme/register.go index 466c4f329..93942f6cb 100644 --- a/pkg/client/clientset/versioned/scheme/register.go +++ b/pkg/client/clientset/versioned/scheme/register.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/doc.go b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/doc.go index c334e6f27..32d02a1a0 100644 --- a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/doc.go +++ b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/doc.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/doc.go b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/doc.go index af8696366..50576b381 100644 --- a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/doc.go +++ b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/doc.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/fake_k8s.cni.cncf.io_client.go b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/fake_k8s.cni.cncf.io_client.go index e039808ef..3e07e0eef 100644 --- a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/fake_k8s.cni.cncf.io_client.go +++ b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/fake_k8s.cni.cncf.io_client.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/fake_networkattachmentdefinition.go b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/fake_networkattachmentdefinition.go index 9564be369..05c055f75 100644 --- a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/fake_networkattachmentdefinition.go +++ b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/fake/fake_networkattachmentdefinition.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/generated_expansion.go b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/generated_expansion.go index 03219735a..245ff707b 100644 --- a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/generated_expansion.go +++ b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/generated_expansion.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/k8s.cni.cncf.io_client.go b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/k8s.cni.cncf.io_client.go index b9f5e8e22..9317b8034 100644 --- a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/k8s.cni.cncf.io_client.go +++ b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/k8s.cni.cncf.io_client.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/networkattachmentdefinition.go b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/networkattachmentdefinition.go index cac55f71c..1f0ddac45 100644 --- a/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/networkattachmentdefinition.go +++ b/pkg/client/clientset/versioned/typed/k8s.cni.cncf.io/v1/networkattachmentdefinition.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/informers/externalversions/factory.go b/pkg/client/informers/externalversions/factory.go index 0bbeb4ff8..2476f9fe6 100644 --- a/pkg/client/informers/externalversions/factory.go +++ b/pkg/client/informers/externalversions/factory.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/informers/externalversions/generic.go b/pkg/client/informers/externalversions/generic.go index f480520e0..649781164 100644 --- a/pkg/client/informers/externalversions/generic.go +++ b/pkg/client/informers/externalversions/generic.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go b/pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go index 459122027..edc84dd9b 100644 --- a/pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go +++ b/pkg/client/informers/externalversions/internalinterfaces/factory_interfaces.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/informers/externalversions/k8s.cni.cncf.io/interface.go b/pkg/client/informers/externalversions/k8s.cni.cncf.io/interface.go index d2347d03e..f4d418cb0 100644 --- a/pkg/client/informers/externalversions/k8s.cni.cncf.io/interface.go +++ b/pkg/client/informers/externalversions/k8s.cni.cncf.io/interface.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/informers/externalversions/k8s.cni.cncf.io/v1/interface.go b/pkg/client/informers/externalversions/k8s.cni.cncf.io/v1/interface.go index 412814d75..aa1b0762f 100644 --- a/pkg/client/informers/externalversions/k8s.cni.cncf.io/v1/interface.go +++ b/pkg/client/informers/externalversions/k8s.cni.cncf.io/v1/interface.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/informers/externalversions/k8s.cni.cncf.io/v1/networkattachmentdefinition.go b/pkg/client/informers/externalversions/k8s.cni.cncf.io/v1/networkattachmentdefinition.go index 69b95f73a..35f2f931d 100644 --- a/pkg/client/informers/externalversions/k8s.cni.cncf.io/v1/networkattachmentdefinition.go +++ b/pkg/client/informers/externalversions/k8s.cni.cncf.io/v1/networkattachmentdefinition.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/listers/k8s.cni.cncf.io/v1/expansion_generated.go b/pkg/client/listers/k8s.cni.cncf.io/v1/expansion_generated.go index 7f367379a..cb9a39138 100644 --- a/pkg/client/listers/k8s.cni.cncf.io/v1/expansion_generated.go +++ b/pkg/client/listers/k8s.cni.cncf.io/v1/expansion_generated.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/client/listers/k8s.cni.cncf.io/v1/networkattachmentdefinition.go b/pkg/client/listers/k8s.cni.cncf.io/v1/networkattachmentdefinition.go index 133c09a3e..fb9b666ed 100644 --- a/pkg/client/listers/k8s.cni.cncf.io/v1/networkattachmentdefinition.go +++ b/pkg/client/listers/k8s.cni.cncf.io/v1/networkattachmentdefinition.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Kubernetes Authors +Copyright 2021 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. diff --git a/pkg/utils/cniconfig.go b/pkg/utils/cniconfig.go index f1eaf3797..4b54909bb 100644 --- a/pkg/utils/cniconfig.go +++ b/pkg/utils/cniconfig.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019 Kubernetes Network Plumbing Working Group +// Copyright (c) 2021 Kubernetes Network Plumbing Working Group // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/pkg/utils/cniconfig_test.go b/pkg/utils/cniconfig_test.go index e6eac0d4a..bca65d9d5 100644 --- a/pkg/utils/cniconfig_test.go +++ b/pkg/utils/cniconfig_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019 Kubernetes Network Plumbing Working Group +// Copyright (c) 2021 Kubernetes Network Plumbing Working Group // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/pkg/utils/net-attach-def.go b/pkg/utils/net-attach-def.go index d8e575cd5..7f1a57f17 100644 --- a/pkg/utils/net-attach-def.go +++ b/pkg/utils/net-attach-def.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019 Kubernetes Network Plumbing Working Group +// Copyright (c) 2021 Kubernetes Network Plumbing Working Group // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -65,40 +65,41 @@ func SetNetworkStatus(client kubernetes.Interface, pod *corev1.Pod, statuses []v } } - _, err := setPodNetworkStatus(client, pod, fmt.Sprintf("[%s]", strings.Join(networkStatus, ","))) + err := setPodNetworkStatus(client, pod, fmt.Sprintf("[%s]", strings.Join(networkStatus, ","))) if err != nil { return fmt.Errorf("SetNetworkStatus: failed to update the pod %s in out of cluster comm: %v", pod.Name, err) } return nil } -func setPodNetworkStatus(client kubernetes.Interface, pod *corev1.Pod, networkstatus string) (*corev1.Pod, error) { +func setPodNetworkStatus(client kubernetes.Interface, pod *corev1.Pod, networkstatus string) (error) { if len(pod.Annotations) == 0 { pod.Annotations = make(map[string]string) } coreClient := client.CoreV1() - - pod.Annotations[v1.NetworkStatusAnnot] = networkstatus - pod.Annotations[v1.OldNetworkStatusAnnot] = networkstatus - pod = pod.DeepCopy() var err error + name := pod.Name + namespace := pod.Namespace - if resultErr := retry.RetryOnConflict(retry.DefaultBackoff, func() error { + resultErr := retry.RetryOnConflict(retry.DefaultRetry, func() error { + pod, err = coreClient.Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{}) if err != nil { - // Re-get the pod unless it's the first attempt to update - pod, err = coreClient.Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{}) - if err != nil { - return err - } + return err } - pod, err = coreClient.Pods(pod.Namespace).UpdateStatus(context.TODO(), pod, metav1.UpdateOptions{}) + if len(pod.Annotations) == 0 { + pod.Annotations = make(map[string]string) + } + pod.Annotations[v1.NetworkStatusAnnot] = networkstatus + pod.Annotations[v1.OldNetworkStatusAnnot] = networkstatus + _, err = coreClient.Pods(namespace).UpdateStatus(context.TODO(), pod, metav1.UpdateOptions{}) return err - }); resultErr != nil { - return nil, fmt.Errorf("status update failed for pod %s/%s: %v", pod.Namespace, pod.Name, resultErr) + }) + if resultErr != nil { + return fmt.Errorf("status update failed for pod %s/%s: %v", pod.Namespace, pod.Name, resultErr) } - return pod, nil + return nil } // GetNetworkStatus returns pod's network status diff --git a/pkg/utils/net-attach-def_test.go b/pkg/utils/net-attach-def_test.go index f55e567cd..20d496ef8 100644 --- a/pkg/utils/net-attach-def_test.go +++ b/pkg/utils/net-attach-def_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019 Kubernetes Network Plumbing Working Group +// Copyright (c) 2021 Kubernetes Network Plumbing Working Group // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -99,6 +99,8 @@ var _ = Describe("Netwok Attachment Definition manipulations", func() { err = SetNetworkStatus(clientSet, pod, fakeStatus) Expect(err).NotTo(HaveOccurred()) + pod, err = clientSet.CoreV1().Pods("fakeNamespace1").Get(context.TODO(), "fakePod1", metav1.GetOptions{}) + Expect(err).NotTo(HaveOccurred()) getStatuses, err := GetNetworkStatus(pod) Expect(err).NotTo(HaveOccurred()) Expect(fakeStatus).To(Equal(getStatuses))