514 words

Add Kubectl Context to Cmder Prompt

Add a .lua file under .\config\myconfig.lua_

function custom_prompt()
  -- Exectute kubectl command to get context (Note the output will contain a newline)
  local handle = io.popen("kubectl config current-context")
  local context = handle:read("*a")
  handle:close()

  cwd = clink.get_cwd()
  prompt = "\x1b[1;32;40m{cwd} {git}{hg} \x1b[0;35;40m{context}\x1b[1;30;40m{lamb} \x1b[0m"
  new_value = string.gsub(prompt, "{cwd}", cwd)
  add_context = string.gsub(new_value, "{context}", context)
  clink.prompt.value = string.gsub(add_context, "{lamb}", "λ")
end

clink.prompt.register_filter(custom_prompt, 1)

Write ConfigMap Value as File

To write a value from a ConfigMap to a file inside a container use a volume mount with a subPath

ConfigMap.yaml

kind: ConfigMap
apiVersion: v1
metadata:
  name: my-config
data:
  appsettings.json: |-
    {
      "ApplicationName": "My Application",
        "OtherSettings" : {
      ...
        }
     }

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      volumes:
      - name: config-volume
        configMap:
          name: my-config
      containers:
      - name: MyDeployment
        image: MyImage
        volumeMounts:
          - name: config-volume
            mountPath: /app/Conf/appsettings.json
            subPath: appsettings.json
            readOnly: true

Using iframeResizer in a Vue.js Component

Using the iframeresizer library in a Vue.js component

<template>
  <div>
    <iframe id="myiframe" :src="http://example.com/url/of/page"></iframe>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import iframeResizer from "iframe-resizer";

@Component({})
export default class IframeComponent extends Vue {
  mounted() {
    this.resize();
  }
  resize() {
    (iframeResizer as any).iframeResizer({ checkOrigin: false }, "#myiframe");
  }
}
</script>

Configure SPA Routing in .NET Core Application

When serving a Single Page Application (SPA) from a .NET Core service which also contains an API some configuration is required to return the SPA for relevant requests.

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.Use(async (context, next) =>
            {
                string path = context.Request.Path.Value;

                if (!path.StartsWith("/api") && !Path.HasExtension(path))
                {
                    /* 
                       If this is not an API request or a request for static content (e.g. css/javascript) 
                       then return the index.html of the SPA 
                    */
                    context.Response.ContentType = "text/html";
                    await context.Response.SendFileAsync("path/to/index.html");
                }
                else
                {
                    await next.Invoke();
                }
            });

           // ...
    }

Global Error Handling in Vue.js

Handle errors from Vue components

 Vue.config.errorHandler = function(err: Error, vm: Vue, info: string) {
      // ...
    };

Handle javascript errors

 window.onerror = function(msg: any, url: any, line: any, col: any, error: any){
      // ...
    };

Handle errors from rejected promises as these are not caught by window.onerror

 window.addEventListener("unhandledrejection", function(event: any){
      event.preventDefault();
      // ...
    });

Kubectl Cheatsheet

GENERAL

    kubectl version
    kubectl cluster-info

CONTEXT

    kubectl config current-context
    kubectl config use-context cp08

INFORMATION

    kubectl get services                  # List all services in the namespace
    kubectl get pods --all-namespaces     # List all pods in all namespaces
    kubectl get pods -o wide              # List all pods in the namespace, with more details
    kubectl get rc <rc-name>              # List a particular replication controller
    kubectl get pods -l env=production    # List all pods with a label env=production
    kubectl get netpol                    # Get Network Policies

APPLY

    kubectl apply -f .                    # Apply all scripts in this folder and sub folders
    kubectl delete -f .                   # Delete all scripts in this folder and sub folders

VERBOSE

    kubectl describe nodes <node-name>
    kubectl describe pods <pod-name>
    kubectl describe pods <rc-name>       # Lists pods created by <rc-name> using common prefix

OTHER

    kubectl cp <file> <pod>:<file_path>
    kubectl exec -it <pod> -- /bin/bash
    kubectl exec -it <pod> --container=<container> /bin/sh