From 26109ee821cf355d7668d7e8905c27645483ae48 Mon Sep 17 00:00:00 2001 From: frekw Date: Tue, 7 May 2024 16:52:55 +0200 Subject: [PATCH] Add `--reproducible` flag to `flux push artifact` This makes the pushed artifact have the exact same hash if the contents are the same. E.g ``` flux push artifact oci://repo/image:tag1 --source deploy --revision="test" --path=deploy --reproducible flux push artifact oci://repo/image:tag2 --source deploy --revision="test" --path=deploy --reproducible ``` will both result in the same sha hash, tagged with `tag1` and `tag2`. This is useful when producing flux artifacts in a monorepo setup where you don't want to unnecessarily push new artifacts unless something has actually changed. Signed-off-by: frekw --- cmd/flux/push_artifact.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cmd/flux/push_artifact.go b/cmd/flux/push_artifact.go index 228a826e..735b78d5 100644 --- a/cmd/flux/push_artifact.go +++ b/cmd/flux/push_artifact.go @@ -105,15 +105,16 @@ The command can read the credentials from '~/.docker/config.json' but they can a } type pushArtifactFlags struct { - path string - source string - revision string - creds string - provider flags.SourceOCIProvider - ignorePaths []string - annotations []string - output string - debug bool + path string + source string + revision string + creds string + provider flags.SourceOCIProvider + ignorePaths []string + annotations []string + output string + debug bool + reproducible bool } var pushArtifactArgs = newPushArtifactFlags() @@ -135,6 +136,7 @@ func init() { pushArtifactCmd.Flags().StringVarP(&pushArtifactArgs.output, "output", "o", "", "the format in which the artifact digest should be printed, can be 'json' or 'yaml'") pushArtifactCmd.Flags().BoolVarP(&pushArtifactArgs.debug, "debug", "", false, "display logs from underlying library") + pushArtifactCmd.Flags().BoolVar(&pushArtifactArgs.reproducible, "reproducible", false, "ensure reproducible image digests by setting the created timestamp to '1970-01-01T00:00:00Z'") pushCmd.AddCommand(pushArtifactCmd) } @@ -202,6 +204,11 @@ func pushArtifactCmdRun(cmd *cobra.Command, args []string) error { Annotations: annotations, } + if pushArtifactArgs.reproducible { + zeroTime := time.Unix(0, 0) + meta.Created = zeroTime.Format(time.RFC3339) + } + ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) defer cancel()