Add a meeting Rate this page

A

In this video we will take a look at queue, builder deployments plugin. We will see why and how can we use it?

A

So what does wms plugin do deployments plugin provides to users the whole code, the operator project, following the good practices to deploy and manage an image on a kubernetes cluster, which is a very common use case.

A

So for doing that, deployments, plugin provides a controller implementation for deploying and managing the image on the cluster. Along with the controller, it provides tests for checking the reconciliation implemented using env test sample. Mls are also added to the project for creating new custom resources, and these sample emails are updated with the specs used operand, which is the image the user wants to deploy and manage will be added on the manager via an environment variable. We will take a look at that later.

A

It provides api with spec and status implementation done with conditional status.

A

Finalizer implementation is also added.

A

So when to use diplomats plugin, if you are just getting started and want to learn about operators how to build them and use them, you can use diplomas plugin, since it will reduce the complexity of trying out operators and experimenting with them.

A

You can use this plugin if you're looking to deploy and manage an image using operator pattern and if you're looking to speed up deployments, plugin can come in very handy so how to use this plugin after creating a new project with queue builder in it, you can create apis using this plugin and while creating the apis, it is important to specify some flags.

A

The first important flag is the image flag for informing the image the user wants to deploy and manage. The second important flag is run as user for specifying the run as user value for the security context. Now comes two optional flags image control command for specifying the command to be used to initialize. The container image is run in the second optional flag. Is image container port for specifying the port of the container.

A

So what users win when they use this plugin.

A

So the users get tests for the reconciliation implemented by default, using envtest so by this users will not need to write the test implementation for their controllers from scratch. They will have a choice of just to build on top of the test scaffolded in the project, according to their custom controllers,.

A

So now comes the finalizer, so the reconciliation implemented adds the finalizer that demonstrates how to perform operations before letting the kubernetes api delete the resource, and all that depends on it. The finalizer operation added by default is to create a new event when the user deletes the custom resource.

A

So the image which user needs to deploy gets added on the manager via an environment variable.

A

So the reason of doing that is, if user wants to change the version or the whole image used in the operator, then there is no need to change the project code, build a new operator image and then deploy user can just change the value assigned to the environment variable in the manager ml and create a new custom resource.

A

So the controller implementation added by default adds and updates the status conditions of the customer source created accordingly. This provides useful information to the user about the health of the cr created.

A

So, let's see how easy it is to build a new operator using this plugin, so for that we will be doing a demo in which we will build a new operator using the memcached sample.

A

So now we will be building a new operator using deployments plugin and run it locally. So the first step will be to create a new project directory in our gopath.

A

Now we will initialize our project through cube builder init.

A

Now we will create our api.

A

So we'll specify the group.

A

Version.

A

And kind.

A

We will specify the plugin we want to. We want to use that is deployment plugin through plugins flag.

A

We will inform the image through image flag and we will specify the value of the run as user for security context through run as user flag.

A

Now we will skip, folder manifests.

A

So now we will take a look at the operator project, scaffolded.

A

So, in the main cache types dot go, we can see mean spec struct, which defines the desired state of the mean cache we can see. Size field is defined and we can also see some validations on top of that which defines the minimum and maximum values.

A

So the idea is that authors can use the open api and do these validations accordingly.

A

In the memcache status struct, which defines the observed state of the meme cache conditions, field is added and we will see what is added in that in our demo and the memcache struct, which defines the schema for the memcached api and the meme cache destruct, which contains a list of the meme cache are same as we do with other plugins and same with the init function.

A

So, let's take a look at meme, cache controller dot go file here we first define our finalizer string, which we'll use while adding a finalizer to our meme cache instance.

A

Then we define some strings for managing our status conditions in the memcacity conciller struct we can see a recorder is added. This is for implementing our finalizer operation, which is to create a new event.

A

So now, in the reconcile function, we first fetch the memcached instance.

A

We do that for checking whether the customer source for the kind meme cache is applied on the cluster or not, and if not, we return nil to stop the reconciliation.

A

If we fetch the meme cache instance successfully, we update the status of the memcache instance with starting reconciliation.

A

After that, we check whether the memecast instance have a finalizer or not, and if not, we add a finalizer to it.

A

Then we check that the memcached instance is marked to be deleted or not, which is indicated by the deletion timestamp, and if it is marked to be deleted, we perform our finalizer operation for performing the finalizer operation. We use do finalizer operation for meme cache function in this function. We create a new event with a a message informing that the customer source is being deleted.

A

A to-do comment is also added for the users informing that what other cleaner steps they can perform using this to finalize our operation function after performing the finalizer operation, we update the status of the memcached instance and remove the finalizer.

A

After that, we check if the deployment already exists and if not, we create a new deployment for creating a new deployment. We use deployment for memcached function.

A

So this function just returns. A new deployment object.

A

But if we see closely, we can see that the image we are getting from a function called image for meme cache function.

A

And this image for meme cache function is getting the image value from an environment variable. This is the environment variable we talked about, which is added to the manager.

A

So here is the environment variable which is used to store the value of the image.

A

So after the deployment is being created successfully, we check whether the deployment size is the same as defined by the size spec of the customer source, which we are reconciling and if it is not, we resize our deployment.

A

So now, after updating the size, we want to requeue the reconciliation for ensuring the state and after that, we set the status of the memcached instance with a message informing that the deployment for the custom resource has been created successfully.

A

So if we take a look at controller test.go file, this adds test for the reconciliation implemented by default using env test.

A

So for checking the reconciled logic, we first create a cr for the kind meme cache we check the cr was created successfully or not. We use the memcached sd conciler to reconcile that cr created and in the end, we check our deployment created in this way. It tests, whether our controller works properly or not.

A

So this is the sample ml, which is added to the operator project by default. So the user can use this ml for creating a custom resource directly in this spec we can see only size is present, but if user will add new specs for the memcache instance, this sample ml will be updated with the specs used.

A

So now we will run make install for installing the custom resource definitions.

A

So now, if we do cube sheet, you'll get crd, we can see our crd has been created.

A

So since we are running our operator locally, we need to export the environment variable. We are using to store the value of the image before running the operator outside the cluster. So now we will run our controller.

A

Through make run.

A

So our controller has run successfully.

A

So now we will create our custom resource by applying the sample ml, which is added in the operator project, so we'll apply the ml.

A

And so now, if we do cubes, it will get mean caches.

A

We can see an instance has been created, so, let's check our deployment has been created or not.

A

So yes, deployment has been created around and our pod is running and if we see at the logs of the controller, it says adding finalizer for mean cache and creating a new deployment.

A

So, let's see to the status conditions, we talk about added to the cr.

A

So these are the status conditions added and we can see a message which is informing that deployment for the custom source has been created successfully.

A

So we talked about the finalizer operation added by default, which is when the user will delete the custom resource. A new event will be created.

A

So if we delete the custom resource and look at the events, we can see a new event has been created and the message says that customer source is being deleted from the namespace default and if we see the logs of the controller, it says performing finalizer operations for the meme cache and after the finalizer operation has performed, it says, removing the finalizer for meme cache and after it is saying that meme cache resource not found since we have deleted the cr.

A

So we also talked about two optional flags which we can specify while creating an api with deploy image plugin. So let's create a new api with also specifying image, container command and image container port flag and see what is scaffolded in the operator project. When we specify those plugins too, so we will first initialize the project through queue builder in it.

A

So this is the command we previously used and now we will add the image container command flag, specifying the command which should be used for initializing the container and the image control port flag for specifying the port of the container.

A

Now we will scaffold the manifests.

A

So, let's take a look at the operator project when we also specify the container command and container port flex, so in the memcache types.go file.

A

We can see container port has been added to the memcache spec and in the sample ml for creating the custom resource we can see. Container port has been added to the spec with the value we specified, while creating the api.

A

And now, in the name, cache controller dot go file.

A

If we take a look in the deployment for meme cache function,.

A

In the container type containers type we can see, ports field has now been added.

A

And the commands field also have been added with the value of the command we specified, while creating the api.

A

So with this I would like to conclude the session here: is the link of diplomats, plugin documentation and feel free to make any suggestions or raise an issue on github? Thank you.
youtube image
From YouTube: Demo: Kubebuilder Deploy-Image plugin

Description

Deploy-Image is a Kubebuilder Golang plugin that provides users an operator solution for deploying and managing an image on a Kubernetes cluster, which is a very common use case.

- Documentation: https://master.book.kubebuilder.io/plugins/deploy-image-plugin-v1-alpha.html

- Project: https://github.com/kubernetes-sigs/kubebuilder

Please feel free to make any suggestions and raise an issue on Github.

#devops #kubernetes #operators #golang