<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vasudev Parmar on Medium]]></title>
        <description><![CDATA[Stories by Vasudev Parmar on Medium]]></description>
        <link>https://medium.com/@vsdvprmr?source=rss-e4701d33c6e7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FiwzH47V8xkkwFDzsa8rPQ.jpeg</url>
            <title>Stories by Vasudev Parmar on Medium</title>
            <link>https://medium.com/@vsdvprmr?source=rss-e4701d33c6e7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 09:43:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vsdvprmr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to list down the jobs running on Flink (part 2)]]></title>
            <link>https://medium.com/@vsdvprmr/how-to-list-down-the-jobs-running-on-flink-part-2-d2cf25d1f92a?source=rss-e4701d33c6e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2cf25d1f92a</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[flink]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Vasudev Parmar]]></dc:creator>
            <pubDate>Sat, 16 Sep 2023 13:31:41 GMT</pubDate>
            <atom:updated>2023-09-16T13:31:41.084Z</atom:updated>
            <content:encoded><![CDATA[<p>In a <a href="https://medium.com/@vsdvprmr/how-to-list-down-the-jobs-running-on-flink-8df1d19157a0">previous article</a>, I discussed listing the Flink jobs with K8s custom api-resource FlinkSessionJob and with Flink Dashboard.</p><p>Here with the client-go library, let&#39;s find out how to list down all the running Flink jobs. first, go through <a href="https://medium.com/@vsdvprmr/how-to-create-a-custom-resource-deployment-using-the-client-go-library-52f7ba0587eb">this article</a> where how to use the client-go library to create custom resource deployment is discussed in detail.</p><p>FlinkSessionJob is also a custom Flink resource. so they can be easily listed down with the help of inbuilt functions that the client-go library provides</p><pre>s.k8sClient.Resource(schema.GroupVersionResource{<br>  Group:    &quot;flink.apache.org&quot;,<br>  Version:  &quot;v1beta1&quot;,<br>  Resource: &quot;flinksessionjobs&quot;,<br> }).Namespace(namespace).List(context.TODO(), listOptions)</pre><p>In the above code, the namespace and list options are variable. The namespace is not a mandatory field if not specified then the above snippet will list flinksessionjobs from all the namespaces. whereas listOptions can also be empty but it provides more control over filtering the flinksessionjobs as per requirements <em>ex:- listing flinksessionjobs with particular labels.</em></p><p>Let&#39;s break down listOptions and understand how to use them to filter the flinksessionjob with code.</p><pre># flinkdeployment is variable <br>matchLabels := map[string]string{<br>  &quot;deployment&quot;: Flink deployment,<br> }</pre><pre>func BuildListOptions(matchLabels map[string]string) metav1.ListOptions {<br> labelSelector := metav1.LabelSelector{<br>  MatchLabels: matchLabels,<br> }<br> return metav1.ListOptions{<br>  LabelSelector: metav1.FormatLabelSelector(&amp;labelSelector),<br> }<br>}</pre><p>This will filter out the flinksessionjobs with the label <em>“deployment”: Flink deployment</em>.</p><p>To get the running flink job one more filter has to be applied on flinksessionjob. first, let’s have a look at the flinksessionjob yaml manifest.</p><pre>apiVersion: flink.apache.org/v1beta1<br>kind: FlinkSessionJob<br>metadata:<br>  creationTimestamp: &quot;2023-09-16T07:43:18Z&quot;<br>  finalizers:<br>  - flinksessionjobs.flink.apache.org/finalizer<br>  generation: 1<br>  labels:<br>    deployment: Flink deployment<br>  name: job<br>  namespace: flink-operator<br>  resourceVersion: &quot;529192&quot;<br>  uid: 2a7f9a0b-6bcb-4b0d-8671-2d829dc6cb63<br>spec:<br>  deploymentName: demo<br>  job:<br>    args: []<br>    entryClass: org.apache.flink.streaming.examples.wordcount.WordCount<br>    jarURI: https://repo1.maven.org/maven2/org/apache/flink/flink-examples-streaming_2.12/1.16.1/flink-examples-streaming_2.12-1.16.1-WordCount.jar<br>    parallelism: 2<br>    state: running<br>    upgradeMode: stateless<br>status:<br>  jobStatus:<br>    jobId: ffffffff94a1595d0000000000000001<br>    savepointInfo:<br>      lastPeriodicSavepointTimestamp: 0<br>      savepointHistory: []<br>    state: RUNNING<br>  lifecycleState: DEPLOYED<br>  reconciliationStatus:<br>    lastReconciledSpec: &#39;{&quot;spec&quot;:{&quot;job&quot;:{&quot;jarURI&quot;:&quot;https://repo1.maven.org/maven2/org/apache/flink/flink-examples-streaming_2.12/1.16.1/flink-examples-streaming_2.12-1.16.1-WordCount.jar&quot;,&quot;parallelism&quot;:2,&quot;entryClass&quot;:&quot;org.apache.flink.streaming.examples.wordcount.WordCount&quot;,&quot;args&quot;:[],&quot;state&quot;:&quot;running&quot;,&quot;savepointTriggerNonce&quot;:null,&quot;initialSavepointPath&quot;:null,&quot;upgradeMode&quot;:&quot;stateless&quot;,&quot;allowNonRestoredState&quot;:null},&quot;restartNonce&quot;:null,&quot;flinkConfiguration&quot;:null,&quot;deploymentName&quot;:&quot;demo-isolated-0&quot;},&quot;resource_metadata&quot;:{&quot;apiVersion&quot;:&quot;flink.apache.org/v1beta1&quot;,&quot;metadata&quot;:{&quot;generation&quot;:1},&quot;firstDeployment&quot;:true}}&#39;<br>    reconciliationTimestamp: 1694850210161<br>    state: DEPLOYED</pre><p>Now it is straightforward to extract the status field, map through it, and compare the state field of jobStatus, as if it is RUNNING then add it to your list.</p><p>Conclusion:-<br>When automating Flink deployments, the client-go library proves invaluable due to its extensive set of predefined functions that can perform the same tasks as any Kubernetes CLI command. For more detailed information about the client-go library, please click on the following link: [<a href="https://pkg.go.dev/k8s.io/client-go">https://pkg.go.dev/k8s.io/client-go</a>].</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2cf25d1f92a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to list down the jobs running on Flink]]></title>
            <link>https://medium.com/@vsdvprmr/how-to-list-down-the-jobs-running-on-flink-8df1d19157a0?source=rss-e4701d33c6e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/8df1d19157a0</guid>
            <category><![CDATA[flink]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Vasudev Parmar]]></dc:creator>
            <pubDate>Sat, 24 Jun 2023 16:40:58 GMT</pubDate>
            <atom:updated>2023-06-24T16:40:58.110Z</atom:updated>
            <content:encoded><![CDATA[<p>Apache Flink is the open source that is being used for data streaming. it can handle batch and real-time data streaming, and have rich API libraries, it can scale horizontally adjusting the parallelism of operators, allowing it to handle large-scale data processing workloads.</p><p>K8s doesn&#39;t support Flink directly, so to handle all the requirements for Flink deployment new Custom Resource Definitions and Custom Resource can be created.</p><blockquote>In Apache Flink, there are two primary modes for submitting and executing jobs: Flink Deployment and Flink Session Jobs.</blockquote><blockquote><a href="https://nightlies.apache.org/flink/flink-kubernetes-operator-docs-release-1.2/docs/try-flink-kubernetes-operator/quick-start/">Here</a> is the link to set up the Flink cluster which will create <em>flinkdeployments</em> and <em>flinksessionjobs </em>in K8s. Once it is done, let&#39;s verify if the same with K8s command <em>Kubectl api-resources | grep flink</em></blockquote><figure><img alt="api-resources for flink" src="https://cdn-images-1.medium.com/max/1024/1*2Z3rlyf1O-IMgdMEh31oPg.png" /></figure><p>With a simple Yaml manifest, I&#39;ll show how to create a flink deployment in Minikube.</p><pre>apiVersion: flink.apache.org/v1beta1<br>kind: FlinkDeployment<br>metadata:<br>  name: demo<br>  labels:<br>    flink-deployment: app-test<br>spec:<br>  image: flink:1.16<br>  flinkVersion: v1_16<br>  jobManager:<br>    resource:<br>      memory: &quot;1200m&quot;<br>      cpu: 0.3<br>  taskManager:<br>    resource:<br>      memory: &quot;1200m&quot;<br>      cpu: 0.3<br>  serviceAccount: flink</pre><blockquote>Now port-forward the service to any host so that flink dashboard can be seen and command for this is :- <em>kubectl port-forward svc/{flinkdeploymentname}-rest 8081:8081</em></blockquote><p>Note:- for every flink deployment one service will be created with <em>{flinkdeploymentname}-rest </em>and it will have port 8081</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hLTGxCSsvhbIN2xqxzh7bQ.png" /></figure><p>No jobs are running for now, so create a YAML file with the kind “FlinkSessionJob” and while creating this YAML file mention deploymentName under the spec as shown below, so that the job will be run on a particular flinkdeployment</p><pre>apiVersion: flink.apache.org/v1beta1<br>kind: FlinkSessionJob<br>metadata:<br>  name: test<br>spec:<br>  deploymentName: demo<br>  job:<br>    jarURI: https://repo1.maven.org/maven2/org/apache/flink/flink-examples-streaming_2.12/1.16.1/flink-examples-streaming_2.12-1.16.1-TopSpeedWindowing.jar<br>    parallelism: 1<br>    upgradeMode: stateless </pre><blockquote>Apply this Yaml to K8s with command <em>kubectl apply -f filename.yaml</em></blockquote><p>Now check the dashboard again, there will be one job running</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dj_4J7aPqsYYuXsKWevzJw.png" /></figure><p>To see more details about the job, there is an endpoint <em>/jobs/overview</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5z-cksDeLg_kA8zUyORajg.png" /></figure><p>this is how we can list the jobs which are present on any flink deployment, they might be in a RUNNING or CANCELED state.</p><p>Conclusion:- <br>1. To get all the Jobs we can use k8s and list down all the flinksessionjobs which are been created for particular flinkdeployments.<br>2. Or inside the service all the jobs information are been stored from there either describe the service or use Flink dashboard to list down all the jobs</p><p>In the next part, I’ll show how we can do the same with the Gin framework using the Client-go library.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8df1d19157a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a custom resource deployment using the client-go library]]></title>
            <link>https://medium.com/@vsdvprmr/how-to-create-a-custom-resource-deployment-using-the-client-go-library-52f7ba0587eb?source=rss-e4701d33c6e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/52f7ba0587eb</guid>
            <category><![CDATA[client-go]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[gin]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Vasudev Parmar]]></dc:creator>
            <pubDate>Sat, 27 May 2023 07:44:27 GMT</pubDate>
            <atom:updated>2023-05-27T07:51:13.213Z</atom:updated>
            <content:encoded><![CDATA[<p>In the <a href="https://medium.com/@vsdvprmr/performing-crud-operations-on-nginx-deployment-with-go-gin-framework-a1dfbf7846ac">previous article</a>, we discussed the process of creating an Nginx deployment using the client-go library. Now, let’s focus on exploring the steps involved in creating a custom resource deployment.</p><p>first, look at the <em>Nginx deployment</em> K8s manifest:-</p><pre>apiVersion: apps/v1   <br>kind: Deployment<br>metadata:<br>  name: nginx-deployment<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      app: todo<br>  template:<br>    metadata:<br>      name: nginx-pod<br>      labels:<br>        app: todo<br>    spec:<br>      containers:<br>        - name: nginx-container<br>          image: nginx:latest<br>          ports:<br>            - containerPort: 80</pre><p>Kubernetes provides a set of built-in resource types like Pods, Deployments, Services, ConfigMaps, etc., like for nginx deployment those are specific in the above YAML file.</p><blockquote>To get the API-resources present in the K8s cluster in CMD run the command <em>kubectl api-resources</em></blockquote><p>In complex applications or specialized domains, using the existing resources requirement may not be fulfilled. That’s where Custom Resources came into the picture.</p><p>custom resources extend the Kubernetes API and provide a flexible and scalable way to model and manage complex applications and infrastructure within the Kubernetes framework.</p><p>To define a custom resource in Kubernetes, you need to perform the following steps:</p><p>Define the Custom Resource Definition (CRD):</p><ul><li>Create a YAML or JSON file that describes the custom resource and its schema.</li><li>The CRD defines the API endpoints, structure, and validation rules for your custom resource.</li><li>The CRD should specify the group, version, and kind of custom resource.</li></ul><pre>apiVersion: apiextensions.k8s.io/v1<br>kind: CustomResourceDefinition<br>metadata:<br>  name: myresources.example.com<br>spec:<br>  group: example.com<br>  version: v1<br>  scope: Namespaced<br>  names:<br>    plural: myresources<br>    singular: myresource<br>    kind: MyResource</pre><blockquote>Create CRD with command <em>kubectl apply -f crd-definition.yaml</em></blockquote><p>Create the Custom Resource (CR):</p><pre>apiVersion: example.com/v1<br>kind: MyResource<br>metadata:<br>  name: myresource-sample<br>spec:<br>  # Define the desired state of your resource here</pre><blockquote>Apply CR with command <em>kubectl apply -f custom-resource.yaml</em></blockquote><p>It was very brief about CRD and CR. let’s talk about the CR deployment with client-go library,</p><p>Nginx deployment was straight-forward where K8s client <em>“k8s.io/client-go/kubernetes” </em>was used</p><blockquote>client.AppsV1().Deployments(“namespace”).Create(context.TODO(), deployment, metav1.CreateOptions{})</blockquote><p>Here’s a breakdown of the code:</p><ul><li>client: It refers to an instance of the Kubernetes client that is already configured to communicate with a Kubernetes cluster. This client is typically created using the client-go library.</li><li>AppsV1(): It specifies the version of the Apps API group to which the Deployment resource belongs. In this case, it&#39;s using the v1 version.</li><li>Deployments(&quot;namespace&quot;): It selects the &quot;Deployments&quot; resource within the specified namespace, which in this case is &quot;namespace&quot;. Replace &quot;default&quot; with the actual namespace where you want to create the Deployment.</li><li>Create(context.TODO(), deployment, metav1.CreateOptions{}): it invokes the Create method to create the Deployment resource with the deployment object passed as a parameter.</li></ul><p>The same client will not work for Custom Resource Deployment, and Go provide dynamic client <em>“k8s.io/client-go/dynamic” </em>for interacting with the Kubernetes API. It allows one to work with Kubernetes resources without requiring specific knowledge or code generation for each resource type. With the dynamic client, CRUD operations can be performed on resources using generic API calls. It’s useful when one needs to work with custom resources or when one wants to work with Kubernetes resources without having to define their specific types in code.</p><ol><li>Create Dynamic client</li></ol><pre>var kubeconfig = &quot;your kube config file path&quot;<br>config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, kubeconfig)<br> if err != nil {<br>  log.Fatalf(&quot;Error building kubeconfig: %v&quot;, err)<br> }<br><br>// Create a dynamic client<br> client, err := dynamic.NewForConfig(config)<br> if err != nil {<br>  log.Fatalf(&quot;Error creating dynamic client: %v&quot;, err)<br> }else{<br>  log.Println(&quot;Dynamic client created&quot;)<br> }</pre><p>2. Create the unstructured object with the desired YAML content (Using flinksessionjobs which is Custom Resouce)</p><pre>obj := &amp;unstructured.Unstructured{<br>  Object: map[string]interface{}{<br>   &quot;apiVersion&quot;: &quot;flink.apache.org/v1beta1&quot;,<br>   &quot;kind&quot;:       &quot;FlinkSessionJob&quot;,<br>   &quot;metadata&quot;: map[string]interface{}{<br>    &quot;name&quot;: &quot;name of your application&quot;,<br>   },<br>   &quot;spec&quot;: map[string]interface{}{<br>    &quot;deploymentName&quot;: &quot;Deployment name to which this FlinkSessionJob belongs&quot;,<br>    &quot;job&quot;: map[string]interface{}{<br>     &quot;jarURI&quot;:      &quot;Your Jar file&quot;,<br>     &quot;parallelism&quot;: 4,<br>     &quot;upgradeMode&quot;: &quot;upgrade mode&quot;,<br>     &quot;state&quot;: &quot;running&quot;,<br>    },<br>   },<br>  },<br> }</pre><p>3. Set the GroupVersionKind for the object</p><pre>obj.SetGroupVersionKind(schema.GroupVersionKind{<br>  Group:   &quot;flink.apache.org&quot;,<br>  Version: &quot;v1beta1&quot;,<br>  Kind:    &quot;FlinkSessionJob&quot;,<br> })</pre><p>4. Use the client to create custom resource</p><pre>result, err := client.Resource(schema.GroupVersionResource{<br>  Group:    &quot;flink.apache.org&quot;,<br>  Version:  &quot;v1beta1&quot;,<br>  Resource: &quot;flinksessionjobs&quot;,<br> }).Namespace(&quot;namespace&quot;).Create(context.TODO(), obj, metav1.CreateOptions{})<br><br>if err != nil {<br> fmt.Println(&quot;Error creating custom resource: %v&quot;, err)<br>}else{<br>fmt.Println(&quot;Custom resource created successfully &quot;, result)<br>}</pre><p>This code creates Custom Resources of the type flinksessionjobs in the flink.apache.org API group with version v1beta1 within the &quot;default&quot; namespace. It utilizes the client.Resource the method from the Kubernetes client to perform the creation operation. The obj object represents the configuration or instance of the flinksessionjobs resource.</p><p>5. To list all the flinksessionjobs under any namespace</p><pre>list,err :=client.Resource(schema.GroupVersionResource{<br>  Group:    &quot;flink.apache.org&quot;,<br>  Version:  &quot;v1beta1&quot;,<br>  Resource: &quot;flinksessionjobs&quot;,<br> }).Namespace(&quot;namespace&quot;).List(context.TODO(), metav1.ListOptions{})</pre><p>This is how one can use Dynamic client to create Custom Resource using the Client-go library</p><p>In summary, <em>“k8s.io/client-go/dynamic”</em> provides a generic and dynamic way to interact with Kubernetes resources, while <em>“k8s.io/client-go/kubernetes”</em> offers a typed and higher-level client for working with specific resource types. The choice between the two depends on your specific requirements and the level of flexibility and type safety you need in your code as specified above for Nginx deployment and FlinkSessionJobs(CR).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52f7ba0587eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Performing Crud operations on Nginx Deployment with Go-Gin framework]]></title>
            <link>https://medium.com/@vsdvprmr/performing-crud-operations-on-nginx-deployment-with-go-gin-framework-a1dfbf7846ac?source=rss-e4701d33c6e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1dfbf7846ac</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[gin]]></category>
            <category><![CDATA[client-go]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Vasudev Parmar]]></dc:creator>
            <pubDate>Sat, 20 May 2023 09:34:39 GMT</pubDate>
            <atom:updated>2023-05-20T09:34:39.368Z</atom:updated>
            <content:encoded><![CDATA[<h3>Performing Crud operation on Nginx Deployment with Go-Gin framework</h3><p>The client-go library is a Go client library provided by Kubernetes, which allows you to interact with Kubernetes clusters programmatically.</p><p>Why use code when directly with ‘<em>kubectl’ </em>commands any Kubernetes manifest can be applied here are the following advantages of the <em>client-go </em>library over ‘<em>kubectl’</em> commands :-</p><ul><li>Automation: Enables programmatically automating Kubernetes operations.</li><li>Customization: Provides flexibility and extensibility for tailored interactions with the Kubernetes API.</li><li>Integration: Seamlessly integrates Kubernetes operations with Go applications.</li><li>Performance: Offers faster and more efficient execution compared to external kubectl commands.</li><li>Error Handling: Provides structured error types and better error handling capabilities.</li></ul><p>Here I used the following steps to create nginx deployment through <em>client-go</em> library</p><h4>Steps</h4><ol><li>Pull the <em>Gin</em> and <em>client-go</em> library.</li></ol><pre>go get -u github.com/gin-gonic/gin<br>go get -u k8s.io/client-go@v0.24.0</pre><p>2. import libraries into the project</p><pre>import (<br> &quot;github.com/gin-gonic/gin&quot;<br> v1 &quot;k8s.io/api/apps/v1&quot;<br> metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;<br> &quot;k8s.io/client-go/kubernetes&quot;<br> &quot;k8s.io/client-go/tools/clientcmd&quot;<br>)</pre><p>3. Set up the route</p><pre>func main() {<br><br> r := gin.Default()<br> r.GET(&quot;/&quot;, func(c *gin.Context) {<br>  c.String(http.StatusOK, &quot;Hello, world!&quot;)<br> })<br> r.Run(&quot;:8080&quot;)<br>}</pre><p>4. Now create a client that talks to K8s APIs</p><pre><br>func GetKubernetesClient() (*kubernetes.Clientset, error) {<br> var kubeconfig = &quot;your kube config file path&quot;<br>/*** <br>BuildConfigFromFlags used as helper function that <br>builds configs from a master url or a kubeconfig filepath <br>***/<br> config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, kubeconfig)<br> if err != nil {<br>  return nil, err<br> }<br><br>/** <br>setup the client with configuration config<br>**/<br><br> clientset, err := kubernetes.NewForConfig(config)<br> if err != nil {<br>  return nil, err<br> }<br><br> return clientset, nil<br>}</pre><p>5. Create a Route to trigger the deployment</p><pre>func main() {<br> r := gin.Default()<br> r.GET(&quot;/&quot;, func(c *gin.Context) {<br>  c.String(http.StatusOK, &quot;Hello, world!&quot;)<br> })<br> r.POST(&quot;/create&quot;, allRoutes)<br> r.Run(&quot;:8080&quot;)<br>}</pre><pre>//payload <br>type DeploymentRequest struct {<br> Replicas       int32  `json:&quot;replicas&quot;`<br> DeploymentName string `json:&quot;deploymentName&quot;`<br>}<br><br>func int32Ptr(i int32) *int32 {<br> return &amp;i<br>}</pre><pre>func allRoutes(c *gin.Context) {<br>//Get the client<br> clientset, err := GetKubernetesClient()<br>//handle the error <br> if err != nil {<br>  fmt.Print(err)<br>  c.String(http.StatusInternalServerError, &quot;Failed to connect to the Kubernetes cluster&quot;)<br>  return<br> }<br><br> switch c.Request.Method {<br> case &quot;POST&quot;:<br>/**<br>DeploymentRequest is a payload <br>**/<br>  var req DeploymentRequest<br>  if err := c.ShouldBindJSON(&amp;req); err != nil {<br>   c.String(http.StatusBadRequest, &quot;Invalid request payload&quot;)<br>   return<br>  }<br><br>  if err := createNginxDeployment(clientset, req); err != nil {<br>   c.String(http.StatusInternalServerError, &quot;Failed to create Nginx deployment&quot;)<br>   return<br>  }<br><br>  c.String(http.StatusOK, &quot;Nginx deployment created successfully&quot;)<br><br> default:<br>  c.String(http.StatusMethodNotAllowed, &quot;Method not allowed&quot;)<br> }<br>}</pre><blockquote>create the<em> createNginxDeployment function</em></blockquote><pre>/**<br>just like the kubernetes manifest for the nginx deployment here <br>also we will create a template <br><br>**/<br>func createNginxDeployment(clientset *kubernetes.Clientset, req DeploymentRequest) error {<br> deployment := &amp;appsv1.Deployment{<br>  ObjectMeta: metav1.ObjectMeta{<br>   Name: req.DeploymentName,<br>  },<br>  Spec: appsv1.DeploymentSpec{<br>   Selector: &amp;metav1.LabelSelector{<br>    MatchLabels: map[string]string{<br>     &quot;app&quot;: &quot;nginx&quot;,<br>    },<br>   },<br>   Replicas: int32Ptr(req.Replicas),<br>   Template: corev1.PodTemplateSpec{<br>    ObjectMeta: metav1.ObjectMeta{<br>     Labels: map[string]string{<br>      &quot;app&quot;: &quot;nginx&quot;,<br>     },<br>    },<br>    Spec: corev1.PodSpec{<br>     Containers: []corev1.Container{<br>      {<br>       Name:  &quot;nginx&quot;,<br>       Image: &quot;nginx:latest&quot;,<br>      },<br>     },<br>    },<br>   },<br>  },<br> }<br>//using inbuild function to create the deployment in kubernetes config<br> _, err := clientset.AppsV1().Deployments(&quot;default&quot;).Create(context.TODO(), deployment, metav1.CreateOptions{})<br> if err != nil {<br>  return err<br> }<br><br> return nil<br>}</pre><p>6. To list down all the pods that are running</p><blockquote>update the main funtion with <em>GET</em> route</blockquote><pre>func main() {<br> r := gin.Default()<br> r.GET(&quot;/&quot;, func(c *gin.Context) {<br>  c.String(http.StatusOK, &quot;Hello, world!&quot;)<br> })<br> r.GET(&quot;/getPods&quot;, allRoutes)<br> r.POST(&quot;/create&quot;, allRoutes)<br> r.Run(&quot;:8080&quot;)<br>}</pre><blockquote>update the allRoute function to handle the GET request</blockquote><pre>case &quot;GET&quot;:<br>  // List pods<br><br>  podList, err := clientset.CoreV1().Pods(&quot;default&quot;).List(context.TODO(), metav1.ListOptions{})<br>  if err != nil {<br>   c.String(http.StatusInternalServerError, &quot;Failed to list pods&quot;)<br>   return<br>  }<br><br>  var podNames []string<br>  for _, pod := range podList.Items {<br>   podNames = append(podNames, pod.Name)<br>  }<br><br>  for i, val := range podNames {<br>   fmt.Println(i, &quot; &quot;, val)<br>  }<br><br>  c.String(http.StatusOK, &quot;Pods listed successfully&quot;)</pre><p>7. To update the Nginx deployment</p><blockquote>update the main function with <em>PUT</em> route</blockquote><pre>func main() {<br> r := gin.Default()<br> r.GET(&quot;/&quot;, func(c *gin.Context) {<br>  c.String(http.StatusOK, &quot;Hello, world!&quot;)<br> })<br> r.GET(&quot;/getPods&quot;, allRoutes)<br> r.POST(&quot;/create&quot;, allRoutes)<br> r.PUT(&quot;/update/:name&quot;, allRoutes)<br> r.Run(&quot;:8080&quot;)<br>}</pre><blockquote>Handle put request in the allroutes function</blockquote><pre> case &quot;PUT&quot;:<br>  deploymentName := c.Param(&quot;name&quot;)<br>  var req DeploymentRequest<br>  if err := c.ShouldBindJSON(&amp;req); err != nil {<br>   c.String(http.StatusBadRequest, &quot;Invalid request payload&quot;)<br>   return<br>  }<br><br>  if err := updateNginxDeployment(clientset, deploymentName, req.Replicas); err != nil {<br>   c.String(http.StatusInternalServerError, &quot;Failed to update Nginx deployment&quot;)<br>   return<br>  }<br><br>  c.String(http.StatusOK, &quot;Nginx deployment updated successfully&quot;)<br></pre><blockquote>create the <em>updateNginxDeployment </em>function</blockquote><pre>func updateNginxDeployment(clientset *kubernetes.Clientset, deploymentName string, replicas int32) error {<br> deploymentClient := clientset.AppsV1().Deployments(&quot;default&quot;)<br><br> deployment, err := deploymentClient.Get(context.TODO(), deploymentName, metav1.GetOptions{})<br> if err != nil {<br>  return err<br> }<br><br> deployment.Spec.Replicas = int32Ptr(replicas)<br><br> _, err = deploymentClient.Update(context.TODO(), deployment, metav1.UpdateOptions{})<br> if err != nil {<br>  return err<br> }<br><br> return nil<br>}</pre><p>8. To delete the Nginx deployment</p><blockquote>update the main function with <em>DELETE</em> route</blockquote><pre>func main() {<br> r := gin.Default()<br> r.GET(&quot;/&quot;, func(c *gin.Context) {<br>  c.String(http.StatusOK, &quot;Hello, world!&quot;)<br> })<br> r.GET(&quot;/getPods&quot;, allRoutes)<br> r.POST(&quot;/create&quot;, allRoutes)<br> r.PUT(&quot;/update/:name&quot;, allRoutes)<br> r.DELETE(&quot;/delete/:name&quot;, allRoutes)<br> r.Run(&quot;:8080&quot;)<br>}</pre><blockquote>Handle delete request in the allroutes function</blockquote><pre> case &quot;DELETE&quot;:<br>  deploymentName := c.Param(&quot;name&quot;)<br>  if err := deleteNginxDeployment(clientset, deploymentName); err != nil {<br>   c.String(http.StatusInternalServerError, &quot;Failed to delete Nginx deployment&quot;)<br>   return<br>  }<br><br>  c.String(http.StatusOK, &quot;Nginx deployment deleted successfully&quot;)</pre><blockquote>create the <em>deleteNginxDeployment </em>function</blockquote><pre>func deleteNginxDeployment(clientset *kubernetes.Clientset, deploymentName string) error {<br> deploymentClient := clientset.AppsV1().Deployments(&quot;default&quot;)<br> deletePolicy := metav1.DeletePropagationForeground<br> if err := deploymentClient.Delete(context.TODO(), deploymentName, metav1.DeleteOptions{<br>  PropagationPolicy: &amp;deletePolicy,<br> }); err != nil {<br>  return err<br> }<br> return nil<br>}</pre><p>With client-go, you can create deployments with different configurations, such as defining the number of replicas, specifying container images, setting environment variables, configuring volume mounts, defining resource limits, and more.</p><p>The client-go library provides a comprehensive set of APIs and resources for interacting with the Kubernetes API server. You can use it to create deployments, services, pods, config maps, secrets, and other Kubernetes resources programmatically.</p><p>In conclusion, by utilizing the client-go library, you have the flexibility to define and customize deployments according to your specific requirements, enabling you to create and manage various types of deployments in Kubernetes.</p><p>In my upcoming blog posts, I’ll be writing about how to create custom resources using the <em>client-go</em> library.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1dfbf7846ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Memory: How Data is Stored in a Computer’s Memory]]></title>
            <link>https://medium.com/@vsdvprmr/understanding-memory-how-data-is-stored-in-a-computers-memory-861e2c197bf2?source=rss-e4701d33c6e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/861e2c197bf2</guid>
            <dc:creator><![CDATA[Vasudev Parmar]]></dc:creator>
            <pubDate>Sun, 14 May 2023 17:58:46 GMT</pubDate>
            <atom:updated>2023-08-30T06:26:06.419Z</atom:updated>
            <content:encoded><![CDATA[<p>Memory is a crucial part of any computer system that allows us to store and access data efficiently. In this article, we will discuss how memory works and how data is stored in it.</p><p>Here are some key points to understand memory:</p><ul><li>Memory is like a bounded canvas of slots that can store data.</li><li>Each memory slot can store a fixed number of bits <em>(8 bits)</em>, which are represented in binary form <em>(0/1)</em>.</li><li>The amount of memory available is limited, and it is possible to run out of memory if we try to store too much data. So <strong>space complexity</strong> should be considered while writing efficient code.</li></ul><p>Let’s consider an example:</p><p>Suppose we have a variable named “foobar” with the value of 1. This variable will be stored in a memory slot in the form of bits only if there is any free memory available.</p><p>The unit of memory is bits, and one memory slot can hold 8 bits, which equals 1 byte.</p><p>For instance, the binary representation of the integer value 1 is 0000 0001 in an 8-bit format.</p><p>But, what if we have an integer that is larger than 8 bits, such as a 32-bit integer?</p><p>In that case, it will require 4 (32 bits/8 bits) <em>back-to-back memory slots</em> to store that integer. Similarly, if we have a list with two 32-bit integers, each integer will require 4 <em>back-to-back memory slots.</em></p><p>These types of integers are called <em>fixed-width integers</em> because they take a constant and fixed amount of memory slots to store any integer. For instance, a 64-bit integer will always take 8 (64 bits/8 bits) <em>back-to-back memory slots.</em></p><p>Moreover, characters and strings are stored in memory as binary data, with each character represented by a unique <strong>ASCII code</strong>. Strings are stored sequentially in memory, starting from a memory address that is referenced by a pointer.</p><p><em>For instance, if we have the string “Hello,” the “H” character will be stored at a specific memory address, followed by the “e” character at the next address, and so on. The pointer will reference the address of the first character, “H.”</em></p><p>It’s important to note that strings in memory are usually stored with a <strong>null terminator</strong>, which is a special character represented by the <strong>value zero</strong>. The null terminator indicates the end of the string and helps the program determine the string’s length.</p><p><em>Additionally,</em> <em>the memory address of any slot can be stored and pointed to another memory slot.</em></p><p>In conclusion, memory is an essential component of computer systems. It has a finite capacity to store data and requires a structured approach to use it efficiently. Understanding how memory works is crucial for programmers to optimize the usage of available memory and create more efficient software.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=861e2c197bf2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>