Skip to content

Commit

Permalink
add creationTime attribute (#235)
Browse files Browse the repository at this point in the history
* add creationTime attribute

* align json schema with spec
  • Loading branch information
mandelsoft authored Jan 25, 2023
1 parent fdf5499 commit 6efb44d
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 16 deletions.
1 change: 1 addition & 0 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (*ResourceSpecHandler) Add(ctx clictx.Context, ictx inputs.Context, elem ad

cd.Labels = r.Labels
cd.Provider = r.Provider
cd.CreationTime = metav1.NewTimestampP()

err = handle(ctx, ictx, elem.Source(), cv, r.Sources, srcs.ResourceSpecHandler{})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmds/ocm/commands/ocmcmds/common/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"encoding/json"
"fmt"

_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types"

"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/spf13/pflag"
"golang.org/x/text/cases"
"golang.org/x/text/language"

_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types"

"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/addhdlrs"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/options/dryrunoption"
Expand Down
1 change: 1 addition & 0 deletions cmds/ocm/commands/ocmcmds/componentarchive/create/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func (o *Command) Run() error {
desc.Provider.Name = metav1.ProviderName(o.Provider)
desc.Provider.Labels = o.ProviderLabels
desc.Labels = o.Labels
desc.CreationTime = metav1.NewTimestampP()

err = compdesc.Validate(desc)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/ocm/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ A component descriptor describes:
- a history of [Repository Contexts](#repository-contexts) describing
former repository locations of the component version along a transportation
chain
- an optional creation timestamp (RFC3339) rounded to seconds.
- a set of [Labels](#labels) to assign arbitrary kinds of information to the
component version, which is not formally defined by the Open Component Model
- an optional set of [Sources](#sources), that were used to generate the
Expand Down
4 changes: 4 additions & 0 deletions hack/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ go-bindata:
$(GOPATH)/bin/goimports:
go install -v golang.org/x/tools/cmd/goimports@latest

Linux_jq:
$(info -> jq is missing)
$(info - sudo apt-get install jq / sudo dnf install jq / sudo zypper install jq / sudo pacman -S jq)

Darwin_sed: Darwin
$(info -> GNU sed is missing)
$(info - brew install gnu-sed)
Expand Down
73 changes: 73 additions & 0 deletions pkg/contexts/ocm/compdesc/meta/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
package v1

import (
"time"

"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/open-component-model/ocm/pkg/errors"
)

const (
Expand Down Expand Up @@ -73,6 +77,9 @@ type ObjectMeta struct {
Labels Labels `json:"labels,omitempty"`
// Provider described the component provider
Provider Provider `json:"provider"`
// CreationTime is the creation time of component version
// +optional
CreationTime *Timestamp `json:"creationTime,omitempty"`
}

// GetName returns the name of the object.
Expand Down Expand Up @@ -151,3 +158,69 @@ func (o *Provider) Copy() *Provider {
Labels: o.Labels.Copy(),
}
}

////////////////////////////////////////////////////////////////////////////////

type _time = time.Time

// Timestamp is time rounded to seconds.
type Timestamp struct {
_time
}

func NewTimestamp() Timestamp {
return Timestamp{time.Now().Round(time.Second)}
}

func NewTimestampP() *Timestamp {
return &Timestamp{time.Now().Round(time.Second)}
}

func NewTimestampFor(t time.Time) Timestamp {
return Timestamp{t.Round(time.Second)}
}

// MarshalJSON implements the json.Marshaler interface.
// The time is a quoted string in RFC 3339 format, with sub-second precision added if present.
func (t Timestamp) MarshalJSON() ([]byte, error) {
if y := t.Year(); y < 0 || y >= 10000 {
// RFC 3339 is clear that years are 4 digits exactly.
// See golang.org/issue/4556#c15 for more discussion.
return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
}

b := make([]byte, 0, len(time.RFC3339)+2)
b = append(b, '"')
b = t.AppendFormat(b, time.RFC3339)
b = append(b, '"')
return b, nil
}

// UnmarshalJSON implements the json.Unmarshaler interface.
// The time is expected to be a quoted string in RFC 3339 format.
func (t *Timestamp) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
// Fractional seconds are handled implicitly by Parse.
tt, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
*t = NewTimestampFor(tt)
return err
}

func (t *Timestamp) Time() time.Time {
return t._time
}

func (t *Timestamp) Equal(o Timestamp) bool {
return t._time.Equal(o._time)
}

func (t *Timestamp) UTC() Timestamp {
return NewTimestampFor(t._time.UTC())
}

func (t *Timestamp) Add(d time.Duration) Timestamp {
return NewTimestampFor(t._time.Add(d))
}

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pkg/contexts/ocm/compdesc/versions/v2/componentdescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type ObjectMeta struct {
// describing the object.
// +optional
Labels metav1.Labels `json:"labels,omitempty"`
// CreationTime is the creation time of the component version
// +optional
CreationTime *metav1.Timestamp `json:"creationTime,omitempty"`
}

// GetName returns the name of the object.
Expand Down
4 changes: 2 additions & 2 deletions pkg/contexts/ocm/compdesc/versions/v2/jsonscheme/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6efb44d

Please sign in to comment.