77 "path"
88 "path/filepath"
99 "strings"
10+ "time"
1011
1112 "k8s.io/kube-openapi/pkg/util/sets"
1213
@@ -56,7 +57,7 @@ func NewGitStorage(path string) (*GitStorage, error) {
5657}
5758
5859// handle handles different operations on git
59- func (s * GitStorage ) handle (gvr schema.GroupVersionResource , oldObj , obj * unstructured.Unstructured , delete bool ) {
60+ func (s * GitStorage ) handle (timestamp time. Time , gvr schema.GroupVersionResource , oldObj , obj * unstructured.Unstructured , delete bool ) {
6061 filePath , content , err := decodeUnstructuredObject (gvr , obj )
6162 if err != nil {
6263 klog .Warningf ("Decoding %q failed: %v" , filePath , err )
@@ -84,14 +85,14 @@ func (s *GitStorage) handle(gvr schema.GroupVersionResource, oldObj, obj *unstru
8485 // Add it first before removing it.
8586 if os .IsNotExist (err ) {
8687 klog .Info ("Observed delete of file we haven't previously observed. Adding it first." )
87- s .handle (gvr , nil , obj , false )
88- s .handle (gvr , nil , obj , true )
88+ s .handle (timestamp , gvr , nil , obj , false )
89+ s .handle (timestamp , gvr , nil , obj , true )
8990 return
9091 } else {
9192 klog .Errorf ("Error removing %q: %v" , filePath , err )
9293 }
9394 }
94- if err := s .commitRemove (filePath , "unknown" , ocCommand ); err != nil {
95+ if err := s .commitRemove (timestamp , filePath , "unknown" , ocCommand ); err != nil {
9596 klog .Error (err )
9697 }
9798
@@ -114,33 +115,33 @@ func (s *GitStorage) handle(gvr schema.GroupVersionResource, oldObj, obj *unstru
114115 switch operation {
115116 case gitOpAdded :
116117 klog .Infof ("Calling commitAdd for %s" , filePath )
117- if err := s .commitAdd (filePath , modifyingUser , ocCommand ); err != nil {
118+ if err := s .commitAdd (timestamp , filePath , modifyingUser , ocCommand ); err != nil {
118119 klog .Error (err )
119120 }
120121 case gitOpModified :
121122 klog .Infof ("Calling commitModify for %s" , filePath )
122- if err := s .commitModify (filePath , modifyingUser , ocCommand ); err != nil {
123+ if err := s .commitModify (timestamp , filePath , modifyingUser , ocCommand ); err != nil {
123124 klog .Error (err )
124125 }
125126 default :
126127 klog .Errorf ("unhandled case for %s: %d" , filePath , operation )
127128 }
128129}
129130
130- func (s * GitStorage ) OnAdd (gvr schema.GroupVersionResource , obj interface {}) {
131+ func (s * GitStorage ) OnAdd (timestamp time. Time , gvr schema.GroupVersionResource , obj interface {}) {
131132 objUnstructured := obj .(* unstructured.Unstructured )
132133
133- s .handle (gvr , nil , objUnstructured , false )
134+ s .handle (timestamp , gvr , nil , objUnstructured , false )
134135}
135136
136- func (s * GitStorage ) OnUpdate (gvr schema.GroupVersionResource , oldObj , obj interface {}) {
137+ func (s * GitStorage ) OnUpdate (timestamp time. Time , gvr schema.GroupVersionResource , oldObj , obj interface {}) {
137138 objUnstructured := obj .(* unstructured.Unstructured )
138139 oldObjUnstructured := oldObj .(* unstructured.Unstructured )
139140
140- s .handle (gvr , oldObjUnstructured , objUnstructured , false )
141+ s .handle (timestamp , gvr , oldObjUnstructured , objUnstructured , false )
141142}
142143
143- func (s * GitStorage ) OnDelete (gvr schema.GroupVersionResource , obj interface {}) {
144+ func (s * GitStorage ) OnDelete (timestamp time. Time , gvr schema.GroupVersionResource , obj interface {}) {
144145 objUnstructured , ok := obj .(* unstructured.Unstructured )
145146 if ! ok {
146147 tombstone , ok := obj .(cache.DeletedFinalStateUnknown )
@@ -155,7 +156,7 @@ func (s *GitStorage) OnDelete(gvr schema.GroupVersionResource, obj interface{})
155156 }
156157 }
157158
158- s .handle (gvr , nil , objUnstructured , true )
159+ s .handle (timestamp , gvr , nil , objUnstructured , true )
159160}
160161
161162// guessAtModifyingUsers tries to figure out who modified the resource
@@ -229,47 +230,48 @@ func (s *GitStorage) exec(command string, args ...string) error {
229230 return nil
230231}
231232
232- func (s * GitStorage ) commitAdd ( path , author , ocCommand string ) error {
233+ func (s * GitStorage ) commit ( timestamp time. Time , path , author , commitMessage string ) error {
233234 authorString := fmt .
Sprintf (
"%s <[email protected] >" ,
author )
234- commitMessage := fmt .Sprintf ("added %s" , ocCommand )
235+ dateString := timestamp .Format (time .RFC3339 )
236+
237+ return s .exec ("git" , "commit" , "--author" , authorString , "--date" , dateString , "-m" , commitMessage )
238+ }
235239
240+ func (s * GitStorage ) commitAdd (timestamp time.Time , path , author , ocCommand string ) error {
236241 if err := s .exec ("git" , "add" , path ); err != nil {
237242 return err
238243 }
239244
240- if err := s .exec ("git" , "commit" , "--author" , authorString , "-m" , commitMessage ); err != nil {
245+ commitMessage := fmt .Sprintf ("added %s" , ocCommand )
246+ if err := s .commit (timestamp , path , author , commitMessage ); err != nil {
241247 return err
242248 }
243249
244250 klog .Infof ("Add: %v -- %v added %v" , path , author , ocCommand )
245251 return nil
246252}
247253
248- func (s * GitStorage ) commitModify (path , author , ocCommand string ) error {
249- authorString := fmt .
Sprintf (
"%s <[email protected] >" ,
author )
250- commitMessage := fmt .Sprintf ("modifed %s" , ocCommand )
251-
254+ func (s * GitStorage ) commitModify (timestamp time.Time , path , author , ocCommand string ) error {
252255 if err := s .exec ("git" , "add" , path ); err != nil {
253256 return err
254257 }
255258
256- if err := s .exec ("git" , "commit" , "--author" , authorString , "-m" , commitMessage ); err != nil {
259+ commitMessage := fmt .Sprintf ("modifed %s" , ocCommand )
260+ if err := s .commit (timestamp , path , author , commitMessage ); err != nil {
257261 return err
258262 }
259263
260264 klog .Infof ("Modified: %v -- %v updated %v" , path , author , ocCommand )
261265 return nil
262266}
263267
264- func (s * GitStorage ) commitRemove (path , author , ocCommand string ) error {
265- authorString := fmt .
Sprintf (
"%s <[email protected] >" ,
author )
266- commitMessage := fmt .Sprintf ("removed %s" , ocCommand )
267-
268+ func (s * GitStorage ) commitRemove (timestamp time.Time , path , author , ocCommand string ) error {
268269 if err := s .exec ("git" , "rm" , path ); err != nil {
269270 return err
270271 }
271272
272- if err := s .exec ("git" , "commit" , "--author" , authorString , "-m" , commitMessage ); err != nil {
273+ commitMessage := fmt .Sprintf ("removed %s" , ocCommand )
274+ if err := s .commit (timestamp , path , author , commitMessage ); err != nil {
273275 return err
274276 }
275277
0 commit comments