github.com/emate/nomad@v0.8.2-wo-binpacking/nomad/structs/diff.go (about) 1 package structs 2 3 import ( 4 "fmt" 5 "reflect" 6 "sort" 7 "strings" 8 9 "github.com/hashicorp/nomad/helper/flatmap" 10 "github.com/mitchellh/hashstructure" 11 ) 12 13 // DiffType denotes the type of a diff object. 14 type DiffType string 15 16 var ( 17 DiffTypeNone DiffType = "None" 18 DiffTypeAdded DiffType = "Added" 19 DiffTypeDeleted DiffType = "Deleted" 20 DiffTypeEdited DiffType = "Edited" 21 ) 22 23 func (d DiffType) Less(other DiffType) bool { 24 // Edited > Added > Deleted > None 25 // But we do a reverse sort 26 if d == other { 27 return false 28 } 29 30 if d == DiffTypeEdited { 31 return true 32 } else if other == DiffTypeEdited { 33 return false 34 } else if d == DiffTypeAdded { 35 return true 36 } else if other == DiffTypeAdded { 37 return false 38 } else if d == DiffTypeDeleted { 39 return true 40 } else if other == DiffTypeDeleted { 41 return false 42 } 43 44 return true 45 } 46 47 // JobDiff contains the diff of two jobs. 48 type JobDiff struct { 49 Type DiffType 50 ID string 51 Fields []*FieldDiff 52 Objects []*ObjectDiff 53 TaskGroups []*TaskGroupDiff 54 } 55 56 // Diff returns a diff of two jobs and a potential error if the Jobs are not 57 // diffable. If contextual diff is enabled, objects within the job will contain 58 // field information even if unchanged. 59 func (j *Job) Diff(other *Job, contextual bool) (*JobDiff, error) { 60 // COMPAT: Remove "Update" in 0.7.0. Update pushed down to task groups 61 // in 0.6.0 62 diff := &JobDiff{Type: DiffTypeNone} 63 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 64 filter := []string{"ID", "Status", "StatusDescription", "Version", "Stable", "CreateIndex", 65 "ModifyIndex", "JobModifyIndex", "Update", "SubmitTime"} 66 67 if j == nil && other == nil { 68 return diff, nil 69 } else if j == nil { 70 j = &Job{} 71 diff.Type = DiffTypeAdded 72 newPrimitiveFlat = flatmap.Flatten(other, filter, true) 73 diff.ID = other.ID 74 } else if other == nil { 75 other = &Job{} 76 diff.Type = DiffTypeDeleted 77 oldPrimitiveFlat = flatmap.Flatten(j, filter, true) 78 diff.ID = j.ID 79 } else { 80 if j.ID != other.ID { 81 return nil, fmt.Errorf("can not diff jobs with different IDs: %q and %q", j.ID, other.ID) 82 } 83 84 oldPrimitiveFlat = flatmap.Flatten(j, filter, true) 85 newPrimitiveFlat = flatmap.Flatten(other, filter, true) 86 diff.ID = other.ID 87 } 88 89 // Diff the primitive fields. 90 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, false) 91 92 // Datacenters diff 93 if setDiff := stringSetDiff(j.Datacenters, other.Datacenters, "Datacenters", contextual); setDiff != nil && setDiff.Type != DiffTypeNone { 94 diff.Objects = append(diff.Objects, setDiff) 95 } 96 97 // Constraints diff 98 conDiff := primitiveObjectSetDiff( 99 interfaceSlice(j.Constraints), 100 interfaceSlice(other.Constraints), 101 []string{"str"}, 102 "Constraint", 103 contextual) 104 if conDiff != nil { 105 diff.Objects = append(diff.Objects, conDiff...) 106 } 107 108 // Task groups diff 109 tgs, err := taskGroupDiffs(j.TaskGroups, other.TaskGroups, contextual) 110 if err != nil { 111 return nil, err 112 } 113 diff.TaskGroups = tgs 114 115 // Periodic diff 116 if pDiff := primitiveObjectDiff(j.Periodic, other.Periodic, nil, "Periodic", contextual); pDiff != nil { 117 diff.Objects = append(diff.Objects, pDiff) 118 } 119 120 // ParameterizedJob diff 121 if cDiff := parameterizedJobDiff(j.ParameterizedJob, other.ParameterizedJob, contextual); cDiff != nil { 122 diff.Objects = append(diff.Objects, cDiff) 123 } 124 125 // Check to see if there is a diff. We don't use reflect because we are 126 // filtering quite a few fields that will change on each diff. 127 if diff.Type == DiffTypeNone { 128 for _, fd := range diff.Fields { 129 if fd.Type != DiffTypeNone { 130 diff.Type = DiffTypeEdited 131 break 132 } 133 } 134 } 135 136 if diff.Type == DiffTypeNone { 137 for _, od := range diff.Objects { 138 if od.Type != DiffTypeNone { 139 diff.Type = DiffTypeEdited 140 break 141 } 142 } 143 } 144 145 if diff.Type == DiffTypeNone { 146 for _, tg := range diff.TaskGroups { 147 if tg.Type != DiffTypeNone { 148 diff.Type = DiffTypeEdited 149 break 150 } 151 } 152 } 153 154 return diff, nil 155 } 156 157 func (j *JobDiff) GoString() string { 158 out := fmt.Sprintf("Job %q (%s):\n", j.ID, j.Type) 159 160 for _, f := range j.Fields { 161 out += fmt.Sprintf("%#v\n", f) 162 } 163 164 for _, o := range j.Objects { 165 out += fmt.Sprintf("%#v\n", o) 166 } 167 168 for _, tg := range j.TaskGroups { 169 out += fmt.Sprintf("%#v\n", tg) 170 } 171 172 return out 173 } 174 175 // TaskGroupDiff contains the diff of two task groups. 176 type TaskGroupDiff struct { 177 Type DiffType 178 Name string 179 Fields []*FieldDiff 180 Objects []*ObjectDiff 181 Tasks []*TaskDiff 182 Updates map[string]uint64 183 } 184 185 // Diff returns a diff of two task groups. If contextual diff is enabled, 186 // objects' fields will be stored even if no diff occurred as long as one field 187 // changed. 188 func (tg *TaskGroup) Diff(other *TaskGroup, contextual bool) (*TaskGroupDiff, error) { 189 diff := &TaskGroupDiff{Type: DiffTypeNone} 190 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 191 filter := []string{"Name"} 192 193 if tg == nil && other == nil { 194 return diff, nil 195 } else if tg == nil { 196 tg = &TaskGroup{} 197 diff.Type = DiffTypeAdded 198 diff.Name = other.Name 199 newPrimitiveFlat = flatmap.Flatten(other, filter, true) 200 } else if other == nil { 201 other = &TaskGroup{} 202 diff.Type = DiffTypeDeleted 203 diff.Name = tg.Name 204 oldPrimitiveFlat = flatmap.Flatten(tg, filter, true) 205 } else { 206 if !reflect.DeepEqual(tg, other) { 207 diff.Type = DiffTypeEdited 208 } 209 if tg.Name != other.Name { 210 return nil, fmt.Errorf("can not diff task groups with different names: %q and %q", tg.Name, other.Name) 211 } 212 diff.Name = other.Name 213 oldPrimitiveFlat = flatmap.Flatten(tg, filter, true) 214 newPrimitiveFlat = flatmap.Flatten(other, filter, true) 215 } 216 217 // Diff the primitive fields. 218 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, false) 219 220 // Constraints diff 221 conDiff := primitiveObjectSetDiff( 222 interfaceSlice(tg.Constraints), 223 interfaceSlice(other.Constraints), 224 []string{"str"}, 225 "Constraint", 226 contextual) 227 if conDiff != nil { 228 diff.Objects = append(diff.Objects, conDiff...) 229 } 230 231 // Restart policy diff 232 rDiff := primitiveObjectDiff(tg.RestartPolicy, other.RestartPolicy, nil, "RestartPolicy", contextual) 233 if rDiff != nil { 234 diff.Objects = append(diff.Objects, rDiff) 235 } 236 237 // Reschedule policy diff 238 reschedDiff := primitiveObjectDiff(tg.ReschedulePolicy, other.ReschedulePolicy, nil, "ReschedulePolicy", contextual) 239 if reschedDiff != nil { 240 diff.Objects = append(diff.Objects, reschedDiff) 241 } 242 243 // EphemeralDisk diff 244 diskDiff := primitiveObjectDiff(tg.EphemeralDisk, other.EphemeralDisk, nil, "EphemeralDisk", contextual) 245 if diskDiff != nil { 246 diff.Objects = append(diff.Objects, diskDiff) 247 } 248 249 // Update diff 250 // COMPAT: Remove "Stagger" in 0.7.0. 251 if uDiff := primitiveObjectDiff(tg.Update, other.Update, []string{"Stagger"}, "Update", contextual); uDiff != nil { 252 diff.Objects = append(diff.Objects, uDiff) 253 } 254 255 // Tasks diff 256 tasks, err := taskDiffs(tg.Tasks, other.Tasks, contextual) 257 if err != nil { 258 return nil, err 259 } 260 diff.Tasks = tasks 261 262 return diff, nil 263 } 264 265 func (tg *TaskGroupDiff) GoString() string { 266 out := fmt.Sprintf("Group %q (%s):\n", tg.Name, tg.Type) 267 268 if len(tg.Updates) != 0 { 269 out += "Updates {\n" 270 for update, count := range tg.Updates { 271 out += fmt.Sprintf("%d %s\n", count, update) 272 } 273 out += "}\n" 274 } 275 276 for _, f := range tg.Fields { 277 out += fmt.Sprintf("%#v\n", f) 278 } 279 280 for _, o := range tg.Objects { 281 out += fmt.Sprintf("%#v\n", o) 282 } 283 284 for _, t := range tg.Tasks { 285 out += fmt.Sprintf("%#v\n", t) 286 } 287 288 return out 289 } 290 291 // TaskGroupDiffs diffs two sets of task groups. If contextual diff is enabled, 292 // objects' fields will be stored even if no diff occurred as long as one field 293 // changed. 294 func taskGroupDiffs(old, new []*TaskGroup, contextual bool) ([]*TaskGroupDiff, error) { 295 oldMap := make(map[string]*TaskGroup, len(old)) 296 newMap := make(map[string]*TaskGroup, len(new)) 297 for _, o := range old { 298 oldMap[o.Name] = o 299 } 300 for _, n := range new { 301 newMap[n.Name] = n 302 } 303 304 var diffs []*TaskGroupDiff 305 for name, oldGroup := range oldMap { 306 // Diff the same, deleted and edited 307 diff, err := oldGroup.Diff(newMap[name], contextual) 308 if err != nil { 309 return nil, err 310 } 311 diffs = append(diffs, diff) 312 } 313 314 for name, newGroup := range newMap { 315 // Diff the added 316 if old, ok := oldMap[name]; !ok { 317 diff, err := old.Diff(newGroup, contextual) 318 if err != nil { 319 return nil, err 320 } 321 diffs = append(diffs, diff) 322 } 323 } 324 325 sort.Sort(TaskGroupDiffs(diffs)) 326 return diffs, nil 327 } 328 329 // For sorting TaskGroupDiffs 330 type TaskGroupDiffs []*TaskGroupDiff 331 332 func (tg TaskGroupDiffs) Len() int { return len(tg) } 333 func (tg TaskGroupDiffs) Swap(i, j int) { tg[i], tg[j] = tg[j], tg[i] } 334 func (tg TaskGroupDiffs) Less(i, j int) bool { return tg[i].Name < tg[j].Name } 335 336 // TaskDiff contains the diff of two Tasks 337 type TaskDiff struct { 338 Type DiffType 339 Name string 340 Fields []*FieldDiff 341 Objects []*ObjectDiff 342 Annotations []string 343 } 344 345 // Diff returns a diff of two tasks. If contextual diff is enabled, objects 346 // within the task will contain field information even if unchanged. 347 func (t *Task) Diff(other *Task, contextual bool) (*TaskDiff, error) { 348 diff := &TaskDiff{Type: DiffTypeNone} 349 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 350 filter := []string{"Name", "Config"} 351 352 if t == nil && other == nil { 353 return diff, nil 354 } else if t == nil { 355 t = &Task{} 356 diff.Type = DiffTypeAdded 357 diff.Name = other.Name 358 newPrimitiveFlat = flatmap.Flatten(other, filter, true) 359 } else if other == nil { 360 other = &Task{} 361 diff.Type = DiffTypeDeleted 362 diff.Name = t.Name 363 oldPrimitiveFlat = flatmap.Flatten(t, filter, true) 364 } else { 365 if !reflect.DeepEqual(t, other) { 366 diff.Type = DiffTypeEdited 367 } 368 if t.Name != other.Name { 369 return nil, fmt.Errorf("can not diff tasks with different names: %q and %q", t.Name, other.Name) 370 } 371 diff.Name = other.Name 372 oldPrimitiveFlat = flatmap.Flatten(t, filter, true) 373 newPrimitiveFlat = flatmap.Flatten(other, filter, true) 374 } 375 376 // Diff the primitive fields. 377 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, false) 378 379 // Constraints diff 380 conDiff := primitiveObjectSetDiff( 381 interfaceSlice(t.Constraints), 382 interfaceSlice(other.Constraints), 383 []string{"str"}, 384 "Constraint", 385 contextual) 386 if conDiff != nil { 387 diff.Objects = append(diff.Objects, conDiff...) 388 } 389 390 // Config diff 391 if cDiff := configDiff(t.Config, other.Config, contextual); cDiff != nil { 392 diff.Objects = append(diff.Objects, cDiff) 393 } 394 395 // Resources diff 396 if rDiff := t.Resources.Diff(other.Resources, contextual); rDiff != nil { 397 diff.Objects = append(diff.Objects, rDiff) 398 } 399 400 // LogConfig diff 401 lDiff := primitiveObjectDiff(t.LogConfig, other.LogConfig, nil, "LogConfig", contextual) 402 if lDiff != nil { 403 diff.Objects = append(diff.Objects, lDiff) 404 } 405 406 // Dispatch payload diff 407 dDiff := primitiveObjectDiff(t.DispatchPayload, other.DispatchPayload, nil, "DispatchPayload", contextual) 408 if dDiff != nil { 409 diff.Objects = append(diff.Objects, dDiff) 410 } 411 412 // Artifacts diff 413 diffs := primitiveObjectSetDiff( 414 interfaceSlice(t.Artifacts), 415 interfaceSlice(other.Artifacts), 416 nil, 417 "Artifact", 418 contextual) 419 if diffs != nil { 420 diff.Objects = append(diff.Objects, diffs...) 421 } 422 423 // Services diff 424 if sDiffs := serviceDiffs(t.Services, other.Services, contextual); sDiffs != nil { 425 diff.Objects = append(diff.Objects, sDiffs...) 426 } 427 428 // Vault diff 429 vDiff := vaultDiff(t.Vault, other.Vault, contextual) 430 if vDiff != nil { 431 diff.Objects = append(diff.Objects, vDiff) 432 } 433 434 // Template diff 435 tmplDiffs := primitiveObjectSetDiff( 436 interfaceSlice(t.Templates), 437 interfaceSlice(other.Templates), 438 nil, 439 "Template", 440 contextual) 441 if tmplDiffs != nil { 442 diff.Objects = append(diff.Objects, tmplDiffs...) 443 } 444 445 return diff, nil 446 } 447 448 func (t *TaskDiff) GoString() string { 449 var out string 450 if len(t.Annotations) == 0 { 451 out = fmt.Sprintf("Task %q (%s):\n", t.Name, t.Type) 452 } else { 453 out = fmt.Sprintf("Task %q (%s) (%s):\n", t.Name, t.Type, strings.Join(t.Annotations, ",")) 454 } 455 456 for _, f := range t.Fields { 457 out += fmt.Sprintf("%#v\n", f) 458 } 459 460 for _, o := range t.Objects { 461 out += fmt.Sprintf("%#v\n", o) 462 } 463 464 return out 465 } 466 467 // taskDiffs diffs a set of tasks. If contextual diff is enabled, unchanged 468 // fields within objects nested in the tasks will be returned. 469 func taskDiffs(old, new []*Task, contextual bool) ([]*TaskDiff, error) { 470 oldMap := make(map[string]*Task, len(old)) 471 newMap := make(map[string]*Task, len(new)) 472 for _, o := range old { 473 oldMap[o.Name] = o 474 } 475 for _, n := range new { 476 newMap[n.Name] = n 477 } 478 479 var diffs []*TaskDiff 480 for name, oldGroup := range oldMap { 481 // Diff the same, deleted and edited 482 diff, err := oldGroup.Diff(newMap[name], contextual) 483 if err != nil { 484 return nil, err 485 } 486 diffs = append(diffs, diff) 487 } 488 489 for name, newGroup := range newMap { 490 // Diff the added 491 if old, ok := oldMap[name]; !ok { 492 diff, err := old.Diff(newGroup, contextual) 493 if err != nil { 494 return nil, err 495 } 496 diffs = append(diffs, diff) 497 } 498 } 499 500 sort.Sort(TaskDiffs(diffs)) 501 return diffs, nil 502 } 503 504 // For sorting TaskDiffs 505 type TaskDiffs []*TaskDiff 506 507 func (t TaskDiffs) Len() int { return len(t) } 508 func (t TaskDiffs) Swap(i, j int) { t[i], t[j] = t[j], t[i] } 509 func (t TaskDiffs) Less(i, j int) bool { return t[i].Name < t[j].Name } 510 511 // serviceDiff returns the diff of two service objects. If contextual diff is 512 // enabled, all fields will be returned, even if no diff occurred. 513 func serviceDiff(old, new *Service, contextual bool) *ObjectDiff { 514 diff := &ObjectDiff{Type: DiffTypeNone, Name: "Service"} 515 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 516 517 if reflect.DeepEqual(old, new) { 518 return nil 519 } else if old == nil { 520 old = &Service{} 521 diff.Type = DiffTypeAdded 522 newPrimitiveFlat = flatmap.Flatten(new, nil, true) 523 } else if new == nil { 524 new = &Service{} 525 diff.Type = DiffTypeDeleted 526 oldPrimitiveFlat = flatmap.Flatten(old, nil, true) 527 } else { 528 diff.Type = DiffTypeEdited 529 oldPrimitiveFlat = flatmap.Flatten(old, nil, true) 530 newPrimitiveFlat = flatmap.Flatten(new, nil, true) 531 } 532 533 // Diff the primitive fields. 534 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) 535 536 // Checks diffs 537 if cDiffs := serviceCheckDiffs(old.Checks, new.Checks, contextual); cDiffs != nil { 538 diff.Objects = append(diff.Objects, cDiffs...) 539 } 540 541 return diff 542 } 543 544 // serviceDiffs diffs a set of services. If contextual diff is enabled, unchanged 545 // fields within objects nested in the tasks will be returned. 546 func serviceDiffs(old, new []*Service, contextual bool) []*ObjectDiff { 547 oldMap := make(map[string]*Service, len(old)) 548 newMap := make(map[string]*Service, len(new)) 549 for _, o := range old { 550 oldMap[o.Name] = o 551 } 552 for _, n := range new { 553 newMap[n.Name] = n 554 } 555 556 var diffs []*ObjectDiff 557 for name, oldService := range oldMap { 558 // Diff the same, deleted and edited 559 if diff := serviceDiff(oldService, newMap[name], contextual); diff != nil { 560 diffs = append(diffs, diff) 561 } 562 } 563 564 for name, newService := range newMap { 565 // Diff the added 566 if old, ok := oldMap[name]; !ok { 567 if diff := serviceDiff(old, newService, contextual); diff != nil { 568 diffs = append(diffs, diff) 569 } 570 } 571 } 572 573 sort.Sort(ObjectDiffs(diffs)) 574 return diffs 575 } 576 577 // serviceCheckDiff returns the diff of two service check objects. If contextual 578 // diff is enabled, all fields will be returned, even if no diff occurred. 579 func serviceCheckDiff(old, new *ServiceCheck, contextual bool) *ObjectDiff { 580 diff := &ObjectDiff{Type: DiffTypeNone, Name: "Check"} 581 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 582 583 if reflect.DeepEqual(old, new) { 584 return nil 585 } else if old == nil { 586 old = &ServiceCheck{} 587 diff.Type = DiffTypeAdded 588 newPrimitiveFlat = flatmap.Flatten(new, nil, true) 589 } else if new == nil { 590 new = &ServiceCheck{} 591 diff.Type = DiffTypeDeleted 592 oldPrimitiveFlat = flatmap.Flatten(old, nil, true) 593 } else { 594 diff.Type = DiffTypeEdited 595 oldPrimitiveFlat = flatmap.Flatten(old, nil, true) 596 newPrimitiveFlat = flatmap.Flatten(new, nil, true) 597 } 598 599 // Diff the primitive fields. 600 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) 601 602 // Diff Header 603 if headerDiff := checkHeaderDiff(old.Header, new.Header, contextual); headerDiff != nil { 604 diff.Objects = append(diff.Objects, headerDiff) 605 } 606 607 // Diff check_restart 608 if crDiff := checkRestartDiff(old.CheckRestart, new.CheckRestart, contextual); crDiff != nil { 609 diff.Objects = append(diff.Objects, crDiff) 610 } 611 612 return diff 613 } 614 615 // checkHeaderDiff returns the diff of two service check header objects. If 616 // contextual diff is enabled, all fields will be returned, even if no diff 617 // occurred. 618 func checkHeaderDiff(old, new map[string][]string, contextual bool) *ObjectDiff { 619 diff := &ObjectDiff{Type: DiffTypeNone, Name: "Header"} 620 var oldFlat, newFlat map[string]string 621 622 if reflect.DeepEqual(old, new) { 623 return nil 624 } else if len(old) == 0 { 625 diff.Type = DiffTypeAdded 626 newFlat = flatmap.Flatten(new, nil, false) 627 } else if len(new) == 0 { 628 diff.Type = DiffTypeDeleted 629 oldFlat = flatmap.Flatten(old, nil, false) 630 } else { 631 diff.Type = DiffTypeEdited 632 oldFlat = flatmap.Flatten(old, nil, false) 633 newFlat = flatmap.Flatten(new, nil, false) 634 } 635 636 diff.Fields = fieldDiffs(oldFlat, newFlat, contextual) 637 return diff 638 } 639 640 // checkRestartDiff returns the diff of two service check check_restart 641 // objects. If contextual diff is enabled, all fields will be returned, even if 642 // no diff occurred. 643 func checkRestartDiff(old, new *CheckRestart, contextual bool) *ObjectDiff { 644 diff := &ObjectDiff{Type: DiffTypeNone, Name: "CheckRestart"} 645 var oldFlat, newFlat map[string]string 646 647 if reflect.DeepEqual(old, new) { 648 return nil 649 } else if old == nil { 650 diff.Type = DiffTypeAdded 651 newFlat = flatmap.Flatten(new, nil, true) 652 diff.Type = DiffTypeAdded 653 } else if new == nil { 654 diff.Type = DiffTypeDeleted 655 oldFlat = flatmap.Flatten(old, nil, true) 656 } else { 657 diff.Type = DiffTypeEdited 658 oldFlat = flatmap.Flatten(old, nil, true) 659 newFlat = flatmap.Flatten(new, nil, true) 660 } 661 662 diff.Fields = fieldDiffs(oldFlat, newFlat, contextual) 663 return diff 664 } 665 666 // serviceCheckDiffs diffs a set of service checks. If contextual diff is 667 // enabled, unchanged fields within objects nested in the tasks will be 668 // returned. 669 func serviceCheckDiffs(old, new []*ServiceCheck, contextual bool) []*ObjectDiff { 670 oldMap := make(map[string]*ServiceCheck, len(old)) 671 newMap := make(map[string]*ServiceCheck, len(new)) 672 for _, o := range old { 673 oldMap[o.Name] = o 674 } 675 for _, n := range new { 676 newMap[n.Name] = n 677 } 678 679 var diffs []*ObjectDiff 680 for name, oldCheck := range oldMap { 681 // Diff the same, deleted and edited 682 if diff := serviceCheckDiff(oldCheck, newMap[name], contextual); diff != nil { 683 diffs = append(diffs, diff) 684 } 685 } 686 687 for name, newCheck := range newMap { 688 // Diff the added 689 if old, ok := oldMap[name]; !ok { 690 if diff := serviceCheckDiff(old, newCheck, contextual); diff != nil { 691 diffs = append(diffs, diff) 692 } 693 } 694 } 695 696 sort.Sort(ObjectDiffs(diffs)) 697 return diffs 698 } 699 700 // vaultDiff returns the diff of two vault objects. If contextual diff is 701 // enabled, all fields will be returned, even if no diff occurred. 702 func vaultDiff(old, new *Vault, contextual bool) *ObjectDiff { 703 diff := &ObjectDiff{Type: DiffTypeNone, Name: "Vault"} 704 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 705 706 if reflect.DeepEqual(old, new) { 707 return nil 708 } else if old == nil { 709 old = &Vault{} 710 diff.Type = DiffTypeAdded 711 newPrimitiveFlat = flatmap.Flatten(new, nil, true) 712 } else if new == nil { 713 new = &Vault{} 714 diff.Type = DiffTypeDeleted 715 oldPrimitiveFlat = flatmap.Flatten(old, nil, true) 716 } else { 717 diff.Type = DiffTypeEdited 718 oldPrimitiveFlat = flatmap.Flatten(old, nil, true) 719 newPrimitiveFlat = flatmap.Flatten(new, nil, true) 720 } 721 722 // Diff the primitive fields. 723 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) 724 725 // Policies diffs 726 if setDiff := stringSetDiff(old.Policies, new.Policies, "Policies", contextual); setDiff != nil { 727 diff.Objects = append(diff.Objects, setDiff) 728 } 729 730 return diff 731 } 732 733 // parameterizedJobDiff returns the diff of two parameterized job objects. If 734 // contextual diff is enabled, all fields will be returned, even if no diff 735 // occurred. 736 func parameterizedJobDiff(old, new *ParameterizedJobConfig, contextual bool) *ObjectDiff { 737 diff := &ObjectDiff{Type: DiffTypeNone, Name: "ParameterizedJob"} 738 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 739 740 if reflect.DeepEqual(old, new) { 741 return nil 742 } else if old == nil { 743 old = &ParameterizedJobConfig{} 744 diff.Type = DiffTypeAdded 745 newPrimitiveFlat = flatmap.Flatten(new, nil, true) 746 } else if new == nil { 747 new = &ParameterizedJobConfig{} 748 diff.Type = DiffTypeDeleted 749 oldPrimitiveFlat = flatmap.Flatten(old, nil, true) 750 } else { 751 diff.Type = DiffTypeEdited 752 oldPrimitiveFlat = flatmap.Flatten(old, nil, true) 753 newPrimitiveFlat = flatmap.Flatten(new, nil, true) 754 } 755 756 // Diff the primitive fields. 757 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) 758 759 // Meta diffs 760 if optionalDiff := stringSetDiff(old.MetaOptional, new.MetaOptional, "MetaOptional", contextual); optionalDiff != nil { 761 diff.Objects = append(diff.Objects, optionalDiff) 762 } 763 764 if requiredDiff := stringSetDiff(old.MetaRequired, new.MetaRequired, "MetaRequired", contextual); requiredDiff != nil { 765 diff.Objects = append(diff.Objects, requiredDiff) 766 } 767 768 return diff 769 } 770 771 // Diff returns a diff of two resource objects. If contextual diff is enabled, 772 // non-changed fields will still be returned. 773 func (r *Resources) Diff(other *Resources, contextual bool) *ObjectDiff { 774 diff := &ObjectDiff{Type: DiffTypeNone, Name: "Resources"} 775 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 776 777 if reflect.DeepEqual(r, other) { 778 return nil 779 } else if r == nil { 780 r = &Resources{} 781 diff.Type = DiffTypeAdded 782 newPrimitiveFlat = flatmap.Flatten(other, nil, true) 783 } else if other == nil { 784 other = &Resources{} 785 diff.Type = DiffTypeDeleted 786 oldPrimitiveFlat = flatmap.Flatten(r, nil, true) 787 } else { 788 diff.Type = DiffTypeEdited 789 oldPrimitiveFlat = flatmap.Flatten(r, nil, true) 790 newPrimitiveFlat = flatmap.Flatten(other, nil, true) 791 } 792 793 // Diff the primitive fields. 794 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) 795 796 // Network Resources diff 797 if nDiffs := networkResourceDiffs(r.Networks, other.Networks, contextual); nDiffs != nil { 798 diff.Objects = append(diff.Objects, nDiffs...) 799 } 800 801 return diff 802 } 803 804 // Diff returns a diff of two network resources. If contextual diff is enabled, 805 // non-changed fields will still be returned. 806 func (r *NetworkResource) Diff(other *NetworkResource, contextual bool) *ObjectDiff { 807 diff := &ObjectDiff{Type: DiffTypeNone, Name: "Network"} 808 var oldPrimitiveFlat, newPrimitiveFlat map[string]string 809 filter := []string{"Device", "CIDR", "IP"} 810 811 if reflect.DeepEqual(r, other) { 812 return nil 813 } else if r == nil { 814 r = &NetworkResource{} 815 diff.Type = DiffTypeAdded 816 newPrimitiveFlat = flatmap.Flatten(other, filter, true) 817 } else if other == nil { 818 other = &NetworkResource{} 819 diff.Type = DiffTypeDeleted 820 oldPrimitiveFlat = flatmap.Flatten(r, filter, true) 821 } else { 822 diff.Type = DiffTypeEdited 823 oldPrimitiveFlat = flatmap.Flatten(r, filter, true) 824 newPrimitiveFlat = flatmap.Flatten(other, filter, true) 825 } 826 827 // Diff the primitive fields. 828 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) 829 830 // Port diffs 831 resPorts := portDiffs(r.ReservedPorts, other.ReservedPorts, false, contextual) 832 dynPorts := portDiffs(r.DynamicPorts, other.DynamicPorts, true, contextual) 833 if resPorts != nil { 834 diff.Objects = append(diff.Objects, resPorts...) 835 } 836 if dynPorts != nil { 837 diff.Objects = append(diff.Objects, dynPorts...) 838 } 839 840 return diff 841 } 842 843 // networkResourceDiffs diffs a set of NetworkResources. If contextual diff is enabled, 844 // non-changed fields will still be returned. 845 func networkResourceDiffs(old, new []*NetworkResource, contextual bool) []*ObjectDiff { 846 makeSet := func(objects []*NetworkResource) map[string]*NetworkResource { 847 objMap := make(map[string]*NetworkResource, len(objects)) 848 for _, obj := range objects { 849 hash, err := hashstructure.Hash(obj, nil) 850 if err != nil { 851 panic(err) 852 } 853 objMap[fmt.Sprintf("%d", hash)] = obj 854 } 855 856 return objMap 857 } 858 859 oldSet := makeSet(old) 860 newSet := makeSet(new) 861 862 var diffs []*ObjectDiff 863 for k, oldV := range oldSet { 864 if newV, ok := newSet[k]; !ok { 865 if diff := oldV.Diff(newV, contextual); diff != nil { 866 diffs = append(diffs, diff) 867 } 868 } 869 } 870 for k, newV := range newSet { 871 if oldV, ok := oldSet[k]; !ok { 872 if diff := oldV.Diff(newV, contextual); diff != nil { 873 diffs = append(diffs, diff) 874 } 875 } 876 } 877 878 sort.Sort(ObjectDiffs(diffs)) 879 return diffs 880 881 } 882 883 // portDiffs returns the diff of two sets of ports. The dynamic flag marks the 884 // set of ports as being Dynamic ports versus Static ports. If contextual diff is enabled, 885 // non-changed fields will still be returned. 886 func portDiffs(old, new []Port, dynamic bool, contextual bool) []*ObjectDiff { 887 makeSet := func(ports []Port) map[string]Port { 888 portMap := make(map[string]Port, len(ports)) 889 for _, port := range ports { 890 portMap[port.Label] = port 891 } 892 893 return portMap 894 } 895 896 oldPorts := makeSet(old) 897 newPorts := makeSet(new) 898 899 var filter []string 900 name := "Static Port" 901 if dynamic { 902 filter = []string{"Value"} 903 name = "Dynamic Port" 904 } 905 906 var diffs []*ObjectDiff 907 for portLabel, oldPort := range oldPorts { 908 // Diff the same, deleted and edited 909 if newPort, ok := newPorts[portLabel]; ok { 910 diff := primitiveObjectDiff(oldPort, newPort, filter, name, contextual) 911 if diff != nil { 912 diffs = append(diffs, diff) 913 } 914 } else { 915 diff := primitiveObjectDiff(oldPort, nil, filter, name, contextual) 916 if diff != nil { 917 diffs = append(diffs, diff) 918 } 919 } 920 } 921 for label, newPort := range newPorts { 922 // Diff the added 923 if _, ok := oldPorts[label]; !ok { 924 diff := primitiveObjectDiff(nil, newPort, filter, name, contextual) 925 if diff != nil { 926 diffs = append(diffs, diff) 927 } 928 } 929 } 930 931 sort.Sort(ObjectDiffs(diffs)) 932 return diffs 933 934 } 935 936 // configDiff returns the diff of two Task Config objects. If contextual diff is 937 // enabled, all fields will be returned, even if no diff occurred. 938 func configDiff(old, new map[string]interface{}, contextual bool) *ObjectDiff { 939 diff := &ObjectDiff{Type: DiffTypeNone, Name: "Config"} 940 if reflect.DeepEqual(old, new) { 941 return nil 942 } else if len(old) == 0 { 943 diff.Type = DiffTypeAdded 944 } else if len(new) == 0 { 945 diff.Type = DiffTypeDeleted 946 } else { 947 diff.Type = DiffTypeEdited 948 } 949 950 // Diff the primitive fields. 951 oldPrimitiveFlat := flatmap.Flatten(old, nil, false) 952 newPrimitiveFlat := flatmap.Flatten(new, nil, false) 953 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) 954 return diff 955 } 956 957 // ObjectDiff contains the diff of two generic objects. 958 type ObjectDiff struct { 959 Type DiffType 960 Name string 961 Fields []*FieldDiff 962 Objects []*ObjectDiff 963 } 964 965 func (o *ObjectDiff) GoString() string { 966 out := fmt.Sprintf("\n%q (%s) {\n", o.Name, o.Type) 967 for _, f := range o.Fields { 968 out += fmt.Sprintf("%#v\n", f) 969 } 970 for _, o := range o.Objects { 971 out += fmt.Sprintf("%#v\n", o) 972 } 973 out += "}" 974 return out 975 } 976 977 func (o *ObjectDiff) Less(other *ObjectDiff) bool { 978 if reflect.DeepEqual(o, other) { 979 return false 980 } else if other == nil { 981 return false 982 } else if o == nil { 983 return true 984 } 985 986 if o.Name != other.Name { 987 return o.Name < other.Name 988 } 989 990 if o.Type != other.Type { 991 return o.Type.Less(other.Type) 992 } 993 994 if lO, lOther := len(o.Fields), len(other.Fields); lO != lOther { 995 return lO < lOther 996 } 997 998 if lO, lOther := len(o.Objects), len(other.Objects); lO != lOther { 999 return lO < lOther 1000 } 1001 1002 // Check each field 1003 sort.Sort(FieldDiffs(o.Fields)) 1004 sort.Sort(FieldDiffs(other.Fields)) 1005 1006 for i, oV := range o.Fields { 1007 if oV.Less(other.Fields[i]) { 1008 return true 1009 } 1010 } 1011 1012 // Check each object 1013 sort.Sort(ObjectDiffs(o.Objects)) 1014 sort.Sort(ObjectDiffs(other.Objects)) 1015 for i, oV := range o.Objects { 1016 if oV.Less(other.Objects[i]) { 1017 return true 1018 } 1019 } 1020 1021 return false 1022 } 1023 1024 // For sorting ObjectDiffs 1025 type ObjectDiffs []*ObjectDiff 1026 1027 func (o ObjectDiffs) Len() int { return len(o) } 1028 func (o ObjectDiffs) Swap(i, j int) { o[i], o[j] = o[j], o[i] } 1029 func (o ObjectDiffs) Less(i, j int) bool { return o[i].Less(o[j]) } 1030 1031 type FieldDiff struct { 1032 Type DiffType 1033 Name string 1034 Old, New string 1035 Annotations []string 1036 } 1037 1038 // fieldDiff returns a FieldDiff if old and new are different otherwise, it 1039 // returns nil. If contextual diff is enabled, even non-changed fields will be 1040 // returned. 1041 func fieldDiff(old, new, name string, contextual bool) *FieldDiff { 1042 diff := &FieldDiff{Name: name, Type: DiffTypeNone} 1043 if old == new { 1044 if !contextual { 1045 return nil 1046 } 1047 diff.Old, diff.New = old, new 1048 return diff 1049 } 1050 1051 if old == "" { 1052 diff.Type = DiffTypeAdded 1053 diff.New = new 1054 } else if new == "" { 1055 diff.Type = DiffTypeDeleted 1056 diff.Old = old 1057 } else { 1058 diff.Type = DiffTypeEdited 1059 diff.Old = old 1060 diff.New = new 1061 } 1062 return diff 1063 } 1064 1065 func (f *FieldDiff) GoString() string { 1066 out := fmt.Sprintf("%q (%s): %q => %q", f.Name, f.Type, f.Old, f.New) 1067 if len(f.Annotations) != 0 { 1068 out += fmt.Sprintf(" (%s)", strings.Join(f.Annotations, ", ")) 1069 } 1070 1071 return out 1072 } 1073 1074 func (f *FieldDiff) Less(other *FieldDiff) bool { 1075 if reflect.DeepEqual(f, other) { 1076 return false 1077 } else if other == nil { 1078 return false 1079 } else if f == nil { 1080 return true 1081 } 1082 1083 if f.Name != other.Name { 1084 return f.Name < other.Name 1085 } else if f.Old != other.Old { 1086 return f.Old < other.Old 1087 } 1088 1089 return f.New < other.New 1090 } 1091 1092 // For sorting FieldDiffs 1093 type FieldDiffs []*FieldDiff 1094 1095 func (f FieldDiffs) Len() int { return len(f) } 1096 func (f FieldDiffs) Swap(i, j int) { f[i], f[j] = f[j], f[i] } 1097 func (f FieldDiffs) Less(i, j int) bool { return f[i].Less(f[j]) } 1098 1099 // fieldDiffs takes a map of field names to their values and returns a set of 1100 // field diffs. If contextual diff is enabled, even non-changed fields will be 1101 // returned. 1102 func fieldDiffs(old, new map[string]string, contextual bool) []*FieldDiff { 1103 var diffs []*FieldDiff 1104 visited := make(map[string]struct{}) 1105 for k, oldV := range old { 1106 visited[k] = struct{}{} 1107 newV := new[k] 1108 if diff := fieldDiff(oldV, newV, k, contextual); diff != nil { 1109 diffs = append(diffs, diff) 1110 } 1111 } 1112 1113 for k, newV := range new { 1114 if _, ok := visited[k]; !ok { 1115 if diff := fieldDiff("", newV, k, contextual); diff != nil { 1116 diffs = append(diffs, diff) 1117 } 1118 } 1119 } 1120 1121 sort.Sort(FieldDiffs(diffs)) 1122 return diffs 1123 } 1124 1125 // stringSetDiff diffs two sets of strings with the given name. 1126 func stringSetDiff(old, new []string, name string, contextual bool) *ObjectDiff { 1127 oldMap := make(map[string]struct{}, len(old)) 1128 newMap := make(map[string]struct{}, len(new)) 1129 for _, o := range old { 1130 oldMap[o] = struct{}{} 1131 } 1132 for _, n := range new { 1133 newMap[n] = struct{}{} 1134 } 1135 if reflect.DeepEqual(oldMap, newMap) && !contextual { 1136 return nil 1137 } 1138 1139 diff := &ObjectDiff{Name: name} 1140 var added, removed bool 1141 for k := range oldMap { 1142 if _, ok := newMap[k]; !ok { 1143 diff.Fields = append(diff.Fields, fieldDiff(k, "", name, contextual)) 1144 removed = true 1145 } else if contextual { 1146 diff.Fields = append(diff.Fields, fieldDiff(k, k, name, contextual)) 1147 } 1148 } 1149 1150 for k := range newMap { 1151 if _, ok := oldMap[k]; !ok { 1152 diff.Fields = append(diff.Fields, fieldDiff("", k, name, contextual)) 1153 added = true 1154 } 1155 } 1156 1157 sort.Sort(FieldDiffs(diff.Fields)) 1158 1159 // Determine the type 1160 if added && removed { 1161 diff.Type = DiffTypeEdited 1162 } else if added { 1163 diff.Type = DiffTypeAdded 1164 } else if removed { 1165 diff.Type = DiffTypeDeleted 1166 } else { 1167 // Diff of an empty set 1168 if len(diff.Fields) == 0 { 1169 return nil 1170 } 1171 1172 diff.Type = DiffTypeNone 1173 } 1174 1175 return diff 1176 } 1177 1178 // primitiveObjectDiff returns a diff of the passed objects' primitive fields. 1179 // The filter field can be used to exclude fields from the diff. The name is the 1180 // name of the objects. If contextual is set, non-changed fields will also be 1181 // stored in the object diff. 1182 func primitiveObjectDiff(old, new interface{}, filter []string, name string, contextual bool) *ObjectDiff { 1183 oldPrimitiveFlat := flatmap.Flatten(old, filter, true) 1184 newPrimitiveFlat := flatmap.Flatten(new, filter, true) 1185 delete(oldPrimitiveFlat, "") 1186 delete(newPrimitiveFlat, "") 1187 1188 diff := &ObjectDiff{Name: name} 1189 diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) 1190 1191 var added, deleted, edited bool 1192 for _, f := range diff.Fields { 1193 switch f.Type { 1194 case DiffTypeEdited: 1195 edited = true 1196 break 1197 case DiffTypeDeleted: 1198 deleted = true 1199 case DiffTypeAdded: 1200 added = true 1201 } 1202 } 1203 1204 if edited || added && deleted { 1205 diff.Type = DiffTypeEdited 1206 } else if added { 1207 diff.Type = DiffTypeAdded 1208 } else if deleted { 1209 diff.Type = DiffTypeDeleted 1210 } else { 1211 return nil 1212 } 1213 1214 return diff 1215 } 1216 1217 // primitiveObjectSetDiff does a set difference of the old and new sets. The 1218 // filter parameter can be used to filter a set of primitive fields in the 1219 // passed structs. The name corresponds to the name of the passed objects. If 1220 // contextual diff is enabled, objects' primitive fields will be returned even if 1221 // no diff exists. 1222 func primitiveObjectSetDiff(old, new []interface{}, filter []string, name string, contextual bool) []*ObjectDiff { 1223 makeSet := func(objects []interface{}) map[string]interface{} { 1224 objMap := make(map[string]interface{}, len(objects)) 1225 for _, obj := range objects { 1226 hash, err := hashstructure.Hash(obj, nil) 1227 if err != nil { 1228 panic(err) 1229 } 1230 objMap[fmt.Sprintf("%d", hash)] = obj 1231 } 1232 1233 return objMap 1234 } 1235 1236 oldSet := makeSet(old) 1237 newSet := makeSet(new) 1238 1239 var diffs []*ObjectDiff 1240 for k, v := range oldSet { 1241 // Deleted 1242 if _, ok := newSet[k]; !ok { 1243 diffs = append(diffs, primitiveObjectDiff(v, nil, filter, name, contextual)) 1244 } 1245 } 1246 for k, v := range newSet { 1247 // Added 1248 if _, ok := oldSet[k]; !ok { 1249 diffs = append(diffs, primitiveObjectDiff(nil, v, filter, name, contextual)) 1250 } 1251 } 1252 1253 sort.Sort(ObjectDiffs(diffs)) 1254 return diffs 1255 } 1256 1257 // interfaceSlice is a helper method that takes a slice of typed elements and 1258 // returns a slice of interface. This method will panic if given a non-slice 1259 // input. 1260 func interfaceSlice(slice interface{}) []interface{} { 1261 s := reflect.ValueOf(slice) 1262 if s.Kind() != reflect.Slice { 1263 panic("InterfaceSlice() given a non-slice type") 1264 } 1265 1266 ret := make([]interface{}, s.Len()) 1267 1268 for i := 0; i < s.Len(); i++ { 1269 ret[i] = s.Index(i).Interface() 1270 } 1271 1272 return ret 1273 }