CloudNativeGame / fake-time-injector

A sidecar injector which change the time inside the container instead of the host.
Apache License 2.0
40 stars 7 forks source link

[feature] libfaketime 支援相對時間 #22

Open huli-ctw opened 5 months ago

huli-ctw commented 5 months ago

libfaketime 有支援相對時間的用法:https://github.com/wolfcw/libfaketime#:~:text=4d)%20Using%20offsets%20for%20relative%20dates

如 "-120" 或是 "+120" 等等

儘管在 yaml 中看似可以傳入相對時間,但是在 plugin 中都是轉成了絕對時間:https://github.com/CloudNativeGame/fake-time-injector/blob/main/plugins/faketime/faketime.go#L56

func calculateFakeTime(t string) (string, error) {
    if strings.Contains(t, ":") {
        t_, err := time.Parse("2006-01-02 15:04:05", t)
        if err != nil {
            return "", err
        }
        s := t_.UTC().String()
        return s, nil
    }
    // 解析时间间隔字符串为Duration类型
    duration, err := time.ParseDuration(t)
    if err != nil {
        return "", err
    }
    s := time.Now().UTC().Add(duration).String()
    return s, nil
}

而 libFakeTimePatches 是直接使用了這個絕對時間:https://github.com/CloudNativeGame/fake-time-injector/blob/main/plugins/faketime/faketime.go#L217

    Env := []apiv1.EnvVar{
        {Name: "LD_PRELOAD", Value: LibFakeTimePath},
        {Name: "FAKETIME", Value: fmt.Sprintf("@%s", fakeTime)},
    }

這造成了在 libfaketime 中無法使用相對時間來調整,希望能加上相關功能,能夠讓 FAKETIME 也支援如 "+120" 這類的語法,或至少在 annotation 的參數如果是數字的話,就直接使用這個數字,而不是用 calculateFakeTime 回傳新的 date time

songkang7 commented 1 month ago

@huli-ctw 已支持相对时间 https://github.com/CloudNativeGame/fake-time-injector/pull/24