-
Notifications
You must be signed in to change notification settings - Fork 468
/
kubelet_data_volume.go
68 lines (55 loc) · 2.21 KB
/
kubelet_data_volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package bootstrap
import (
"bytes"
_ "embed"
"fmt"
"os/exec"
"text/template"
"github.com/go-logr/logr"
"github.com/spf13/afero"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
nodeagentv1alpha1 "github.com/gardener/gardener/pkg/nodeagent/apis/config/v1alpha1"
)
var (
//go:embed templates/scripts/format-kubelet-data-volume.tpl.sh
formatKubeletDataVolumeTplContent string
formatKubeletDataVolumeTpl *template.Template
// ExecScript is a function for executing the formatting script.
// Exposed for testing.
ExecScript = func(scriptPath string) ([]byte, error) {
return exec.Command("/usr/bin/env", "bash", scriptPath).CombinedOutput()
}
)
func init() {
formatKubeletDataVolumeTpl = template.Must(template.New("format-kubelet-data-volume").Parse(formatKubeletDataVolumeTplContent))
}
func formatKubeletDataDevice(log logr.Logger, fs afero.Afero, kubeletDataVolumeSize int64) error {
log.Info("Rendering script")
var formatKubeletDataVolumeScript bytes.Buffer
if err := formatKubeletDataVolumeTpl.Execute(&formatKubeletDataVolumeScript, map[string]interface{}{"kubeletDataVolumeSize": kubeletDataVolumeSize}); err != nil {
return fmt.Errorf("failed rendering script: %w", err)
}
log.Info("Creating temporary file")
tmpFile, err := fs.TempFile(nodeagentv1alpha1.TempDir, "format-kubelet-data-volume-")
if err != nil {
return fmt.Errorf("unable to create temporary directory: %w", err)
}
defer func() {
log.Info("Removing temporary file", "path", tmpFile.Name())
utilruntime.HandleError(fs.Remove(tmpFile.Name()))
}()
log.Info("Writing script into temporary file", "path", tmpFile.Name())
if err := fs.WriteFile(tmpFile.Name(), formatKubeletDataVolumeScript.Bytes(), 0755); err != nil {
return fmt.Errorf("unable to write script into temporary file %q: %w", tmpFile.Name(), err)
}
log.Info("Executing script")
output, err := ExecScript(tmpFile.Name())
if err != nil {
return fmt.Errorf("failed executing formatter bash script: %w (output: %q)", err, string(output))
}
log.Info("Successfully formatted kubelet data volume", "output", string(output))
return nil
}