|
8 | 8 | "github.com/aws/aws-sdk-go-v2/aws" |
9 | 9 | "github.com/aws/aws-sdk-go-v2/service/ec2" |
10 | 10 | "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 11 | + "github.com/go-logr/logr" |
11 | 12 | "github.com/stretchr/testify/assert" |
12 | 13 | "github.com/stretchr/testify/require" |
13 | 14 | gce "google.golang.org/api/compute/v1" |
@@ -184,6 +185,7 @@ func TestReconcileAWS(t *testing.T) { |
184 | 185 |
|
185 | 186 | r := &NodeLabelController{ |
186 | 187 | Client: k8s, |
| 188 | + Logger: logr.Discard(), |
187 | 189 | Labels: tt.labelsToCopy, |
188 | 190 | Annotations: tt.annotationsToCopy, |
189 | 191 | Cloud: "aws", |
@@ -315,6 +317,7 @@ func TestReconcileGCP(t *testing.T) { |
315 | 317 |
|
316 | 318 | r := &NodeLabelController{ |
317 | 319 | Client: k8s, |
| 320 | + Logger: logr.Discard(), |
318 | 321 | Labels: tt.labelsToCopy, |
319 | 322 | Annotations: tt.annotationsToCopy, |
320 | 323 | Cloud: "gcp", |
@@ -658,3 +661,184 @@ func createNode(config mockNode) *corev1.Node { |
658 | 661 | }, |
659 | 662 | } |
660 | 663 | } |
| 664 | + |
| 665 | +// TestPredicateToReconcileFlow tests the full flow from event through predicate to reconcile. |
| 666 | +// This simulates what controller-runtime does: events are filtered by predicates, and only |
| 667 | +// if the predicate allows, reconcile is called. |
| 668 | +func TestPredicateToReconcileFlow(t *testing.T) { |
| 669 | + tests := []struct { |
| 670 | + name string |
| 671 | + monitoredLabels []string |
| 672 | + initialNode mockNode |
| 673 | + updatedNode *mockNode // nil means no update step |
| 674 | + expectReconcileOnCreate bool |
| 675 | + expectReconcileOnUpdate bool |
| 676 | + expectTagsCreated []string // tag keys we expect to be created |
| 677 | + }{ |
| 678 | + { |
| 679 | + name: "node created without monitored labels then labels added", |
| 680 | + monitoredLabels: []string{"env", "team"}, |
| 681 | + initialNode: mockNode{ |
| 682 | + Name: "node1", |
| 683 | + Labels: map[string]string{"kubernetes.io/hostname": "node1"}, |
| 684 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 685 | + }, |
| 686 | + updatedNode: &mockNode{ |
| 687 | + Name: "node1", |
| 688 | + Labels: map[string]string{"kubernetes.io/hostname": "node1", "env": "prod", "team": "platform"}, |
| 689 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 690 | + }, |
| 691 | + expectReconcileOnCreate: false, // no monitored labels yet |
| 692 | + expectReconcileOnUpdate: true, // monitored labels added |
| 693 | + expectTagsCreated: []string{"env", "team"}, |
| 694 | + }, |
| 695 | + { |
| 696 | + name: "node created with monitored labels already present", |
| 697 | + monitoredLabels: []string{"env"}, |
| 698 | + initialNode: mockNode{ |
| 699 | + Name: "node1", |
| 700 | + Labels: map[string]string{"env": "prod"}, |
| 701 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 702 | + }, |
| 703 | + updatedNode: nil, |
| 704 | + expectReconcileOnCreate: true, |
| 705 | + expectReconcileOnUpdate: false, |
| 706 | + expectTagsCreated: []string{"env"}, |
| 707 | + }, |
| 708 | + { |
| 709 | + name: "node created without labels then only some monitored labels added", |
| 710 | + monitoredLabels: []string{"env", "team", "region"}, |
| 711 | + initialNode: mockNode{ |
| 712 | + Name: "node1", |
| 713 | + Labels: map[string]string{}, |
| 714 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 715 | + }, |
| 716 | + updatedNode: &mockNode{ |
| 717 | + Name: "node1", |
| 718 | + Labels: map[string]string{"env": "prod"}, // only env, not team or region |
| 719 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 720 | + }, |
| 721 | + expectReconcileOnCreate: false, |
| 722 | + expectReconcileOnUpdate: true, |
| 723 | + expectTagsCreated: []string{"env"}, // only env should be synced |
| 724 | + }, |
| 725 | + { |
| 726 | + name: "node update that does not change monitored labels triggers resync", |
| 727 | + monitoredLabels: []string{"env"}, |
| 728 | + initialNode: mockNode{ |
| 729 | + Name: "node1", |
| 730 | + Labels: map[string]string{"env": "prod"}, |
| 731 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 732 | + }, |
| 733 | + updatedNode: &mockNode{ |
| 734 | + Name: "node1", |
| 735 | + Labels: map[string]string{"env": "prod", "unrelated": "change"}, |
| 736 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 737 | + }, |
| 738 | + expectReconcileOnCreate: true, |
| 739 | + expectReconcileOnUpdate: true, // resync: node has monitored labels |
| 740 | + expectTagsCreated: []string{"env"}, |
| 741 | + }, |
| 742 | + { |
| 743 | + name: "multiple labels added in single update", |
| 744 | + monitoredLabels: []string{"env", "team", "cost-center"}, |
| 745 | + initialNode: mockNode{ |
| 746 | + Name: "node1", |
| 747 | + Labels: map[string]string{}, |
| 748 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 749 | + }, |
| 750 | + updatedNode: &mockNode{ |
| 751 | + Name: "node1", |
| 752 | + Labels: map[string]string{ |
| 753 | + "env": "prod", |
| 754 | + "team": "platform", |
| 755 | + "cost-center": "12345", |
| 756 | + }, |
| 757 | + ProviderID: "aws:///us-east-1a/i-1234567890abcdef0", |
| 758 | + }, |
| 759 | + expectReconcileOnCreate: false, |
| 760 | + expectReconcileOnUpdate: true, |
| 761 | + expectTagsCreated: []string{"env", "team", "cost-center"}, |
| 762 | + }, |
| 763 | + } |
| 764 | + |
| 765 | + for _, tt := range tests { |
| 766 | + t.Run(tt.name, func(t *testing.T) { |
| 767 | + scheme := runtime.NewScheme() |
| 768 | + require.NoError(t, corev1.AddToScheme(scheme)) |
| 769 | + |
| 770 | + // Start with initial node in the fake client |
| 771 | + k8s := fake.NewClientBuilder(). |
| 772 | + WithScheme(scheme). |
| 773 | + WithObjects(createNode(tt.initialNode)). |
| 774 | + Build() |
| 775 | + |
| 776 | + mock := &mockEC2Client{currentTags: []types.TagDescription{}} |
| 777 | + |
| 778 | + controller := &NodeLabelController{ |
| 779 | + Client: k8s, |
| 780 | + Logger: logr.Discard(), |
| 781 | + Labels: tt.monitoredLabels, |
| 782 | + Annotations: []string{}, |
| 783 | + Cloud: "aws", |
| 784 | + EC2Client: mock, |
| 785 | + } |
| 786 | + |
| 787 | + // Simulate CREATE event |
| 788 | + initialNodeObj := createNode(tt.initialNode) |
| 789 | + createAllowed := shouldProcessNodeCreate(initialNodeObj, tt.monitoredLabels, []string{}) |
| 790 | + |
| 791 | + assert.Equal(t, tt.expectReconcileOnCreate, createAllowed, |
| 792 | + "Create predicate returned unexpected result") |
| 793 | + |
| 794 | + if createAllowed { |
| 795 | + _, err := controller.Reconcile(context.Background(), ctrl.Request{ |
| 796 | + NamespacedName: client.ObjectKey{Name: tt.initialNode.Name}, |
| 797 | + }) |
| 798 | + require.NoError(t, err) |
| 799 | + |
| 800 | + // Verify tags were created |
| 801 | + createdKeys := make([]string, 0, len(mock.createdTags)) |
| 802 | + for _, tag := range mock.createdTags { |
| 803 | + createdKeys = append(createdKeys, aws.ToString(tag.Key)) |
| 804 | + } |
| 805 | + assert.ElementsMatch(t, tt.expectTagsCreated, createdKeys, |
| 806 | + "Created tags don't match expected") |
| 807 | + } |
| 808 | + |
| 809 | + // Simulate UPDATE event if provided |
| 810 | + if tt.updatedNode != nil { |
| 811 | + // Reset mock for update test |
| 812 | + mock.createdTags = nil |
| 813 | + mock.currentTags = []types.TagDescription{} // EC2 has no tags yet (simulating missed create) |
| 814 | + |
| 815 | + // Update the node in the fake client |
| 816 | + updatedNodeObj := createNode(*tt.updatedNode) |
| 817 | + err := k8s.Update(context.Background(), updatedNodeObj) |
| 818 | + require.NoError(t, err) |
| 819 | + |
| 820 | + // Match the actual predicate logic: allow if labels changed OR if node has monitored labels (resync) |
| 821 | + updateAllowed := shouldProcessNodeUpdate(initialNodeObj, updatedNodeObj, tt.monitoredLabels, []string{}) || |
| 822 | + shouldProcessNodeCreate(updatedNodeObj, tt.monitoredLabels, []string{}) |
| 823 | + |
| 824 | + assert.Equal(t, tt.expectReconcileOnUpdate, updateAllowed, |
| 825 | + "Update predicate returned unexpected result") |
| 826 | + |
| 827 | + if updateAllowed { |
| 828 | + _, err := controller.Reconcile(context.Background(), ctrl.Request{ |
| 829 | + NamespacedName: client.ObjectKey{Name: tt.updatedNode.Name}, |
| 830 | + }) |
| 831 | + require.NoError(t, err) |
| 832 | + |
| 833 | + // Verify tags were created |
| 834 | + createdKeys := make([]string, 0, len(mock.createdTags)) |
| 835 | + for _, tag := range mock.createdTags { |
| 836 | + createdKeys = append(createdKeys, aws.ToString(tag.Key)) |
| 837 | + } |
| 838 | + assert.ElementsMatch(t, tt.expectTagsCreated, createdKeys, |
| 839 | + "Created tags on update don't match expected") |
| 840 | + } |
| 841 | + } |
| 842 | + }) |
| 843 | + } |
| 844 | +} |
0 commit comments