Exec plugin on linux

Exec plugin on linux in 60 seconds

This is a (no reading allowed!) 60 second copy/paste guided example. Full plugin docs here.

This demo writes and uses a somewhat ridiculous exec plugin (written in bash) that generates a ConfigMap.

This is a guide to try it without damaging your current setup.

requirements

  • linux, git, curl, Go 1.13

Make a place to work

  1. DEMO=$(mktemp -d)

Create a kustomization

Make a kustomization directory to hold all your config:

  1. MYAPP=$DEMO/myapp
  2. mkdir -p $MYAPP

Make a deployment config:

  1. # $MYAPP/deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: the-deployment
  6. spec:
  7. replicas: 3
  8. template:
  9. spec:
  10. containers:
  11. - name: the-container
  12. image: monopole/hello:1
  13. command: ["/hello",
  14. "--port=8080",
  15. "--date=$(THE_DATE)",
  16. "--enableRiskyFeature=$(ENABLE_RISKY)"]
  17. ports:
  18. - containerPort: 8080
  19. env:
  20. - name: THE_DATE
  21. valueFrom:
  22. configMapKeyRef:
  23. name: the-map
  24. key: today
  25. - name: ALT_GREETING
  26. valueFrom:
  27. configMapKeyRef:
  28. name: the-map
  29. key: altGreeting
  30. - name: ENABLE_RISKY
  31. valueFrom:
  32. configMapKeyRef:
  33. name: the-map
  34. key: enableRisky
  35. EOF

Make a service config:

  1. # $MYAPP/service.yaml
  2. kind: Service
  3. apiVersion: v1
  4. metadata:
  5. name: the-service
  6. spec:
  7. type: LoadBalancer
  8. ports:
  9. - protocol: TCP
  10. port: 8666
  11. targetPort: 8080
  12. EOF

Now make a config file for the plugin you’re about to write.

This config file is just another k8s resource object. The values of its apiVersion and kind fields are used to find the plugin code on your filesystem (more on this later).

  1. # $MYAPP/cmGenerator.yaml
  2. apiVersion: myDevOpsTeam
  3. kind: SillyConfigMapGenerator
  4. metadata:
  5. name: whatever
  6. argsOneLiner: Bienvenue true
  7. EOF

Finally, make a kustomization file referencing all of the above:

  1. # $MYAPP/kustomization.yaml
  2. commonLabels:
  3. app: hello
  4. resources:
  5. - deployment.yaml
  6. - service.yaml
  7. generators:
  8. - cmGenerator.yaml
  9. EOF

Review the files

  1. ls -C1 $MYAPP

Make a home for plugins

Plugins must live in a particular place for kustomize to find them.

This demo will use the ephemeral directory:

  1. PLUGIN_ROOT=$DEMO/kustomize/plugin

The plugin config defined above in $MYAPP/cmGenerator.yaml specifies:

  1. apiVersion: myDevOpsTeam
  2. kind: SillyConfigMapGenerator

This means the plugin must live in a directory named:

  1. MY_PLUGIN_DIR=$PLUGIN_ROOT/myDevOpsTeam/sillyconfigmapgenerator
  2. mkdir -p $MY_PLUGIN_DIR

The directory name is the plugin config’s apiVersion followed by its lower-cased kind.

A plugin gets its own directory to hold itself, its tests and any supplemental data files it might need.

Create the plugin

There are two kinds of plugins, exec and Go.

Make an exec plugin, installing it to the correct directory and file name. The file name must match the plugin’s kind (in this case, SillyConfigMapGenerator):

  1. # $MY_PLUGIN_DIR/SillyConfigMapGenerator
  2. #!/bin/bash
  3. # Skip the config file name argument.
  4. shift
  5. today=`date +%F`
  6. echo "
  7. kind: ConfigMap
  8. apiVersion: v1
  9. metadata:
  10. name: the-map
  11. data:
  12. today: $today
  13. altGreeting: "$1"
  14. enableRisky: "$2"
  15. "
  16. EOF

By definition, an exec plugin must be executable:

  1. chmod a+x $MY_PLUGIN_DIR/SillyConfigMapGenerator

Install kustomize

Per the instructions:

  1. curl -s "https://raw.githubusercontent.com/\
  2. kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
  3. mkdir -p $DEMO/bin
  4. mv kustomize $DEMO/bin

Review the layout

  1. tree $DEMO

Build your app, using the plugin

  1. XDG_CONFIG_HOME=$DEMO $DEMO/bin/kustomize build --enable_alpha_plugins $MYAPP

Above, if you had set

  1. PLUGIN_ROOT=$HOME/.config/kustomize/plugin

there would be no need to use XDG_CONFIG_HOME in the kustomize command above.