@@ -269,6 +269,9 @@ func (_ *Package) CheckChanges(a, e, changes *Package) error {
269269// It just avoids unnecessary failures from running e.g. concurrent apt-get installs
270270var packageManagerLock sync.Mutex
271271
272+ // packageManagerLastUpdated is the last time the package manager update was done
273+ var packageManagerLastUpdated time.Time
274+
272275func (_ * Package ) RenderLocal (t * local.LocalTarget , a , e , changes * Package ) error {
273276 packageManagerLock .Lock ()
274277 defer packageManagerLock .Unlock ()
@@ -326,27 +329,18 @@ func (_ *Package) RenderLocal(t *local.LocalTarget, a, e, changes *Package) erro
326329 env = append (env , "DEBIAN_FRONTEND=noninteractive" )
327330 }
328331
329- if d .IsDebianFamily () {
330- // Each time apt-get install is run, the timestamp of /var/lib/dpkg/status is updated.
331- // To avoid unnecessary apt-get update calls, we check the timestamp of this file.
332- // If it is newer than 10 minutes, we skip apt-get update.
333- stat , err := os .Stat ("/var/lib/dpkg/status" )
332+ // If the package manager update was less than 10 minutes ago, skip updating the package list.
333+ if d .IsDebianFamily () && time .Since (packageManagerLastUpdated ) > 10 * time .Minute {
334+ args := []string {"apt-get" , "update" }
335+ klog .Infof ("Running command %s" , args )
336+ cmd := exec .Command (args [0 ], args [1 :]... )
337+ cmd .Env = env
338+ output , err := cmd .CombinedOutput ()
334339 if err != nil {
335- klog .Infof ("Error getting dpkg status info: %v" , err )
336- } else {
337- if stat .ModTime ().After (time .Now ().Add (- 10 * time .Minute )) {
338- klog .Infof ("Skipping package install as /var/lib/dpkg/status is newer than 10 minutes" )
339- } else {
340- args := []string {"apt-get" , "update" }
341- klog .Infof ("Running command %s" , args )
342- cmd := exec .Command (args [0 ], args [1 :]... )
343- cmd .Env = env
344- output , err := cmd .CombinedOutput ()
345- if err != nil {
346- klog .Infof ("Error fetching the latest list of available packages: %v:\n %s" , err , string (output ))
347- }
348- }
340+ return fmt .Errorf ("error fetching the list of available packages: %v:\n %s" , err , string (output ))
349341 }
342+ // Successful package list update, updating the last updated time.
343+ packageManagerLastUpdated = time .Now ()
350344 }
351345
352346 var args []string
@@ -387,6 +381,8 @@ func (_ *Package) RenderLocal(t *local.LocalTarget, a, e, changes *Package) erro
387381 }
388382 return fmt .Errorf ("error installing package %q: %v: %s" , e .Name , err , string (output ))
389383 }
384+ // Successful package install, updating the last updated time.
385+ packageManagerLastUpdated = time .Now ()
390386 } else {
391387 if changes .Healthy != nil {
392388 if d .IsDebianFamily () {
0 commit comments