Author Archives: Niklas

Example of the Builder Pattern in Java

A small example of the Builder Pattern in Java. We are going to build cars with different colors, brands and models 🙂

Car.java

public class Car {
    private String brand;
    private String color;
    private String model;

    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
}

CarBuilder.java

public class CarBuilder {
    private Car car;
    
    private CarBuilder() {
        car = new Car();
    }
    public static CarBuilder aCar() {
        return new CarBuilder();
    }
    public CarBuilder withBrand(String brand) {
        car.setBrand(brand);
        return this;
    }
    public CarBuilder withColor(String color) {
        car.setColor(color);
        return this;
    }
    public CarBuilder withModel(String model) {
        car.setModel(model);
        return this;
    }
    public Car build() {
        return car;
    }
}

With these two classes above we can now build some cars:
Main.java

public class Main {
    public static void main(String[] args) {
        Car myFirstCar = CarBuilder.aCar().withBrand("Volvo")
                                          .withColor("Blue")
                                          .withModel("XC90")
                                          .build();

        Car mySecondCar = CarBuilder.aCar().withBrand("Skoda")
                                           .withColor("Grey")
                                           .withModel("130L")
                                           .build();
        System.out.println("My first car was a " + myFristCar.getBrand());
        System.out.println("My second car was a " + mySecondCar.getBrand());
    }
}

Should print:

My first car was a Volvo
My Second car was a Skoda

Tested on Ubuntu 20.04.4 LTS and Java 21

Kubernetes: Reference a section or value inside a manifest

A colleague showed me this neat trick so I didn’t have to write the same information twice in a Ingress manifest file. I needed to map two hosts to the same path, and instead of duplicate the same information twice we can just reference the “http” section in the previous definition in the second host.

Example:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: default
  annotations:
spec:
  ingressClassName: nginx
  rules:
    - host: my-first.domain.se
      http: &http-paths
        paths:
          - path: /my-application
            pathType: Prefix
            backend:
              service:
                name: my-application-service
                port:
                  number: 8080
    - host: my-second.domain.se
      http: *http-paths

The trick here is to use the &-sign to mark a place in manifest that you want to reference. In this example I named the reference “&http-paths”. When we later define the second host (my-second.domain.se) we can just de-reference the reference with the *-sign, here show as “*http-paths”. This will “copy” the whole http section with path, service and port, from the first definition and “paste” it into the second host section.
In Kubernetes this will be de-referenced and look like I put the same information in both hosts

Tested on Tanzu Kubernetes v1.22

K9s: Adjust memory and cpu warning levels

Maybe you are like me and feel that the default memory warning level of 70% is a little off in the clusters you work.

Here is how to change them:
1. Open .config/k9s/config.yaml in your favorite editor (of course VIM 😉 )
2. Edit the section called “thresholds”

 thresholds:
    cpu:
      critical: 90
      warn: 70
    memory:
      critical: 90
      warn: 70

3. Save and restart k9s – done!

Apart from this, K9s is a wonderful tool that I don’t want to live a day without 🙂
You can find it here: https://k9scli.io/. Just download and enjoy!

Tested on K9s v.0.31.9 and Ubuntu 20.04.4 LTS (WSL2)