Lookup pod by owner instead of label 52/3552/1
authorchengli3 <cheng1.li@intel.com>
Mon, 8 Jun 2020 03:10:21 +0000 (03:10 +0000)
committerchengli3 <cheng1.li@intel.com>
Mon, 8 Jun 2020 03:10:21 +0000 (03:10 +0000)
Currently, CNF is represented by deployment. It means that one CNF is
one deployment with special label name `SdewanPurpose`. We use
"Deployment" + "label" to identify a CNF.
To apply rules for CNF, we first need to find out the pods and then
extract the its IP address. It makes more sense to find pod by its owner
deployment/replicaset, than using the label match. Because the pod label
may not be the same with the deployment.

Signed-off-by: chengli3 <cheng1.li@intel.com>
Change-Id: I4174e502c7d50d48f47d61622380e57922b5cf32

platform/crd-ctrlr/src/cnfprovider/openprovider.go
platform/crd-ctrlr/src/main.go

index c53dae3..2001812 100644 (file)
@@ -3,6 +3,7 @@ package cnfprovider
 import (
        "context"
        "errors"
+       "fmt"
        appsv1 "k8s.io/api/apps/v1"
        corev1 "k8s.io/api/core/v1"
        "k8s.io/apimachinery/pkg/runtime"
@@ -41,8 +42,16 @@ func NewOpenWrt(namespace string, sdewanPurpose string, k8sClient client.Client)
 func (p *OpenWrtProvider) AddOrUpdateObject(handler basehandler.ISdewanHandler, instance runtime.Object) (bool, error) {
        reqLogger := log.WithValues(handler.GetType(), handler.GetName(instance), "cnf", p.Deployment.Name)
        ctx := context.Background()
+       ReplicaSetList := &appsv1.ReplicaSetList{}
+       err := p.K8sClient.List(ctx, ReplicaSetList, client.MatchingLabels{"sdewanPurpose": p.SdewanPurpose})
+       if err != nil {
+               return false, err
+       }
+       if len(ReplicaSetList.Items) != 1 {
+               return false, errors.New(fmt.Sprintf("More than one of repicaset exist with label: sdewanPurpose=%s", p.SdewanPurpose))
+       }
        podList := &corev1.PodList{}
-       err := p.K8sClient.List(ctx, podList, client.MatchingLabels{"sdewanPurpose": p.SdewanPurpose})
+       err = p.K8sClient.List(ctx, podList, client.MatchingFields{"OwnBy": ReplicaSetList.Items[0].ObjectMeta.Name})
        if err != nil {
                return false, err
        }
index 6ff7658..5475a07 100644 (file)
@@ -21,7 +21,9 @@ import (
        "fmt"
        "os"
 
+       corev1 "k8s.io/api/core/v1"
        rbacv1 "k8s.io/api/rbac/v1"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/apimachinery/pkg/runtime"
        clientgoscheme "k8s.io/client-go/kubernetes/scheme"
        _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
@@ -82,6 +84,10 @@ func main() {
                }
                return fieldValues
        })
+       if err != nil {
+               setupLog.Error(err, "unable to start manager")
+               os.Exit(1)
+       }
        err = mgr.GetFieldIndexer().IndexField(context.Background(), &rbacv1.ClusterRoleBinding{}, ".subjects", func(rawObj runtime.Object) []string {
                var fieldValues []string
                clusterrolebinding := rawObj.(*rbacv1.ClusterRoleBinding)
@@ -98,6 +104,20 @@ func main() {
                setupLog.Error(err, "unable to start manager")
                os.Exit(1)
        }
+       err = mgr.GetFieldIndexer().IndexField(context.Background(), &corev1.Pod{}, "OwnBy", func(rawObj runtime.Object) []string {
+               // grab the job object, extract the owner...
+               var fieldValues []string
+               pod := rawObj.(*corev1.Pod)
+               owner := metav1.GetControllerOf(pod)
+               if owner == nil || owner.Kind != "ReplicaSet" {
+                       return nil
+               }
+               return append(fieldValues, owner.Name)
+       })
+       if err != nil {
+               setupLog.Error(err, "unable to start manager")
+               os.Exit(1)
+       }
 
        if err = (&controllers.Mwan3PolicyReconciler{
                Client: mgr.GetClient(),