forked from openshift/machine-config-operator
/
update.go
1026 lines (924 loc) · 34.9 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package daemon
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"strconv"
"strings"
"syscall"
"time"
igntypes "github.com/coreos/ignition/config/v2_2/types"
"github.com/coreos/ignition/config/validate"
"github.com/golang/glog"
"github.com/google/renameio"
drain "github.com/openshift/kubernetes-drain"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
"github.com/openshift/machine-config-operator/pkg/daemon/constants"
errors "github.com/pkg/errors"
"github.com/vincent-petithory/dataurl"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
const (
// defaultDirectoryPermissions houses the default mode to use when no directory permissions are provided
defaultDirectoryPermissions os.FileMode = 0755
// defaultFilePermissions houses the default mode to use when no file permissions are provided
defaultFilePermissions os.FileMode = 0644
// coreUser is "core" and currently the only permissible user name
coreUserName = "core"
// SSH Keys for user "core" will only be written at /home/core/.ssh
coreUserSSHPath = "/home/core/.ssh/"
// fipsCommand is the command to use when enabling or disabling FIPS
fipsCommand = "/usr/libexec/rhcos-tools/coreos-fips"
)
func writeFileAtomicallyWithDefaults(fpath string, b []byte) error {
return writeFileAtomically(fpath, b, defaultDirectoryPermissions, defaultFilePermissions, -1, -1)
}
// writeFileAtomically uses the renameio package to provide atomic file writing, we can't use renameio.WriteFile
// directly since we need to 1) Chown 2) go through a buffer since files provided can be big
func writeFileAtomically(fpath string, b []byte, dirMode, fileMode os.FileMode, uid, gid int) error {
if err := os.MkdirAll(filepath.Dir(fpath), dirMode); err != nil {
return fmt.Errorf("failed to create directory %q: %v", filepath.Dir(fpath), err)
}
t, err := renameio.TempFile("", fpath)
if err != nil {
return err
}
defer t.Cleanup()
// Set permissions before writing data, in case the data is sensitive.
if err := t.Chmod(fileMode); err != nil {
return err
}
w := bufio.NewWriter(t)
if _, err := w.Write(b); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
if uid != -1 && gid != -1 {
if err := t.Chown(uid, gid); err != nil {
return err
}
}
return t.CloseAtomicallyReplace()
}
func getNodeRef(node *corev1.Node) *corev1.ObjectReference {
return &corev1.ObjectReference{
Kind: "Node",
Name: node.GetName(),
UID: node.GetUID(),
}
}
// updateOSAndReboot is the last step in an update(), and it can also
// be called as a special case for the "bootstrap pivot".
func (dn *Daemon) updateOSAndReboot(newConfig *mcfgv1.MachineConfig) (retErr error) {
if err := dn.updateOS(newConfig); err != nil {
return err
}
if out, err := dn.storePendingState(newConfig, 1); err != nil {
return errors.Wrapf(err, "failed to log pending config: %s", string(out))
}
defer func() {
if retErr != nil {
if dn.recorder != nil {
dn.recorder.Eventf(getNodeRef(dn.node), corev1.EventTypeNormal, "PendingConfigRollBack", fmt.Sprintf("Rolling back pending config %s: %v", newConfig.GetName(), retErr))
}
if out, err := dn.storePendingState(newConfig, 0); err != nil {
retErr = errors.Wrapf(retErr, "error rolling back pending config %v: %s", err, string(out))
return
}
}
}()
if dn.recorder != nil {
dn.recorder.Eventf(getNodeRef(dn.node), corev1.EventTypeNormal, "PendingConfig", fmt.Sprintf("Written pending config %s", newConfig.GetName()))
}
// Skip draining of the node when we're not cluster driven
if dn.onceFrom == "" {
dn.logSystem("Update prepared; beginning drain")
dn.recorder.Eventf(getNodeRef(dn.node), corev1.EventTypeNormal, "Drain", "Draining node to update config.")
backoff := wait.Backoff{
Steps: 5,
Duration: 10 * time.Second,
Factor: 2,
}
var lastErr error
if err := wait.ExponentialBackoff(backoff, func() (bool, error) {
err := drain.Drain(dn.kubeClient, []*corev1.Node{dn.node}, &drain.DrainOptions{
DeleteLocalData: true,
Force: true,
GracePeriodSeconds: 600,
IgnoreDaemonsets: true,
})
if err == nil {
return true, nil
}
lastErr = err
glog.Infof("Draining failed with: %v, retrying", err)
return false, nil
}); err != nil {
if err == wait.ErrWaitTimeout {
return errors.Wrapf(lastErr, "failed to drain node (%d tries): %v", backoff.Steps, err)
}
return errors.Wrap(err, "failed to drain node")
}
dn.logSystem("drain complete")
}
// reboot. this function shouldn't actually return.
return dn.reboot(fmt.Sprintf("Node will reboot into config %v", newConfig.GetName()), defaultRebootTimeout, rebootCommand(fmt.Sprintf("Node will reboot into config %v", newConfig.GetName())))
}
var errUnreconcilable = errors.New("unreconcilable")
// update the node to the provided node configuration.
func (dn *Daemon) update(oldConfig, newConfig *mcfgv1.MachineConfig) (retErr error) {
if dn.nodeWriter != nil {
state, err := getNodeAnnotationExt(dn.node, constants.MachineConfigDaemonStateAnnotationKey, true)
if err != nil {
return err
}
if state != constants.MachineConfigDaemonStateDegraded && state != constants.MachineConfigDaemonStateUnreconcilable {
if err := dn.nodeWriter.SetWorking(dn.kubeClient.CoreV1().Nodes(), dn.nodeLister, dn.name); err != nil {
return errors.Wrap(err, "error setting node's state to Working")
}
}
}
dn.catchIgnoreSIGTERM()
defer func() {
if retErr != nil {
dn.cancelSIGTERM()
}
}()
oldConfigName := oldConfig.GetName()
newConfigName := newConfig.GetName()
glog.Infof("Checking Reconcilable for config %v to %v", oldConfigName, newConfigName)
// We skip out of Reconcilable if there is no Kind and we are in runOnce mode. The
// reason is that there is a good chance a previous state is not available to match against.
if oldConfig.Kind == "" && dn.onceFrom != "" {
glog.Info("Missing kind in old config. Assuming no prior state.")
} else {
// make sure we can actually reconcile this state
diff, reconcilableError := Reconcilable(oldConfig, newConfig)
if reconcilableError != nil {
wrappedErr := fmt.Errorf("can't reconcile config %s with %s: %v", oldConfigName, newConfigName, reconcilableError)
if dn.recorder != nil {
mcRef := &corev1.ObjectReference{
Kind: "MachineConfig",
Name: newConfig.GetName(),
UID: newConfig.GetUID(),
}
dn.recorder.Eventf(mcRef, corev1.EventTypeWarning, "FailedToReconcile", wrappedErr.Error())
}
return errors.Wrapf(errUnreconcilable, "%v", wrappedErr)
}
dn.logSystem("Starting update from %s to %s: %+v", oldConfigName, newConfigName, diff)
}
// update files on disk that need updating
if err := dn.updateFiles(oldConfig, newConfig); err != nil {
return err
}
defer func() {
if retErr != nil {
if err := dn.updateFiles(newConfig, oldConfig); err != nil {
retErr = errors.Wrapf(retErr, "error rolling back files writes %v", err)
return
}
}
}()
if err := dn.updateSSHKeys(newConfig.Spec.Config.Passwd.Users); err != nil {
return err
}
defer func() {
if retErr != nil {
if err := dn.updateSSHKeys(oldConfig.Spec.Config.Passwd.Users); err != nil {
retErr = errors.Wrapf(retErr, "error rolling back SSH keys updates %v", err)
return
}
}
}()
if err := dn.storeCurrentConfigOnDisk(newConfig); err != nil {
return err
}
defer func() {
if retErr != nil {
if err := dn.storeCurrentConfigOnDisk(oldConfig); err != nil {
retErr = errors.Wrapf(retErr, "error rolling back current config on disk %v", err)
return
}
}
}()
// kargs
if err := dn.updateKernelArguments(oldConfig, newConfig); err != nil {
return err
}
defer func() {
if retErr != nil {
if err := dn.updateKernelArguments(newConfig, oldConfig); err != nil {
retErr = errors.Wrapf(retErr, "error rolling back kernel arguments %v", err)
return
}
}
}()
if err := dn.updateFIPS(oldConfig, newConfig); err != nil {
return err
}
defer func() {
if retErr != nil {
if err := dn.updateFIPS(newConfig, oldConfig); err != nil {
retErr = errors.Wrapf(retErr, "error rolling back FIPS %v", err)
return
}
}
}()
return dn.updateOSAndReboot(newConfig)
}
func (dn *Daemon) updateFIPS(current, desired *mcfgv1.MachineConfig) error {
if current.Spec.Fips == desired.Spec.Fips {
return nil
}
arg := "enable"
if !desired.Spec.Fips {
arg = "disable"
}
cmd := exec.Command(fipsCommand, arg)
dn.logSystem("Running %s %s", fipsCommand, arg)
if out, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "%s FIPS: %s", arg, string(out))
}
return nil
}
// MachineConfigDiff represents an ad-hoc difference between two MachineConfig objects.
// At some point this may change into holding just the files/units that changed
// and the MCO would just operate on that. For now we're just doing this to get
// improved logging.
type MachineConfigDiff struct {
osUpdate bool
kargs bool
fips bool
passwd bool
files bool
units bool
}
// Reconcilable checks the configs to make sure that the only changes requested
// are ones we know how to do in-place. If we can reconcile, (nil, nil) is returned.
// Otherwise, if we can't do it in place, the node is marked as degraded;
// the returned string value includes the rationale.
//
// we can only update machine configs that have changes to the files,
// directories, links, and systemd units sections of the included ignition
// config currently.
func Reconcilable(oldConfig, newConfig *mcfgv1.MachineConfig) (*MachineConfigDiff, error) {
oldIgn := oldConfig.Spec.Config
newIgn := newConfig.Spec.Config
// Ignition section
// First check if this is a generally valid Ignition Config
rpt := validate.ValidateWithoutSource(reflect.ValueOf(newIgn))
if rpt.IsFatal() {
return nil, errors.Errorf("invalid Ignition config found: %v", rpt)
}
// if the config versions are different, all bets are off. this probably
// shouldn't happen, but if it does, we can't deal with it.
if oldIgn.Ignition.Version != newIgn.Ignition.Version {
return nil, fmt.Errorf("ignition version mismatch between old and new config: old: %s new: %s",
oldIgn.Ignition.Version, newIgn.Ignition.Version)
}
// everything else in the ignition section doesn't matter to us, since the
// rest of the stuff in this section has to do with fetching remote
// resources, and the mcc should've fully rendered those out before the
// config gets here.
// Networkd section
// we don't currently configure the network in place. we can't fix it if
// something changed here.
if !reflect.DeepEqual(oldIgn.Networkd, newIgn.Networkd) {
return nil, errors.New("ignition networkd section contains changes")
}
// Passwd section
// we don't currently configure Groups in place. we don't configure Users except
// for setting/updating SSHAuthorizedKeys for the only allowed user "core".
// otherwise we can't fix it if something changed here.
passwdChanged := !reflect.DeepEqual(oldIgn.Passwd, newIgn.Passwd)
if passwdChanged {
if !reflect.DeepEqual(oldIgn.Passwd.Groups, newIgn.Passwd.Groups) {
return nil, errors.New("ignition Passwd Groups section contains changes")
}
if !reflect.DeepEqual(oldIgn.Passwd.Users, newIgn.Passwd.Users) {
// check if the prior config is empty and that this is the first time running.
// if so, the SSHKey from the cluster config and user "core" must be added to machine config.
if len(oldIgn.Passwd.Users) > 0 && len(newIgn.Passwd.Users) >= 1 {
// there is an update to Users, we must verify that it is ONLY making an acceptable
// change to the SSHAuthorizedKeys for the user "core"
for _, user := range newIgn.Passwd.Users {
if user.Name != coreUserName {
return nil, errors.New("ignition passwd user section contains unsupported changes: non-core user")
}
}
glog.Infof("user data to be verified before ssh update: %v", newIgn.Passwd.Users[len(newIgn.Passwd.Users)-1])
if err := verifyUserFields(newIgn.Passwd.Users[len(newIgn.Passwd.Users)-1]); err != nil {
return nil, err
}
}
}
}
// Storage section
// we can only reconcile files right now. make sure the sections we can't
// fix aren't changed.
if !reflect.DeepEqual(oldIgn.Storage.Disks, newIgn.Storage.Disks) {
return nil, errors.New("ignition disks section contains changes")
}
if !reflect.DeepEqual(oldIgn.Storage.Filesystems, newIgn.Storage.Filesystems) {
return nil, errors.New("ignition filesystems section contains changes")
}
if !reflect.DeepEqual(oldIgn.Storage.Raid, newIgn.Storage.Raid) {
return nil, errors.New("ignition raid section contains changes")
}
if !reflect.DeepEqual(oldIgn.Storage.Directories, newIgn.Storage.Directories) {
return nil, errors.New("ignition directories section contains changes")
}
if !reflect.DeepEqual(oldIgn.Storage.Links, newIgn.Storage.Links) {
// This means links have been added, as opposed as being removed as it happened with
// https://bugzilla.redhat.com/show_bug.cgi?id=1677198. This doesn't really change behavior
// since we still don't support links but we allow old MC to remove links when upgrading.
if len(newIgn.Storage.Links) != 0 {
return nil, errors.New("ignition links section contains changes")
}
}
// Special case files append: if the new config wants us to append, then we
// have to force a reprovision since it's not idempotent
for _, f := range newIgn.Storage.Files {
if f.Append {
return nil, fmt.Errorf("ignition file %v includes append", f.Path)
}
}
// Systemd section
// we can reconcile any state changes in the systemd section.
// we made it through all the checks. reconcile away!
glog.V(2).Info("Configs are reconcilable")
return &MachineConfigDiff{
osUpdate: oldConfig.Spec.OSImageURL != newConfig.Spec.OSImageURL,
kargs: !reflect.DeepEqual(oldConfig.Spec.KernelArguments, newConfig.Spec.KernelArguments),
fips: oldConfig.Spec.Fips != newConfig.Spec.Fips,
passwd: passwdChanged,
files: !reflect.DeepEqual(oldIgn.Storage.Files, newIgn.Storage.Files),
units: !reflect.DeepEqual(oldIgn.Systemd.Units, newIgn.Systemd.Units),
}, nil
}
// verifyUserFields returns nil if the user Name = "core", if 1 or more SSHKeys exist for
// this user and if all other fields in User are empty.
// Otherwise, an error will be returned and the proposed config will not be reconcilable.
// At this time we do not support non-"core" users or any changes to the "core" user
// outside of SSHAuthorizedKeys.
func verifyUserFields(pwdUser igntypes.PasswdUser) error {
emptyUser := igntypes.PasswdUser{}
tempUser := pwdUser
if tempUser.Name == coreUserName && len(tempUser.SSHAuthorizedKeys) >= 1 {
tempUser.Name = ""
tempUser.SSHAuthorizedKeys = nil
if !reflect.DeepEqual(emptyUser, tempUser) {
return errors.New("ignition passwd user section contains unsupported changes: non-sshKey changes")
}
glog.Info("SSH Keys reconcilable")
} else {
return errors.New("ignition passwd user section contains unsupported changes: user must be core and have 1 or more sshKeys")
}
return nil
}
// generateKargsCommand performs a diff between the old/new MC kernelArguments,
// and generates the command line arguments suitable for `rpm-ostree kargs`.
// Note what we really should be doing though is also looking at the *current*
// kernel arguments in case there was drift. But doing that requires us knowing
// what the "base" arguments are. See https://github.com/ostreedev/ostree/issues/479
func generateKargsCommand(oldConfig, newConfig *mcfgv1.MachineConfig) []string {
oldKargs := make(map[string]bool)
for _, arg := range oldConfig.Spec.KernelArguments {
oldKargs[arg] = true
}
newKargs := make(map[string]bool)
for _, arg := range newConfig.Spec.KernelArguments {
newKargs[arg] = true
}
cmdArgs := []string{}
for _, arg := range oldConfig.Spec.KernelArguments {
if !newKargs[arg] {
cmdArgs = append(cmdArgs, "--delete="+arg)
}
}
for _, arg := range newConfig.Spec.KernelArguments {
if !oldKargs[arg] {
cmdArgs = append(cmdArgs, "--append="+arg)
}
}
return cmdArgs
}
// updateKernelArguments adjusts the kernel args
func (dn *Daemon) updateKernelArguments(oldConfig, newConfig *mcfgv1.MachineConfig) error {
diff := generateKargsCommand(oldConfig, newConfig)
if len(diff) == 0 {
return nil
}
if dn.OperatingSystem != machineConfigDaemonOSRHCOS {
return fmt.Errorf("Updating kargs on non-RHCOS nodes is not supported: %v", diff)
}
args := append([]string{"kargs"}, diff...)
dn.logSystem("Running rpm-ostree %v", args)
return exec.Command("rpm-ostree", args...).Run()
}
// updateFiles writes files specified by the nodeconfig to disk. it also writes
// systemd units. there is no support for multiple filesystems at this point.
//
// in addition to files, we also write systemd units to disk. we mask, enable,
// and disable unit files when appropriate. this function relies on the system
// being restarted after an upgrade, so it doesn't daemon-reload or restart
// any services.
//
// it is worth noting that this function explicitly doesn't rely on the ignition
// implementation of file, unit writing, enabling or disabling. this is because
// ignition is built on the assumption that it is working with a fresh system,
// where as we are trying to reconcile a system that has already been running.
//
// in the future, this function should do any additional work to confirm that
// whatever has been written is picked up by the appropriate daemons, if
// required. in particular, a daemon-reload and restart for any unit files
// touched.
func (dn *Daemon) updateFiles(oldConfig, newConfig *mcfgv1.MachineConfig) error {
glog.Info("Updating files")
if err := dn.writeFiles(newConfig.Spec.Config.Storage.Files); err != nil {
return err
}
if err := dn.writeUnits(newConfig.Spec.Config.Systemd.Units); err != nil {
return err
}
if err := dn.deleteStaleData(oldConfig, newConfig); err != nil {
return err
}
return nil
}
// deleteStaleData performs a diff of the new and the old config. It then deletes
// all the files, units that are present in the old config but not in the new one.
// this function will error out if it fails to delete a file (with the exception
// of simply warning if the error is ENOENT since that's the desired state).
func (dn *Daemon) deleteStaleData(oldConfig, newConfig *mcfgv1.MachineConfig) error {
glog.Info("Deleting stale data")
newFileSet := make(map[string]struct{})
for _, f := range newConfig.Spec.Config.Storage.Files {
newFileSet[f.Path] = struct{}{}
}
for _, f := range oldConfig.Spec.Config.Storage.Files {
if _, ok := newFileSet[f.Path]; !ok {
if _, err := os.Stat(origFileName(f.Path)); err == nil {
if err := os.Rename(origFileName(f.Path), f.Path); err != nil {
return err
}
glog.V(2).Infof("Restored file %q", f.Path)
continue
}
glog.V(2).Infof("Deleting stale config file: %s", f.Path)
if err := os.Remove(f.Path); err != nil {
newErr := fmt.Errorf("unable to delete %s: %s", f.Path, err)
if !os.IsNotExist(err) {
return newErr
}
// otherwise, just warn
glog.Warningf("%v", newErr)
}
glog.Infof("Removed stale file %q", f.Path)
}
}
newUnitSet := make(map[string]struct{})
newDropinSet := make(map[string]struct{})
for _, u := range newConfig.Spec.Config.Systemd.Units {
for j := range u.Dropins {
path := filepath.Join(pathSystemd, u.Name+".d", u.Dropins[j].Name)
newDropinSet[path] = struct{}{}
}
path := filepath.Join(pathSystemd, u.Name)
newUnitSet[path] = struct{}{}
}
for _, u := range oldConfig.Spec.Config.Systemd.Units {
for j := range u.Dropins {
path := filepath.Join(pathSystemd, u.Name+".d", u.Dropins[j].Name)
if _, ok := newDropinSet[path]; !ok {
glog.V(2).Infof("Deleting stale systemd dropin file: %s", path)
if err := os.Remove(path); err != nil {
newErr := fmt.Errorf("unable to delete %s: %s", path, err)
if !os.IsNotExist(err) {
return newErr
}
// otherwise, just warn
glog.Warningf("%v", newErr)
}
glog.Infof("Removed stale systemd dropin %q", path)
}
}
path := filepath.Join(pathSystemd, u.Name)
if _, ok := newUnitSet[path]; !ok {
if err := dn.disableUnit(u); err != nil {
glog.Warningf("Unable to disable %s: %s", u.Name, err)
}
glog.V(2).Infof("Deleting stale systemd unit file: %s", path)
if err := os.Remove(path); err != nil {
newErr := fmt.Errorf("unable to delete %s: %s", path, err)
if !os.IsNotExist(err) {
return newErr
}
// otherwise, just warn
glog.Warningf("%v", newErr)
}
glog.Infof("Removed stale systemd unit %q", path)
}
}
return nil
}
// enableUnit enables a systemd unit via symlink
func (dn *Daemon) enableUnit(unit igntypes.Unit) error {
// The link location
wantsPath := filepath.Join(wantsPathSystemd, unit.Name)
// sanity check that we don't return an error when the link already exists
if _, err := os.Stat(wantsPath); err == nil {
glog.Infof("%s already exists. Not making a new symlink", wantsPath)
return nil
}
// The originating file to link
servicePath := filepath.Join(pathSystemd, unit.Name)
err := renameio.Symlink(servicePath, wantsPath)
if err != nil {
glog.Warningf("Cannot enable unit %s: %s", unit.Name, err)
} else {
glog.Infof("Enabled %s", unit.Name)
glog.V(2).Infof("Symlinked %s to %s", servicePath, wantsPath)
}
return err
}
// disableUnit disables a systemd unit via symlink removal
func (dn *Daemon) disableUnit(unit igntypes.Unit) error {
// The link location
wantsPath := filepath.Join(wantsPathSystemd, unit.Name)
// sanity check so we don't return an error when the unit was already disabled
if _, err := os.Stat(wantsPath); err != nil {
glog.Infof("%s was not present. No need to remove", wantsPath)
return nil
}
glog.V(2).Infof("Disabling unit at %s", wantsPath)
return os.Remove(wantsPath)
}
// writeUnits writes the systemd units to disk
func (dn *Daemon) writeUnits(units []igntypes.Unit) error {
for _, u := range units {
// write the dropin to disk
for i := range u.Dropins {
glog.Infof("Writing systemd unit dropin %q", u.Dropins[i].Name)
dpath := filepath.Join(pathSystemd, u.Name+".d", u.Dropins[i].Name)
if err := writeFileAtomicallyWithDefaults(dpath, []byte(u.Dropins[i].Contents)); err != nil {
return fmt.Errorf("failed to write systemd unit dropin %q: %v", u.Dropins[i].Name, err)
}
glog.V(2).Infof("Wrote systemd unit dropin at %s", dpath)
}
fpath := filepath.Join(pathSystemd, u.Name)
// check if the unit is masked. if it is, we write a symlink to
// /dev/null and continue
if u.Mask {
glog.V(2).Info("Systemd unit masked")
if err := os.RemoveAll(fpath); err != nil {
return fmt.Errorf("failed to remove unit %q: %v", u.Name, err)
}
glog.V(2).Infof("Removed unit %q", u.Name)
if err := renameio.Symlink(pathDevNull, fpath); err != nil {
return fmt.Errorf("failed to symlink unit %q to %s: %v", u.Name, pathDevNull, err)
}
glog.V(2).Infof("Created symlink unit %q to %s", u.Name, pathDevNull)
continue
}
if u.Contents != "" {
glog.Infof("Writing systemd unit %q", u.Name)
// write the unit to disk
if err := writeFileAtomicallyWithDefaults(fpath, []byte(u.Contents)); err != nil {
return fmt.Errorf("failed to write systemd unit %q: %v", u.Name, err)
}
glog.V(2).Infof("Successfully wrote systemd unit %q: ", u.Name)
}
// if the unit doesn't note if it should be enabled or disabled then
// skip all linking.
// if the unit should be enabled, then enable it.
// otherwise the unit is disabled. run disableUnit to ensure the unit is
// disabled. even if the unit wasn't previously enabled the result will
// be fine as disableUnit is idempotent.
// Note: we have to check for legacy unit.Enable and honor it
glog.Infof("Enabling systemd unit %q", u.Name)
if u.Enable {
if err := dn.enableUnit(u); err != nil {
return err
}
glog.V(2).Infof("Enabled systemd unit %q", u.Name)
}
if u.Enabled != nil {
if *u.Enabled {
if err := dn.enableUnit(u); err != nil {
return err
}
glog.V(2).Infof("Enabled systemd unit %q", u.Name)
} else {
if err := dn.disableUnit(u); err != nil {
return err
}
glog.V(2).Infof("Disabled systemd unit %q", u.Name)
}
}
}
return nil
}
// writeFiles writes the given files to disk.
// it doesn't fetch remote files and expects a flattened config file.
func (dn *Daemon) writeFiles(files []igntypes.File) error {
for _, file := range files {
glog.Infof("Writing file %q", file.Path)
contents, err := dataurl.DecodeString(file.Contents.Source)
if err != nil {
return err
}
mode := defaultFilePermissions
if file.Mode != nil {
mode = os.FileMode(*file.Mode)
}
var (
uid, gid = -1, -1
)
// set chown if file information is provided
if file.User != nil || file.Group != nil {
uid, gid, err = getFileOwnership(file)
if err != nil {
return fmt.Errorf("failed to retrieve file ownership for file %q: %v", file.Path, err)
}
}
if err := createOrigFile(file.Path); err != nil {
return err
}
if err := writeFileAtomically(file.Path, contents.Data, defaultDirectoryPermissions, mode, uid, gid); err != nil {
return err
}
}
return nil
}
func origFileName(fpath string) string {
return fpath + ".mcdorig"
}
func createOrigFile(fpath string) error {
if _, err := os.Stat(fpath); err != nil {
// the file isn't there, no need to back it up
// we could check ENOENT only maybe?
return nil
}
if _, err := os.Stat(origFileName(fpath)); err == nil {
// the orig file is already there and we avoid creating a new one to preserve the real default
return nil
}
if out, err := exec.Command("cp", "-a", "--reflink=auto", fpath, origFileName(fpath)).CombinedOutput(); err != nil {
return errors.Wrapf(err, "creating orig file for %q: %s", fpath, string(out))
}
return nil
}
// This is essentially ResolveNodeUidAndGid() from Ignition; XXX should dedupe
func getFileOwnership(file igntypes.File) (int, int, error) {
uid, gid := 0, 0 // default to root
if file.User != nil {
if file.User.ID != nil {
uid = *file.User.ID
} else if file.User.Name != "" {
osUser, err := user.Lookup(file.User.Name)
if err != nil {
return uid, gid, fmt.Errorf("failed to retrieve UserID for username: %s", file.User.Name)
}
glog.V(2).Infof("Retrieved UserId: %s for username: %s", osUser.Uid, file.User.Name)
uid, _ = strconv.Atoi(osUser.Uid)
}
}
if file.Group != nil {
if file.Group.ID != nil {
gid = *file.Group.ID
} else if file.Group.Name != "" {
osGroup, err := user.LookupGroup(file.Group.Name)
if err != nil {
return uid, gid, fmt.Errorf("failed to retrieve GroupID for group: %s", file.Group.Name)
}
glog.V(2).Infof("Retrieved GroupID: %s for group: %s", osGroup.Gid, file.Group.Name)
gid, _ = strconv.Atoi(osGroup.Gid)
}
}
return uid, gid, nil
}
func (dn *Daemon) atomicallyWriteSSHKey(keys string) error {
authKeyPath := filepath.Join(coreUserSSHPath, "authorized_keys")
// Keys should only be written to "/home/core/.ssh"
// Once Users are supported fully this should be writing to PasswdUser.HomeDir
glog.Infof("Writing SSHKeys at %q", authKeyPath)
if err := writeFileAtomicallyWithDefaults(authKeyPath, []byte(keys)); err != nil {
return err
}
glog.V(2).Infof("Wrote SSHKeys at %s", authKeyPath)
return nil
}
// Update a given PasswdUser's SSHKey
func (dn *Daemon) updateSSHKeys(newUsers []igntypes.PasswdUser) error {
if len(newUsers) == 0 {
return nil
}
// we're also appending all keys for any user to core, so for now
// we pass this to atomicallyWriteSSHKeys to write.
var concatSSHKeys string
for _, k := range newUsers[len(newUsers)-1].SSHAuthorizedKeys {
concatSSHKeys = concatSSHKeys + string(k) + "\n"
}
if !dn.mock {
// Note we write keys only for the core user and so this ignores the user list
if err := dn.atomicallyWriteSSHKey(concatSSHKeys); err != nil {
return err
}
}
return nil
}
// updateOS updates the system OS to the one specified in newConfig
func (dn *Daemon) updateOS(config *mcfgv1.MachineConfig) error {
if dn.OperatingSystem != machineConfigDaemonOSRHCOS {
glog.V(2).Info("Updating of non RHCOS nodes are not supported")
return nil
}
newURL := config.Spec.OSImageURL
osMatch, err := compareOSImageURL(dn.bootedOSImageURL, newURL)
if err != nil {
return err
}
if osMatch {
return nil
}
if dn.recorder != nil {
dn.recorder.Eventf(getNodeRef(dn.node), corev1.EventTypeNormal, "InClusterUpgrade", fmt.Sprintf("In cluster upgrade to %s", newURL))
}
glog.Infof("Updating OS to %s", newURL)
if err := dn.NodeUpdaterClient.RunPivot(newURL); err != nil {
return fmt.Errorf("failed to run pivot: %v", err)
}
return nil
}
func (dn *Daemon) getPendingStateLegacyLogger() (string, error) {
glog.Info("logger doesn't support --jounald, grepping the journal")
cmdLiteral := "journalctl -o cat | grep OPENSHIFT_MACHINE_CONFIG_DAEMON_LEGACY_LOG_HACK"
cmd := exec.Command("bash", "-c", cmdLiteral)
var combinedOutput bytes.Buffer
cmd.Stdout = &combinedOutput
cmd.Stderr = &combinedOutput
if err := cmd.Start(); err != nil {
return "", errors.Wrap(err, "failed shelling out to journalctl -o cat")
}
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
status, ok := exiterr.Sys().(syscall.WaitStatus)
if ok {
// grep exit with 1 if it doesn't find anything
// from man: Normally, the exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred
if status.ExitStatus() == 1 {
return "", nil
}
if status.ExitStatus() > 1 {
return "", errors.Wrapf(fmt.Errorf("grep exited with %s", combinedOutput.Bytes()), "failed to grep on journal output: %v", exiterr)
}
}
} else {
return "", errors.Wrap(err, "command wait error")
}
}
journalOutput := combinedOutput.Bytes()
// just an extra safety check?
if len(journalOutput) == 0 {
return "", nil
}
return dn.processJournalOutput(journalOutput)
}
func (dn *Daemon) processJournalOutput(journalOutput []byte) (string, error) {
lines := strings.Split(strings.TrimSpace(string(journalOutput)), "\n")
last := lines[len(lines)-1]
type journalMsg struct {
Message string `json:"MESSAGE,omitempty"`
BootID string `json:"BOOT_ID,omitempty"`
Pending string `json:"PENDING,omitempty"`
OldLogger string `json:"OPENSHIFT_MACHINE_CONFIG_DAEMON_LEGACY_LOG_HACK,omitempty"` // unused today
}
entry := &journalMsg{}
if err := json.Unmarshal([]byte(last), entry); err != nil {
return "", errors.Wrap(err, "getting pending state from journal")
}
if entry.Pending == "0" {
return "", nil
}
if entry.BootID == dn.bootID {
return "", fmt.Errorf("pending config %s bootID %s matches current! Failed to reboot?", entry.Message, dn.bootID)
}
return entry.Message, nil
}
// getPendingState loads the JSON state we cache across attempting to apply
// a config+reboot. If no pending state is available, ("", nil) will be returned.
// The bootID is stored in the pending state; if it is unchanged, we assume
// that we failed to reboot; that for now should be a fatal error, in order to avoid
// reboot loops.
func (dn *Daemon) getPendingState() (string, error) {
if !dn.loggerSupportsJournal {
return dn.getPendingStateLegacyLogger()
}
journalOutput, err := exec.Command("journalctl", "-o", "json", fmt.Sprintf("MESSAGE_ID=%s", pendingStateMessageID)).CombinedOutput()
if err != nil {
return "", errors.Wrap(err, "error running journalctl -o json")
}
if len(journalOutput) == 0 {
return "", nil
}
return dn.processJournalOutput(journalOutput)
}
func (dn *Daemon) storePendingStateLegacyLogger(pending *mcfgv1.MachineConfig, isPending int) ([]byte, error) {
glog.Info("logger doesn't support --jounald, logging json directly")
if isPending == 1 {
if err := dn.writePendingConfig(pending); err != nil {
return nil, err
}
} else {
if err := os.Remove(pendingConfigPath); err != nil {
return nil, err
}
}
oldLogger := exec.Command("logger", fmt.Sprintf(`{"MESSAGE": "%s", "BOOT_ID": "%s", "PENDING": "%d", "OPENSHIFT_MACHINE_CONFIG_DAEMON_LEGACY_LOG_HACK": "1"}`, pending.GetName(), dn.bootID, isPending))
return oldLogger.CombinedOutput()
}
func (dn *Daemon) storePendingState(pending *mcfgv1.MachineConfig, isPending int) ([]byte, error) {
if !dn.loggerSupportsJournal {
return dn.storePendingStateLegacyLogger(pending, isPending)
}
logger := exec.Command("logger", "--journald")
var pendingState bytes.Buffer
pendingState.WriteString(fmt.Sprintf(`MESSAGE_ID=%s
MESSAGE=%s
BOOT_ID=%s
PENDING=%d`, pendingStateMessageID, pending.GetName(), dn.bootID, isPending))
logger.Stdin = &pendingState
return logger.CombinedOutput()
}
// Log a message to the systemd journal as well as our stdout
func (dn *Daemon) logSystem(format string, a ...interface{}) {
message := fmt.Sprintf(format, a...)
glog.Info(message)
// Since we're chrooted into the host rootfs with /run mounted,
// we can just talk to the journald socket. Doing this as a
// subprocess rather than talking to journald in process since
// I worry about the golang library having a connection pre-chroot.
logger := exec.Command("logger")
var log bytes.Buffer
log.WriteString(fmt.Sprintf("machine-config-daemon[%d]: %s", os.Getpid(), message))
logger.Stdin = &log
if err := logger.Run(); err != nil {
glog.Errorf("failed to invoke logger: %v", err)
}
}
func (dn *Daemon) catchIgnoreSIGTERM() {
dn.updateActiveLock.Lock()
defer dn.updateActiveLock.Unlock()
if dn.updateActive {
return
}
dn.updateActive = true
}
func (dn *Daemon) cancelSIGTERM() {
dn.updateActiveLock.Lock()
defer dn.updateActiveLock.Unlock()
if dn.updateActive {
dn.updateActive = false
}
}
// reboot is the final step. it tells systemd-logind to reboot the machine,
// cleans up the agent's connections, and then sleeps for 7 days. if it wakes up
// and manages to return, it returns a scary error message.
func (dn *Daemon) reboot(rationale string, timeout time.Duration, rebootCmd *exec.Cmd) error {
// We'll only have a recorder if we're cluster driven
if dn.recorder != nil {
dn.recorder.Eventf(getNodeRef(dn.node), corev1.EventTypeNormal, "Reboot", rationale)
}
dn.logSystem("initiating reboot: %s", rationale)