Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for SOURCE_DATE_EPOCH in tkn bundle push #2137

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/cmd/tkn_bundle_push.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Authentication:
Input:
Valid input in any form is valid Tekton YAML or JSON with a fully-specified "apiVersion" and "kind". To pass multiple objects in a single input, use "---" separators in YAML or a top-level "[]" in JSON.

Created time:
Setting created time of the OCI Image Configuration layer can be done by either providing it via --ctime parameter or setting the SOURCE_DATE_EPOCH environment variable.


### Options

Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-bundle-push.1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Authentication:
Input:
Valid input in any form is valid Tekton YAML or JSON with a fully\-specified "apiVersion" and "kind". To pass multiple objects in a single input, use "\-\-\-" separators in YAML or a top\-level "[]" in JSON.

.PP
Created time:
Setting created time of the OCI Image Configuration layer can be done by either providing it via \-\-ctime parameter or setting the SOURCE\_DATE\_EPOCH environment variable.


.SH OPTIONS
.PP
Expand Down
60 changes: 41 additions & 19 deletions pkg/cmd/bundle/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@
package bundle

import (
"context"
"errors"
"fmt"
"io"
"os"
"strconv"
"time"

"github.com/google/go-containerregistry/pkg/name"
"github.com/jonboulle/clockwork"
Copy link
Member

@chmouel chmouel Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks addressing other parts of the code

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/bundle"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/params"
)

const sourceDateEpochEnv = "SOURCE_DATE_EPOCH"

type pushOptions struct {
cliparams cli.Params
stream *cli.Stream
Expand All @@ -35,6 +41,7 @@ type pushOptions struct {
remoteOptions bundle.RemoteOptions
annotationParams []string
annotations map[string]string
ctimeParam string
ctime time.Time
}

Expand All @@ -55,9 +62,10 @@ Authentication:

Input:
Valid input in any form is valid Tekton YAML or JSON with a fully-specified "apiVersion" and "kind". To pass multiple objects in a single input, use "---" separators in YAML or a top-level "[]" in JSON.
`

var ctime string
Created time:
Setting created time of the OCI Image Configuration layer can be done by either providing it via --ctime parameter or setting the SOURCE_DATE_EPOCH environment variable.
`

c := &cobra.Command{
Use: "push",
Expand All @@ -77,11 +85,6 @@ Input:
return err
}

var err error
if opts.ctime, err = parseTime(ctime); err != nil {
return err
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -91,30 +94,33 @@ Input:
Err: cmd.OutOrStderr(),
}

return opts.Run(args)
return opts.Run(cmd.Context(), args)
},
}
c.Flags().StringSliceVarP(&opts.bundleContentPaths, "filenames", "f", []string{}, "List of fully-qualified file paths containing YAML or JSON defined Tekton objects to include in this bundle")
c.Flags().StringSliceVarP(&opts.annotationParams, "annotate", "", []string{}, "OCI Manifest annotation in the form of key=value to be added to the OCI image. Can be provided multiple times to add multiple annotations.")
c.Flags().StringVar(&ctime, "ctime", "", "YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS or RFC3339 formatted created time to set, defaults to current time. In non RFC3339 syntax dates are in UTC timezone.")
c.Flags().StringVar(&opts.ctimeParam, "ctime", "", "YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS or RFC3339 formatted created time to set, defaults to current time. In non RFC3339 syntax dates are in UTC timezone.")
bundle.AddRemoteFlags(c.Flags(), &opts.remoteOptions)

return c
}

// Reads the positional arguments and the `-f` flag to fill in the `bunldeContents` parameter with all of the raw Tekton
// contents.
func (p *pushOptions) parseArgsAndFlags(args []string) (err error) {
func (p *pushOptions) parseArgsAndFlags(ctx context.Context, args []string) (err error) {
p.ref, _ = name.ParseReference(args[0], name.StrictValidation, name.Insecure)

// If there are file paths specified, then read them and include their contents.
for _, path := range p.bundleContentPaths {
if path == "-" {
// If this flag's value is '-', assume the user has piped input into stdin.
stdinContents, err := io.ReadAll(p.stream.In)
if err != nil || len(stdinContents) == 0 {
if err != nil {
return fmt.Errorf("failed to read bundle contents from stdin: %w", err)
}
if len(stdinContents) == 0 {
return errors.New("failed to read bundle contents from stdin: empty input")
}
p.bundleContents = append(p.bundleContents, string(stdinContents))
continue
}
Expand All @@ -126,14 +132,20 @@ func (p *pushOptions) parseArgsAndFlags(args []string) (err error) {
p.bundleContents = append(p.bundleContents, string(contents))
}

p.annotations, err = params.ParseParams(p.annotationParams)
if p.annotations, err = params.ParseParams(p.annotationParams); err != nil {
return err
}

return err
if p.ctime, err = determineCTime(p.ctimeParam, clockwork.FromContext(ctx)); err != nil {
return err
}

return nil
}

// Run performs the principal logic of reading and parsing the input, creating the bundle, and publishing it.
func (p *pushOptions) Run(args []string) error {
if err := p.parseArgsAndFlags(args); err != nil {
func (p *pushOptions) Run(ctx context.Context, args []string) error {
if err := p.parseArgsAndFlags(ctx, args); err != nil {
return err
}

Expand All @@ -150,12 +162,22 @@ func (p *pushOptions) Run(args []string) error {
return err
}

// to help with testing
var now = time.Now
func determineCTime(t string, clock clockwork.Clock) (parsed time.Time, err error) {
// if given the parameter don't lookup the SOURCE_DATE_EPOCH env var
if t == "" {
if sourceDateEpoch, found := os.LookupEnv(sourceDateEpochEnv); found && sourceDateEpoch != "" {
timestamp, err := strconv.ParseInt(sourceDateEpoch, 10, 64)
if err != nil {
// rather than ignore, report that SOURCE_DATE_EPOCH cannot be
// parsed, given that it is set seems like the best option
return time.Time{}, err
}
return time.Unix(timestamp, 0), nil
}
}

func parseTime(t string) (parsed time.Time, err error) {
if t == "" {
return now(), nil
return clock.Now(), nil
}

parsed, err = time.Parse(time.DateOnly, t)
Expand Down
Loading
Loading