@@ -55,7 +55,10 @@ func init() {
55
55
func TestTargetAllocatorReconciler_GetCollector (t * testing.T ) {
56
56
testCollector := & v1beta1.OpenTelemetryCollector {
57
57
ObjectMeta : metav1.ObjectMeta {
58
- Name : "my-instance-collector" ,
58
+ Name : "test" ,
59
+ Labels : map [string ]string {
60
+ collectorTargetAllocatorLabelName : "label-ta" ,
61
+ },
59
62
},
60
63
}
61
64
fakeClient := fake .NewFakeClient (testCollector )
@@ -105,6 +108,36 @@ func TestTargetAllocatorReconciler_GetCollector(t *testing.T) {
105
108
assert .Nil (t , collector )
106
109
assert .Errorf (t , err , "error getting owner for TargetAllocator default/test: opentelemetrycollectors.opentelemetry.io \" non_existent\" not found" )
107
110
})
111
+ t .Run ("collector attached by label" , func (t * testing.T ) {
112
+ ta := v1alpha1.TargetAllocator {
113
+ ObjectMeta : metav1.ObjectMeta {
114
+ Name : "label-ta" ,
115
+ },
116
+ }
117
+ collector , err := reconciler .getCollector (context .Background (), ta )
118
+ require .NoError (t , err )
119
+ assert .Equal (t , testCollector , collector )
120
+ })
121
+ t .Run ("multiple collectors attached by label" , func (t * testing.T ) {
122
+ testCollector2 := testCollector .DeepCopy ()
123
+ testCollector2 .SetName ("test2" )
124
+ fakeClient := fake .NewFakeClient (testCollector , testCollector2 )
125
+ reconciler := NewTargetAllocatorReconciler (
126
+ fakeClient ,
127
+ testScheme ,
128
+ record .NewFakeRecorder (10 ),
129
+ config .New (),
130
+ testLogger ,
131
+ )
132
+ ta := v1alpha1.TargetAllocator {
133
+ ObjectMeta : metav1.ObjectMeta {
134
+ Name : "label-ta" ,
135
+ },
136
+ }
137
+ collector , err := reconciler .getCollector (context .Background (), ta )
138
+ assert .Nil (t , collector )
139
+ assert .Errorf (t , err , "found multiple OpenTelemetry collectors annotated with the same Target Allocator: %s/%s" , ta .Namespace , ta .Name )
140
+ })
108
141
}
109
142
110
143
func TestGetTargetAllocatorForCollector (t * testing.T ) {
@@ -123,3 +156,23 @@ func TestGetTargetAllocatorForCollector(t *testing.T) {
123
156
}}
124
157
assert .Equal (t , expected , requests )
125
158
}
159
+
160
+ func TestGetTargetAllocatorRequestsFromLabel (t * testing.T ) {
161
+ testCollector := & v1beta1.OpenTelemetryCollector {
162
+ ObjectMeta : metav1.ObjectMeta {
163
+ Name : "test" ,
164
+ Namespace : "default" ,
165
+ Labels : map [string ]string {
166
+ collectorTargetAllocatorLabelName : "label-ta" ,
167
+ },
168
+ },
169
+ }
170
+ requests := getTargetAllocatorRequestsFromLabel (context .Background (), testCollector )
171
+ expected := []reconcile.Request {{
172
+ NamespacedName : types.NamespacedName {
173
+ Name : "label-ta" ,
174
+ Namespace : "default" ,
175
+ },
176
+ }}
177
+ assert .Equal (t , expected , requests )
178
+ }
0 commit comments