 The following is a Coderland presentation about our newest attraction, the Compile Driver. Hello, this is Doug Tidwell for Coderland. In this video, we'll take a look at Knative itself. We'll cover the three parts of Knative, we'll talk about how to install it on Minixift, and we'll deploy our image processing code as a serverless function. With all that done, we'll talk about ways to invoke the code from the command line and from the React front end we looked at in the previous video. So let's talk a little bit about Knative. It has three components. Knative build, as you would expect, builds and packages your code. Knative eventing lets you define events that should trigger your code and Knative serving manages your functions, including scaling them to zero whenever they're not being used. To keep it simple, we'll focus on Knative serving here. We've already built the code for you, so you don't have to worry about Knative build. And instead of setting up events, we'll simply call the serverless function when we need it. We'll cover the other parts of Knative some other time. We're assuming you've been through the first video and have the cloned repose installed on your machine. If not, you can find the image manipulation code at this URL. And Don Schinck's React front end here. Now let's move on to the install. In this video, we'll use MiniShift, a single node version of OpenShift, Red Hat's Kubernetes distribution. The simplest way to get it is to install the CDK. The Red Hat container development kit available again at the URL on your screen. You can, of course, get a copy of the CDK if you're a member of the Red Hat developer program. If you're not, you should be. It's free. Once you've got the CDK installed, it's time to get started. The overview of what you'll do now used to be, one, starting a Kubernetes cluster, two, installing Istio, then three, installing Knative, and finally, D, installing your code. But that process is so two weeks ago. Our good friend Kamesh Sampath has an awesome Knative tutorial that we'll be leaning on throughout this video. The tutorial is at this URL, and we cannot praise it highly enough. Go ahead and open that URL in the second browser tab so you can follow along, and we really, really hope you're following along. Click the setup link in the tutorial and make sure you have all of the prerequisites installed. Now, clone the Knative tutorial repo. With that done, set the tutorial home environment variable and switch to tutorial home slash work. Now, clone the Knative operators repo from the work directory. Before you run the magic script that makes your life easy, take out one line. I'll edit it with Nano, but obviously use whatever editor you like. I'm deleting the OpenShift version setting. As of mid-February 2019, if you're using the latest CDK, you've got version 3.11.43. Even better, the CDK makes sure everything is in sync, so you can just delete this line. Save the file, exit your text editor, and run it. Our sincere apologies if you enjoy typing dozens and dozens of commands and configuration statements, but Kamesh's wonderful script does all the work for you. That's great news for those of us who like our installations to be automatic and painless. But make no mistake, the script takes a while. It does a massive amount of work for you, and it's at least one order of magnitude faster than doing things yourself. Once the script is finished, run the three commands you should always run after MiniShift starts. Eval, Dollar, Perrin, MiniShift, OC, ENV. Eval, Dollar, Perrin, MiniShift, Docker, ENV. And OC, Login, Dash, U, Admin, Dash, P, Admin. That sets up the OC command, the Docker environment, and logs you in. This takes us to the moment you've possibly been waiting for. Step D, deploying your serverless code. The commands you'll need are currently in the Configure OpenShift Project section in part one of Kamesh's tutorial. Start with OC, new project, Knative tutorial, then the two policy commands you see here. Once you run those, you're all set to deploy your code. Switch to the Image Overlay repo you cloned on your machine earlier. It contains a file called service.yaml that tells Knative how to deploy your code. The yaml file gives your service a name and, most importantly, has the URL of the container image for your serverless code. Now take a deep breath and type OC apply dash in Knative tutorial dash f service dot yaml. Let's go to the console to see what we just deployed. Type MiniShift console and it will pop up in your default browser. When you see this panel, enter admin as your username and password and you should see the service you just deployed. There are running pods associated with the service. I took my time opening the console, so as you can see, Knative is already terminating the services pods because no one invoked the service. The number of pods will soon drop to zero. When we invoke it later, the system will start a pod for us, or multiple pods depending on the demand on the serverless function. Your service is finally deployed, so let's invoke it. As with anything managed by Istio, we need to get the IP address and port number for the ingress gateway. We'll send our requests to that address and Istio will figure out how to route our request to the right service with a complication that we'll talk about in a minute. Stand back from the screen because here comes the command to get the address. First, we get the IP address of the MiniShift cluster. That's pretty simple. The hard part is getting the port number. This part of the command uses JSON to extract it, then we wrap things up with slash overlay image. Cut and paste this from the tutorial unless you really enjoy typing. Here's what the IP address of the endpoint looks like on my system. Your mileage of course may vary. The aforementioned complication here is that Knative uses the HTTP host header to route the request to our service. Type oc-in KnativeTutorial, get services.serving.knative.dev to see the domains of your services. As you can see here, the domain is the name of the service, then the namespace, then example.com. That tells Istio which service in which namespace should handle the request. The value of that domain has to be the value of the host header when you invoke the service. Unless you change the configuration of the cluster, which we're not going to do here. So now that we have all the pieces of the puzzle, let's use curl to invoke the service. Here's the delightfully simple command. We use dash x to make this a post. We set the host header with the domain of the service. We specify that we're sending and receiving JSON. And we pass in the file sample input dot text as the data. Finally, we have the actual URL. Hit enter and within a few seconds, you should get a screen full of JSON, including a bunch of base64 encoded data. If there are no pods running when you invoke the service, you may have to try it again, but everything should work. Obviously you'll want to redirect that output to a file, extract the image data and decode it to see the updated photo just as we did in the first video. You can also edit the curl test shell script or curl test dot command to change the URL of the service and add the host header. That will save you a lot of typing. Although you're no doubt thrilled that you invoked your service from the command line, you'd probably like to use Don's React interface to work with the service as well. And that's where things get complicated. Or actually that's where things get more complicated. The problem is that Don's code uses an XML HTTP request and XHR to access the remote function and update the image. But an XHR isn't allowed to set the value of the host header. To get around this problem, I wrote a proxy that can change the host header. Don's code can call my proxy, which fixes the header and calls the function in Knative. Look, another URL on your screen. This is the address of the repo for my proxy. So we'll point Don's code to the proxy. When Don's code calls the proxy, the proxy fixes the host header and sends the data to Knative. The results are sent back to Don's code. Obviously that means you have to configure Don's code and the proxy to use the correct values. To do that, go to the terminal where you're running Don's code and set the variable ReactAppOverlayUrl to HTTP colon slash slash localhost port 8888 slash overlay image. Now create a new terminal and switch to the directory with the Knative proxy. Set the variable proxy URL to the IP address you discovered earlier, again with slash overlay image, and set the variable proxy host to the value of the services domain. NPM install, then NPM start, and the proxy is up and running. Now switch to the terminal where you're running Don's code. Restart it and reload the browser if you need to. Click the take picture button and everything should work. An updated image from a serverless function managed by Knative just popped up on your screen. How cool is that? You'll see a message in the proxies window that indicates Don's code has called the proxy successfully. If you get any errors, just reload the web page and try it again. As we were building the Compile Driver, we had high hopes to replace the proxy with an Istio wildcard routing rule and some Kubernetes magic, but for reasons we couldn't discern, I wasn't able to get it to work. So we're going with the proxy even though it's a hack. We're all programmers here after all, and as you know, your Cluj that works beats my elegant architecture that doesn't. Watch this space, we'll post a new video if, or when, we get the non-proxy version working. And with that, we'll bring our discussion of the Compile Driver to a close. The image manipulation code is deployed as a serverless function that scales to zero when it's not being used. When we need it though, Knative fires up a pod and we can use it. We encourage you to do more exploring with Knative. Kamesh's tutorial is a fantastic resource that's always being improved and expanded, and watch for more serverless content here at the Red Hat Developer Program as well. We appreciate you playing along here. We hope this has been useful. We're always expanding Coderland, so please let us hear from you. We look forward to your comments, suggestions, and PRs. Thanks so much for watching. For Coderland, this is Doug Tidwell saying may all your bugs be shallow. Cheers.