Monday, May 8, 2023

What is Union in python?

Union here has nothing to do with C or the kind of unions found in C. It just means that you can provide either a str or a Path for the filename argument... whatever a Path is in this context.

Generally if you see a function you have no idea what arguments it takes, so the typing library (and syntax) gives you some possibilities to give some information there. If you want to say "give me either a string or a path, but nothing else" Union is the thing to do that. 

references:

https://stackoverflow.com/questions/40816798/how-to-provide-argument-as-union

a good example of python logger wrapper

This one is pretty much sophisticated one.  

references:

https://bbengfort.github.io/2016/01/logging-mixin/

What are the args and kwargs in python

*args (Non-Keyword Arguments)

**kwargs (Keyword Arguments)


he special syntax *args in function definitions in Python is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list. 


The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args.

What *args allows you to do is take in more arguments than the number of formal arguments that you previously defined. With *args, any number of extra arguments can be tacked on to your current formal parameters (including zero extra arguments).

For example, we want to make a multiply function that takes any number of arguments and is able to multiply them all together. It can be done using *args.

Using the *, the variable that we associate with the * becomes iterable meaning you can do things like iterate over it, run some higher-order functions such as map and filter, etc.

The special syntax **kwargs in function definitions in Python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is that the double star allows us to pass through keyword arguments (and any number of them).


A keyword argument is where you provide a name to the variable as you pass it into the function.

One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn’t seem to be any order in which they were printed out. 

references:

https://www.geeksforgeeks.org/args-kwargs-python/


Wednesday, May 3, 2023

Python Root Logger and problems

Let’s look at the below code:


# 1. code inside myprojectmodule.py

import logging

logging.basicConfig(file='module.log')


#-----------------------------


# 2. code inside main.py (imports the code from myprojectmodule.py)

import logging

import myprojectmodule  # This runs the code in myprojectmodule.py


logging.basicConfig(file='main.log')  # No effect, because!

Imagine you have one or more modules in your project. And these modules use the basic root module. Then, when importing the module (‘myprojectmodule.py‘), all of that module’s code will run and logger gets configured.


Once configured, the root logger in the main file (that imported the ‘myprojectmodule‘ module) will no longer be able to change the root logger settings. Because, the logging.basicConfig() once set cannot be changed.


That means, if you want to log the messages from myprojectmodule to one file and the logs from the main module in another file, root logger can’t that.


references:

https://www.machinelearningplus.com/python/python-logging-guide/


kubectl restartng pods


kubectl scale deployment base-migrate --replicas=0 -n bpa-ns deployment.apps/base-migrate scaled

kubectl scale deployment base-migrate --replicas=1 -n bpa-ns deployment.apps/base-migrate scaled

kubectl rollout restart deployment db-migrate -n test-ns

 references:

https://loft.sh/blog/how-to-restart-pods-in-kubectl-a-tutorial-with-examples/

Monday, May 1, 2023

What is keep alived

keepalived is a framework for both load balancing and high availability that implements VRRP. This is a protocol that you see on some routers and has been implemented in keepalived. It creates a Virtual IP (or VIP, or floating IP) that acts as a gateway to route traffic to all participating hosts. This VIP that can provide a high availability setup and fail over to another host in the event that one is down.


references:

https://docs.technotim.live/posts/keepalived-ha-loadbalancer/#:~:text=keepalived%20is%20a%20framework%20for,traffic%20to%20all%20participating%20hosts.


Kubernetes Getting to know the ingress controller

In a nutshell, an ingress controller is a reverse proxy for the Kubernetes universe. It acts as a reverse proxy, routing traffic from the outside world to the correct service within a Kubernetes cluster, and allows you to configure an HTTP or HTTPS load balancer for the said cluster.


To better understand this, let’s take a step back first and look at the Ingress itself. A Kubernetes Ingress is an API object that determines how incoming traffic from the internet should reach the internal cluster Services, which then in turn send requests to groups of Pods. The Ingress itself has no power over the system — it is actually a configuration request for the ingress controller.


The ingress controller accepts traffic from outside the Kubernetes platform and load balances it to Pods running inside the platform, this way adding a layer of abstraction to traffic routing. Ingress controllers convert configurations from Ingress resources into routing rules recognized and implemented by reverse proxies.


Ingress controllers are used to expose multiple services from within your Kubernetes cluster to the outside world, using a single endpoint — for example, a DNS name or IP address —  to access them. Specifically, ingress controllers are used to:


Expose multiple services under a single DNS name

Implement path-based routing, where different URLs map to different services

Implement host-based routing, where different hostnames map to different services

Implement basic authentication or other access control methods for your applications

Implement rate limiting for your applications

Offload SSL/TLS termination from your applications to the ingress controller

references:

https://traefik.io/blog/reverse-proxy-vs-ingress-controller-vs-api-gateway/#:~:text=Long%20story%20short%2C%20ingress%20controllers,network%20proxy%20tailored%20for%20microservices.