Skip to content

Commit

Permalink
refactor: refactor away from deprecated wait.Poll calls
Browse files Browse the repository at this point in the history
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:
- #3812
- kubernetes-sigs/controller-runtime#2150
- https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0

Signed-off-by: Balazs Nadasdi <balazs@weave.works>
  • Loading branch information
yitsushi authored and AsmaaNabilBakr committed Nov 6, 2023
1 parent e69e266 commit 3de63c4
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 59 deletions.
25 changes: 15 additions & 10 deletions core/clustersmngr/cluster/delegating_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
7 changes: 6 additions & 1 deletion core/clustersmngr/cluster/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 0 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
21 changes: 10 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
9 changes: 3 additions & 6 deletions pkg/run/install/install_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/run/install/install_fluent_bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions pkg/run/install/install_vcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 1 addition & 2 deletions pkg/run/session/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions pkg/run/session/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
12 changes: 4 additions & 8 deletions pkg/run/watch/setup_dev_ks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 3de63c4

Please sign in to comment.