@@ -6,14 +6,14 @@ package publish
66import (
77 "context"
88 "errors"
9+ "fmt"
910 "log"
10- "os"
1111 "path/filepath"
12+ "strings"
13+ "time"
1214
1315 "github.com/spf13/cobra"
14- "sigs.k8s.io/kustomize/api/ifc"
15- "sigs.k8s.io/kustomize/api/konfig"
16- "sigs.k8s.io/kustomize/api/resource"
16+ "k8s.io/utils/clock"
1717 "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
1818 "sigs.k8s.io/kustomize/kyaml/filesys"
1919
@@ -24,13 +24,15 @@ import (
2424)
2525
2626type publishOptions struct {
27- registry string
28- noVerify bool
27+ registry string
28+ createdAt time.Time
29+ noVerify bool
2930}
3031
3132// NewCmdEdit returns an instance of 'edit' subcommand.
3233func NewCmdPublish (
33- fSys filesys.FileSystem , v ifc.Validator , rf * resource.Factory ,
34+ fSys filesys.FileSystem ,
35+ clock clock.PassiveClock ,
3436) * cobra.Command {
3537 var o publishOptions
3638
@@ -42,7 +44,7 @@ func NewCmdPublish(
4244 publish <registry>
4345` ,
4446 RunE : func (cmd * cobra.Command , args []string ) error {
45- err := o .Validate (args )
47+ err := o .Validate (clock , args )
4648 if err != nil {
4749 return err
4850 }
@@ -51,18 +53,40 @@ func NewCmdPublish(
5153
5254 Args : cobra .MinimumNArgs (1 ),
5355 }
54- cmd .Flags ().BoolVar (& o .noVerify , "no-verify" , false ,
56+
57+ // cmd.Flags().StringVar(
58+ // &o.createdAt,
59+ // "created-at",
60+ // "",
61+ // "The timestamp of the published artifact. It must be supplied for reproducible builds. Defaults to the current timestamp.",
62+ // )
63+ cmd .Flags ().BoolVar (
64+ & o .noVerify ,
65+ "no-verify" ,
66+ false ,
5567 "skip validation for resources" ,
5668 )
5769 return cmd
5870}
5971
6072// Validate validates addResource command.
61- func (o * publishOptions ) Validate (args []string ) error {
73+ func (o * publishOptions ) Validate (clock clock. PassiveClock , args []string ) error {
6274 if len (args ) == 0 {
6375 return errors .New ("must specify a registry" )
6476 }
6577 o .registry = args [0 ]
78+
79+ o .createdAt = clock .Now ()
80+
81+ // if o.createdAt == "" {
82+ // o.createdAt = time.Now().Format(time.RFC3339)
83+ // } else {
84+ // parsed, err := time.Parse("", o.createdAt)
85+ // if err != nil {
86+ // return err
87+ // }
88+ // o.createdAt = parsed.Format(time.RFC3339)
89+ // }
6690 return nil
6791}
6892
@@ -73,54 +97,35 @@ func (o *publishOptions) RunPublish(fSys filesys.FileSystem) error {
7397 return err
7498 }
7599
76- if _ , err = mf .Read (); err != nil {
100+ kustomization , err := mf .Read ()
101+ if err != nil {
77102 return err
78103 }
79104
80105 var dir string = filepath .Dir (mf .GetPath ())
81106
82- fs , err := file .New (dir )
107+ fs , err := file .New ("" )
83108 if err != nil {
84109 return err
85110 }
86111 defer fs .Close ()
87112
88113 ctx := context .Background ()
89- fileDescriptors := make ([]v1.Descriptor , 0 )
90-
91- if err = fSys .Walk (dir , func (path string , info os.FileInfo , err error ) error {
92- if err != nil {
93- return err
94- }
95-
96- if info .IsDir () {
97- return nil
98- }
99-
100- var mediaType string = "application/vnd.kustomize.unknown.v1beta1"
101- var b string = filepath .Base (path )
102- for _ , kfilename := range konfig .RecognizedKustomizationFileNames () {
103- if b == kfilename {
104- mediaType = "application/vnd.kustomize.config.k8s.io.v1beta1+yaml"
105- break
106- }
107- }
108-
109- fileDescriptor , err := fs .Add (ctx , path , mediaType , "" )
110- if err != nil {
111- return err
112- }
113- fileDescriptors = append (fileDescriptors , fileDescriptor )
114-
115- return nil
116- }); err != nil {
114+ fileDescriptor , err := fs .Add (ctx , "." , "" , dir )
115+ if err != nil {
117116 return err
118117 }
119118
120- artifactType := "application/vnd.kustomize.artifact"
121119 opts := oras.PackManifestOptions {
122- Layers : fileDescriptors ,
120+ Layers : []v1.Descriptor {
121+ fileDescriptor ,
122+ },
123+ ManifestAnnotations : map [string ]string {
124+ "org.opencontainers.image.created" : o .createdAt .Format (time .RFC3339 ),
125+ },
123126 }
127+
128+ artifactType := fmt .Sprintf ("application/vnd.%s+%s" , strings .ToLower (strings .ReplaceAll (kustomization .APIVersion , "/" , "." )), strings .ToLower (kustomization .Kind ))
124129 manifestDescriptor , err := oras .PackManifest (ctx , fs , oras .PackManifestVersion1_1 , artifactType , opts )
125130 if err != nil {
126131 return err
@@ -136,12 +141,21 @@ func (o *publishOptions) RunPublish(fSys filesys.FileSystem) error {
136141 return err
137142 }
138143
144+ // }
145+ // reg, err := remote.NewRegistry(o.registry)
146+ // reg.PlainHTTP = true
147+
148+ // dst, err := reg.Repository(ctx, "destination")
149+ // if err != nil {
150+ // panic(err) // Handle error
151+ // }
152+
139153 desc , err := oras .Copy (ctx , fs , tag , dst , tag , oras .DefaultCopyOptions )
140154 if err != nil {
141155 return err
142156 }
143157
144- log .Printf (" SUCCESS: copied %s desc %q \n " , dir , desc )
158+ log .Printf (` SUCCESS: published %s:%s@%s\n` , o . registry , tag , desc . Digest )
145159
146160 return nil
147161}
0 commit comments