Calling DigitalOcean APIs on Kluster's add event | Writing K8S Operator - Part 3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi hello everyone my name is vivek i am a software developer i make videos about containers kubernetes go as a programming language distributed systems and sometimes about software engineering in general so if you have been liking the kinds of videos that i make you should consider subscribing to the channel and you should follow me on twitter as well in this particular video we are going to continue uh writing the operator that we have been writing in some of the previous videos uh if i try to very quickly summarize the operator that we are going to write so the name of the operator is cluster k l u s t e r and this operator is going to provision kubernetes clusters on digital version automatically so we are going to or we have already introduced a resource of type cluster and as soon as a cluster resource is created on on your local kubernetes cluster in our case kind a respective digital solution cluster would be created on on your digital ocean account so this is what we are going to achieve as part of this playlist uh a lot of things have already been achieved so for example we were already able to register a new new kubernetes resource we were able to generate crds and crs etc we were also able to write a very basic skeleton of operator where as soon as a particular cluster resource is created we we would be able to handle add and delete events on on the cluster resources so that is what we have already achieved in this particular video we are actually going to build or write the code that is going to create cluster on digital version so i have already opened the source code here if i try to run git status just to make sure that we have not made any changes we don't see any changes there now if we go to our source file cluster.go if you see here we have the cluster spec here so as soon as a particular cluster resource is created on your kubernetes cluster we are able to handle the add and delete events and if you see here in process next item we are getting the cluster spec of the resource or the spec of the resource that was created now what we have to do is we have to create we have to call digital vision apis to actually spin up a kubernetes cluster using the specifications or using the spec that have been specified here so to do that what i'm going to do is let's let's just go ahead and try to create a new directory named digitalocean and inside knew that inside this new directory i'm going to create a file named digitalocean.go now if you are actually writing this this application this product that is going to be deployed on production uh this is not how you are going to do the things in that case maybe you will be creating an abstraction that is going to have some some behavior and then your providers are actually going to implement those behaviors so what i'm trying to say is uh if it was if it was a production ready code we would you would design an interface and this interface is going to have some methods for example create cluster delete cluster and get config maybe and in after that these these providers for example cloud providers uh digital ocean is going to implement this interface similarly let's say aks is going to implement this interface eks and and etc so this is how you are going to do this uh if you are writing this code to run on production but right now we are just trying to understand the concepts of operator that is the reason i'm just going to simplify it so let's call it package deal and inside package view we are going to have a function that says create and we know that we are going to create kubernetes cluster so we don't necessarily have to specify create cluster in the name so if we talk about create create is actually [Music] create should have access to create should have access to the token that we are going to use to authenticate against digital ocean apis so first of all let's just go ahead and try to look into the digitization api to create a kubernetes cluster so this is digital ocean api reference if we go to kubernetes and create a new kubernetes cluster and if you if we go to uh go examples here if you see here we are initially or we are first of all initializing the client so let's just go ahead and try to do this so import and then we are going to say go to dot new from token and then here we are going to specify the token and then this returns client okay now the thing is how are we going to figure out the token so if you remember from our previous videos what we are doing is when we create a particular cluster we specify a secret that contains the digitalocean token so what we are going to do is this is this is the secret name but if we talk about if we just have the secret name how are we going to get the token out of that secret first of all we will have to query that secret from kubernetes api server and to do that we will have to have the namespace as well so what we are going to do is we when we specify token secret in in cluster spec we are going to specify in the form of namespace slash name so in our case let's say we we have the token in default namespace and name of the name of the token is do secret so what we are going to do now is when we call so first of all we will have to in cluster dot go we will have to import this new package that we have introduced so what we are going to do is we are going to say github.com okay and then we are going to say du dot create and we will also have to we will also have to pass the name of the secret so let's let's do that and name of the secret is if we go have to go ahead and have a look into types dot go so if you if you see here clusters pick and for cluster spec we have we have token secret so let's try to do that so crystal dot spec dot token secret okay now what we have to do is now that we have secret name let's let's query kubernetes api server to actually get that secret so that we can get the token from that secret but but before doing that let's go ahead and try to create the secret just to show you how the secret is going to look like so first of all this is my this is my digital lotion token and what we are going to do is we are going to say cube cutter create secret and generic we are going to create this in default namespace and we are going to name this doc grid because that is the name that we are specifying in cluster resource so do secret and from literal key we know that secret is secret is a kubernetes resource where we can store values in the form of key and value pairs so key is going to be token and value is going to be value is going to be my digital lotion token and let's go ahead and create that now that now that we have created the created the secret if we get the secret here we go so this is what we have to do we have to get the secret that have been specified or that has been specified in cluster resource and then we will have to get the value for token key this is what we have to do so let's just go ahead and try to do that so get token and pass the secret name here and it returns let's say token and error token is going to be of type string and error would be error and it expects name okay and we have to pass token here and if error is not equals to nil in that case we are going to return error okay so now from cluster.gov we are calling digitalocean.create and digitalocean.create returns an error and before actually creating the cluster we know that we will have to get the token using which we are going to call the digital ocean apis and that is the reason we are doing all these things now what we have to do is here we will have to call kubernetes api server but to call kubernetes api server we will have to have kubernetes client set so let's go back again uh to where we are calling do dot create form and let's see if we have kubernetes client set here and if you see we we don't have kubernetes client set initialized so we have this client set for custom resources but we don't have a kubernetes client set so what we are going to do is lets add another field in controller so that we can access it uh from from other methods as well so we are going to call it client and this is going to be of type kubernetes dot interface okay and when we call new control controller we will also expect a client for standard kubernetes resources and this is going to be of type kubernetes dot interface here we go now we will have to change our main dot go to pass this particular parameter while calling new controller so here we are calling new controller so let's just go ahead and try to try to create client set for kubernetes native resources using the config that we already have so let me just go ahead and refer upload so okay so kubernetes.config can be used and this gives us client and error so if error is we are going to log this but you can do uh anything that you want locating standard client and this is the error and let's go ahead and pass this client to new controller okay so now now i think we are on in a good shape so in cluster.go we have client set standard landsat field in controller so when we call do dot create we can also pass c dot client here and this returns and hit it okay here we go let's name it c and pass that to to get token and git token is now expecting a client of type kubernetes dot interface okay so now that we have kubernetes client set here we can easily call client dot core v1 because we know that secrets are in core group and v1 version support v1 dot secrets and we also have to figure out namespace so here we are going to specify namespace dot okay and for gate we have to specify context and the name and obviously meta v1 meter even dot get options but before that let's go ahead and try to try to discuss how are we going to specify how are we going to figure out namespace and name so if we go and have a look into this this manifest again we know that we are specifying we are specifying secret with namespace and name so here here we see we have namespace and name excuse me so let's what we have to do is we have to just split this string with slash and then first string is going to be name space and second is going to be name so it's it's just uh that simple so this is not actually secret name let's call it secret and in git token what we are going to do is we are going to split secret so strings dot split secret from slash and the first part is going to be namespace so 0s element is going to be namespace and first element is going to be name here we go so we have to just get meta v1 package all right so this gets us secret and error if error is not equals to nil in that case read on empty string and error but if error is nil it simply means that we got the secret so what we have to return is we have to return if you see here we have to written secret dot data and data value of data is map of string and string so we have to get the value for token so s dot data and we have to we have to return the value for key token this can be a cons this can be improved but right now let's just uh have it this way so now that we have now that we have token and we have initialized the digital ocean client uh let's just go ahead and actually call a digital ocean api to create the kubernetes cluster so we are going to refer the official api reference again so to actually call to actually call kubernetes.create we will have to first create the request and the request is if you see here kubernetes cluster create request so let's do that let's initialize that so go to dot open use cluster create request or let's do this let's just copy so this should be a reference so equals coolant equals to this and then we are going to call dot kubernetes dot cluster and this is going to this kubernetes dot create is going to expect our t request as input and okay so it also expects a context so let's create a new context here and here we go this dot create returns if you see here kubernetes cluster response and error so cluster response and error so if error is not equals to nil in that case what we want to do is we are going to [Music] return error and if there is no error in that case we are going to return we are going to return cluster id and then nil so cluster id is going to be cluster dot id and then we are going to return nil so we will have to change the signature of create method to return string and error so we will have to change these lines because now we return to we return two fields instead of one so this according to me should work now but what we have to do is let's just go ahead in dot go and make sure okay so we get cluster id here and let's just print the cluster id so fmt dot print f or [Music] so it looks like things should work now okay so the problem that i i think that we are going to face is we have not specified reason and version to be correct values so this is what is going to fail first and the other thing that i just remembered is we have not actually initialized i mean we have initialized but but with empty values so we'll have to specify values for create request as well if we look into this particular documentation name reason version is required and then node goals so name is going to be okay so apart from kubernetes client set and secret name uh if you now see we will also need the clusters pick here so for example if we create this cluster resource with this particular specification we need this specification in this particular package so that we can create the api digitalocean api with these uh these configurations so let's go ahead and do that as well so here we will have to pass we will have to pass cluster spec as well or if we just pass cluster space this would also be a good idea so we are passing cluster stick here and this is going to be of type so if you see here this is going to be off type v1 alpha 1 dot cluster spec so so we know that this is our module name inside module we have to go for package apis vivxing.tip and then v1 alpha one so package apis group is the week same dot tab and then version is v1 alpha 1 and that is what is the type of this spec so v1 alpha 1 dot cluster spec okay so and here we have to pass the secret name so we know the secret name is spec dot token secret okay so we are in a good shape here name is going to be spec dot name because we know that we are specifying name to be cluster name now this is redundant we we see here so either i mean i think we can remove this field from crd spec or cr spec and we can create the cluster with the same name as resource name so let's just we are not going to do this now we are going to now as of now we are going to create the cluster with cluster.spec.name and apart from name the other required field is reason and version so reason is going to be this version is going to be this so now reason can we can get the reason from spec dot reason and similarly spec dot version okay and we will also have to specify node pools here if you see node rules is also required item so you know what tools is a slice of type node poll create request and we have to initialize okay and inside node pool we can see what are all the resources what are all the fields that are required so size name and count so size and then name and then count so let's let's say we want to create a kubernetes cluster with with with three nodes in it so ideally we should get the count as part of this specification so let's just go ahead and see if we have uh that yeah so we are getting the count in node poll itself so that that is good what we are going to do now is so let's see three just to just to compile the code i'm going to hard code it to three so now i think we are good the only thing that we have to that we have to now figure out is what are all the supported values for reason version and then for node poles so if we see if we look at the crd we can also specify node pools here and node pose is obviously a slice that has that has count name and size so let's just go ahead and try to do that so node pools node pulses and array and count is let's say three name is going to be let's say dummy node pool and size is going to be now we will have to figure out we will have to figure out what are all the sizes that size is off this is this size is the size of instance or notes that are going to be created so we will have to figure out three things here what are all the reasons that are supported version and then size of the node poll so let's see how we can figure those things out so if you say here this looks like a valid size here so let's just go ahead and copy these things so reason is going to be nyc one version is going to be so this is very stale version i don't think this is going to be supported now so we will have to figure out version manually and then let's specify signs to be this one okay so yeah uh if you are not getting something don't worry about it i'm just going to i'm going to uh summarize this in in a bit so now all we have to figure out is kubernetes version and to do that let's see if there is another api uh to to do that so retrieve an existing kubernetes cluster update delete delete retrieve cue config credentials upgrade retrieve user information and here we go list available reasons node sizes and versions of kubernetes so this is this is the api that we can call okay here we go okay so if you see here these are all the these are all the sizes we have already figured out size so we don't have to worry about that and these are all the these are all the versions that are supported so let's say we are going to use this particular version we we are going to or we want to create kubernetes cluster with this particular version so let's just go ahead and try to do that now i think we are we are pretty good so let's just go ahead and try to run this so go build and let's see if we have the crd already created if we don't have the crd we will have to create the crd so create hyphen f manifests and then crd is clustered start yaml okay and we have the crd now so let's run our operator and create a cluster so if you see here let me open my my digital ocean console so if we go to kubernetes we should not see if you see here we don't have any we don't have any kubernetes cluster but as soon as we go ahead and we try to create this manifest so this cluster resource a particular cluster with this particular specification should be created on digitalocean so this is what our our approach or uh our operator was supposed to do so quitter and then cluster one if everything goes as expected okay yeah i think something went wrong because cluster id is not is not displayed here but yeah let's see if we are if we have kubernetes cluster okay so so obviously something went wrong and that is the reason we don't see cluster id created here so let's go ahead and try to try to debug that okay so we did not print the error here that might be the problem i mean we would at least uh get to know the error so printf error percentage is i'm going to delete this cluster resource for now and we are going to build the application again and then we are going to run this okay so yeah yeah now we know what exactly the problem is so it says validation validation error worker node poll missing name invalid name and then missing side slug and now i remember when we called when we called distillation api we did not actually specify size and name and count so this is the problem so what we have to do now is we have to get the size and we know that size is node poles 0 dot size so stick dot node post 0 dot size similarly spec dot node bools 1 dot size and count is also going to be spec dot node rules zero dot count and this should work now if i if i try to explain it i mean this is not very good code i would say so for example if you don't specify node pools here this is going to this is going to break etc so maybe you you will have to write your own logic to validate to validate if this pick has node poles or not similarly if we have more than one node pools in that case all those node pools should be specified as value of this node this node for node pole so all those things are yet to be handled but you you get the idea now i'm going to do the same thing so i'm going to delete i'm going to delete the cluster and build the application once again and then run it okay create hyphen f manifest and cluster one and here we go cluster id that we have is this and if i refresh my console again i should get a kubernetes cluster here it obviously is going to take some time so yeah here we go we have a cluster with uh name cluster 0 that we have specified in our spec and we have specified reason to be nyc1 if you see here and we have also specified this particular version that we see here and this is what we this is what we wanted to do now if i just go ahead and if i just go ahead and try to create another another cluster resource i should get another kubernetes cluster created in my in my digital lucian account so let's let's go ahead and try to do that so manifests and we are going to copy cluster one [Music] cluster two and let's change cluster two to have let's name it one one and we are going to specify let's say count to be four let's keep it free and we know that the secret is in default name space itself so let's just go ahead and try to create uh this cluster as well so cluster two and as soon as i hit enter our operator is going to create another cluster on digital ocean with name cluster cluster one so here we go and if i refresh this again we have another cluster being created so yeah i mean this is what this is what i wanted to talk about in this particular video if i try to summarize the things again so the things that we have done in this particular in this particular video is so in cluster.gov we had already written this the source file in cluster.gov we already had uh the clusters pick this cluster spec is the specification using which a particular cluster resource was being created so we already had this cluster spec all we had to do was we had to use this particular cluster spec to create a cluster in digital version to do that what we did is we created another source file named digitalocean.go inside do package and we created a create method that expects kubernetes client set and that cluster spec now we need we need this kubernetes client set because we have to we have to actually get the secret name that has been specified in this particular resource we have to get that secret to get the token out of it so that is the reason we need kubernetes client set and we call get token and in get token we call kubernetes api to get the secret then we return the token and once we have the token we make a request to we make a request to digital ocean digital ocean api to actually get the cluster created now the things that that we are going to talk about in the next video are about sub resources so for example if i go ahead and try to get the cluster name cluster hyphen zero and hyphen o yaml if you see here we see all the specification but what if we want to so for example if i delete if i delete this particular resource this kubernetes cluster from digital version should also uh get deleted so in that case we will have to specify will we will have to persist cluster id as well somewhere as as part of this resource so that those are those are the things that we are going to uh talk about so for example if i if i for example run get clustered cluster hyphen zero so the things that we are going to talk about in the next video is how are we going to have cluster id first being persisted as part of this resource so that when we delete this cluster cluster from digital ocean should all it should also be deleted and we are also going to see how we can see an additional column here so for example as soon as we create this resource we see that the cluster is being created in kubernetes but that doesn't necessarily mean that the cluster has been created so initially for a lot for some time this cluster is in is in let's say creating state so we should be able to see the see the state here so for example here we should we should see two more columns maybe or one in one column we should have cluster id in another column we should have uh if the cluster has been created successfully or not and we are also going to look into status sub resources so these are all the things that we are going to look into in the next video uh but for now thank you so much for watching i really appreciate it if you think this is going to be helpful uh to your friends to your colleagues share it with them and thank you so much for watching
Info
Channel: Vivek Singh
Views: 312
Rating: undefined out of 5
Keywords: kubernetes
Id: Wtyj0V4Inmg
Channel Id: undefined
Length: 43min 2sec (2582 seconds)
Published: Sun Aug 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.