How to synchronize Custom resource when its specification is updated - kubernetes

In a Kubernetes operator based on operator-sdk, do you know how to write code to synchronize CR resource when CR specification is updated with kubectl apply? Could you please provide some code samples?

It is mostly up to how you deploy things. The default skeleton gives you a Kustomize-based deployment structure so kustomize build config/default | kubectl apply -f. This is also wrapped up for you behind make deploy. There is also make install for just installing the generated CRD files.

Implementing it a go-lang based operator is pretty complex, and I would recommand studying the kubebuilder documentation and example in order to achieve that: https://book.kubebuilder.io/cronjob-tutorial/controller-implementation.html#implementing-a-controller

Related

Operator-SDK and NewController function

I am building an operator using operator-sdk version 1.2 and I do understand we have a reconciliation loop but I was referring to some GitHub repos and unable to make out the use of the NewController function. It seems that these GitHub repos are developed using operator-sdk but in operator-sdk 1.2, I do not need to find any Newcontroller function.
For example, I was referring to https://github.com/oracle/mysql-operator and looking at the https://github.com/oracle/mysql-operator/blob/master/pkg/controllers/cluster/controller.go and I do not find NewControllerfunction in the current operator-sdk.
Also, I do not understand how this MySQL operator is using kubeconfig? Do we need to pass the kubeconfig location to execute the command in the container? Is there a way to read the kube config without passing kubeconfig location in operator-sdk?
If you're building a new operator and you plan to use Operator SDK, then I recommend reading the official Operator SDK: Go tutorial. You can find another example of Go-based operator here.
Concerning the kube config, it will use your default location if you don't specify anything. So the default kubeconfig your kubectl is configured with.

prometheus-operator (helm chart) & alert manager

I have a query related with prometheus-operator helm chart & alert manager combination.
Currently we are using prometheus-operator helm chart:
https://github.com/helm/charts/tree/master/stable/prometheus-operator
and I wrote a simple rule in values.yml (this is just a sample code) to generate an alert:
further I am using alertmanager config/routes/receivers to send alerts. It's working perfectly fine.
But as part of real-time implementation, I may be having so many alert rules. Is there any way where I can bring these all rules in separate rules file & configure the path (rule file path) in values.yml (under: additionalPrometheusRules section)
I also saw kube-prometheus-stack & additionalPrometheusRulesMap (in values.yml):
https://github.com/prometheus-community/helm-charts/blob/main/charts/kube-prometheus-stack/values.yaml
But didn't fine any solution. Anyone can help me on this?
So helm doesn't allow includes in values.yaml files typically. I read that there's a way to do it, but it depends on how the chart is built and typically upstream maintainers don't use templates that way afaik (could be wrong there, but I've never noticed it).
Your problem is exactly the same problem I've been trying to solve adequately, and I think I came up with something. It's not perfect, but it is better than having one huge monolithic values.yaml file.
helm allows the operator to specify multiple values.yaml files using the paradigm, -f values1.yaml -f values2.yaml -f some-more-values.yaml, so I broke my values file up into multiple logically-divided yaml files.
There might be gotchas, so be aware, but so far for this use-case, it seems to be working. I'm still testing things out. https://helm.sh/docs/helm/helm_install/
You can also add your own custom rules file using config maps. In this way, you can avoid over alerting and get notified for specific alerts only.

How to step-into kubernetes source code using IDE?

Is it possible to run kubectl commands and see the run-time execution using an IDE like GoLand for example?
I would like to open 'apply.go' source code and see how the code is executed when I type: kubectl apply -f/hit Enter step-by-step.
Go does offer a debugger, Delve. But I don’t think this will be very helpful. Kubectl code is kind of hard to understand even if you already know it and kubectl apply is the single most complex thing in all of kubectl. So complex that the logic is being moved to the server side, in newer versions.
Lens is the best IDE for kubernetes that I have used upto now.
It serves everything in a plate , as there is no need to mug up the commands of kubectl,
you can make changes in any of kubernetes yaml file from lens and it will apply changes in to your cluster.
install lens for your OS: https://github.com/lensapp/lens/releases/
you can know more about lens from this link : https://github.com/lensapp/lens/

Is there a way to validate CloudFormation templates before running them?

I would like to validate my CloudFormation templates before running them. I know about the aws cloudformation validate-template ... cli command, but that ignores incorrect property names. I don't know what the point of that cli command is if it won't catch these kind of mistakes.
I want something that will catch those kind of mistakes before running the templates. An IDE or external service that does this would be fine.
We had a quite similar issue with erroneous Cloud-Formation templates and created (I’m a co-author) a command-line tool, that validates them - besides the standard AWS validation it also has many custom checks, that were essential for us:
https://github.com/Appliscale/perun
I believe it doesn't support property names validation yet, but any feature requests (or pull request even better), are welcome. We will do our best to address them as soon as we can.
After installing Perun, to validate the template you can use the command validate:
~ $ perun validate <PATH TO THE TEMPLATE>
Moreover, it also allows managing (creation, updates etc.) CF stacks and monitoring the status updates.
The cfn-lint tool was built for this exact purpose. It is actively maintained by the AWS team and it has a couple of IDE integrations.
Same issue with me. There is no way to validate the property name. But you can reduce the mistake using Atom IDE with plugins cloudformation, it helps me to create a resources property so I can reduce my typo mistakes.

Verify that all values for a kubernetes helm chart have been used

I'd like to check that my kubernetes helm chart does not define unused values in values.yaml. This should include any subcharts such that if you've defined subchart.foo.bar: ??? in the top-level values.yaml that key is definitely used in the subchart, or possibly as a short-cut mentioned in the subchart/values.yaml.
This is needed to prevent us from shipping bogus "documentation" in the values.yaml, for example if a key in a subchart has been changed or removed.
Ideally there would also be some possibility to report on which subchart values have not been overridden in the top-level chart, though this is less concerning.
Are there any existing tools that can help with this?
Since the Helm v3 release you can now define a schema for your values. On commands like helm install your provided values are automatically validated against the schema.
Please see the official documentation: https://helm.sh/docs/topics/charts/#schema-files
Schema validation works for subcharts too, this is also mentioned in the documentation on the link above.
AFAIK, there isn't a tool for that. However, it shouldn't be that hard to make one, even using bash. For example, you need to export all key/value pairs like this test.test1.test2 and grep for that string recursively in the templates folder. If you want to read yaml using bash, you can install shyaml. If you know how to code in Python, even better.
helm lint --detect-unused-values