Complete the code to define a basic Custom Resource Definition (CRD) in Kubernetes.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: myresources.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
scope: [1]
names:
plural: myresources
singular: myresource
kind: MyResource
shortNames:
- mrThe scope field defines if the CRD is namespaced or cluster-wide. Namespaced means the resource exists within a namespace.
Complete the command to create an Operator using the Operator SDK CLI.
operator-sdk init --domain example.com --repo github.com/example/[1]The --repo flag specifies the Go module path for the operator project. It is common to name it after your operator, like my-operator.
Fix the error in the reconciliation function signature in a Go Operator controller.
func (r *MyResourceReconciler) Reconcile(ctx context.Context, req [1]) (ctrl.Result, error) {The Reconcile method receives a ctrl.Request parameter which contains information about the object to reconcile.
Fill both blanks to create a watch on a Kubernetes resource in the Operator controller setup.
func (r *MyResourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&[1]{}).
Owns(&[2]{}).
Complete(r)
}The For method watches the primary resource type, here MyResource. The Owns method watches related resources like Deployment owned by the operator.
Fill all three blanks to define a simple reconciliation loop that fetches a resource, checks if it exists, and returns without error if not found.
func (r *MyResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var resource [1]
err := r.Client.Get(ctx, req.NamespacedName, &[3])
if [2] {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
// Continue reconciliation logic here
return ctrl.Result{}, nil
}The variable resource is of the custom resource type. The code checks if err != nil to handle errors. The variable resource is passed to Get to fetch the object.