feat: function creation timestamp (#651)

This commit is contained in:
Luke Kingland 2021-11-12 01:25:38 +09:00 committed by GitHub
parent fc1e874a5d
commit 1bf17ec976
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 2 deletions

View File

@ -481,7 +481,8 @@ func (c *Client) Create(f Function) (err error) {
return
}
// Write the Function metadata (func.yaml)
// Mark it as having been created via this client library and Write (save)
f.Created = time.Now()
if err = writeConfig(f); err != nil {
return
}

View File

@ -12,6 +12,7 @@ import (
"path/filepath"
"reflect"
"testing"
"time"
fn "knative.dev/kn-plugin-func"
"knative.dev/kn-plugin-func/mock"
@ -877,6 +878,29 @@ func TestRuntimes(t *testing.T) {
}
}
// TestCreateStamp ensures that the creation timestamp is set on functions
// which are successfully initialized using the client library.
func TestCreateStamp(t *testing.T) {
root := "testdata/example.com/testCreateStamp"
defer using(t, root)()
start := time.Now()
client := fn.New(fn.WithRegistry(TestRegistry))
if err := client.New(context.Background(), fn.Function{Root: root}); err != nil {
t.Fatal(err)
}
f, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}
if !f.Created.After(start) {
t.Fatalf("expected function timestamp to be after '%v', got '%v'", start, f.Created)
}
}
// Helpers ----
// USING: Make specified dir. Return deferrable cleanup fn.

View File

@ -63,6 +63,7 @@ builders:
envs: []
annotations: {}
labels: []
created: 2021-01-01T00:00:00+00:00
`
if err := ioutil.WriteFile("func.yaml", []byte(funcYaml), 0600); err != nil {
t.Fatal(err)

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"regexp"
"strings"
"time"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/api/resource"
@ -146,6 +147,7 @@ type Config struct {
Annotations map[string]string `yaml:"annotations"`
Options Options `yaml:"options"`
Labels Labels `yaml:"labels"`
Created time.Time `yaml:"created"`
// Add new values to the toConfig/fromConfig functions.
}
@ -268,6 +270,7 @@ func fromConfig(c Config) (f Function) {
Annotations: c.Annotations,
Options: c.Options,
Labels: c.Labels,
Created: c.Created,
}
}
@ -289,6 +292,7 @@ func toConfig(f Function) Config {
Annotations: f.Annotations,
Options: f.Options,
Labels: f.Labels,
Created: f.Created,
}
}

View File

@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
)
type Function struct {
@ -75,6 +76,11 @@ type Function struct {
// Health endpoints specified by the language pack
HealthEndpoints HealthEndpoints
// Created time is the moment that creation was successfully completed
// according to the client which is in charge of what constitutes being
// fully "Created" (aka initialized)
Created time.Time
}
// NewFunction loads a Function from a path on disk. use .Initialized() to determine if

View File

@ -18,7 +18,8 @@
"envs",
"annotations",
"options",
"labels"
"labels",
"created"
],
"properties": {
"name": {
@ -96,6 +97,10 @@
"$ref": "#/definitions/Label"
},
"type": "array"
},
"created": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": false,