istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/kube/pod_controller.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kube
    16  
    17  import (
    18  	"time"
    19  
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/client-go/tools/cache"
    23  
    24  	"istio.io/istio/pkg/kube"
    25  	"istio.io/istio/pkg/kube/controllers"
    26  	"istio.io/istio/pkg/queue"
    27  	"istio.io/istio/pkg/test/framework/components/echo"
    28  )
    29  
    30  var _ cache.Controller = &podController{}
    31  
    32  type podHandler func(pod *corev1.Pod) error
    33  
    34  type podHandlers struct {
    35  	added   podHandler
    36  	updated podHandler
    37  	deleted podHandler
    38  }
    39  
    40  type podController struct {
    41  	q        queue.Instance
    42  	informer cache.Controller
    43  }
    44  
    45  func newPodController(cfg echo.Config, handlers podHandlers) *podController {
    46  	s := newPodSelector(cfg)
    47  	podListWatch := cache.NewFilteredListWatchFromClient(cfg.Cluster.Kube().CoreV1().RESTClient(),
    48  		"pods",
    49  		cfg.Namespace.Name(),
    50  		func(options *metav1.ListOptions) {
    51  			if len(options.LabelSelector) > 0 {
    52  				options.LabelSelector += ","
    53  			}
    54  			options.LabelSelector += s.String()
    55  		})
    56  	q := queue.NewQueue(1 * time.Second)
    57  	_, informer := cache.NewInformer(podListWatch, &corev1.Pod{}, 0, controllers.EventHandler[*corev1.Pod]{
    58  		AddFunc: func(pod *corev1.Pod) {
    59  			q.Push(func() error {
    60  				return handlers.added(pod)
    61  			})
    62  		},
    63  		UpdateFunc: func(old, cur *corev1.Pod) {
    64  			q.Push(func() error {
    65  				if old.GetResourceVersion() != cur.GetResourceVersion() {
    66  					return handlers.updated(cur)
    67  				}
    68  				return nil
    69  			})
    70  		},
    71  		DeleteFunc: func(pod *corev1.Pod) {
    72  			q.Push(func() error {
    73  				return handlers.deleted(pod)
    74  			})
    75  		},
    76  	})
    77  
    78  	return &podController{
    79  		q:        q,
    80  		informer: informer,
    81  	}
    82  }
    83  
    84  func (c *podController) Run(stop <-chan struct{}) {
    85  	go c.informer.Run(stop)
    86  	kube.WaitForCacheSync("pod controller", stop, c.informer.HasSynced)
    87  	c.q.Run(stop)
    88  }
    89  
    90  func (c *podController) HasSynced() bool {
    91  	return c.q.HasSynced()
    92  }
    93  
    94  func (c *podController) WaitForSync(stopCh <-chan struct{}) bool {
    95  	return cache.WaitForNamedCacheSync("echo", stopCh, c.informer.HasSynced)
    96  }
    97  
    98  func (c *podController) LastSyncResourceVersion() string {
    99  	return c.informer.LastSyncResourceVersion()
   100  }