<?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 vivek soundrapandi on Medium]]></title>
        <description><![CDATA[Stories by vivek soundrapandi on Medium]]></description>
        <link>https://medium.com/@vivekhas3?source=rss-238a2902dddd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*fMqrdQmLyj9d_Cfz.</url>
            <title>Stories by vivek soundrapandi on Medium</title>
            <link>https://medium.com/@vivekhas3?source=rss-238a2902dddd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:19:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vivekhas3/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[Crawling with Puppeteer]]></title>
            <link>https://medium.com/@vivekhas3/crawling-with-puppeteer-55a5ef4cce91?source=rss-238a2902dddd------2</link>
            <guid isPermaLink="false">https://medium.com/p/55a5ef4cce91</guid>
            <category><![CDATA[puppeteer]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[vivek soundrapandi]]></dc:creator>
            <pubDate>Tue, 27 Aug 2019 13:16:47 GMT</pubDate>
            <atom:updated>2019-08-27T13:16:47.246Z</atom:updated>
            <content:encoded><![CDATA[<p>Puppeteer is a node JS library that provides an API to control chrome browser and do almost all the operations that a dev/user can do manually. This includes:</p><ul><li>visiting a web page</li><li>logging in and performing user actions</li><li>interacting with Single Page Applications[<strong>SPA</strong>]</li><li>Taking screenshots</li><li>Downloading files</li><li>capture timeline trace of web tools etc.</li></ul><p>This is little different from other crawlers available, as this is a real browser, just without the GUI parts. This means the crawling experience is much better, and lets us do things like save to PDF or images. Another advantage with puppeteer is its ease of use , the syntax is simple and user driven as if its declarative.</p><p>I wanted to show a simple use case of going through a complex user journey on a <strong>SPA</strong> while creating a useful utility for myself. So, I decided to write a utility to automate the ticket booking process for movies in <a href="https://jazzcinemas.com/">jazzcinemas</a> website(which I book regularly). The entire ticket booking process is a good mix of lot use cases that one face while writing a crawler.</p><p>With its powerful features, puppeteer can be used as a good End to End[<strong>E2E</strong>] testing utility as well.</p><h3><strong>The setup:</strong></h3><p>First step would be to install nodeJS and puppeteer as a npm dependency. That is all needed. Incase you don’t want to pollute your local system for trying it out, grab this <a href="https://hub.docker.com/r/iamviveks/puppeteer">docker image</a> from my docker hub and execute the script within it.</p><h3><strong>Visit website and Login:</strong></h3><p>Now that we have puppeteer installed, we can get started by visiting the website and logging in with our creds. The code is pretty straight forward:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f61edbac57c0eaa5cdacb9fbe7f7064/href">https://medium.com/media/8f61edbac57c0eaa5cdacb9fbe7f7064/href</a></iframe><h3><strong>Running with head and taking screenshots:</strong></h3><p>By default puppeteer runs in headless mode, but if you are writing a scrapper for the first time, and for easier debugging, you could configure it to run with a browser head, so that you can see whats happening with your commands.</p><pre>const browser = await puppeteer.launch( { headless: false, dumpio: true, args: [&#39;--no-sandbox&#39;]});</pre><p>Capturing screenshot is also very straight forward:</p><pre>await page.screenshot({path: &#39;ticket.png&#39;, fullPage: true});</pre><p>Skipping fullPage attribute will result in screenshot of current visible section.</p><h3><strong>Injecting 3rd party scripts:</strong></h3><p>As its a real browser, we can dynamically load custom JS library/ scripts to enhance our crawling experience. For example, instead of querying the DOM using vanilla JS, one can inject jQuery and make use of it.</p><pre>//Inject custom scripts</pre><pre>await page.addScriptTag({url: &#39;https://code.jquery.com/jquery-3.2.1.min.js&#39;});</pre><h3><strong>Locating/Actioning on dynamically loaded content:</strong></h3><p>When it comes to Single Page Applications, most content is rendered lazily based on user actions such as click/scroll etc. Dynamically loaded contents cannot be looked up using the initial page object. A page object will only have access to the DOM that was loaded along with the page.</p><p>To access dynamic content, puppeteer allows us to execute inside the user action via evaluate() method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9547d30140b6d5d12597498a942ff803/href">https://medium.com/media/9547d30140b6d5d12597498a942ff803/href</a></iframe><p>Once we call evaluate, we can perform desired actions inside evaluate’s callback. Going forward, we no longer need to wait for pre-determined seconds instead wait for a specific dynamically loaded element to appear on the screen.</p><h3><strong>Actions inside Iframe:</strong></h3><p>Evaluate can also be used to locate iframe’s and perform actions on elements within iframe as long as the iframe is from the same domain.</p><h3><strong>Final screenshot:</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*V7dSob5LMofJU69zsOarow.png" /></figure><p>I have not automated the part to fill the card details. I will do that manually ;-)</p><p>Full code is available as <a href="https://gist.github.com/viveksoundrapandi/e291081cab117dec42c02d8e75be566e">gist here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55a5ef4cce91" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pythonic Forwardable]]></title>
            <link>https://medium.com/@vivekhas3/forwardable-in-python-c72dbc709563?source=rss-238a2902dddd------2</link>
            <guid isPermaLink="false">https://medium.com/p/c72dbc709563</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[vivek soundrapandi]]></dc:creator>
            <pubDate>Wed, 14 Aug 2019 13:51:32 GMT</pubDate>
            <atom:updated>2019-08-14T13:51:32.507Z</atom:updated>
            <content:encoded><![CDATA[<p>When I started acclimatising myself with a project in Ruby, i came across an inbuilt module in ruby called the <a href="https://ruby-doc.org/stdlib-2.5.1/libdoc/forwardable/rdoc/Forwardable.html">Forwardable</a>. The Forwardable module provides delegation of specified methods to a designated object.</p><p>For example, lets say we have class for ADT Queue. For simplicity let us consider an Array based Queue. In its core the Queue class will have an array and perform queue specific operations on top of it.</p><pre><em>class </em>Queue<br>  <em>def </em>initialize<br>      @q = []<br>  <em>end<br><br>  def </em>enqueue<br><br>  <em>end<br>  <br>  def </em>dequeue<br>      <br>  <em>end<br><br>end</em></pre><p>Though enqueue/dequeue are operations specific to a Queue by nature they can be translated to an array’s push and shift operations respectively thereby avoid the re-implementation of those logic. Hence, we could just delegate the methods of Queue to the array class as desired.</p><pre><em>class </em>Queue<br>  <em>extend </em>Forwardable<br><br>  def_delegator :@q, :push, :enqueue<br>  def_delegator :@q, :shift, :dequeue<br><br>  <em>def </em>initialize<br>    @q = []<br>  <em>end<br>end<br><br></em>q = Queue.new<br>q.enqueue 1, 2, 3, 4, 5</pre><p>The below would be a basic equivalent of forwardable module as a python Mixin.</p><h3><strong>The setup:</strong></h3><p>A module in ruby is ruby’s way of extending a class’s capability with additional behaviours[closest equivalent could be Mixins in python]. So, we start with writing a Mixin class.</p><pre>class Forwardable(object):<br>    def __init__(self, *args, **kwargs):<br>        self._delegates = []<br>        return super().__init__(*args, **kwargs)</pre><p>The class has a delegates attribute which will be an array of tuples(NamedTuples can also be used for better readability)with</p><ul><li>composed object name at index 0</li><li>attribute to listen for at index 1and</li><li>delegated attribute at index 2.</li></ul><p>Now let us add getter and setter for our delegates attribute:</p><pre>class Forwardable(object):<br>    def __init__(self, *args, **kwargs):<br>        self._delegates = []<br>        return super().__init__(*args, **kwargs)</pre><pre><a href="http://twitter.com/property">@property</a><br>    def delegates(self):<br>        return self._delegates</pre><pre><a href="http://twitter.com/delegates">@delegates</a>.setter<br>    def delegates(self, delegates):<br>        self._delegates = delegates</pre><p>The next step will be to listen on the attribute call and delegate it to the composed object if it is in our _delegates ADT. To achieve this, we can make use of python’s getattr method.</p><h3><strong>__getattr___:</strong></h3><p>In Python, when we try to access an attribute within an object , first the attribute is looked up upon in the following hierarchy:</p><ol><li>Current objects scope</li><li>Current class’s scope</li><li>Parent classes based on MRO principle[a strategy used by python to avoid the problem of multiple inheritance]</li></ol><p>Now, when an attribute is not found in ay of the above, python makes a call to <strong>__getattr__</strong> method which in turn raises an AttributeError.</p><h3><strong>Core Logic:</strong></h3><p>We can tap into this behaviour of python, by overriding the getattr method and introduce a simple lookup into the _delegates array and delegate if necessary.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/393222f87ca2bae6bf66f2281ab1b11c/href">https://medium.com/media/393222f87ca2bae6bf66f2281ab1b11c/href</a></iframe><p>The raise part at the end is a mandate as we override default methods like getattr , we will not be able to call super.</p><h3><strong>Usage:</strong></h3><p>As this is a mixin i can plug and play with any class down the inheritance and it will work fine.</p><pre>class Queue(Forwardable):<br>    def __init__(self):<br>        self.q = []<br>        self.delegates = [(&quot;q&quot;,&quot;enqueue&quot;, &quot;append&quot;)]<br>que = Queue()<br>que.enqueue(1)<br>print que.q</pre><pre>Output:<br>&gt;&gt;&gt;[1]</pre><p><a href="https://gist.github.com/viveksoundrapandi/74878a3fbf21818a9c45594ec158c9c0">Gist to full code</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e4151f2a3588ab07f811fdf861b1973/href">https://medium.com/media/0e4151f2a3588ab07f811fdf861b1973/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c72dbc709563" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes operators — a new way to manage resources]]></title>
            <link>https://medium.com/@vivekhas3/kubernetes-operators-a-new-way-to-manage-resources-4bc2f5e88dba?source=rss-238a2902dddd------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bc2f5e88dba</guid>
            <category><![CDATA[thoughtworks]]></category>
            <category><![CDATA[kubernetes-operator]]></category>
            <category><![CDATA[techradar]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[vivek soundrapandi]]></dc:creator>
            <pubDate>Tue, 21 May 2019 02:57:54 GMT</pubDate>
            <atom:updated>2019-05-21T04:32:12.591Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kubernetes operators — a new way to manage resources</h3><p><strong><em>Pre-Requisites:</em></strong></p><p>I am gonna talk about “<em>a new way of packaging, deploying and managing a kubernetes application”</em>. Before I jump into the actual topic, let me call out certain pre-requisites for reading this article:</p><ol><li>Basic understanding of kubernetes concepts</li><li>Basic understanding of helm charts</li></ol><p><strong><em>Why this write-up?:</em></strong></p><p>I have been reading about Kubernetes operators of late, after I came across the topic from the recent edition of <a href="https://www.thoughtworks.com/radar/tools/kubernetes-operators#.XN1UJ_SlaQ0.link">Thoughtworks Tech Radar</a>. Over this learning journey, I found it little difficult to understand in the first read as the information available was at an advanced level or scattered across for one to read through and collate everything together. An article collating my reading materials might help someone to get started sooner.</p><p><strong><em>Kubernetes Control Plane:</em></strong></p><p>Before we dive in deep, a refresh of some kubernetes concepts will be a nice warmup. Kubernetes internally is based out of observer pattern. For any resource that we define(say pods), a bunch of software program areresponsible for deploying the resource, ensuring that they are of the right size, performing upgrades etc. Lets call it the <strong>controller</strong>. Kublet engine continuously watches for the resource and any change in its definition is immediately notified to the controller(Our observer) so that it can decide whether and what to act upon it. This whole setup is called the <strong>Kubernetes Control Loop</strong></p><p>One such examples are the <strong>ReplicaSetControllers</strong>, which are responsible for deploying, rolling, rescheduling of our pods based on the replica count (any attribute) specified in our resource definition.</p><p>Another concept to be clear about is that, every controller needs at-least 1 resource to act upon. Without a resource, a controller is not meant to do anything. So this resource is called the <strong>Primary Resource</strong>. In our above example the primary resource for replicaSetController is the pod. A controller can also act upon other resources called <strong>SecondaryResource</strong>. For example, In case of a <strong>DaemonSetController, </strong>pods are the primary resource and the nodes are the secondary resource for that controller(as daemon pods can run only 1 instance per node).</p><p><strong>Controller vs Operator:</strong></p><p>Operators are a new way to effectively manage our custom resources(Domain specific resources) by extending the functionality of the kubernetes control plane in such a way that any operation on our resources can be made by issuing kubectl command or to the kubelet REST API as if it were a default kubernetes resource. Initially, k8s control plane was largely inextensible, but following updates made them extensible which gave birth for operators. Operators were first introduced by CoreOS. Operators are nothing but controllers that can act upon custom resources instead of k8s default resources. These custom resources have their own life cycle and managing them will be completely unique to their domain.</p><p>Lets take an example before diving deeper. Assume we need a stateful application such as a MySQL instance. Now, a mysql instance cannot be rolled up/down, upgraded just like that as incase of stateless pods. Also being stateful taking regular backups, and ability to restore them on time also plays a key role in their lifecycle management. So, a mysql operator takes care of all these operations and all we need to do is declaratively tell what needs to be done via yaml files. So, even without much knowledge about mysql administration, one can get started with complex setups like a multi-master database setup with regular backups, scale up/down , version upgrade easily.</p><p>So we are back to the first line of the article:</p><p><em>“Operators are responsible for packaging, deploying and managing a Kubernetes application”</em></p><p>In our case, the application is MySQL.</p><p><strong>What makes an Operator?:</strong></p><p>Now we know operators are custom controllers acting upon custom resources. The custom resource is called as <strong>Custom Resource Definition(CRD).</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*_4yjZ5kYf8cRjdgqG8gBkw.png" /><figcaption>Operator internals</figcaption></figure><p>As once can see from the above image, once we define a CRD via yaml, the custom controller, creates the resource within the cluster as per the yaml. Now the operator runs the custom controller loop, to continuously monitor for any change to the state of the resource or the CRD and immediately notifies the change to the Custom Controller, which decides whether to act upon the change and what actions to perform.</p><p>In our case, lets say we are running a single pod MySQL instance and suddenly i wanted to have 3 replicas. All we need is to change the CRD and apply the same.</p><p>Our initial CRD</p><pre>apiVersion: mysql.oracle.com/v1alpha1<br>kind: Cluster<br>metadata:<br>    name: my-app-db<br>    namespace: my-namespace</pre><p>Now scaling to 3 replicas:</p><pre>apiVersion: mysql.oracle.com/v1alpha1<br>kind: Cluster<br>metadata:<br>  name: my-app-db<br>  namespace: my-namespace<br>spec:<br>  members: 3</pre><p>That’s all. Within moments we will be able to see 3 MySQL instances running as pods in our cluster.</p><p>Not just with stateful applications, even cloud providers have started exposing their resources as CRDS via their own operators. For example, lets say we need an S3 bucket as part of our application, usually these kind of resources are managed by platform independent applications such as terraform. While this is completely fine, this creates a separate experience for the administrators to manages their resources by configuring separate CI/CD pipelines etc. To avoid this disjointed experience, cluster administrators can choose to install the desired cloud providers operator and manage their resources via these operators while still being within the cluster. A sample S3 CRD looks like this:</p><pre>apiVersion: service-operator.aws/v1alpha1 <br>kind: S3Bucket <br>metadata:   <br>     name: Private <br>spec:   <br>    versioning: true   <br>    accessControl: PublicRead   <br>    website:     <br>        enabled: true     <br>        indexPage: index.html     <br>        errorPage: 500.html   <br>     logging:     <br>        enabled: false     <br>        prefix: archive</pre><p>AWS operator provides other cloud resources as CRD as well such DynamoDB, SQS queue and many more. More such resources can be found <a href="https://operatorhub.io/operator/alpha/aws-service-operator.v0.0.1">here</a>.</p><p>One can go to <a href="https://operatorhub.io/">operatorhub.io</a> to get full list of available operators in the market.</p><p><strong>Basic Internal working:</strong></p><p>Before going to the usage part, I think its good to know some of the basic internal components of an operator and their functionalities. In order to add any operator to our cluster, one need to install the <strong>Operator Lifecycle Manager(OLM)</strong> to the cluster . Here’s the command to install the OLM:</p><pre>kubectl create -f https://raw.githubusercontent.com/operator-framework/operator-lifecycle-manager/master/deploy/upstream/quickstart/olm.yaml</pre><p>This is a one time activity to be done by the cluster admin. So, what does this OLM do?</p><p>Upon installation of OLM, we will be seeing 2 new pods in our cluster as follows:</p><ul><li><strong>OLM operator</strong></li><li><strong>Catalog operator</strong></li></ul><p>In other words is installing an OLM installs 2 other operators. In other words these are called meta-operators, as their role is to assist in installing and managing other operators. These operators treats the operator itself as a CRD and acts upon ay changes to them. So why 2 meta-operators then?</p><p><strong>OLM operator:</strong></p><p>If we had to write an operator , we would need to represent our operators properties as a yaml file called a <strong>ClusterServiceVersion(CSV)</strong>[weird abbreviation]. The CSV tells some meta-data about the application and how to install it into the cluster. So, the OLM operator is responsible for managing the lifecycle of the application specified in the CSV.</p><p><strong>Catalog operator:</strong></p><p>As the name implies it maintains the list of operator catalog as available in the operatorhub.io repository. This helps it know where to find the given operator. The catalog operator also takes care of managing the life cycle of the CSV , as the CSV itself goes through a control loop and it needs to be up and running until the OLM operator completes the full installation of the application specified in the operator.</p><p>To understand their full working , further reading is a must. One can find the more information <a href="https://github.com/operator-framework/operator-lifecycle-manager/blob/master/Documentation/design/architecture.md">here</a>. But to get started with operators , I felt this is a good stop.</p><p><strong>Demo with MySQL Operator:</strong></p><p>I have cluster running with OLM pre-installed.Here’s the output of kubectl get pods:</p><pre>olm              olm-operator-7d96575b46-vhz96           1/1     Running            4          1d</pre><ol><li>Lets go to the <a href="https://github.com/oracle/mysql-operator">MySQL operator github page</a> and install it in our cluster.</li><li>Install the operator via helm chart</li></ol><pre>helm install --name mysql-operator mysql-operator --set rbac.enabled=false</pre><p>3. Create our CRD</p><pre>apiVersion: mysql.oracle.com/v1alpha1<br>kind: Cluster<br>metadata:<br>  name: my-app-db<br>  namespace: my-namespace<br>spec:<br>  members: 3</pre><p>Here I am declaratively saying we need a pod of kind MySQL with 3 members in it.</p><p>4. Apply our CRD</p><pre>kubectl apply -f cluster.yaml</pre><p>5. Check if our custom resource is in running state</p><pre>kubectl -n my-namespace get mysqlclusters</pre><p>6. As we have not specified the root password in our CRD, the operator would have auto generated one, and put it in the secret.</p><pre>kubectl -n my-namespace get secret my-app-db-root-password -o jsonpath=&quot;{.data.password}&quot; | base64 --decode</pre><p>7. Once we grab the password, lets just use the mysql client to see it working</p><pre>kubectl run mysql-client -n my-namespace --image=mysql:5.7 -it --rm --restart=Never -- mysql -h my-app-db -u root -p</pre><p>8. We can see our pods in action with get pods command:</p><pre>my-namespace     my-app-db-0                             2/2     Running            2          1h<br>my-namespace     my-app-db-1                             2/2     Running            4          1h<br>my-namespace     my-app-db-2                             2/2     Running            2          1h</pre><p>Now, one can play around by changing other properties in the CRD such as multi-master setup/ default root password and many more.</p><p>As a follow up to this article, I will try to write one on how to write our own custom operators to abstract our domain specific problems. Thanks for the long read.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bc2f5e88dba" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>