Skip to content

Commit 5bba53a

Browse files
committed
fix sed inplace for macos
Signed-off-by: apedriza <[email protected]>
1 parent 6f3016f commit 5bba53a

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

internal/controller/k0smotron.io/k0smotroncluster_entrypoint.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (scope *kmcScope) generateEntrypointCM(kmc *km.Cluster) (v1.ConfigMap, erro
4242
"KineDataSourceURLPlaceholder": kineDataSourceURLPlaceholder,
4343
"K0sControllerArgs": getControllerFlags(kmc),
4444
"PrivilegedPortIsUsed": kmc.Spec.Service.APIPort <= 1024,
45+
"UniversalSedInplace": universalSedInplace,
4546
})
4647
if err != nil {
4748
return v1.ConfigMap{}, err
@@ -106,15 +107,19 @@ func getControllerFlags(kmc *km.Cluster) string {
106107
return strings.Join(flags, " ")
107108
}
108109

109-
const entrypointTemplate = `
110+
const (
111+
entrypointTemplate = `
110112
#!/bin/sh
111113
114+
# Universal in-place "sedi" function for sed
115+
{{ .UniversalSedInplace }}
116+
112117
# Put the k0s.yaml in place
113118
mkdir /etc/k0s && echo "$K0SMOTRON_K0S_YAML" > /etc/k0s/k0s.yaml
114119
115120
# Substitute the kine datasource URL from the env var
116121
escaped_url=$(printf '%s' "$K0SMOTRON_KINE_DATASOURCE_URL" | sed 's/[&/\]/\\&/g')
117-
sed -i "s {{ .KineDataSourceURLPlaceholder }} $escaped_url g" /etc/k0s/k0s.yaml
122+
sedi "s|{{ .KineDataSourceURLPlaceholder }}|$escaped_url|g" /etc/k0s/k0s.yaml
118123
119124
{{if .PrivilegedPortIsUsed}}
120125
apk add --no-cache libcap
@@ -124,3 +129,15 @@ apk add --no-cache libcap
124129
# Run the k0s controller
125130
k0s controller {{ .K0sControllerArgs }}
126131
`
132+
universalSedInplace = `
133+
sedi() {
134+
# Try GNU sed
135+
if sed --version >/dev/null 2>&1; then
136+
sed -i "$@"
137+
else
138+
# BSD sed (macOS) or BusyBox sed
139+
sed -i '' "$@"
140+
fi
141+
}
142+
`
143+
)

internal/controller/k0smotron.io/k0smotroncluster_entrypoint_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package k0smotronio
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"os/exec"
2223
"strings"
@@ -139,12 +140,12 @@ func TestKineDataSourceURLSubstitution(t *testing.T) {
139140
if strings.Contains(line, "escaped_url=$(printf") && strings.Contains(line, "K0SMOTRON_KINE_DATASOURCE_URL") {
140141
printfLine = line
141142
}
142-
if strings.Contains(line, "sed -i") && strings.Contains(line, "$escaped_url") {
143+
if strings.Contains(line, "sedi") && strings.Contains(line, "$escaped_url") {
143144
sedLine = line
144145
}
145146
}
146147
require.NotEmpty(t, printfLine, "printf command not found in entrypoint script")
147-
require.NotEmpty(t, sedLine, "sed command not found in entrypoint script")
148+
require.NotEmpty(t, sedLine, "sedi command not found in entrypoint script")
148149

149150
testK0sConfig := `apiVersion: k0s.k0sproject.io/v1beta1
150151
kind: ClusterConfig
@@ -169,10 +170,11 @@ spec:
169170
os.Setenv("K0SMOTRON_KINE_DATASOURCE_URL", tc.kineDataSourceURL)
170171
t.Cleanup(func() { os.Unsetenv("K0SMOTRON_KINE_DATASOURCE_URL") })
171172

172-
actualSedCmd := strings.Replace(sedLine, "/etc/k0s/k0s.yaml", tmpFile.Name(), 1)
173+
actualSediCmd := strings.Replace(sedLine, "/etc/k0s/k0s.yaml", tmpFile.Name(), 1)
173174

174-
combinedScript := printfLine + "\n" + actualSedCmd
175+
combinedScript := universalSedInplace + "\n" + printfLine + "\n" + actualSediCmd
175176

177+
fmt.Println(combinedScript)
176178
cmd := exec.Command("sh", "-c", combinedScript)
177179
cmd.Env = append(os.Environ(), "K0SMOTRON_KINE_DATASOURCE_URL="+tc.kineDataSourceURL)
178180

0 commit comments

Comments
 (0)