Skip to content

Commit c544aed

Browse files
authored
Use dynamic path for installer logs in V3 (#3240)
* Use app slug for logs path in V3 Signed-off-by: Steven Crespo <[email protected]> * f Signed-off-by: Steven Crespo <[email protected]> --------- Signed-off-by: Steven Crespo <[email protected]>
1 parent dd99792 commit c544aed

File tree

6 files changed

+73
-5
lines changed

6 files changed

+73
-5
lines changed

cmd/installer/goods/support/host-support-bundle.tmpl.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ spec:
287287
args: [ "sysinfo" ]
288288
- copy:
289289
collectorName: installer-logs
290-
path: /var/log/embedded-cluster/*.log
290+
path: {{ .LogsDir }}/*.log
291291
- copy:
292292
collectorName: installer-logs-old
293293
path: {{ .DataDir }}/logs/*.log

pkg/runtimeconfig/defaults.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,33 @@ func KotsadmNamespace(ctx context.Context, kcli client.Client) (string, error) {
7878
return namespace, nil
7979
}
8080

81-
// EmbeddedClusterLogsSubDir returns the path to the directory where embedded-cluster logs
81+
// EmbeddedClusterLogsPath returns the path to the directory where embedded-cluster logs
8282
// are stored.
83+
// For V2 compatibility, returns "/var/log/embedded-cluster".
84+
// For V3 (ENABLE_V3=1), returns "/var/log/{appslug}".
85+
func EmbeddedClusterLogsPath() string {
86+
// Use the app slug for V3 installations
87+
if os.Getenv("ENABLE_V3") == "1" {
88+
return filepath.Join("/var/log", AppSlug())
89+
}
90+
// V2 backwards compatibility
91+
return "/var/log/embedded-cluster"
92+
}
93+
94+
// EmbeddedClusterLogsSubDir returns the path to the directory where embedded-cluster logs
95+
// are stored and ensures the directory exists.
96+
// For V2 compatibility, returns "/var/log/embedded-cluster".
97+
// For V3 (ENABLE_V3=1), returns "/var/log/{appslug}".
8398
func EmbeddedClusterLogsSubDir() string {
84-
path := "/var/log/embedded-cluster"
99+
path := EmbeddedClusterLogsPath()
85100
if err := os.MkdirAll(path, 0755); err != nil {
86101
logrus.Fatalf("unable to create embedded-cluster logs dir: %s", err)
87102
}
88103
return path
89104
}
90105

91-
// PathToLog returns the full path to a log file. This function does not check
92-
// if the file exists.
106+
// PathToLog returns the full path to a log file and ensures the parent directory exists.
107+
// This function does not check if the file itself exists.
93108
func PathToLog(name string) string {
94109
return filepath.Join(EmbeddedClusterLogsSubDir(), name)
95110
}

pkg/runtimeconfig/defaults_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package runtimeconfig
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// TestEmbeddedClusterLogsPath tests the log path selection logic.
11+
func TestEmbeddedClusterLogsPath(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
enableV3 string
15+
want string
16+
}{
17+
{
18+
name: "V2 returns static path",
19+
enableV3: "",
20+
want: "/var/log/embedded-cluster",
21+
},
22+
{
23+
name: "V2 with ENABLE_V3=0 returns static path",
24+
enableV3: "0",
25+
want: "/var/log/embedded-cluster",
26+
},
27+
{
28+
name: "V3 returns dynamic path with app slug",
29+
enableV3: "1",
30+
want: filepath.Join("/var/log", AppSlug()),
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
t.Setenv("ENABLE_V3", tt.enableV3)
37+
38+
got := EmbeddedClusterLogsPath()
39+
40+
assert.Equal(t, tt.want, got)
41+
})
42+
}
43+
}

pkg/support/materialize.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type TemplateData struct {
1717
DataDir string
1818
K0sDataDir string
1919
OpenEBSDataDir string
20+
LogsDir string
2021
IsAirgap bool
2122
ReplicatedAppURL string
2223
ProxyRegistryURL string
@@ -36,6 +37,7 @@ func MaterializeSupportBundleSpec(rc runtimeconfig.RuntimeConfig, isAirgap bool)
3637
DataDir: rc.EmbeddedClusterHomeDirectory(),
3738
K0sDataDir: rc.EmbeddedClusterK0sSubDir(),
3839
OpenEBSDataDir: rc.EmbeddedClusterOpenEBSLocalSubDir(),
40+
LogsDir: runtimeconfig.EmbeddedClusterLogsPath(),
3941
IsAirgap: isAirgap,
4042
ReplicatedAppURL: netutils.MaybeAddHTTPS(domains.ReplicatedAppDomain),
4143
ProxyRegistryURL: netutils.MaybeAddHTTPS(domains.ProxyRegistryDomain),

tests/dryrun/install_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ func testDefaultInstallationImpl(t *testing.T) {
117117
"KUBECONFIG": "/var/lib/embedded-cluster/k0s/pki/admin.conf",
118118
})
119119

120+
// --- validate log directory (V2 static path) --- //
121+
logDir := runtimeconfig.EmbeddedClusterLogsSubDir()
122+
assert.Equal(t, "/var/log/embedded-cluster", logDir, "V2 should use static log directory")
123+
120124
// --- validate commands --- //
121125
// Get expected hostname to validate it's included in the kubelet args
122126
expectedHostname, err := nodeutil.GetHostname("")

tests/dryrun/v3_install_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func validateHappyPathOnline(t *testing.T, hcli *helm.MockClient) {
111111
assert.Equal(t, int64(0), in.Spec.AirgapUncompressedSize, "Installation.Spec.AirgapUncompressedSize should be 0 for online installations")
112112
assert.Equal(t, "80-32767", in.Spec.RuntimeConfig.Network.NodePortRange, "Installation.Spec.RuntimeConfig.Network.NodePortRange should be set to default range")
113113

114+
// Validate log directory (V3 dynamic path based on app slug)
115+
logDir := runtimeconfig.EmbeddedClusterLogsSubDir()
116+
assert.Equal(t, "/var/log/fake-app-slug", logDir, "V3 should use dynamic log directory based on app slug")
117+
114118
// Validate that HTTP collectors are present in host preflight spec for online installations
115119
assertCollectors(t, dr.HostPreflightSpec.Collectors, map[string]struct {
116120
match func(*troubleshootv1beta2.HostCollect) bool

0 commit comments

Comments
 (0)