T
I was able to combine your two example files by first converting them to JSON.
foo.json
{
"apiVersion": "extensions/v1beta1",
"kind": "Ingress",
"metadata": {
"name": "nginx-ingress"
},
"spec": {
"tls": [
{
"hosts": [
"foo.bar.com"
],
"secretName": "tls-secret"
}
],
"rules": [
{
"host": "foo.bar.com",
"http": {
"paths": [
{
"path": "/",
"backend": {
"serviceName": "foo-web-svc",
"servicePort": 80
}
},
{
"path": "/api",
"backend": {
"serviceName": "foo-rest-svc",
"servicePort": 80
}
}
]
}
}
]
}
}
baz.json
{
"apiVersion": "extensions/v1beta1",
"kind": "Ingress",
"metadata": {
"name": "nginx-ingress"
},
"spec": {
"tls": [
{
"hosts": [
"baz.bar.com"
],
"secretName": "tls-secret"
}
],
"rules": [
{
"host": "baz.bar.com",
"http": {
"paths": [
{
"path": "/",
"backend": {
"serviceName": "baz-web-svc",
"servicePort": 80
}
},
{
"path": "/api",
"backend": {
"serviceName": "baz-rest-svc",
"servicePort": 80
}
}
]
}
}
]
}
}
You can then combine the rules for each by using jq.
levi@La-Tower:~$ jq 'reduce inputs as $i (.; .spec.rules += $i.spec.rules)' foo.json baz.json
{
"apiVersion": "extensions/v1beta1",
"kind": "Ingress",
"metadata": {
"name": "nginx-ingress"
},
"spec": {
"tls": [
{
"hosts": [
"foo.bar.com"
],
"secretName": "tls-secret"
}
],
"rules": [
{
"host": "foo.bar.com",
"http": {
"paths": [
{
"path": "/",
"backend": {
"serviceName": "foo-web-svc",
"servicePort": 80
}
},
{
"path": "/api",
"backend": {
"serviceName": "foo-rest-svc",
"servicePort": 80
}
}
]
}
},
{
"host": "baz.bar.com",
"http": {
"paths": [
{
"path": "/",
"backend": {
"serviceName": "baz-web-svc",
"servicePort": 80
}
},
{
"path": "/api",
"backend": {
"serviceName": "baz-rest-svc",
"servicePort": 80
}
}
]
}
}
]
}
}
While yaml is more common JSON is perfectly valid for the Kubernetes API. All credit for the jq logic goes to @Jeff Mercado and his answer.