aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/configserver/noderepository/NodeSpec.java
blob: d902fb7b3c42fb445f4ca9e399300b270d3e2186 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.configserver.noderepository;

import com.fasterxml.jackson.databind.JsonNode;
import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.config.provision.DockerImage;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.WireguardKey;
import com.yahoo.config.provision.WireguardKeyWithTimestamp;
import com.yahoo.vespa.hosted.node.admin.task.util.file.DiskSize;

import java.net.URI;
import java.time.Instant;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static com.yahoo.config.provision.NodeResources.DiskSpeed.fast;
import static com.yahoo.config.provision.NodeResources.DiskSpeed.slow;

/**
 * @author stiankri
 */
public class NodeSpec {

    private final String hostname;
    private final String id;
    private final NodeState state;
    private final NodeType type;
    private final CloudAccount cloudAccount;
    private final String flavor;

    private final Optional<DockerImage> wantedDockerImage;
    private final Optional<DockerImage> currentDockerImage;

    private final Optional<Version> wantedVespaVersion;
    private final Optional<Version> currentVespaVersion;

    private final Optional<Version> wantedOsVersion;
    private final Optional<Version> currentOsVersion;

    private final Optional<Long> wantedRestartGeneration;
    private final Optional<Long> currentRestartGeneration;

    private final long wantedRebootGeneration;
    private final long currentRebootGeneration;

    private final Optional<Instant> wantedFirmwareCheck;
    private final Optional<Instant> currentFirmwareCheck;

    private final Optional<String> modelName;

    private final OrchestratorStatus orchestratorStatus;
    private final Optional<ApplicationId> owner;
    private final Optional<NodeMembership> membership;

    private final NodeResources resources;
    private final NodeResources realResources;
    private final Set<String> ipAddresses;
    private final Set<String> additionalIpAddresses;

    private final NodeReports reports;
    private final List<Event> events;

    private final Optional<String> parentHostname;
    private final Optional<URI> archiveUri;

    private final Optional<ApplicationId> exclusiveTo;

    private final List<TrustStoreItem> trustStore;

    private final Optional<WireguardKeyWithTimestamp> wireguardKeyWithTimestamp;

    private final boolean wantToRebuild;

    public NodeSpec(
            String hostname,
            String id,
            Optional<DockerImage> wantedDockerImage,
            Optional<DockerImage> currentDockerImage,
            NodeState state,
            NodeType type,
            CloudAccount cloudAccount,
            String flavor,
            Optional<Version> wantedVespaVersion,
            Optional<Version> currentVespaVersion,
            Optional<Version> wantedOsVersion,
            Optional<Version> currentOsVersion,
            OrchestratorStatus orchestratorStatus,
            Optional<ApplicationId> owner,
            Optional<NodeMembership> membership,
            Optional<Long> wantedRestartGeneration,
            Optional<Long> currentRestartGeneration,
            long wantedRebootGeneration,
            long currentRebootGeneration,
            Optional<Instant> wantedFirmwareCheck,
            Optional<Instant> currentFirmwareCheck,
            Optional<String> modelName,
            NodeResources resources,
            NodeResources realResources,
            Set<String> ipAddresses,
            Set<String> additionalIpAddresses,
            NodeReports reports,
            List<Event> events,
            Optional<String> parentHostname,
            Optional<URI> archiveUri,
            Optional<ApplicationId> exclusiveTo,
            List<TrustStoreItem> trustStore,
            Optional<WireguardKeyWithTimestamp> wireguardPubkey,
            boolean wantToRebuild) {

        if (state == NodeState.active) {
            requireOptional(owner, "owner");
            requireOptional(membership, "membership");
            requireOptional(wantedVespaVersion, "wantedVespaVersion");
            requireOptional(wantedDockerImage, "wantedDockerImage");
            requireOptional(wantedRestartGeneration, "restartGeneration");
            requireOptional(currentRestartGeneration, "currentRestartGeneration");
        }

        this.hostname = Objects.requireNonNull(hostname);
        this.id = Objects.requireNonNull(id);
        this.wantedDockerImage = Objects.requireNonNull(wantedDockerImage);
        this.currentDockerImage = Objects.requireNonNull(currentDockerImage);
        this.state = Objects.requireNonNull(state);
        this.type = Objects.requireNonNull(type);
        this.cloudAccount = Objects.requireNonNull(cloudAccount);
        this.flavor = Objects.requireNonNull(flavor);
        this.modelName = Objects.requireNonNull(modelName);
        this.wantedVespaVersion = Objects.requireNonNull(wantedVespaVersion);
        this.currentVespaVersion = Objects.requireNonNull(currentVespaVersion);
        this.wantedOsVersion = Objects.requireNonNull(wantedOsVersion);
        this.currentOsVersion = Objects.requireNonNull(currentOsVersion);
        this.orchestratorStatus = Objects.requireNonNull(orchestratorStatus);
        this.owner = Objects.requireNonNull(owner);
        this.membership = Objects.requireNonNull(membership);
        this.wantedRestartGeneration = wantedRestartGeneration;
        this.currentRestartGeneration = currentRestartGeneration;
        this.wantedRebootGeneration = wantedRebootGeneration;
        this.currentRebootGeneration = currentRebootGeneration;
        this.wantedFirmwareCheck = Objects.requireNonNull(wantedFirmwareCheck);
        this.currentFirmwareCheck = Objects.requireNonNull(currentFirmwareCheck);
        this.resources = Objects.requireNonNull(resources);
        this.realResources = Objects.requireNonNull(realResources);
        this.ipAddresses = Set.copyOf(ipAddresses);
        this.additionalIpAddresses = Set.copyOf(additionalIpAddresses);
        this.reports = Objects.requireNonNull(reports);
        this.events = List.copyOf(events);
        this.parentHostname = Objects.requireNonNull(parentHostname);
        this.archiveUri = Objects.requireNonNull(archiveUri);
        this.exclusiveTo = Objects.requireNonNull(exclusiveTo);
        this.trustStore = Objects.requireNonNull(trustStore);
        this.wireguardKeyWithTimestamp = Objects.requireNonNull(wireguardPubkey);
        this.wantToRebuild = wantToRebuild;
    }

    public String hostname() {
        return hostname;
    }

    /** Returns unique node ID */
    public String id() {
        return id;
    }

    public NodeState state() {
        return state;
    }

    public NodeType type() {
        return type;
    }

    public CloudAccount cloudAccount() {
        return cloudAccount;
    }

    public String flavor() {
        return flavor;
    }

    public Optional<DockerImage> wantedDockerImage() {
        return wantedDockerImage;
    }

    public Optional<DockerImage> currentDockerImage() {
        return currentDockerImage;
    }

    public Optional<Version> wantedVespaVersion() {
        return wantedVespaVersion;
    }

    public Optional<Version> currentVespaVersion() {
        return currentVespaVersion;
    }

    public Optional<Version> currentOsVersion() {
        return currentOsVersion;
    }

    public Optional<Version> wantedOsVersion() {
        return wantedOsVersion;
    }

    public Optional<Long> wantedRestartGeneration() {
        return wantedRestartGeneration;
    }

    public Optional<Long> currentRestartGeneration() {
        return currentRestartGeneration;
    }

    public long wantedRebootGeneration() {
        return wantedRebootGeneration;
    }

    public long currentRebootGeneration() {
        return currentRebootGeneration;
    }

    public Optional<Instant> wantedFirmwareCheck() {
        return wantedFirmwareCheck;
    }

    public Optional<Instant> currentFirmwareCheck() {
        return currentFirmwareCheck;
    }

    public Optional<String> modelName() {
        return modelName;
    }

    public OrchestratorStatus orchestratorStatus() {
        return orchestratorStatus;
    }

    public Optional<ApplicationId> owner() {
        return owner;
    }

    public Optional<NodeMembership> membership() {
        return membership;
    }

    public NodeResources resources() {
        return resources;
    }

    public NodeResources realResources() {
        return realResources;
    }

    public double vcpu() {
        return realResources.vcpu();
    }

    public double memoryGb() {
        return realResources.memoryGb();
    }

    public DiskSize diskSize() {
        return DiskSize.of(realResources.diskGb(), DiskSize.Unit.GB);
    }

    public double diskGb() {
        return realResources.diskGb();
    }

    public boolean isFastDisk() {
        return realResources.diskSpeed() == fast;
    }

    public double bandwidthGbps() {
        return realResources.bandwidthGbps();
    }

    public Set<String> ipAddresses() {
        return ipAddresses;
    }

    public Set<String> additionalIpAddresses() {
        return additionalIpAddresses;
    }

    public NodeReports reports() { return reports; }

    public List<Event> events() {
        return events;
    }

    public Optional<String> parentHostname() {
        return parentHostname;
    }

    public Optional<URI> archiveUri() {
        return archiveUri;
    }

    public Optional<ApplicationId> exclusiveTo() {
        return exclusiveTo;
    }

    public List<TrustStoreItem> trustStore() {
        return trustStore;
    }

    public Optional<WireguardKeyWithTimestamp> wireguardKeyWithTimestamp() { return wireguardKeyWithTimestamp; }

    public boolean wantToRebuild() {
        return wantToRebuild;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof NodeSpec that)) return false;

        return Objects.equals(hostname, that.hostname) &&
                Objects.equals(id, that.id) &&
                Objects.equals(wantedDockerImage, that.wantedDockerImage) &&
                Objects.equals(currentDockerImage, that.currentDockerImage) &&
                Objects.equals(state, that.state) &&
                Objects.equals(type, that.type) &&
                Objects.equals(cloudAccount, that.cloudAccount) &&
                Objects.equals(flavor, that.flavor) &&
                Objects.equals(modelName, that.modelName) &&
                Objects.equals(wantedVespaVersion, that.wantedVespaVersion) &&
                Objects.equals(currentVespaVersion, that.currentVespaVersion) &&
                Objects.equals(wantedOsVersion, that.wantedOsVersion) &&
                Objects.equals(currentOsVersion, that.currentOsVersion) &&
                Objects.equals(orchestratorStatus, that.orchestratorStatus) &&
                Objects.equals(owner, that.owner) &&
                Objects.equals(membership, that.membership) &&
                Objects.equals(wantedRestartGeneration, that.wantedRestartGeneration) &&
                Objects.equals(currentRestartGeneration, that.currentRestartGeneration) &&
                Objects.equals(wantedRebootGeneration, that.wantedRebootGeneration) &&
                Objects.equals(currentRebootGeneration, that.currentRebootGeneration) &&
                Objects.equals(wantedFirmwareCheck, that.wantedFirmwareCheck) &&
                Objects.equals(currentFirmwareCheck, that.currentFirmwareCheck) &&
                Objects.equals(resources, that.resources) &&
                Objects.equals(realResources, that.realResources) &&
                Objects.equals(ipAddresses, that.ipAddresses) &&
                Objects.equals(additionalIpAddresses, that.additionalIpAddresses) &&
                Objects.equals(reports, that.reports) &&
                Objects.equals(events, that.events) &&
                Objects.equals(parentHostname, that.parentHostname) &&
                Objects.equals(archiveUri, that.archiveUri) &&
                Objects.equals(exclusiveTo, that.exclusiveTo) &&
                Objects.equals(trustStore, that.trustStore) &&
                Objects.equals(wireguardKeyWithTimestamp, that.wireguardKeyWithTimestamp) &&
                Objects.equals(wantToRebuild, that.wantToRebuild);
    }

    @Override
    public int hashCode() {
        return Objects.hash(
                hostname,
                id,
                wantedDockerImage,
                currentDockerImage,
                state,
                type,
                cloudAccount,
                flavor,
                modelName,
                wantedVespaVersion,
                currentVespaVersion,
                wantedOsVersion,
                currentOsVersion,
                orchestratorStatus,
                owner,
                membership,
                wantedRestartGeneration,
                currentRestartGeneration,
                wantedRebootGeneration,
                currentRebootGeneration,
                wantedFirmwareCheck,
                currentFirmwareCheck,
                resources,
                realResources,
                ipAddresses,
                additionalIpAddresses,
                reports,
                events,
                parentHostname,
                archiveUri,
                exclusiveTo,
                trustStore,
                wireguardKeyWithTimestamp,
                wantToRebuild);
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + " {"
                + " hostname=" + hostname
                + " id=" + id
                + " wantedDockerImage=" + wantedDockerImage
                + " currentDockerImage=" + currentDockerImage
                + " state=" + state
                + " type=" + type
                + " cloudAccount=" + cloudAccount
                + " flavor=" + flavor
                + " modelName=" + modelName
                + " wantedVespaVersion=" + wantedVespaVersion
                + " currentVespaVersion=" + currentVespaVersion
                + " wantedOsVersion=" + wantedOsVersion
                + " currentOsVersion=" + currentOsVersion
                + " orchestratorStatus=" + orchestratorStatus
                + " owner=" + owner
                + " membership=" + membership
                + " wantedRestartGeneration=" + wantedRestartGeneration
                + " currentRestartGeneration=" + currentRestartGeneration
                + " wantedRebootGeneration=" + wantedRebootGeneration
                + " currentRebootGeneration=" + currentRebootGeneration
                + " wantedFirmwareCheck=" + wantedFirmwareCheck
                + " currentFirmwareCheck=" + currentFirmwareCheck
                + " resources=" + resources
                + " realResources=" + realResources
                + " ipAddresses=" + ipAddresses
                + " additionalIpAddresses=" + additionalIpAddresses
                + " reports=" + reports
                + " events=" + events
                + " parentHostname=" + parentHostname
                + " archiveUri=" + archiveUri
                + " exclusiveTo=" + exclusiveTo
                + " trustStore=" + trustStore
                + " wireguardPubkey=" + wireguardKeyWithTimestamp
                + " wantToRebuild=" + wantToRebuild
                + " }";
    }

    public static class Builder {
        private String hostname;
        private String id;
        private NodeState state;
        private NodeType type;
        private CloudAccount cloudAccount = CloudAccount.empty;
        private String flavor;
        private Optional<DockerImage> wantedDockerImage = Optional.empty();
        private Optional<DockerImage> currentDockerImage = Optional.empty();
        private Optional<Version> wantedVespaVersion = Optional.empty();
        private Optional<Version> currentVespaVersion = Optional.empty();
        private Optional<Version> wantedOsVersion = Optional.empty();
        private Optional<Version> currentOsVersion = Optional.empty();
        private OrchestratorStatus orchestratorStatus = OrchestratorStatus.NO_REMARKS;
        private Optional<ApplicationId> owner = Optional.empty();
        private Optional<NodeMembership> membership = Optional.empty();
        private Optional<Long> wantedRestartGeneration = Optional.empty();
        private Optional<Long> currentRestartGeneration = Optional.empty();
        private long wantedRebootGeneration;
        private long currentRebootGeneration;
        private Optional<Instant> wantedFirmwareCheck = Optional.empty();
        private Optional<Instant> currentFirmwareCheck = Optional.empty();
        private Optional<String> modelName = Optional.empty();
        private NodeResources resources;
        private NodeResources realResources;
        private Set<String> ipAddresses = Set.of();
        private Set<String> additionalIpAddresses = Set.of();
        private NodeReports reports = new NodeReports();
        private List<Event> events = List.of();
        private Optional<String> parentHostname = Optional.empty();
        private Optional<URI> archiveUri = Optional.empty();
        private Optional<ApplicationId> exclusiveTo = Optional.empty();
        private List<TrustStoreItem> trustStore = List.of();
        private Optional<WireguardKeyWithTimestamp> wireguardPubkey = Optional.empty();
        private boolean wantToRebuild = false;

        public Builder() {}

        public Builder(NodeSpec node) {
            hostname(node.hostname);
            id(node.id);
            state(node.state);
            type(node.type);
            flavor(node.flavor);
            resources(node.resources);
            realResources(node.realResources);
            ipAddresses(node.ipAddresses);
            additionalIpAddresses(node.additionalIpAddresses);
            wantedRebootGeneration(node.wantedRebootGeneration);
            currentRebootGeneration(node.currentRebootGeneration);
            orchestratorStatus(node.orchestratorStatus);
            reports(new NodeReports(node.reports));
            events(node.events);
            node.wantedDockerImage.ifPresent(this::wantedDockerImage);
            node.currentDockerImage.ifPresent(this::currentDockerImage);
            node.wantedVespaVersion.ifPresent(this::wantedVespaVersion);
            node.currentVespaVersion.ifPresent(this::currentVespaVersion);
            node.wantedOsVersion.ifPresent(this::wantedOsVersion);
            node.currentOsVersion.ifPresent(this::currentOsVersion);
            node.owner.ifPresent(this::owner);
            node.membership.ifPresent(this::membership);
            node.wantedRestartGeneration.ifPresent(this::wantedRestartGeneration);
            node.currentRestartGeneration.ifPresent(this::currentRestartGeneration);
            node.wantedFirmwareCheck.ifPresent(this::wantedFirmwareCheck);
            node.currentFirmwareCheck.ifPresent(this::currentFirmwareCheck);
            node.parentHostname.ifPresent(this::parentHostname);
            node.archiveUri.ifPresent(this::archiveUri);
            node.exclusiveTo.ifPresent(this::exclusiveTo);
            trustStore(node.trustStore);
            node.wireguardKeyWithTimestamp.ifPresent(this::wireguardKeyWithTimestamp);
            wantToRebuild(node.wantToRebuild);
        }

        public Builder hostname(String hostname) {
            this.hostname = hostname;
            return this;
        }

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        public Builder wantedDockerImage(DockerImage wantedDockerImage) {
            this.wantedDockerImage = Optional.of(wantedDockerImage);
            return this;
        }

        public Builder currentDockerImage(DockerImage currentDockerImage) {
            this.currentDockerImage = Optional.of(currentDockerImage);
            return this;
        }

        public Builder state(NodeState state) {
            this.state = state;
            return this;
        }

        public Builder type(NodeType nodeType) {
            this.type = nodeType;
            return this;
        }

        public Builder cloudAccount(CloudAccount cloudAccount) {
            this.cloudAccount = cloudAccount;
            return this;
        }

        public Builder flavor(String flavor) {
            this.flavor = flavor;
            return this;
        }

        public Builder wantedVespaVersion(Version wantedVespaVersion) {
            this.wantedVespaVersion = Optional.of(wantedVespaVersion);
            return this;
        }

        public Builder currentVespaVersion(Version vespaVersion) {
            this.currentVespaVersion = Optional.of(vespaVersion);
            return this;
        }

        public Builder wantedOsVersion(Version wantedOsVersion) {
            this.wantedOsVersion = Optional.of(wantedOsVersion);
            return this;
        }

        public Builder currentOsVersion(Version currentOsVersion) {
            this.currentOsVersion = Optional.of(currentOsVersion);
            return this;
        }

        public Builder orchestratorStatus(OrchestratorStatus orchestratorStatus) {
            this.orchestratorStatus = orchestratorStatus;
            return this;
        }

        public Builder owner(ApplicationId owner) {
            this.owner = Optional.of(owner);
            return this;
        }

        public Builder membership(NodeMembership membership) {
            this.membership = Optional.of(membership);
            return this;
        }

        public Builder wantedRestartGeneration(long wantedRestartGeneration) {
            this.wantedRestartGeneration = Optional.of(wantedRestartGeneration);
            return this;
        }

        public Builder currentRestartGeneration(long currentRestartGeneration) {
            this.currentRestartGeneration = Optional.of(currentRestartGeneration);
            return this;
        }

        public Builder wantedRebootGeneration(long wantedRebootGeneration) {
            this.wantedRebootGeneration = wantedRebootGeneration;
            return this;
        }

        public Builder currentRebootGeneration(long currentRebootGeneration) {
            this.currentRebootGeneration = currentRebootGeneration;
            return this;
        }

        public Builder wantedFirmwareCheck(Instant wantedFirmwareCheck) {
            this.wantedFirmwareCheck = Optional.of(wantedFirmwareCheck);
            return this;
        }

        public Builder currentFirmwareCheck(Instant currentFirmwareCheck) {
            this.currentFirmwareCheck = Optional.of(currentFirmwareCheck);
            return this;
        }

        public Builder resources(NodeResources resources) {
            this.resources = resources;
            return this;
        }

        public Builder realResources(NodeResources realResources) {
            this.realResources = realResources;
            return this;
        }

        public Builder vcpu(double vcpu) {
            return realResources(realResources.withVcpu(vcpu));
        }

        public Builder memoryGb(double memoryGb) {
            return realResources(realResources.withMemoryGb(memoryGb));
        }

        public Builder diskGb(double diskGb) {
            return realResources(realResources.withDiskGb(diskGb));
        }

        public Builder fastDisk(boolean fastDisk) {
            return realResources(realResources.with(fastDisk ? fast : slow));
        }

        public Builder bandwidthGbps(double bandwidthGbps) {
            return realResources(realResources.withBandwidthGbps(bandwidthGbps));
        }

        public Builder ipAddresses(Set<String> ipAddresses) {
            this.ipAddresses = ipAddresses;
            return this;
        }

        public Builder additionalIpAddresses(Set<String> additionalIpAddresses) {
            this.additionalIpAddresses = additionalIpAddresses;
            return this;
        }

        public Builder reports(NodeReports reports) {
            this.reports = reports;
            return this;
        }

        public Builder report(String reportId, JsonNode report) {
            this.reports.setReport(reportId, report);
            return this;
        }

        public Builder removeReport(String reportId) {
            reports.removeReport(reportId);
            return this;
        }

        public Builder events(List<Event> events) {
            this.events = events;
            return this;
        }

        public Builder parentHostname(String parentHostname) {
            this.parentHostname = Optional.of(parentHostname);
            return this;
        }

        public Builder archiveUri(URI archiveUri) {
            this.archiveUri = Optional.of(archiveUri);
            return this;
        }

        public Builder exclusiveTo(ApplicationId applicationId) {
            this.exclusiveTo = Optional.of(applicationId);
            return this;
        }

        public Builder trustStore(List<TrustStoreItem> trustStore) {
            this.trustStore = List.copyOf(trustStore);
            return this;
        }

        public Builder wireguardPubkey(WireguardKey wireguardPubkey) {
            this.wireguardPubkey = Optional.of(new WireguardKeyWithTimestamp(wireguardPubkey, Instant.EPOCH));
            return this;
        }

        public Builder wireguardKeyWithTimestamp(WireguardKeyWithTimestamp wireguardPubKey) {
            this.wireguardPubkey = Optional.of(wireguardPubKey);
            return this;
        }

        public Builder wantToRebuild(boolean wantToRebuild) {
            this.wantToRebuild = wantToRebuild;
            return this;
        }

        public Builder updateFromNodeAttributes(NodeAttributes attributes) {
            attributes.getHostId().ifPresent(this::id);
            attributes.getDockerImage().ifPresent(this::currentDockerImage);
            attributes.getCurrentOsVersion().ifPresent(this::currentOsVersion);
            attributes.getRebootGeneration().ifPresent(this::currentRebootGeneration);
            attributes.getRestartGeneration().ifPresent(this::currentRestartGeneration);
            // Always replace entire trust store
            trustStore(attributes.getTrustStore());
            attributes.getWireguardPubkey().ifPresent(this::wireguardPubkey);
            this.reports.updateFromRawMap(attributes.getReports());

            return this;
        }

        public String hostname() {
            return hostname;
        }

        public Optional<DockerImage> wantedDockerImage() {
            return wantedDockerImage;
        }

        public Optional<DockerImage> currentDockerImage() {
            return currentDockerImage;
        }

        public NodeState state() {
            return state;
        }

        public NodeType type() {
            return type;
        }

        public CloudAccount cloudAccount() {
            return cloudAccount;
        }

        public String flavor() {
            return flavor;
        }

        public Optional<Version> wantedVespaVersion() {
            return wantedVespaVersion;
        }

        public Optional<Version> currentVespaVersion() {
            return currentVespaVersion;
        }

        public Optional<Version> wantedOsVersion() {
            return wantedOsVersion;
        }

        public Optional<Version> currentOsVersion() {
            return currentOsVersion;
        }

        public OrchestratorStatus orchestratorStatus() {
            return orchestratorStatus;
        }

        public Optional<ApplicationId> owner() {
            return owner;
        }

        public Optional<NodeMembership> membership() {
            return membership;
        }

        public Optional<Long> wantedRestartGeneration() {
            return wantedRestartGeneration;
        }

        public Optional<Long> currentRestartGeneration() {
            return currentRestartGeneration;
        }

        public long wantedRebootGeneration() {
            return wantedRebootGeneration;
        }

        public long currentRebootGeneration() {
            return currentRebootGeneration;
        }

        public NodeResources resources() {
            return resources;
        }

        public NodeResources realResources() {
            return realResources;
        }

        public Set<String> ipAddresses() {
            return ipAddresses;
        }

        public Set<String> additionalIpAddresses() {
            return additionalIpAddresses;
        }

        public NodeReports reports() {
            return reports;
        }

        public List<Event> events() {
            return events;
        }

        public Optional<String> parentHostname() {
            return parentHostname;
        }

        public Optional<URI> archiveUri() {
            return archiveUri;
        }

        public NodeSpec build() {
            return new NodeSpec(hostname, id, wantedDockerImage, currentDockerImage, state, type, cloudAccount, flavor,
                                wantedVespaVersion, currentVespaVersion, wantedOsVersion, currentOsVersion, orchestratorStatus,
                                owner, membership,
                                wantedRestartGeneration, currentRestartGeneration,
                                wantedRebootGeneration, currentRebootGeneration,
                                wantedFirmwareCheck, currentFirmwareCheck, modelName,
                                resources, realResources, ipAddresses, additionalIpAddresses,
                                reports, events, parentHostname, archiveUri, exclusiveTo, trustStore,
                                wireguardPubkey, wantToRebuild);
        }


        public static Builder testSpec(String hostname) {
            return testSpec(hostname, NodeState.active);
        }

        /**
         * Creates a NodeSpec.Builder that has the given hostname, in a given state, and some
         * reasonable values for the remaining required NodeSpec fields.
         */
        public static Builder testSpec(String hostname, NodeState state) {
            Builder builder = new Builder()
                    .id(hostname)
                    .hostname(hostname)
                    .state(state)
                    .type(NodeType.tenant)
                    .flavor("d-2-8-50")
                    .resources(new NodeResources(2, 8, 50, 10))
                    .realResources(new NodeResources(2, 8, 50, 10))
                    .events(List.of(new Event("operator", "rebooted", Instant.EPOCH)));

            // Set the required allocated fields
            if (EnumSet.of(NodeState.active, NodeState.inactive, NodeState.reserved).contains(state)) {
                builder .owner(ApplicationId.defaultId())
                        .membership(new NodeMembership("container", "my-id", "group", 0, false))
                        .wantedVespaVersion(Version.fromString("7.1.1"))
                        .wantedDockerImage(DockerImage.fromString("docker.domain.tld/repo/image:7.1.1"))
                        .currentRestartGeneration(0)
                        .wantedRestartGeneration(0);
            }

            return builder;
        }
    }

    private static void requireOptional(Optional<?> optional, String name) {
        if (optional == null || optional.isEmpty())
            throw new IllegalArgumentException(name + " must be set, was " + optional);
    }
}