Files
fission-console/console/internal/api/function_timestamps.go
T

71 lines
1.8 KiB
Go

package api
import (
"fmt"
"time"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
const (
functionCreatedAtAnnotation = "fission-console/created-at"
functionUpdatedAtAnnotation = "fission-console/updated-at"
)
func ensureFunctionTimestamps(fn *unstructured.Unstructured, now time.Time) {
if fn == nil {
return
}
annotations := fn.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
if annotations[functionCreatedAtAnnotation] == "" {
if created := fn.GetCreationTimestamp(); !created.IsZero() {
annotations[functionCreatedAtAnnotation] = created.UTC().Format(time.RFC3339)
} else {
annotations[functionCreatedAtAnnotation] = now.UTC().Format(time.RFC3339)
}
}
annotations[functionUpdatedAtAnnotation] = now.UTC().Format(time.RFC3339)
fn.SetAnnotations(annotations)
}
func functionTimestampResponse(fn *unstructured.Unstructured) map[string]string {
result := map[string]string{}
if fn == nil {
return result
}
annotations := fn.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
if created := annotations[functionCreatedAtAnnotation]; created != "" {
result["created_at"] = created
} else if ts := fn.GetCreationTimestamp(); !ts.IsZero() {
result["created_at"] = ts.UTC().Format(time.RFC3339)
}
if updated := annotations[functionUpdatedAtAnnotation]; updated != "" {
result["updated_at"] = updated
} else if v := result["created_at"]; v != "" {
result["updated_at"] = v
}
return result
}
func formatRFC3339Now(now time.Time) string {
return now.UTC().Format(time.RFC3339)
}
func parseRFC3339(value string) (time.Time, error) {
return time.Parse(time.RFC3339, value)
}
func mustParseRFC3339(value string) time.Time {
parsed, err := parseRFC3339(value)
if err != nil {
panic(fmt.Sprintf("parse RFC3339 %q: %v", value, err))
}
return parsed
}