Skip to content

refactor: migrate calls to deprecated Poll* functions #4373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions core/clustersmngr/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ func (cf *clustersManager) watchClusters(ctx context.Context) {

cf.initialClustersLoad <- true

//nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812
if err := wait.PollImmediateInfinite(watchClustersFrequency, func() (bool, error) {
if err := wait.PollUntilContextCancel(ctx, watchClustersFrequency, true, func(ctx context.Context) (bool, error) {
if err := cf.UpdateClusters(ctx); err != nil {
cf.log.Error(err, "Failed to update clusters")
}
Expand Down Expand Up @@ -311,8 +310,7 @@ func (cf *clustersManager) watchNamespaces(ctx context.Context) {
// waits the first load of cluster to start watching namespaces
<-cf.initialClustersLoad

//nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812
if err := wait.PollImmediateInfinite(watchNamespaceFrequency, func() (bool, error) {
if err := wait.PollUntilContextCancel(ctx, watchNamespaceFrequency, true, func(ctx context.Context) (bool, error) {
if err := cf.UpdateNamespaces(ctx); err != nil {
if merr, ok := err.(*multierror.Error); ok {
for _, cerr := range merr.Errors {
Expand Down
14 changes: 7 additions & 7 deletions core/fluxsync/fluxsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func RequestReconciliation(ctx context.Context, k client.Client, name client.Obj

// WaitForSync polls the k8s API until the resources is sync'd, and times out eventually.
func WaitForSync(ctx context.Context, c client.Client, key client.ObjectKey, obj Reconcilable) error {
//nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812
if err := wait.PollImmediate(
if err := wait.PollUntilContextTimeout(
ctx,
k8sPollInterval,
k8sTimeout,
checkResourceSync(ctx, c, key, obj, obj.GetLastHandledReconcileRequest()),
true,
checkResourceSync(c, key, obj, obj.GetLastHandledReconcileRequest()),
); err != nil {
//nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812
if errors.Is(err, wait.ErrWaitTimeout) {
if wait.Interrupted(err) {
return errors.New("sync request timed out. The sync operation may still be in progress")
}

Expand All @@ -63,8 +63,8 @@ func WaitForSync(ctx context.Context, c client.Client, key client.ObjectKey, obj
return nil
}

func checkResourceSync(ctx context.Context, c client.Client, name client.ObjectKey, obj Reconcilable, lastReconcile string) func() (bool, error) {
return func() (bool, error) {
func checkResourceSync(c client.Client, name client.ObjectKey, obj Reconcilable, lastReconcile string) func(context.Context) (bool, error) {
return func(ctx context.Context) (bool, error) {
err := c.Get(ctx, name, obj.AsClientObject())
if err != nil {
return false, err
Expand Down
2 changes: 1 addition & 1 deletion core/server/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (
// installed or not on that cluster.
func (cs *coreServer) IsCRDAvailable(ctx context.Context, msg *pb.IsCRDAvailableRequest) (*pb.IsCRDAvailableResponse, error) {
return &pb.IsCRDAvailableResponse{
Clusters: cs.crd.IsAvailableOnClusters(msg.Name),
Clusters: cs.crd.IsAvailableOnClusters(ctx, msg.Name),
}, nil
}
7 changes: 4 additions & 3 deletions core/server/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (

func TestIsAvailable(t *testing.T) {
g := NewGomegaWithT(t)
c := makeGRPCServer(k8sEnv.Rest, t)

ctx := context.Background()

c := makeGRPCServer(ctx, k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).NotTo(HaveOccurred())

ctx := context.Background()

_, err = client.New(k8sEnv.Rest, client.Options{
Scheme: scheme,
})
Expand Down
2 changes: 1 addition & 1 deletion core/server/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestListEvents(t *testing.T) {

ctx := context.Background()

c := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(ctx, k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())
Expand Down
4 changes: 3 additions & 1 deletion core/server/featureflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
func TestGetFeatureFlags(t *testing.T) {
RegisterFailHandler(Fail)

ctx := context.Background()

featureflags.Set("this is a flag", "you won't find it anywhere else")

scheme, err := kube.CreateScheme()
Expand All @@ -42,7 +44,7 @@ func TestGetFeatureFlags(t *testing.T) {

cfg, err := server.NewCoreConfig(logr.Discard(), &rest.Config{}, "test", clustersManager, hc)
Expect(err).NotTo(HaveOccurred())
coreSrv, err := server.NewCoreServer(cfg)
coreSrv, err := server.NewCoreServer(ctx, cfg)
Expect(err).NotTo(HaveOccurred())

resp, err := coreSrv.GetFeatureFlags(context.Background(), &pb.GetFeatureFlagsRequest{})
Expand Down
14 changes: 7 additions & 7 deletions core/server/fluxruntime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestGetReconciledObjects(t *testing.T) {

ctx := context.Background()

c := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(ctx, k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestGetReconciledObjectsWithSecret(t *testing.T) {

ctx := context.Background()

c := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(ctx, k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())
Expand Down Expand Up @@ -309,7 +309,7 @@ func TestGetChildObjects(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(&ns, deployment, rs).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetChildObjects(ctx, &pb.GetChildObjectsRequest{
ParentUid: string(deployment.UID),
Expand Down Expand Up @@ -385,7 +385,7 @@ func TestListFluxRuntimeObjects(t *testing.T) {
g.Expect(err).To(BeNil())
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(tt.objects...).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)
res, err := c.ListFluxRuntimeObjects(ctx, &pb.ListFluxRuntimeObjectsRequest{})
g.Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -454,7 +454,7 @@ func TestListRuntimeObjects(t *testing.T) {
g.Expect(err).To(BeNil())
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(tt.objects...).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListRuntimeObjects(ctx, &pb.ListRuntimeObjectsRequest{})

Expand Down Expand Up @@ -521,7 +521,7 @@ func TestListFluxCrds(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(crd1, crd2).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListFluxCrds(ctx, &pb.ListFluxCrdsRequest{})

Expand Down Expand Up @@ -579,7 +579,7 @@ func TestListRuntimeCrds(t *testing.T) {
g.Expect(err).To(BeNil())
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(tt.objects...).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListRuntimeCrds(ctx, &pb.ListRuntimeCrdsRequest{})
g.Expect(err).NotTo(HaveOccurred())
Expand Down
10 changes: 5 additions & 5 deletions core/server/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestGetInventoryKustomization(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(&ns, &anotherNs, kust, deployment, rs).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetInventory(ctx, &pb.GetInventoryRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestGetBlankInventoryKustomization(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(kust, deployment).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetInventory(ctx, &pb.GetInventoryRequest{
Namespace: ns,
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestGetInventoryHelmRelease(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, helm1, secret, cm).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetInventory(ctx, &pb.GetInventoryRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestGetInventoryHelmReleaseNoNSResources(t *testing.T) {
WithRESTMapper(testrestmapper.TestOnlyStaticRESTMapper(scheme)).
WithRuntimeObjects(ns, helm1, secret, cm).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetInventory(ctx, &pb.GetInventoryRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestGetInventoryHelmReleaseWithKubeconfig(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, helm1).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetInventory(ctx, &pb.GetInventoryRequest{
Namespace: ns.Name,
Expand Down
36 changes: 18 additions & 18 deletions core/server/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestGetObject(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, kust).Build()

cfg := makeServerConfig(fakeClient, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetObject(ctx, &pb.GetObjectRequest{
Name: appName,
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestGetObjectOtherKinds(t *testing.T) {
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(&ns, dep).Build()
cfg := makeServerConfig(client, t, "")

c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

_, err = c.GetObject(ctx, &pb.GetObjectRequest{
Name: appName,
Expand All @@ -136,7 +136,7 @@ func TestGetObjectOtherKinds(t *testing.T) {
})
g.Expect(err).NotTo(HaveOccurred())

c = makeServer(cfg, t)
c = makeServer(ctx, cfg, t)

res, err := c.GetObject(ctx, &pb.GetObjectRequest{
Name: appName,
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestGetObject_HelmReleaseWithInventory(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, helm1, secret).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetObject(ctx, &pb.GetObjectRequest{
Name: helm1.Name,
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestGetObject_HelmReleaseWithCompressedInventory(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, helm1, secret).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetObject(ctx, &pb.GetObjectRequest{
Name: helm1.Name,
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestGetObject_HelmReleaseCantGetSecret(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, helm1, secret).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetObject(ctx, &pb.GetObjectRequest{
Name: helm1.Name,
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestGetObjectSecret(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, secret).Build()

cfg := makeServerConfig(fakeClient, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetObject(ctx, &pb.GetObjectRequest{
Name: secret.Name,
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestListObjectSingle(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, kust).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestListObjectMultiple(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, kust, helm1, helm2).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestListObjectSingleWithClusterName(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, kust).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -553,7 +553,7 @@ func TestListObjectMultipleWithClusterName(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, kust, helm1, helm2).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -611,7 +611,7 @@ func TestListObject_HelmReleaseWithInventory(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, helm1, secret).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -673,7 +673,7 @@ func TestListObject_HelmReleaseWithInventoryHistory(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, helm1, secret).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -719,7 +719,7 @@ func TestListObject_HelmReleaseCantGetSecret(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, helm1, secret).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Namespace: ns.Name,
Expand Down Expand Up @@ -755,7 +755,7 @@ func TestListObjectsSecret(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, secret).Build()

cfg := makeServerConfig(fakeClient, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Kind: "Secret",
Expand Down Expand Up @@ -808,7 +808,7 @@ func TestListObjectsLabels(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, deployment1, deployment2).Build()

cfg := makeServerConfig(fakeClient, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Kind: "Deployment",
Expand Down Expand Up @@ -877,7 +877,7 @@ func TestListObjectsGitOpsRunSessions(t *testing.T) {

fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, session1, session2).Build()
cfg := makeServerConfig(fakeClient, t, testCluster)
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListObjects(ctx, &pb.ListObjectsRequest{
Namespace: testNS,
Expand Down Expand Up @@ -938,7 +938,7 @@ func TestGetObjectSessionObjects(t *testing.T) {

fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(ns, kust, helm, bucket).Build()
cfg := makeServerConfig(fakeClient, t, testCluster)
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetObject(ctx, &pb.GetObjectRequest{
Name: constants.RunDevKsName,
Expand Down
4 changes: 2 additions & 2 deletions core/server/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestListPolicies(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(policy1, policy2).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.ListPolicies(ctx, &pb.ListPoliciesRequest{})

Expand Down Expand Up @@ -95,7 +95,7 @@ func TestGetPolicy(t *testing.T) {

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(policy).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)
c := makeServer(ctx, cfg, t)

res, err := c.GetPolicy(ctx, &pb.GetPolicyRequest{
PolicyName: policy.Spec.ID,
Expand Down
Loading
Loading