Skip to content

Commit

Permalink
Add generate_example_manifest.go
Browse files Browse the repository at this point in the history
Add script to generate manifest of pod with certain # PVs

Add to --node-affinity flag to generate_example_manifest.go
  • Loading branch information
AndrewSirenko committed Dec 6, 2023
1 parent 551b59a commit 76ae6be
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
51 changes: 51 additions & 0 deletions hack/device_slot_test.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: v1
kind: Pod
metadata:
name: device-limit-tester-{{ len .Volumes }}-volumes
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: {{ .NodeAffinityKey }}
operator: In
values:
- {{ .NodeAffinityValue }}
containers:
- name: device-limit-tester-{{ len .Volumes }}-volumes
image: centos
command: ["/bin/sh"]
args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
volumeMounts:
{{- range $index, $value := .Volumes }}
- name: persistent-storage-{{ $index }}
mountPath: /data-{{ $index }}
{{- end }}
volumes:
{{- range $index, $value := .Volumes }}
- name: persistent-storage-{{ $index }}
persistentVolumeClaim:
claimName: ebs-claim-{{ $index }}
{{- end }}
---
{{- range $index, $value := .Volumes }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ebs-claim-{{ $index }}
spec:
accessModes:
- ReadWriteOnce
storageClassName: ebs-sc
resources:
requests:
storage: 4Gi
---
{{- end }}
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: ebs-sc
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer
55 changes: 55 additions & 0 deletions hack/generate_example_manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"flag"
"fmt"
"os"
"strings"
"text/template"
)

type Manifest struct {
NodeAffinityKey string
NodeAffinityValue string
Volumes []int
}

func main() {
// Parse Command-Line args & flags
nodeAffinityPtr := flag.String("node-affinity", "", "node affinity for pod in form of 'key:value'")
volumeCountPtr := flag.Int("volume-count", 2, "amount of Volumes to provision")
flag.Parse()

nodeAffinityKey, nodeAffinityValue := parseNodeAffinityFlag(nodeAffinityPtr)

manifest := Manifest{
NodeAffinityKey: nodeAffinityKey,
NodeAffinityValue: nodeAffinityValue,
Volumes: make([]int, *volumeCountPtr),
}

// Generate manifest to stdout from template file
var tmplFile = "device_slot_test.tmpl"
tmpl, err := template.New(tmplFile).ParseFiles(tmplFile)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, manifest)
if err != nil {
panic(err)
}
}

func parseNodeAffinityFlag(nodeAffinityPtr *string) (string, string) {
nodeAffinityKey := ""
nodeAffinityValue := ""
if len(*nodeAffinityPtr) > 0 {
nodeAffinity := strings.Split(*nodeAffinityPtr, ":")
if len(nodeAffinity) != 2 {
panic(fmt.Errorf("flag '--node-affinity' must take the form 'key:value'"))
}
nodeAffinityKey = nodeAffinity[0]
nodeAffinityValue = nodeAffinity[1]
}
return nodeAffinityKey, nodeAffinityValue
}

0 comments on commit 76ae6be

Please sign in to comment.