@@ -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,35 @@ 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+ stat , err := os .Stat ("/var/lib/dpkg/status" )
331+ if err != nil {
332+ klog .Infof ("Error getting dpkg status info: %v" , err )
333+ } else {
334+ // If the last update is older than 10 minutes, we should update the list of packages.
335+ if stat .ModTime ().After (time .Now ().Add (- 10 * time .Minute )) {
336+ klog .Infof ("Skipping package install as /var/lib/dpkg/status is newer than 10 minutes" )
337+ } else {
338+ args := []string {"apt-get" , "update" }
339+ klog .Infof ("Running command %s" , args )
340+ cmd := exec .Command (args [0 ], args [1 :]... )
341+ cmd .Env = env
342+ output , err := cmd .CombinedOutput ()
343+ if err != nil {
344+ klog .Infof ("Error fetching the latest list of available packages: %v:\n %s" , err , string (output ))
345+ }
346+ }
347+ }
348+ }
349+
350+ var args []string
351+ if d .IsDebianFamily () {
352+ args = []string {"apt-get" , "install" , "--yes" , "--no-install-recommends" }
328353 } else if d .IsRHELFamily () {
329354
330355 if d .HasDNF () {
@@ -337,7 +362,7 @@ func (_ *Package) RenderLocal(t *local.LocalTarget, a, e, changes *Package) erro
337362 }
338363 args = append (args , pkgs ... )
339364
340- klog .Infof ("running command %s" , args )
365+ klog .Infof ("Running command %s" , args )
341366 cmd := exec .Command (args [0 ], args [1 :]... )
342367 cmd .Env = env
343368 output , err := cmd .CombinedOutput ()
@@ -350,7 +375,7 @@ func (_ *Package) RenderLocal(t *local.LocalTarget, a, e, changes *Package) erro
350375 if strings .Contains (string (output ), "dpkg --configure -a" ) {
351376 klog .Warningf ("found error requiring dpkg repair: %q" , string (output ))
352377 args := []string {"dpkg" , "--configure" , "-a" }
353- klog .Infof ("running command %s" , args )
378+ klog .Infof ("Running command %s" , args )
354379 cmd := exec .Command (args [0 ], args [1 :]... )
355380 dpkgOutput , err := cmd .CombinedOutput ()
356381 if err != nil {
0 commit comments