Skip to content

Commit

Permalink
fix generate-yaml by escaping newlines (kubernetes-sigs#404)
Browse files Browse the repository at this point in the history
We had a problem with generate-yaml.sh because yq rejected the invalid YAML.
To work around this we have to make sure the master.yaml is also a valid YAML.
So functions like Indent are no option. One option to solve this would have been
base64Encode, but the problem is that Ignition has no property to specify that the
inline content is base64 encoded. The only would have been `inline: !! binary |`, but
then yq complains that {{ .CaCert | Base64Encode }} is not correctly encoded.

So the solution was to just replace the new lines with \n
  • Loading branch information
sbueringer authored and pierreprinetti committed Apr 22, 2024
1 parent ec9bdac commit dca304d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ storage:
id: 0
mode: 0640
contents:
inline: |
{{ .CACert | Indent 10}}
inline: "{{ .CACert | EscapeNewLines }}"

- path: /etc/kubernetes/pki/ca.key
filesystem: root
Expand All @@ -20,8 +19,7 @@ storage:
id: 0
mode: 0600
contents:
inline: |
{{ .CAKey | Indent 10}}
inline: "{{ .CAKey | EscapeNewLines }}"

- path: /etc/kubernetes/pki/etcd/ca.crt
filesystem: root
Expand All @@ -31,8 +29,7 @@ storage:
id: 0
mode: 0640
contents:
inline: |
{{ .EtcdCACert | Indent 10}}
inline: "{{ .EtcdCACert | EscapeNewLines }}"

- path: /etc/kubernetes/pki/etcd/ca.key
filesystem: root
Expand All @@ -42,8 +39,7 @@ storage:
id: 0
mode: 0600
contents:
inline: |
{{ .EtcdCAKey | Indent 10}}
inline: "{{ .EtcdCAKey | EscapeNewLines }}"

- path: /etc/kubernetes/pki/front-proxy-ca.crt
filesystem: root
Expand All @@ -53,8 +49,7 @@ storage:
id: 0
mode: 0640
contents:
inline: |
{{ .FrontProxyCACert | Indent 10}}
inline: "{{ .FrontProxyCACert | EscapeNewLines }}"

- path: /etc/kubernetes/pki/front-proxy-ca.key
filesystem: root
Expand All @@ -64,8 +59,7 @@ storage:
id: 0
mode: 0600
contents:
inline: |
{{ .FrontProxyCAKey | Indent 10}}
inline: "{{ .FrontProxyCAKey | EscapeNewLines }}"

- path: /etc/kubernetes/pki/sa.pub
filesystem: root
Expand All @@ -75,8 +69,7 @@ storage:
id: 0
mode: 0640
contents:
inline: |
{{ .SaCert | Indent 10}}
inline: "{{ .SaCert | EscapeNewLines }}"

- path: /etc/kubernetes/pki/sa.key
filesystem: root
Expand All @@ -86,8 +79,7 @@ storage:
id: 0
mode: 0600
contents:
inline: |
{{ .SaKey | Indent 10}}
inline: "{{ .SaKey | EscapeNewLines }}"


- path: /etc/kubernetes/kubeadm_config.yaml
Expand Down
7 changes: 6 additions & 1 deletion pkg/cloud/openstack/services/userdata/machinescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ func masterStartupScript(cluster *clusterv1.Cluster, machine *clusterv1.Machine,
}

fMap := map[string]interface{}{
"Indent": templateYAMLIndent,
"EscapeNewLines": templateEscapeNewLines,
"Indent": templateYAMLIndent,
}

masterStartUpScript := template.Must(template.New("masterStartUp").Funcs(fMap).Parse(script))
Expand Down Expand Up @@ -368,6 +369,10 @@ func getSubnet(netRange clusterv1.NetworkRanges) string {
return netRange.CIDRBlocks[0]
}

func templateEscapeNewLines(s string) string {
return strings.ReplaceAll(s, "\n", "\\n")
}

func templateYAMLIndent(i int, input string) string {
split := strings.Split(input, "\n")
ident := "\n" + strings.Repeat(" ", i)
Expand Down

0 comments on commit dca304d

Please sign in to comment.