...

Source file src/sigs.k8s.io/controller-runtime/pkg/source/example_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/source

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package source_test
    18  
    19  import (
    20  	corev1 "k8s.io/api/core/v1"
    21  	"sigs.k8s.io/controller-runtime/pkg/controller"
    22  	"sigs.k8s.io/controller-runtime/pkg/event"
    23  	"sigs.k8s.io/controller-runtime/pkg/handler"
    24  	"sigs.k8s.io/controller-runtime/pkg/manager"
    25  	"sigs.k8s.io/controller-runtime/pkg/source"
    26  )
    27  
    28  var mgr manager.Manager
    29  var ctrl controller.Controller
    30  
    31  // This example Watches for Pod Events (e.g. Create / Update / Delete) and enqueues a reconcile.Request
    32  // with the Name and Namespace of the Pod.
    33  func ExampleKind() {
    34  	err := ctrl.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, &handler.TypedEnqueueRequestForObject[*corev1.Pod]{}))
    35  	if err != nil {
    36  		// handle it
    37  	}
    38  }
    39  
    40  // This example reads GenericEvents from a channel and enqueues a reconcile.Request containing the Name and Namespace
    41  // provided by the event.
    42  func ExampleChannel() {
    43  	events := make(chan event.GenericEvent)
    44  
    45  	err := ctrl.Watch(
    46  		source.Channel(
    47  			events,
    48  			&handler.EnqueueRequestForObject{},
    49  		),
    50  	)
    51  	if err != nil {
    52  		// handle it
    53  	}
    54  }
    55  

View as plain text