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 controllerruntime 18 19 import ( 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 "k8s.io/apimachinery/pkg/runtime/schema" 22 "sigs.k8s.io/controller-runtime/pkg/builder" 23 "sigs.k8s.io/controller-runtime/pkg/client/config" 24 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" 25 "sigs.k8s.io/controller-runtime/pkg/log" 26 "sigs.k8s.io/controller-runtime/pkg/manager" 27 "sigs.k8s.io/controller-runtime/pkg/manager/signals" 28 "sigs.k8s.io/controller-runtime/pkg/reconcile" 29 "sigs.k8s.io/controller-runtime/pkg/scheme" 30 ) 31 32 // Builder builds an Application ControllerManagedBy (e.g. Operator) and returns a manager.Manager to start it. 33 type Builder = builder.Builder 34 35 // Request contains the information necessary to reconcile a Kubernetes object. This includes the 36 // information to uniquely identify the object - its Name and Namespace. It does NOT contain information about 37 // any specific Event or the object contents itself. 38 type Request = reconcile.Request 39 40 // Result contains the result of a Reconciler invocation. 41 type Result = reconcile.Result 42 43 // Manager initializes shared dependencies such as Caches and Clients, and provides them to Runnables. 44 // A Manager is required to create Controllers. 45 type Manager = manager.Manager 46 47 // Options are the arguments for creating a new Manager. 48 type Options = manager.Options 49 50 // SchemeBuilder builds a new Scheme for mapping go types to Kubernetes GroupVersionKinds. 51 type SchemeBuilder = scheme.Builder 52 53 // GroupVersion contains the "group" and the "version", which uniquely identifies the API. 54 type GroupVersion = schema.GroupVersion 55 56 // GroupResource specifies a Group and a Resource, but does not force a version. This is useful for identifying 57 // concepts during lookup stages without having partially valid types. 58 type GroupResource = schema.GroupResource 59 60 // TypeMeta describes an individual object in an API response or request 61 // with strings representing the type of the object and its API schema version. 62 // Structures that are versioned or persisted should inline TypeMeta. 63 // 64 // +k8s:deepcopy-gen=false 65 type TypeMeta = metav1.TypeMeta 66 67 // ObjectMeta is metadata that all persisted resources must have, which includes all objects 68 // users must create. 69 type ObjectMeta = metav1.ObjectMeta 70 71 var ( 72 // RegisterFlags registers flag variables to the given FlagSet if not already registered. 73 // It uses the default command line FlagSet, if none is provided. Currently, it only registers the kubeconfig flag. 74 RegisterFlags = config.RegisterFlags 75 76 // GetConfigOrDie creates a *rest.Config for talking to a Kubernetes apiserver. 77 // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running 78 // in cluster and use the cluster provided kubeconfig. 79 // 80 // Will log an error and exit if there is an error creating the rest.Config. 81 GetConfigOrDie = config.GetConfigOrDie 82 83 // GetConfig creates a *rest.Config for talking to a Kubernetes apiserver. 84 // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running 85 // in cluster and use the cluster provided kubeconfig. 86 // 87 // Config precedence 88 // 89 // * --kubeconfig flag pointing at a file 90 // 91 // * KUBECONFIG environment variable pointing at a file 92 // 93 // * In-cluster config if running in cluster 94 // 95 // * $HOME/.kube/config if exists. 96 GetConfig = config.GetConfig 97 98 // NewControllerManagedBy returns a new controller builder that will be started by the provided Manager. 99 NewControllerManagedBy = builder.ControllerManagedBy 100 101 // NewWebhookManagedBy returns a new webhook builder that will be started by the provided Manager. 102 NewWebhookManagedBy = builder.WebhookManagedBy 103 104 // NewManager returns a new Manager for creating Controllers. 105 // Note that if ContentType in the given config is not set, "application/vnd.kubernetes.protobuf" 106 // will be used for all built-in resources of Kubernetes, and "application/json" is for other types 107 // including all CRD resources. 108 NewManager = manager.New 109 110 // CreateOrUpdate creates or updates the given object obj in the Kubernetes 111 // cluster. The object's desired state should be reconciled with the existing 112 // state using the passed in ReconcileFn. obj must be a struct pointer so that 113 // obj can be updated with the content returned by the Server. 114 // 115 // It returns the executed operation and an error. 116 CreateOrUpdate = controllerutil.CreateOrUpdate 117 118 // SetControllerReference sets owner as a Controller OwnerReference on owned. 119 // This is used for garbage collection of the owned object and for 120 // reconciling the owner object on changes to owned (with a Watch + EnqueueRequestForOwner). 121 // Since only one OwnerReference can be a controller, it returns an error if 122 // there is another OwnerReference with Controller flag set. 123 SetControllerReference = controllerutil.SetControllerReference 124 125 // SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned 126 // which is closed on one of these signals. If a second signal is caught, the program 127 // is terminated with exit code 1. 128 SetupSignalHandler = signals.SetupSignalHandler 129 130 // Log is the base logger used by controller-runtime. It delegates 131 // to another logr.Logger. You *must* call SetLogger to 132 // get any actual logging. 133 Log = log.Log 134 135 // LoggerFrom returns a logger with predefined values from a context.Context. 136 // The logger, when used with controllers, can be expected to contain basic information about the object 137 // that's being reconciled like: 138 // - `reconciler group` and `reconciler kind` coming from the For(...) object passed in when building a controller. 139 // - `name` and `namespace` from the reconciliation request. 140 // 141 // This is meant to be used with the context supplied in a struct that satisfies the Reconciler interface. 142 LoggerFrom = log.FromContext 143 144 // LoggerInto takes a context and sets the logger as one of its keys. 145 // 146 // This is meant to be used in reconcilers to enrich the logger within a context with additional values. 147 LoggerInto = log.IntoContext 148 149 // SetLogger sets a concrete logging implementation for all deferred Loggers. 150 SetLogger = log.SetLogger 151 ) 152