Tutorial Highlights & Transcript

  • 00:00 - Features of tfk8s

    • This is a presentation on a tool called tfk8s, which makes working with the Terraform Kubernetes Provider a lot easier. There are a few features for this tool, but essentially the different variations of converting Kubernetes YAML files to HCL maps. So you can convert YAML files with one or more manifests, you can convert entire directory trees, which is very helpful. And you can convert Helm charts. Also, you can use commands to output "KubeCTL get" commands as maps instead of YAML, which is pretty useful. Being able to do that is obviously very handy because trying to write these by hand with a number of brackets, you're inevitably gonna have some kind of syntax error. This saves a lot of time, firstly, of doing that manually, but also, debugging syntax errors is just handy being able to automate this. At the moment, I'm sure some people really use that.

      But there's a Terraform built-in function that does a similar thing called yaml.decode. So if you use that in the Terraform code, you would have to put your YAML code directly in and then just call a function around it. I think that this is a lot neater, because you can also use this for multiple manifests. Where before, with YAML decode, you would have to separate out your different manifests into different committees resources. With this, it'll do that for you. Also, getting the extra functionality of against directory trees and Helm charts and it's just a better tool ready.

  • 01:44 - Installation

    • It's a tool, it's written in GO and it uses some regex, as well. Installing it is really simple. I've got the GitHub link, which I can send out afterwards. But the installation I just used brew install, which is nice and easy. But you can also use Mac ports, there's a go install, you can clone and a make install and clone repo.

  • 02:03 - Usage

    • The usage of it is really simple. There's two different ways of doing it. You can either run tfk8s, and just do a flag for the file. Then you can also output and that's optional, you can just print the console, output to a different file. You can do it that way around or the other way around. You can just cat the file, and then pipe that to tfk8s, and put it into the file. I will do some demos of this in a moment.

      Then the other ways we are able to use it, you can use it against the directory, so you just do a find command for the directory name. It's quite long so I'll probably copy and paste that one but using that is really helpful. You can just go through a directory tree and look for any YAML files and convert those to the Terraform files. You can also use it for running Kubernetes resources and output those as maps instead. Also, there's a way to use a flag to strip those server side fields. It's a lot neater.

      The Helm charts are quite straightforward using the Helm template command. You can input that value file, and it will convert that to Kubernetes manifest resources.

  • 03:26 - Demo

    • The majority of this is going to be a demo. On here, I've got a few different examples I'll go through. So the first one, I just made a simple nginx.YAML file. I have these agents that are in the next pod running on port 80. The two different ways of being able to run this, you can do tfk8s and then pass in the file, so like this, and it'll just print to the console. Or the other way round of doing this. The other way, it would be like this. Pipe that to tfk8s. Both pretty simple. The other flags that you can do you can do an output. I can say I want this to go to the nginx pod Terraform file. And that's really handy just being able to start to convert those into different files. Another thing that you can do with this, you can strip out the quotes. If you need to do that, you can just get rid of any of the unnecessary quotes. Another thing to do, you can get the map part if you need that for whatever reason you don't want the Kubernetes resource around it, you can just grab the map. So that is for a single example with just one manifest file.

      If I go here, so I've just made a simple nginx app file. It's got a service in it and a deployment but you can see there's two different manifests here. It's exactly the same, it's so simple. So I can do this to, again, print to the console. Or if I want to output that to a file, that -O flag. You can now separate it into two different Kubernetes manifest resources. You'll notice for the naming, it takes the kind and then there's like an underscore and the name. So all of your resources will be named in that kind of convention here.

      Then I can show a Kubernetes running example. Using this, I could get my namespace and instead of getting that as YAML, I can directly convert the output YAML into map, which is really useful. I could do that for other resources. For example, if I just create a nginx file with a pod in it, I can just do an apply on that. Get pods. Normally, where I could do GitHub web, I could do an output of YAML with that. I can pipe that to tfk8s to get that as map instead. Then if I use -S, I can strip out the server side fields as well, which is really nice and neat. I can grab just the map and server side fields, as well. You can tie them together and it just saves a lot of legwork trying to do that manually.

      I've got a couple more examples I can show. One is as the directory tree. In my directory example, over here, I've got four different directories. Leave that and I just took back some different Kubernetes resources here. I've got a daemon set there, got some deployments there, a couple of services in here, and a web app. If I run this from here, go through each of those valves, we got a Terraform file for each of those, which is pretty powerful, yet something so simple. With each of those, I got also some that had multiple manifests in the directory for a moment. I debriefed a web app, a cat map that will serve the YAML because I got two manifests in the same file here. Again, it will just separate those out into separate Kubernetes manifests, which is really cool.

      Then I've got one more example for the Helm chart. I will create a Helm chart right now. This is just a more related default. So we've got all the template files here, we've got the YAML file here. Where you normally run just the Helm template then you can then use case two. As you can see, just convert that into the entire file with all the manifests. I think the most powerful thing I could do because of that. Here, the deployments were converted into different Kubernetes manifest resources.

      This isn't being used in my specific project at the moment, but I know that we have some different ones where we need to be doing this kind of stuff. I would absolutely use this tool to do that tather than doing that by hand. Anytime that you can automate something just takes out a huge error, which is so easily done when you look at the syntax of these files. It's a really handy tool, so easy to install and so easy to use.

Grace Christman

Grace Christman

DevOps Engineer


Grace is a DevOps Engineer at nClouds and an AWS Certified Developer - Associate and AWS Certified Solutions Architect - Associate.

Contact Us Now

You can also email us directly at sales@nclouds.com for your inquiries or use the form below

Subscribe to Our Newsletter

Join our community of DevOps enthusiasts. Get free tips, advice, and insights from our industry-leading team of AWS experts.