Skip to content

Commit ef90ff9

Browse files
committed
Update the node package list only before installing packages
Signed-off-by: Ciprian Hacman <[email protected]>
1 parent e0629cc commit ef90ff9

File tree

4 files changed

+34
-116
lines changed

4 files changed

+34
-116
lines changed

upup/pkg/fi/nodeup/loader.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import (
2020
"fmt"
2121
"reflect"
2222

23-
"k8s.io/klog/v2"
2423
"k8s.io/kops/upup/pkg/fi"
25-
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
2624
)
2725

2826
type Loader struct {
@@ -43,17 +41,5 @@ func (l *Loader) Build() (map[string]fi.NodeupTask, error) {
4341
tasks = context.Tasks
4442
}
4543

46-
// If there is a package task, we need an update packages task
47-
for _, t := range tasks {
48-
if _, ok := t.(*nodetasks.Package); ok {
49-
klog.Infof("Package task found; adding UpdatePackages task")
50-
tasks["UpdatePackages"] = nodetasks.NewUpdatePackages()
51-
break
52-
}
53-
}
54-
if tasks["UpdatePackages"] == nil {
55-
klog.Infof("No package task found; won't update packages")
56-
}
57-
5844
return tasks, nil
5945
}

upup/pkg/fi/nodeup/nodetasks/package.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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{}
6566
func (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 {

upup/pkg/fi/nodeup/nodetasks/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (s *Service) GetDependencies(tasks map[string]fi.NodeupTask) []fi.NodeupTas
8888
// launching a custom Kubernetes build), they all depend on
8989
// the "docker.service" Service task.
9090
switch v := v.(type) {
91-
case *Package, *UpdatePackages, *UserTask, *GroupTask, *Chattr, *BindMount, *Archive, *Prefix, *UpdateEtcHostsTask:
91+
case *Package, *AptSource, *UserTask, *GroupTask, *Chattr, *BindMount, *Archive, *Prefix, *UpdateEtcHostsTask:
9292
deps = append(deps, v)
9393
case *Service, *PullImageTask, *IssueCert, *BootstrapClientTask, *KubeConfig:
9494
// ignore

upup/pkg/fi/nodeup/nodetasks/update_packages.go

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)