From 3de63c44b9b432a583dff7a674e92301e78bfadb Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Fri, 27 Oct 2023 11:47:07 +0200 Subject: [PATCH] refactor: refactor away from deprecated wait.Poll calls With the update, some other calls had to be updated: - `NewDiscoveryRESTMapper` expects an extra `HTTPClient` argument - `client` does not have `NewDelegatingClient` anymore, instead we can create the same resource with `client.New(...)` Resolves #3812 References: - https://github.com/weaveworks/weave-gitops/issues/3812 - https://github.com/kubernetes-sigs/controller-runtime/pull/2150 - https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0 Signed-off-by: Balazs Nadasdi --- core/clustersmngr/cluster/delegating_cache.go | 25 +++++++++++-------- core/clustersmngr/cluster/single.go | 7 +++++- go.mod | 11 -------- go.sum | 21 ++++++++-------- pkg/run/install/install_dashboard.go | 9 +++---- pkg/run/install/install_fluent_bit.go | 6 ++--- pkg/run/install/install_vcluster.go | 6 ++--- pkg/run/session/connect/connect.go | 3 +-- pkg/run/session/remove.go | 6 ++--- pkg/run/watch/setup_dev_ks.go | 12 +++------ 10 files changed, 47 insertions(+), 59 deletions(-) diff --git a/core/clustersmngr/cluster/delegating_cache.go b/core/clustersmngr/cluster/delegating_cache.go index 4060cb68d8..8c6562a32d 100644 --- a/core/clustersmngr/cluster/delegating_cache.go +++ b/core/clustersmngr/cluster/delegating_cache.go @@ -41,7 +41,12 @@ func (c *delegatingCacheCluster) GetHost() string { } func (c *delegatingCacheCluster) makeCachingClient(leafClient client.Client) (client.Client, error) { - mapper, err := apiutil.NewDiscoveryRESTMapper(c.restConfig) + httpClient, err := rest.HTTPClientFor(c.restConfig) + if err != nil { + return nil, fmt.Errorf("could not create HTTP client from config: %w", err) + } + + mapper, err := apiutil.NewDiscoveryRESTMapper(c.restConfig, httpClient) if err != nil { return nil, fmt.Errorf("could not create RESTMapper from config: %w", err) } @@ -58,16 +63,16 @@ func (c *delegatingCacheCluster) makeCachingClient(leafClient client.Client) (cl // https://github.com/kubernetes-sigs/controller-runtime/pull/2150 delegatingCache := newDelegatingCache(leafClient, cache, c.scheme) - delegatingClient, err := client.NewDelegatingClient(client.NewDelegatingClientInput{ - CacheReader: delegatingCache, - Client: leafClient, - // Non-exact field matches are not supported by the cache. - // https://github.com/kubernetes-sigs/controller-runtime/issues/612 - // TODO: Research if we can change the way we query those events so we can enable the cache for it. - UncachedObjects: []client.Object{&v1.Event{}}, - CacheUnstructured: true, + delegatingClient, err := client.New(c.restConfig, client.Options{ + Cache: &client.CacheOptions{ + Reader: delegatingCache, + // Non-exact field matches are not supported by the cache. + // https://github.com/kubernetes-sigs/controller-runtime/issues/612 + // TODO: Research if we can change the way we query those events so we can enable the cache for it. + DisableFor: []client.Object{&v1.Event{}}, + Unstructured: true, + }, }) - if err != nil { return nil, fmt.Errorf("failed creating DelegatingClient: %w", err) } diff --git a/core/clustersmngr/cluster/single.go b/core/clustersmngr/cluster/single.go index 16721cfbad..7e34d7c3d0 100644 --- a/core/clustersmngr/cluster/single.go +++ b/core/clustersmngr/cluster/single.go @@ -55,7 +55,12 @@ func (c *singleCluster) GetHost() string { } func getClientFromConfig(config *rest.Config, scheme *apiruntime.Scheme) (client.Client, error) { - mapper, err := apiutil.NewDiscoveryRESTMapper(config) + httpClient, err := rest.HTTPClientFor(config) + if err != nil { + return nil, fmt.Errorf("could not create HTTP client from config: %w", err) + } + + mapper, err := apiutil.NewDiscoveryRESTMapper(config, httpClient) if err != nil { return nil, fmt.Errorf("could not create RESTMapper from config: %w", err) } diff --git a/go.mod b/go.mod index 0988d450b4..98dc732dd0 100644 --- a/go.mod +++ b/go.mod @@ -231,14 +231,3 @@ require ( // Use patched version that fixed recursive gets, and force delete for buckets replace github.com/johannesboyne/gofakes3 => github.com/chanwit/gofakes3 v0.0.0-20220715114300-3f51f1961f7b - -// Replace k8s.io packages v0.26 to downgrade controller-runtime to v0.14.6 -replace ( - k8s.io/api => k8s.io/api v0.26.8 - k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.26.8 - k8s.io/apimachinery => k8s.io/apimachinery v0.26.8 - k8s.io/cli-runtime => k8s.io/cli-runtime v0.26.8 - k8s.io/client-go => k8s.io/client-go v0.26.8 - - sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.14.6 -) diff --git a/go.sum b/go.sum index a9226a3a5b..1880459cd6 100644 --- a/go.sum +++ b/go.sum @@ -143,7 +143,6 @@ github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55k github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= -github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc= github.com/emicklei/go-restful/v3 v3.10.0 h1:X4gma4HM7hFm6WMeAsTfqA0GOfdNoCzBIkHGoRLGXuM= github.com/emicklei/go-restful/v3 v3.10.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= @@ -1061,16 +1060,16 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= -k8s.io/api v0.26.8 h1:k2OtFmQPWfDUyAuYAwQPftVygF/vz4BMGSKnd15iddM= -k8s.io/api v0.26.8/go.mod h1:QaflR7cmG3V9lIz0VLBM+ylndNN897OAUAoJDcgwiQw= -k8s.io/apiextensions-apiserver v0.26.8 h1:ESVQ22MH6YfcpflpZMIvkgnHs/EwOgKKSCkS9AfxJOY= -k8s.io/apiextensions-apiserver v0.26.8/go.mod h1:ySo6rPc9ulNtKoZczw7ljCAdZN3DbyxLNat8wuYk4r8= -k8s.io/apimachinery v0.26.8 h1:SzpGtRX3/j/Ylg8Eg65Iobpxi9Jz4vOvI0qcBZyPVrM= -k8s.io/apimachinery v0.26.8/go.mod h1:qYzLkrQ9lhrZRh0jNKo2cfvf/R1/kQONnSiyB7NUJU0= +k8s.io/api v0.27.3 h1:yR6oQXXnUEBWEWcvPWS0jQL575KoAboQPfJAuKNrw5Y= +k8s.io/api v0.27.3/go.mod h1:C4BNvZnQOF7JA/0Xed2S+aUyJSfTGkGFxLXz9MnpIpg= +k8s.io/apiextensions-apiserver v0.27.3 h1:xAwC1iYabi+TDfpRhxh4Eapl14Hs2OftM2DN5MpgKX4= +k8s.io/apiextensions-apiserver v0.27.3/go.mod h1:BH3wJ5NsB9XE1w+R6SSVpKmYNyIiyIz9xAmBl8Mb+84= +k8s.io/apimachinery v0.27.3 h1:Ubye8oBufD04l9QnNtW05idcOe9Z3GQN8+7PqmuVcUM= +k8s.io/apimachinery v0.27.3/go.mod h1:XNfZ6xklnMCOGGFNqXG7bUrQCoR04dh/E7FprV6pb+E= k8s.io/cli-runtime v0.26.8 h1:LFiS+z20j8gt9Iyo4EsbivzrDYPRbFFj8wmpwdhy7cQ= k8s.io/cli-runtime v0.26.8/go.mod h1:j3YQ0OtQnqsQRsMWbmZrKqbOvN2OUu0K+dPffeKPVj0= -k8s.io/client-go v0.26.8 h1:pPuTYaVtLlg/7n6rqs3MsKLi4XgNaJ3rTMyS37Y5CKU= -k8s.io/client-go v0.26.8/go.mod h1:1sBQqKmdy9rWZYQnoedpc0gnRXG7kU3HrKZvBe2QbGM= +k8s.io/client-go v0.27.3 h1:7dnEGHZEJld3lYwxvLl7WoehK6lAq7GvgjxpA3nv1E8= +k8s.io/client-go v0.27.3/go.mod h1:2MBEKuTo6V1lbKy3z1euEGnhPfGZLKTS9tiJ2xodM48= k8s.io/component-base v0.27.3 h1:g078YmdcdTfrCE4fFobt7qmVXwS8J/3cI1XxRi/2+6k= k8s.io/component-base v0.27.3/go.mod h1:JNiKYcGImpQ44iwSYs6dysxzR9SxIIgQalk4HaCNVUY= k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg= @@ -1087,8 +1086,8 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sigs.k8s.io/cli-utils v0.34.0 h1:zCUitt54f0/MYj/ajVFnG6XSXMhpZ72O/3RewIchW8w= sigs.k8s.io/cli-utils v0.34.0/go.mod h1:EXyMwPMu9OL+LRnj0JEMsGG/fRvbgFadcVlSnE8RhFs= -sigs.k8s.io/controller-runtime v0.14.6 h1:oxstGVvXGNnMvY7TAESYk+lzr6S3V5VFxQ6d92KcwQA= -sigs.k8s.io/controller-runtime v0.14.6/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0= +sigs.k8s.io/controller-runtime v0.15.0 h1:ML+5Adt3qZnMSYxZ7gAverBLNPSMQEibtzAgp0UPojU= +sigs.k8s.io/controller-runtime v0.15.0/go.mod h1:7ngYvp1MLT+9GeZ+6lH3LOlcHkp/+tzA/fmHa4iq9kk= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/kustomize/api v0.12.1 h1:7YM7gW3kYBwtKvoY216ZzY+8hM+lV53LUayghNRJ0vM= diff --git a/pkg/run/install/install_dashboard.go b/pkg/run/install/install_dashboard.go index 28fe5f9096..b235c00436 100644 --- a/pkg/run/install/install_dashboard.go +++ b/pkg/run/install/install_dashboard.go @@ -228,8 +228,7 @@ func ReconcileDashboard(ctx context.Context, kubeClient client.Client, dashboard var sourceRequestedAt string - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(interval, timeout, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(_ context.Context) (bool, error) { var err error sourceRequestedAt, err = run.RequestReconciliation(ctx, kubeClient, namespacedName, gvk) @@ -240,8 +239,7 @@ func ReconcileDashboard(ctx context.Context, kubeClient client.Client, dashboard } // wait for the reconciliation of dashboard to be done - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(interval, timeout, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(_ context.Context) (bool, error) { dashboard := &sourcev1b2.HelmChart{} if err := kubeClient.Get(ctx, types.NamespacedName{ Namespace: namespace, @@ -256,8 +254,7 @@ func ReconcileDashboard(ctx context.Context, kubeClient client.Client, dashboard } // wait for dashboard to be ready - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(interval, timeout, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(_ context.Context) (bool, error) { namespacedName := types.NamespacedName{Namespace: namespace, Name: podName} var labels map[string]string = nil diff --git a/pkg/run/install/install_fluent_bit.go b/pkg/run/install/install_fluent_bit.go index e8505e9178..7126c3bfd9 100644 --- a/pkg/run/install/install_fluent_bit.go +++ b/pkg/run/install/install_fluent_bit.go @@ -4,11 +4,12 @@ import ( "context" "encoding/json" "fmt" - sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2" "html/template" "strings" "time" + sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2" + helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" "github.com/weaveworks/weave-gitops/pkg/logger" "github.com/weaveworks/weave-gitops/pkg/run/constants" @@ -270,8 +271,7 @@ func InstallFluentBit(ctx context.Context, log logger.Logger, kubeClient client. log.Actionf("waiting for HelmRelease %s/%s to be ready", helmRelease.Namespace, helmRelease.Name) - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(2*time.Second, 5*time.Minute, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), time.Second*2, time.Minute*5, true, func(_ context.Context) (bool, error) { instance := appsv1.DaemonSet{} if err := kubeClient.Get( ctx, diff --git a/pkg/run/install/install_vcluster.go b/pkg/run/install/install_vcluster.go index 6019bb40da..df42ef86df 100644 --- a/pkg/run/install/install_vcluster.go +++ b/pkg/run/install/install_vcluster.go @@ -3,12 +3,13 @@ package install import ( "context" "fmt" - sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2" "os" "path/filepath" "strings" "time" + sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2" + helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" "github.com/weaveworks/weave-gitops/cmd/gitops/version" coretypes "github.com/weaveworks/weave-gitops/core/server/types" @@ -133,8 +134,7 @@ func installVCluster(kubeClient client.Client, name, namespace, fluxNamespace st } } - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(2*time.Second, 5*time.Minute, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), time.Second*2, time.Minute*5, true, func(_ context.Context) (bool, error) { instance := appsv1.StatefulSet{} if err := kubeClient.Get( context.Background(), diff --git a/pkg/run/session/connect/connect.go b/pkg/run/session/connect/connect.go index cdf5385916..f2ac4bd046 100644 --- a/pkg/run/session/connect/connect.go +++ b/pkg/run/session/connect/connect.go @@ -607,8 +607,7 @@ func (conn *Connection) createServiceAccountToken(vKubeConfig api.Config) (strin token := "" conn.Log.Actionf("Create service account token for %s/%s", serviceAccountNamespace, serviceAccount) - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - err = wait.Poll(time.Second, time.Minute*3, func() (bool, error) { + err = wait.PollUntilContextTimeout(context.Background(), time.Second*2, time.Minute*5, true, func(_ context.Context) (bool, error) { // check if namespace exists _, err := vKubeClient.CoreV1().Namespaces().Get(context.TODO(), serviceAccountNamespace, metav1.GetOptions{}) if err != nil { diff --git a/pkg/run/session/remove.go b/pkg/run/session/remove.go index 972172ba45..67b1c1bfce 100644 --- a/pkg/run/session/remove.go +++ b/pkg/run/session/remove.go @@ -42,8 +42,7 @@ func Remove(kubeClient client.Client, session *InternalSession) error { result = multierror.Append(result, err) } - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(2*time.Second, 5*time.Minute, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), time.Second*2, time.Minute*5, true, func(_ context.Context) (bool, error) { instance := appsv1.StatefulSet{} if err := kubeClient.Get( context.Background(), @@ -62,8 +61,7 @@ func Remove(kubeClient client.Client, session *InternalSession) error { result = multierror.Append(result, err) } - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(2*time.Second, 5*time.Minute, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), time.Second*2, time.Minute*5, true, func(_ context.Context) (bool, error) { pvc := corev1.PersistentVolumeClaim{} if err := kubeClient.Get( context.Background(), diff --git a/pkg/run/watch/setup_dev_ks.go b/pkg/run/watch/setup_dev_ks.go index 9b9c497c10..8310c039c2 100644 --- a/pkg/run/watch/setup_dev_ks.go +++ b/pkg/run/watch/setup_dev_ks.go @@ -429,8 +429,7 @@ func ReconcileDevBucketSourceAndKS(ctx context.Context, log logger.Logger, kubeC } // wait for the reconciliation of dev-bucket to be done - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(interval, timeout, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(_ context.Context) (bool, error) { devBucket := &sourcev1b2.Bucket{} if err := kubeClient.Get(ctx, types.NamespacedName{ Name: constants.RunDevBucketName, @@ -445,8 +444,7 @@ func ReconcileDevBucketSourceAndKS(ctx context.Context, log logger.Logger, kubeC } // wait for devBucket to be ready - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(interval, timeout, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(_ context.Context) (bool, error) { devBucket := &sourcev1b2.Bucket{} if err := kubeClient.Get(ctx, types.NamespacedName{ Name: constants.RunDevBucketName, @@ -473,8 +471,7 @@ func ReconcileDevBucketSourceAndKS(ctx context.Context, log logger.Logger, kubeC return err } - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - if err := wait.Poll(interval, timeout, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(_ context.Context) (bool, error) { devKs := &kustomizev1.Kustomization{} if err := kubeClient.Get(ctx, types.NamespacedName{ Name: constants.RunDevKsName, @@ -489,8 +486,7 @@ func ReconcileDevBucketSourceAndKS(ctx context.Context, log logger.Logger, kubeC } devKs := &kustomizev1.Kustomization{} - //nolint:staticcheck // deprecated, tracking issue: https://github.com/weaveworks/weave-gitops/issues/3812 - devKsErr := wait.Poll(interval, timeout, func() (bool, error) { + devKsErr := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(_ context.Context) (bool, error) { if err := kubeClient.Get(ctx, types.NamespacedName{ Name: constants.RunDevKsName, Namespace: namespace,