...
1 package common
2
3 import (
4 "flag"
5 "os"
6 "strings"
7
8 "github.com/hashicorp/go-version"
9 "github.com/peterbourgon/ff/v3"
10 )
11
12 const (
13 MountPath = "/boot"
14 ArtefactsPath = MountPath + "/versions"
15 ArtefactsTempPath = ArtefactsPath + "/.tmp"
16 LiveBootPath = MountPath + "/live"
17 ScriptsPath = MountPath + "/patching"
18 ScriptsTempPath = ScriptsPath + "/.tmp"
19 EnvFilePath = "/data/patching.env"
20 LegacyScriptsPath = MountPath + "/upgrade"
21
22 PatchsetMount = "/mnt/patchset"
23 RebootPath = "/mnt/run/reboot-required"
24
25 BootFiles = "boot/"
26 ScriptFiles = "patching/"
27
28 KiB = int64(1024)
29 MiB = KiB * KiB
30
31
32 VersionLabel = "feature.node.kubernetes.io/ien-version"
33 ControlPlaneLabel = "node-role.kubernetes.io/control-plane"
34 )
35
36 type Config struct {
37 MountPath string
38 ArtefactsPath string
39 ArtefactsTempPath string
40 LiveBootPath string
41 ScriptsPath string
42 ScriptsTempPath string
43 EnvFilePath string
44 LegacyScriptsPath string
45
46 BootFiles string
47 ScriptFiles string
48
49 PatchsetMount string
50 RebootPath string
51 }
52
53 func NewConfig() (Config, error) {
54 fs := flag.NewFlagSet("patching", flag.ExitOnError)
55 cfg := Config{}
56
57 envFilePath, err := getEnvPath()
58 if err != nil {
59 return Config{}, err
60 }
61
62 cfg.MountPath = *fs.String("mount-path", MountPath, "Where the container boot volume is mounted")
63 cfg.ArtefactsPath = *fs.String("artefacts-path", ArtefactsPath, "Where artefacts are created as part of the patching process")
64 cfg.ArtefactsTempPath = *fs.String("artefacts-temp-path", ArtefactsTempPath, "The temporary location for artefacts while extracting files")
65 cfg.LiveBootPath = *fs.String("live-boot-path", LiveBootPath, "Contains 'filesystem.squashfs'")
66 cfg.ScriptsPath = *fs.String("scripts-path", ScriptsPath, "Contains patching scripts and configuration")
67 cfg.ScriptsTempPath = *fs.String("scripts-temp-path", ScriptsTempPath, "The temporary location for patching scripts while extracting files")
68 cfg.EnvFilePath = *fs.String("env-file-path", envFilePath, "Contains 'patching.env'")
69 cfg.LegacyScriptsPath = *fs.String("legacy-scripts-path", LegacyScriptsPath, "The location used in legacy versions, before '/upgrade' was renamed to '/patching'")
70
71 cfg.BootFiles = BootFiles
72 cfg.ScriptFiles = ScriptFiles
73
74 cfg.PatchsetMount = PatchsetMount
75 cfg.RebootPath = RebootPath
76
77 if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix()); err != nil {
78 return Config{}, err
79 }
80
81 return cfg, nil
82 }
83
84 func ReadCurrentVer() (string, error) {
85 currentVerBytes, err := os.ReadFile("/mnt/version")
86 if err != nil {
87 return "", err
88 }
89 currentVer := strings.TrimSpace(string(currentVerBytes))
90 return currentVer, nil
91 }
92
93 func getEnvPath() (string, error) {
94 ver, err := ReadCurrentVer()
95 if err != nil {
96 return "", err
97 }
98
99 bwcVer, _ := version.NewVersion("v1.14.0")
100 currentVer, err := version.NewVersion(ver)
101 if err != nil {
102 return "", err
103 }
104
105 if currentVer.LessThan(bwcVer) {
106 return "/boot/patching/patching.env", nil
107 }
108 return EnvFilePath, nil
109 }
110
View as plain text