Advanced Topics
The Conditions
field added to the MemcachedStatus
struct simplifies the management of your CR’s conditions. It:
- Enables callers to add and remove conditions.
- Ensures that there are no duplicates.
- Sorts the conditions deterministically to avoid unnecessary repeated reconciliations.
- Automatically handles the each condition’s
LastTransitionTime
. - Provides helper methods to make it easy to determine the state of a condition.
To use conditions in your custom resource, add a Conditions field to the Status struct in _types.go
:
Then, in your controller, you can use Conditions methods to make it easier to set and remove conditions or check their current values.
The operator’s Manager supports the core Kubernetes resource types as found in the client-go package and will also register the schemes of all custom resource types defined in your project.
import (
cachev1alpha1 "github.com/example/memcached-operator/api/v1alpha1
...
)
func init() {
utilruntime.Must(cachev1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
To add a 3rd party resource to an operator, you must add it to the Manager’s scheme. By creating an AddToScheme()
method or reusing one you can easily add a resource to your scheme. An example shows that you define a function and then use the package to create a SchemeBuilder
.
Register with the Manager’s scheme
Call the AddToScheme()
function for your 3rd party resource and pass it the Manager’s scheme via mgr.GetScheme()
or scheme
in main.go
. Example:
If 3rd party resource does not have AddToScheme()
function
Example of registering 3rd party resource from external-dns
:
import (
...
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
...
// DNSEndoints
externaldns "github.com/kubernetes-incubator/external-dns/endpoint"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func init() {
...
log.Info("Registering Components.")
schemeBuilder := &scheme.Builder{GroupVersion: schema.GroupVersion{Group: "externaldns.k8s.io", Version: "v1alpha1"}}
schemeBuilder.Register(&externaldns.DNSEndpoint{}, &externaldns.DNSEndpointList{})
if err := schemeBuilder.AddToScheme(mgr.GetScheme()); err != nil {
os.Exit(1)
}
}
NOTES:
- After adding new import paths to your operator project, run
go mod vendor
if avendor/
directory is present in the root of your project directory to fulfill these dependencies. - Your 3rd party resource needs to be added before add the controller in
"Setup all Controllers"
.
To learn about how metrics work in the Operator SDK read the metrics section of the Kubebuilder documentation.
To implement complex deletion logic, you can add a finalizer to your Custom Resource. This will prevent your Custom Resource from being deleted until you remove the finalizer (ie, after your cleanup logic has successfully run). For more information, see the .
Example:
The following is a snippet from a theoretical controller file controllers/memcached_controller.go
that implements a finalizer handler:
There are two different leader election implementations to choose from, each with its own tradeoff.
- Leader-with-lease: The leader pod periodically renews the leader lease and gives up leadership when it can’t renew the lease. This implementation allows for a faster transition to a new leader when the existing leader is isolated, but there is a possibility of split brain in .
- Leader-for-life: The leader pod only gives up leadership (via garbage collection) when it is deleted. This implementation precludes the possibility of 2 instances mistakenly running as leaders (split brain). However, this method can be subject to a delay in electing a new leader. For instance when the leader pod is on an unresponsive or partitioned node, the dictates how long it takes for the leader pod to be deleted from the node and step down (default 5m).
By default the SDK enables the leader-with-lease implementation. However you should consult the docs above for both approaches to consider the tradeoffs that make sense for your use case.
The following examples illustrate how to use the two options:
Leader for life
A call to leader.Become()
will block the operator as it retries until it can become the leader by creating the configmap named memcached-operator-lock
.
import (
...
"github.com/operator-framework/operator-lib/leader"
)
func main() {
...
err = leader.Become(context.TODO(), "memcached-operator-lock")
if err != nil {
log.Error(err, "Failed to retry for leader lock")
os.Exit(1)
}
}
If the operator is not running inside a cluster leader.Become()
will simply return without error to skip the leader election since it can’t detect the operator’s namespace.
Leader with lease
The leader-with-lease approach can be enabled via the Manager Options for leader election.