Skip to content

Commit 9560240

Browse files
authored
test: fix race (#2)
1 parent 6c0b394 commit 9560240

File tree

14 files changed

+129
-110
lines changed

14 files changed

+129
-110
lines changed

.github/workflows/pr-build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,5 @@ jobs:
102102
run: |
103103
kubectl -n k8sgrowthbook-system get pods
104104
kubectl -n k8sgrowthbook-system logs deploy/k8sgrowthbook-controller
105+
kubectl -n k8sgrowthbook-system logs verify
105106
kubectl -n k8sgrowthbook-system get growthbookinstance -o yaml

api/v1beta1/growthbookclient_types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"fmt"
21-
2220
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2321
)
2422

@@ -40,7 +38,7 @@ type GrowthbookClientSpec struct {
4038
// GetID returns the client ID which is the resource name if not overwritten by spec.ID
4139
func (c *GrowthbookClient) GetID() string {
4240
if c.Spec.ID == "" {
43-
return fmt.Sprintf("%s-%s", c.Name, c.Namespace)
41+
return c.Name
4442
}
4543

4644
return c.Spec.ID

api/v1beta1/growthbookfeature_types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"fmt"
21-
2220
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2321
)
2422

@@ -45,7 +43,7 @@ var (
4543
// GetID returns the feature ID which is the resource name if not overwritten by spec.ID
4644
func (f *GrowthbookFeature) GetID() string {
4745
if f.Spec.ID == "" {
48-
return fmt.Sprintf("%s-%s", f.Name, f.Namespace)
46+
return f.Name
4947
}
5048

5149
return f.Spec.ID

api/v1beta1/growthbookorganization_types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"fmt"
21-
2220
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2321
)
2422

@@ -44,7 +42,7 @@ type GrowthbookOrganizationUser struct {
4442
// GetID returns the organization ID which is the resource name if not overwritten by spec.ID
4543
func (o *GrowthbookOrganization) GetID() string {
4644
if o.Spec.ID == "" {
47-
return fmt.Sprintf("%s-%s", o.Name, o.Namespace)
45+
return o.Name
4846
}
4947

5048
return o.Spec.ID

api/v1beta1/growthbookuser_types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"fmt"
21-
2220
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2321
)
2422

@@ -35,7 +33,7 @@ type GrowthbookUserSpec struct {
3533
// GetID returns the organization ID which is the resource name if not overwritten by spec.ID
3634
func (u *GrowthbookUser) GetID() string {
3735
if u.Spec.ID == "" {
38-
return fmt.Sprintf("%s-%s", u.Name, u.Namespace)
36+
return u.Name
3937
}
4038

4139
return u.Spec.ID

config/tests/base/verify.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ spec:
1414
- /bin/sh
1515
- "-c"
1616
- |
17-
curl --fail -vvv http://growthbook-api/api/features/sdk-token | grep '{"status":200,"features":{"feature-a-k8sgrowthbook-system":{"defaultValue":true},"feature-b-k8sgrowthbook-system":{"defaultValue":false}}'
17+
curl --fail -vvv http://growthbook-api/api/features/sdk-token | grep '{"status":200,"features":{"feature-b":{"defaultValue":true},"feature-a":{"defaultValue":false}}\|{"status":200,"features":{"feature-a":{"defaultValue":true},"feature-b":{"defaultValue":false}}'
1818
resources: {}
1919
securityContext:
2020
allowPrivilegeEscalation: false
@@ -24,7 +24,3 @@ spec:
2424
runAsUser: 1000
2525
terminationMessagePath: /dev/termination-log
2626
terminationMessagePolicy: File
27-
dnsPolicy: ClusterFirst
28-
enableServiceLinks: true
29-
nodeName: rageta-control-plane
30-
preemptionPolicy: PreemptLowerPriority

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/spf13/viper v1.15.0
1111
go.mongodb.org/mongo-driver v1.11.7
1212
golang.org/x/crypto v0.1.0
13+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
1314
k8s.io/api v0.26.3
1415
k8s.io/apimachinery v0.26.3
1516
k8s.io/client-go v0.26.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
374374
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
375375
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
376376
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
377+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
378+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
377379
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
378380
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
379381
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

internal/controllers/instance_controller.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/go-logr/logr"
2828
"go.mongodb.org/mongo-driver/mongo"
2929
"go.mongodb.org/mongo-driver/mongo/options"
30+
"golang.org/x/exp/slices"
3031
corev1 "k8s.io/api/core/v1"
3132
apierrors "k8s.io/apimachinery/pkg/api/errors"
3233
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -302,12 +303,12 @@ func (r *GrowthbookInstanceReconciler) Reconcile(ctx context.Context, req ctrl.R
302303
}
303304

304305
func (r *GrowthbookInstanceReconciler) reconcile(ctx context.Context, instance v1beta1.GrowthbookInstance, logger logr.Logger) (v1beta1.GrowthbookInstance, error) {
305-
msg := "reconcile instance progressing"
306+
/*msg := "reconcile instance progressing"
306307
r.Recorder.Event(&instance, "Normal", "info", msg)
307308
instance = v1beta1.GrowthbookInstanceNotReady(instance, v1beta1.ProgressingReason, msg)
308309
if err := r.patchStatus(ctx, &instance); err != nil {
309310
return instance, err
310-
}
311+
}*/
311312

312313
var err error
313314
var usr, pw string
@@ -363,11 +364,7 @@ func (r *GrowthbookInstanceReconciler) reconcileOrganizations(ctx context.Contex
363364
}
364365

365366
for _, org := range orgs.Items {
366-
instance.Status.SubResourceCatalog = append(instance.Status.SubResourceCatalog, v1beta1.ResourceReference{
367-
Kind: org.Kind,
368-
Name: org.Name,
369-
APIVersion: org.APIVersion,
370-
})
367+
instance = updateResourceCatalog(instance, &org)
371368
}
372369

373370
for _, org := range orgs.Items {
@@ -423,11 +420,7 @@ func (r *GrowthbookInstanceReconciler) reconcileFeatures(ctx context.Context, in
423420
}
424421

425422
for _, feature := range features.Items {
426-
instance.Status.SubResourceCatalog = append(instance.Status.SubResourceCatalog, v1beta1.ResourceReference{
427-
Kind: feature.Kind,
428-
Name: feature.Name,
429-
APIVersion: feature.APIVersion,
430-
})
423+
instance = updateResourceCatalog(instance, &feature)
431424
}
432425

433426
for _, feature := range features.Items {
@@ -459,11 +452,7 @@ func (r *GrowthbookInstanceReconciler) reconcileUsers(ctx context.Context, insta
459452
}
460453

461454
for _, user := range users.Items {
462-
instance.Status.SubResourceCatalog = append(instance.Status.SubResourceCatalog, v1beta1.ResourceReference{
463-
Kind: user.Kind,
464-
Name: user.Name,
465-
APIVersion: user.APIVersion,
466-
})
455+
instance = updateResourceCatalog(instance, &user)
467456
}
468457

469458
for _, user := range users.Items {
@@ -510,11 +499,7 @@ func (r *GrowthbookInstanceReconciler) reconcileClients(ctx context.Context, ins
510499
}
511500

512501
for _, client := range clients.Items {
513-
instance.Status.SubResourceCatalog = append(instance.Status.SubResourceCatalog, v1beta1.ResourceReference{
514-
Kind: client.Kind,
515-
Name: client.Name,
516-
APIVersion: client.APIVersion,
517-
})
502+
instance = updateResourceCatalog(instance, &client)
518503
}
519504

520505
for _, client := range clients.Items {
@@ -579,6 +564,20 @@ func (r *GrowthbookInstanceReconciler) getClientToken(ctx context.Context, clien
579564
}
580565
}
581566

567+
func updateResourceCatalog(instance v1beta1.GrowthbookInstance, resource client.Object) v1beta1.GrowthbookInstance {
568+
resRef := v1beta1.ResourceReference{
569+
Kind: resource.GetObjectKind().GroupVersionKind().Kind,
570+
Name: resource.GetName(),
571+
APIVersion: fmt.Sprintf("%s/%s", resource.GetObjectKind().GroupVersionKind().Group, resource.GetObjectKind().GroupVersionKind().Version),
572+
}
573+
574+
if !slices.Contains(instance.Status.SubResourceCatalog, resRef) {
575+
instance.Status.SubResourceCatalog = append(instance.Status.SubResourceCatalog, resRef)
576+
}
577+
578+
return instance
579+
}
580+
582581
func (r *GrowthbookInstanceReconciler) getUsernamePassword(ctx context.Context, instance v1beta1.GrowthbookInstance, secretReference *v1beta1.SecretReference) (string, string, error) {
583582
if secretReference == nil {
584583
return "", "", errors.New("no secret reference provided")

0 commit comments

Comments
 (0)