Skip to content

Commit 69dd701

Browse files
likan999debarshiray
authored andcommitted
cmd/initContainer: Handle hosts with /etc/localtime as absolute symlink
On Arch Linux and Ubuntu hosts, /etc/localtime is an absolute symbolic link to /usr/share/zoneinfo/SomeTimeZone. So, inside the container, /run/host/etc/localtime also has /usr/share/zoneinfo/SomeTimeZone as its target. #622
1 parent 772451c commit 69dd701

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/cmd/initContainer.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,28 @@ func sanitizeRedirectionTarget(target string) string {
586586
return target
587587
}
588588

589+
func extractTimeZoneFromLocalTimeSymLink(path string) (string, error) {
590+
zoneInfoRoots := []string{
591+
"/run/host/usr/share/zoneinfo",
592+
"/usr/share/zoneinfo",
593+
}
594+
595+
for _, root := range zoneInfoRoots {
596+
if !strings.HasPrefix(path, root) {
597+
continue
598+
}
599+
600+
timeZone, err := filepath.Rel(root, path)
601+
if err != nil {
602+
return "", fmt.Errorf("failed to extract time zone: %w", err)
603+
}
604+
605+
return timeZone, nil
606+
}
607+
608+
return "", errors.New("/etc/localtime points to unknown location")
609+
}
610+
589611
func updateTimeZoneFromLocalTime() error {
590612
localTimeEvaled, err := filepath.EvalSymlinks("/etc/localtime")
591613
if err != nil {
@@ -602,15 +624,9 @@ func updateTimeZoneFromLocalTime() error {
602624

603625
logrus.Debugf("Resolved /etc/localtime to %s", localTimeEvaled)
604626

605-
const zoneInfoRoot = "/run/host/usr/share/zoneinfo"
606-
607-
if !strings.HasPrefix(localTimeEvaled, zoneInfoRoot) {
608-
return errors.New("/etc/localtime points to unknown location")
609-
}
610-
611-
timeZone, err := filepath.Rel(zoneInfoRoot, localTimeEvaled)
627+
timeZone, err := extractTimeZoneFromLocalTimeSymLink(localTimeEvaled)
612628
if err != nil {
613-
return fmt.Errorf("failed to extract time zone: %w", err)
629+
return err
614630
}
615631

616632
if err := writeTimeZone(timeZone); err != nil {

0 commit comments

Comments
 (0)