@@ -24,6 +24,7 @@ import (
2424 "reflect"
2525 "strings"
2626 "sync"
27+ "time"
2728
2829 "k8s.io/klog/v2"
2930 "k8s.io/kops/pkg/apis/kops"
@@ -65,9 +66,9 @@ var _ fi.NodeupHasDependencies = &Package{}
6566func (e * Package ) GetDependencies (tasks map [string ]fi.NodeupTask ) []fi.NodeupTask {
6667 var deps []fi.NodeupTask
6768
68- // UpdatePackages before we install any packages
69+ // AptSource before we install any packages
6970 for _ , v := range tasks {
70- if _ , ok := v .(* UpdatePackages ); ok {
71+ if _ , ok := v .(* AptSource ); ok {
7172 deps = append (deps , v )
7273 }
7374 }
@@ -320,11 +321,37 @@ func (_ *Package) RenderLocal(t *local.LocalTarget, a, e, changes *Package) erro
320321 pkgs = append (pkgs , e .Name )
321322 }
322323
323- var args []string
324324 env := os .Environ ()
325325 if d .IsDebianFamily () {
326- args = []string {"apt-get" , "install" , "--yes" , "--no-install-recommends" }
327326 env = append (env , "DEBIAN_FRONTEND=noninteractive" )
327+ }
328+
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" )
334+ 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+ }
349+ }
350+ }
351+
352+ var args []string
353+ if d .IsDebianFamily () {
354+ args = []string {"apt-get" , "install" , "--yes" , "--no-install-recommends" }
328355 } else if d .IsRHELFamily () {
329356
330357 if d .HasDNF () {
@@ -337,7 +364,7 @@ func (_ *Package) RenderLocal(t *local.LocalTarget, a, e, changes *Package) erro
337364 }
338365 args = append (args , pkgs ... )
339366
340- klog .Infof ("running command %s" , args )
367+ klog .Infof ("Running command %s" , args )
341368 cmd := exec .Command (args [0 ], args [1 :]... )
342369 cmd .Env = env
343370 output , err := cmd .CombinedOutput ()
@@ -350,7 +377,7 @@ func (_ *Package) RenderLocal(t *local.LocalTarget, a, e, changes *Package) erro
350377 if strings .Contains (string (output ), "dpkg --configure -a" ) {
351378 klog .Warningf ("found error requiring dpkg repair: %q" , string (output ))
352379 args := []string {"dpkg" , "--configure" , "-a" }
353- klog .Infof ("running command %s" , args )
380+ klog .Infof ("Running command %s" , args )
354381 cmd := exec .Command (args [0 ], args [1 :]... )
355382 dpkgOutput , err := cmd .CombinedOutput ()
356383 if err != nil {
0 commit comments