controllers

package
v0.0.0-...-29dac4d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeploymentResourceStub

type DeploymentResourceStub struct {
	Expected                         k8sres.Deployment
	okthelpers.MutableResourceObject // OKT K8S resource
	oktres.MutationHelper
}

DeploymentResourceStub an OKT extended Deployment resource

func (*DeploymentResourceStub) GetExpected

func (r *DeploymentResourceStub) GetExpected() *k8sres.Deployment

GetExpected Implements a Stub interface function to get the Expected object

func (*DeploymentResourceStub) GetHashableRefHelper

func (r *DeploymentResourceStub) GetHashableRefHelper() *okthelpers.HashableRefHelper

GetHashableRefHelper provide an helper for the HashableRef interface This will help to defines which object(s) data has to be used to detect modifications thanks to the Hash computation The AddSpec() method of this helper adds the whole "Spec" object in the hashable Ref. The Spec can be something else than the typical K8S Spec resource part when it does not exist in the K8S API definition It can be defined by the MutationHelper for this resource, eventualy in accordance with Pre/PostMutate() methods provided with the helper.

func (*DeploymentResourceStub) GetResourceObject

func (r *DeploymentResourceStub) GetResourceObject() *okthelpers.ResourceObject

GetResourceObject Implement a Stub interface function to get the Mutable Object

func (*DeploymentResourceStub) Init

func (r *DeploymentResourceStub) Init(client k8sclient.Client, namespace, name string) error

Init Initialize OKT resource with K8S runtime object in the same Namespace of the Custom Resource

func (*DeploymentResourceStub) PostMutate

func (r *DeploymentResourceStub) PostMutate(cr k8sclient.Object, scheme *runtime.Scheme) error

PostMutate xx

func (*DeploymentResourceStub) PreMutate

func (r *DeploymentResourceStub) PreMutate(scheme *runtime.Scheme) error

PreMutate xx

type MemcachedReconciler

type MemcachedReconciler struct {
	oktreconciler.AdvancedObject // Reconciler type

	// My resources
	CR cachev1alpha1.Memcached
	// contains filtered or unexported fields
}

MemcachedReconciler reconciles a Memcached object

REPLACEMENT4OKT
type MemcachedReconciler struct {
	client.Client
	Log    logr.Logger
	Scheme *runtime.Scheme
}

ADDED4OKT

func (*MemcachedReconciler) EnterInState

func (r *MemcachedReconciler) EnterInState(engine *oktengines.Stepper)
REPLACED4OKT: OKT Reconciler let you manage the Reconciliation differently

// Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. // TODO(user): Modify the Reconcile function to compare the state specified by // the Memcached object against the actual cluster state, and then // perform operations to make the cluster state reflect the state specified by // the user. // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.7.2/pkg/reconcile

func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	log := r.Log.WithValues("memcached", req.NamespacedName)

	// Fetch the Memcached instance
	memcached := &cachev1alpha1.Memcached{}
	err := r.Get(ctx, req.NamespacedName, memcached)
	if err != nil {
		if errors.IsNotFound(err) {
			// Request object not found, could have been deleted after reconcile request.
			// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
			// Return and don't requeue
			log.Info("Memcached resource not found. Ignoring since object must be deleted")
			return ctrl.Result{}, nil
		}
		// Error reading the object - requeue the request.
		log.Error(err, "Failed to get Memcached")
		return ctrl.Result{}, err
	}

	// Check if the deployment already exists, if not create a new one
	found := &appsv1.Deployment{}
	err = r.Get(ctx, types.NamespacedName{Name: memcached.Name, Namespace: memcached.Namespace}, found)
	if err != nil && errors.IsNotFound(err) {
		// Define a new deployment
		dep := r.deploymentForMemcached(memcached)
		log.Info("Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
		err = r.Create(ctx, dep)
		if err != nil {
			log.Error(err, "Failed to create new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
			return ctrl.Result{}, err
		}
		// Deployment created successfully - return and requeue
		return ctrl.Result{Requeue: true}, nil
	} else if err != nil {
		log.Error(err, "Failed to get Deployment")
		return ctrl.Result{}, err
	}

	// Ensure the deployment size is the same as the spec
	size := memcached.Spec.Size
	if *found.Spec.Replicas != size {
		found.Spec.Replicas = &size
		err = r.Update(ctx, found)
		if err != nil {
			log.Error(err, "Failed to update Deployment", "Deployment.Namespace", found.Namespace, "Deployment.Name", found.Name)
			return ctrl.Result{}, err
		}
		// Ask to requeue after 1 minute in order to give enough time for the
		// pods be created on the cluster side and the operand be able
		// to do the next update step accurately.
		return ctrl.Result{RequeueAfter: time.Minute}, nil
	}

	// Update the Memcached status with the pod names
	// List the pods for this memcached's deployment
	podList := &corev1.PodList{}
	listOpts := []client.ListOption{
		client.InNamespace(memcached.Namespace),
		client.MatchingLabels(labelsForMemcached(memcached.Name)),
	}
	if err = r.List(ctx, podList, listOpts...); err != nil {
		log.Error(err, "Failed to list pods", "Memcached.Namespace", memcached.Namespace, "Memcached.Name", memcached.Name)
		return ctrl.Result{}, err
	}
	podNames := getPodNames(podList.Items)

	// Update status.Nodes if needed
	if !reflect.DeepEqual(podNames, memcached.Status.Nodes) {
		memcached.Status.Nodes = podNames
		err := r.Status().Update(ctx, memcached)
		if err != nil {
			log.Error(err, "Failed to update Memcached status")
			return ctrl.Result{}, err
		}
	}

	return ctrl.Result{}, nil
}

EnterInState is the callback used by the Stepper engine when the Reconciliation enters in a new state

func (*MemcachedReconciler) SetupWithManager

func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error

SetupWithManager sets up the controller with the Manager.

type ResourceMCDeploymentMutator

type ResourceMCDeploymentMutator struct {
	DeploymentResourceStub
	// contains filtered or unexported fields
}

ResourceMCDeploymentMutator xx

func NewResourceMCDeploymentMutator

func NewResourceMCDeploymentMutator(cr *appapi.Memcached, client k8sclient.Client, namespace, name string) (*ResourceMCDeploymentMutator, error)

NewResourceMCDeploymentMutator xx

func (*ResourceMCDeploymentMutator) GetHashableRef

func (r *ResourceMCDeploymentMutator) GetHashableRef() okthash.HashableRef

GetHashableRef xx Note that a Spec reference can always be added by the helper. It is either the K8S Object's Spec or data as defined by OKT dictionary used by the resource code generator

func (*ResourceMCDeploymentMutator) MutateWithCR

func (r *ResourceMCDeploymentMutator) MutateWithCR() (requeueAfterSeconds uint16, err error)

MutateWithCR xx

func (*ResourceMCDeploymentMutator) MutateWithInitialData

func (r *ResourceMCDeploymentMutator) MutateWithInitialData() error

MutateWithInitialData Initialize the Expected object with intial deployment data

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL