Skip to content

Commit

Permalink
fix: improve timezone handling in Kubernetes pods (#5087)
Browse files Browse the repository at this point in the history
  • Loading branch information
zjy365 authored Sep 19, 2024
1 parent 0f6d5cd commit febe93b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ const DelModal = ({
{activePage === Page.DELETION_WARNING && (
<Box my={3}>
{t('Please enter')}
<Box as={'span'} color={'myGray.900'} fontWeight={'bold'} userSelect={'all'}>
<Box
as={'span'}
px={'4px'}
color={'myGray.900'}
fontWeight={'bold'}
userSelect={'all'}
>
{appName}
</Box>
{t('To Confirm')}
Expand Down
77 changes: 73 additions & 4 deletions frontend/providers/applaunchpad/src/utils/kubeFileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { PassThrough, Readable, Writable } from 'stream';
import * as k8s from '@kubernetes/client-node';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';

dayjs.extend(utc);
dayjs.extend(timezone);

export type TFile = {
name: string;
Expand Down Expand Up @@ -84,6 +90,42 @@ export class KubeFileSystem {
});
}

async getPodTimezone(namespace: string, podName: string, containerName: string): Promise<string> {
try {
const dateOutput = await this.execCommand(namespace, podName, containerName, ['date', '+%z']);
const offset = dateOutput.trim();
if (offset) {
return this.getTimezoneFromOffset(offset);
}
} catch (error) {
console.log('Failed to get timezone offset using date command:', error);
}

try {
const timezoneFile = await this.execCommand(namespace, podName, containerName, [
'cat',
'/etc/timezone'
]);
if (timezoneFile.trim()) {
return timezoneFile.trim();
}
} catch (error) {}

try {
const localtimeLink = await this.execCommand(namespace, podName, containerName, [
'readlink',
'-f',
'/etc/localtime'
]);
const match = localtimeLink.match(/zoneinfo\/(.+)$/);
if (match) {
return match[1];
}
} catch (error) {}

return 'Etc/UTC';
}

async ls({
namespace,
podName,
Expand Down Expand Up @@ -127,13 +169,16 @@ export class KubeFileSystem {
const files: TFile[] = [];
const symlinks: TFile[] = [];

const podTimezone = await this.getPodTimezone(namespace, podName, containerName);

lines.forEach((line) => {
const parts = line.split('"');
const name = parts[1];

if (!name || name === '.' || name === '..') return;

const attrs = parts[0].split(' ').filter((v) => !!v);

const file: TFile = {
name: name,
path: (name.startsWith('/') ? '' : path + '/') + name,
Expand All @@ -144,18 +189,18 @@ export class KubeFileSystem {
owner: attrs[2],
group: attrs[3],
size: parseInt(attrs[4]),
updateTime: new Date(attrs.slice(5, 7).join(' '))
updateTime: this.convertToUTC(attrs.slice(5, 7).join(' '), podTimezone)
};

if (isCompatibleMode) {
file.updateTime = new Date(attrs.slice(5, 10).join(' '));
file.updateTime = this.convertToUTC(attrs.slice(5, 10).join(' '), podTimezone);
}

if (file.kind === 'c') {
if (isCompatibleMode) {
file.updateTime = new Date(attrs.slice(7, 11).join(' '));
file.updateTime = this.convertToUTC(attrs.slice(7, 11).join(' '), podTimezone);
} else {
file.updateTime = new Date(attrs.slice(6, 8).join(' '));
file.updateTime = this.convertToUTC(attrs.slice(6, 8).join(' '), podTimezone);
}
file.size = parseInt(attrs[5]);
}
Expand Down Expand Up @@ -335,4 +380,28 @@ export class KubeFileSystem {
}) {
return await this.execCommand(namespace, podName, containerName, ['md5sum', path]);
}

getTimezoneFromOffset(offset: string): string {
const hours = parseInt(offset.slice(1, 3));
const sign = offset.startsWith('-') ? '+' : '-';
return `Etc/GMT${sign}${hours}`;
}

convertToUTC(dateString: string, timezone: string): Date {
dateString = dateString.trim();
timezone = timezone.trim();

if (timezone === 'Etc/UTC') {
timezone = 'UTC';
}

const dt = dayjs.tz(dateString, timezone).utc();

if (!dt.isValid()) {
console.error(`Failed to parse date: "${dateString}" with timezone "${timezone}"`);
return new Date();
}

return dt.toDate();
}
}

0 comments on commit febe93b

Please sign in to comment.