...

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

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

     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 manager_test
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  
    23  	"k8s.io/client-go/rest"
    24  	"sigs.k8s.io/controller-runtime/pkg/cache"
    25  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    26  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    27  	"sigs.k8s.io/controller-runtime/pkg/manager"
    28  	"sigs.k8s.io/controller-runtime/pkg/manager/signals"
    29  )
    30  
    31  var (
    32  	mgr manager.Manager
    33  	// NB: don't call SetLogger in init(), or else you'll mess up logging in the main suite.
    34  	log = logf.Log.WithName("manager-examples")
    35  )
    36  
    37  // This example creates a new Manager that can be used with controller.New to create Controllers.
    38  func ExampleNew() {
    39  	cfg, err := config.GetConfig()
    40  	if err != nil {
    41  		log.Error(err, "unable to get kubeconfig")
    42  		os.Exit(1)
    43  	}
    44  
    45  	mgr, err := manager.New(cfg, manager.Options{})
    46  	if err != nil {
    47  		log.Error(err, "unable to set up manager")
    48  		os.Exit(1)
    49  	}
    50  	log.Info("created manager", "manager", mgr)
    51  }
    52  
    53  // This example creates a new Manager that has a cache scoped to a list of namespaces.
    54  func ExampleNew_limitToNamespaces() {
    55  	cfg, err := config.GetConfig()
    56  	if err != nil {
    57  		log.Error(err, "unable to get kubeconfig")
    58  		os.Exit(1)
    59  	}
    60  
    61  	mgr, err := manager.New(cfg, manager.Options{
    62  		NewCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
    63  			opts.DefaultNamespaces = map[string]cache.Config{
    64  				"namespace1": {},
    65  				"namespace2": {},
    66  			}
    67  			return cache.New(config, opts)
    68  		}},
    69  	)
    70  	if err != nil {
    71  		log.Error(err, "unable to set up manager")
    72  		os.Exit(1)
    73  	}
    74  	log.Info("created manager", "manager", mgr)
    75  }
    76  
    77  // This example adds a Runnable for the Manager to Start.
    78  func ExampleManager_add() {
    79  	err := mgr.Add(manager.RunnableFunc(func(context.Context) error {
    80  		// Do something
    81  		return nil
    82  	}))
    83  	if err != nil {
    84  		log.Error(err, "unable add a runnable to the manager")
    85  		os.Exit(1)
    86  	}
    87  }
    88  
    89  // This example starts a Manager that has had Runnables added.
    90  func ExampleManager_start() {
    91  	if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
    92  		log.Error(err, "unable start the manager")
    93  		os.Exit(1)
    94  	}
    95  }
    96  

View as plain text