Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Template based json patch
func TestX(t *testing.T) {
patchs := []Patch{
Patch{
Name: "telepresence",
Template: []byte(`[
{{ if .Data.Has "/spec/template/metadata/labels/version" }}
{"op": "copy", "from": "/spec/template/metadata/labels/version", "path": "/spec/template/metadata/labels/version-source"},
{{ end }}
{"op": "replace", "path": "/metadata/name", "value": "{{.Data.Value "/metadata/name"}}-{{.NewVersion}}"},
{"op": "replace", "path": "/spec/template/spec/replicas", "value": "1"},
{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "datawire/telepresence-k8s:{{.Vars.TelepresenceVersion}}"},
{"op": "replace", "path": "/spec/template/metadata/labels/version", "value": "{{.NewVersion}}"},
{"op": "replace", "path": "/spec/selector/matchLabels/version", "value": "{{.NewVersion}}"},
{"op": "replace", "path": "/metadata/labels/version", "value": "{{.NewVersion}}"},
{"op": "add", "path": "/spec/template/metadata/labels/telepresence", "value": "test"},
{"op": "add", "path": "/spec/template/spec/containers/0/env/-", "value": {
"name": "TELEPRESENCE_CONTAINER_NAMESPACE",
"valueFrom": {
"fieldRef": {
"apiVersion": "v1",
"fieldPath": "metadata.namespace"
}
}
}
},
{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"},
{"op": "remove", "path": "/spec/template/spec/containers/0/readinessProbe"},
{{ template "basic-remove" }}
]`),
Variables: map[string]string{
"TelepresenceVersion": "y",
},
},
Patch{
Name: "basic-remove",
Template: []byte(`
{"op": "remove", "path": "/metadata/resourceVersion"},
{"op": "remove", "path": "/metadata/uid"},
{"op": "remove", "path": "/metadata/generation"},
{"op": "remove", "path": "/metadata/creationTimestamp"}
`),
},
}
e := NewEngine(patchs)
modified, err := e.Run("telepresence", []byte(org), map[string]string{
"TelepresenceVersion": "x",
})
if err != nil {
fmt.Println(err)
}
fmt.Printf("Modified document: %s\n", modified)
}
var org = `
{
"apiVersion": "extensions/v1beta1",
"kind": "Deployment",
"metadata": {
"annotations": {
"deployment.kubernetes.io/revision": "1",
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"extensions/v1beta1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{},\"creationTimestamp\":null,\"name\":\"productpage-v1\",\"namespace\":\"bookinfo\"},\"spec\":{\"replicas\":1,\"selector\":null,\"strategy\":{},\"template\":{\"metadata\":{\"annotations\":{\"kiali.io/runtimes\":\"go\",\"prometheus.io/path\":\"/metrics\",\"prometheus.io/port\":\"9080\",\"prometheus.io/scheme\":\"http\",\"prometheus.io/scrape\":\"true\",\"sidecar.istio.io/inject\":\"true\"},\"creationTimestamp\":null,\"labels\":{\"app\":\"productpage\",\"version\":\"v1\"}},\"spec\":{\"containers\":[{\"env\":[{\"name\":\"SERVICE_NAME\",\"value\":\"productpage-v1\"},{\"name\":\"HTTP_ADDR\",\"value\":\":9080\"},{\"name\":\"SERVICE_CALL\",\"value\":\"http://reviews:9080/\"}],\"image\":\"docker.io/aslakknutsen/istio-workspace-test:latest\",\"imagePullPolicy\":\"Always\",\"livenessProbe\":{\"httpGet\":{\"path\":\"/healthz\",\"port\":9080},\"initialDelaySeconds\":1,\"periodSeconds\":3},\"name\":\"productpage\",\"ports\":[{\"containerPort\":9080}],\"readinessProbe\":{\"httpGet\":{\"path\":\"/healthz\",\"port\":9080},\"initialDelaySeconds\":1,\"periodSeconds\":3},\"resources\":{}}]}}},\"status\":{}}\n"
},
"creationTimestamp": "2019-07-13T08:46:46Z",
"generation": 1,
"labels": {
"app": "productpage",
"version": "v1"
},
"name": "productpage-v1",
"namespace": "bookinfo",
"resourceVersion": "638482",
"selfLink": "/apis/extensions/v1beta1/namespaces/bookinfo/deployments/productpage-v1",
"uid": "bf2a3655-a54a-11e9-b309-482ae3045b54"
},
"spec": {
"progressDeadlineSeconds": 600,
"replicas": 1,
"revisionHistoryLimit": 10,
"selector": {
"matchLabels": {
"app": "productpage",
"version": "v1"
}
},
"strategy": {
"rollingUpdate": {
"maxSurge": 1,
"maxUnavailable": 1
},
"type": "RollingUpdate"
},
"template": {
"metadata": {
"annotations": {
"kiali.io/runtimes": "go",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "9080",
"prometheus.io/scheme": "http",
"prometheus.io/scrape": "true",
"sidecar.istio.io/inject": "true"
},
"creationTimestamp": null,
"labels": {
"app": "productpage",
"version": "v1"
}
},
"spec": {
"containers": [
{
"env": [
{
"name": "SERVICE_NAME",
"value": "productpage-v1"
},
{
"name": "HTTP_ADDR",
"value": ":9080"
},
{
"name": "SERVICE_CALL",
"value": "http://reviews:9080/"
}
],
"image": "docker.io/aslakknutsen/istio-workspace-test:latest",
"imagePullPolicy": "Always",
"livenessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/healthz",
"port": 9080,
"scheme": "HTTP"
},
"initialDelaySeconds": 1,
"periodSeconds": 3,
"successThreshold": 1,
"timeoutSeconds": 1
},
"name": "productpage",
"ports": [
{
"containerPort": 9080,
"protocol": "TCP"
}
],
"readinessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/healthz",
"port": 9080,
"scheme": "HTTP"
},
"initialDelaySeconds": 1,
"periodSeconds": 3,
"successThreshold": 1,
"timeoutSeconds": 1
},
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File"
}
],
"dnsPolicy": "ClusterFirst",
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"terminationGracePeriodSeconds": 30
}
}
}
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment