Skip to content

Commit 3833bc1

Browse files
authored
fix metricsaggregate sporadic errors (#290)
1 parent 35db9c4 commit 3833bc1

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

cmd/agent/kubernetes.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ import (
99
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts"
1010
rolloutv1alpha1 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
1111
roclientset "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned"
12+
"github.com/pluralsh/deployment-operator/cmd/agent/args"
13+
"github.com/pluralsh/deployment-operator/internal/controller"
14+
"github.com/pluralsh/deployment-operator/pkg/cache"
15+
consoleclient "github.com/pluralsh/deployment-operator/pkg/client"
16+
consolectrl "github.com/pluralsh/deployment-operator/pkg/controller"
17+
"github.com/pluralsh/deployment-operator/pkg/controller/service"
1218
"github.com/prometheus/client_golang/prometheus/promhttp"
1319
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
1420
"k8s.io/apimachinery/pkg/runtime/schema"
1521
"k8s.io/client-go/discovery"
1622
"k8s.io/client-go/dynamic"
1723
"k8s.io/client-go/kubernetes"
1824
"k8s.io/client-go/rest"
25+
metricsclientset "k8s.io/metrics/pkg/client/clientset/versioned"
1926
ctrl "sigs.k8s.io/controller-runtime"
2027
"sigs.k8s.io/controller-runtime/pkg/healthz"
2128
"sigs.k8s.io/controller-runtime/pkg/manager"
2229
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
23-
24-
"github.com/pluralsh/deployment-operator/cmd/agent/args"
25-
"github.com/pluralsh/deployment-operator/internal/controller"
26-
"github.com/pluralsh/deployment-operator/pkg/cache"
27-
consoleclient "github.com/pluralsh/deployment-operator/pkg/client"
28-
consolectrl "github.com/pluralsh/deployment-operator/pkg/controller"
29-
"github.com/pluralsh/deployment-operator/pkg/controller/service"
3030
)
3131

3232
func initKubeManagerOrDie(config *rest.Config) manager.Manager {
@@ -64,7 +64,7 @@ func initKubeManagerOrDie(config *rest.Config) manager.Manager {
6464
return mgr
6565
}
6666

67-
func initKubeClientsOrDie(config *rest.Config) (rolloutsClient *roclientset.Clientset, dynamicClient *dynamic.DynamicClient, kubeClient *kubernetes.Clientset) {
67+
func initKubeClientsOrDie(config *rest.Config) (rolloutsClient *roclientset.Clientset, dynamicClient *dynamic.DynamicClient, kubeClient *kubernetes.Clientset, metricsClient metricsclientset.Interface) {
6868
rolloutsClient, err := roclientset.NewForConfig(config)
6969
if err != nil {
7070
setupLog.Error(err, "unable to create rollouts client")
@@ -83,7 +83,13 @@ func initKubeClientsOrDie(config *rest.Config) (rolloutsClient *roclientset.Clie
8383
os.Exit(1)
8484
}
8585

86-
return rolloutsClient, dynamicClient, kubeClient
86+
metricsClient, err = metricsclientset.NewForConfig(config)
87+
if err != nil {
88+
setupLog.Error(err, "unable to create metrics client")
89+
os.Exit(1)
90+
}
91+
92+
return rolloutsClient, dynamicClient, kubeClient, metricsClient
8793
}
8894

8995
func registerKubeReconcilersOrDie(
@@ -94,7 +100,8 @@ func registerKubeReconcilersOrDie(
94100
extConsoleClient consoleclient.Client,
95101
discoveryClient discovery.DiscoveryInterface,
96102
) {
97-
rolloutsClient, dynamicClient, kubeClient := initKubeClientsOrDie(config)
103+
104+
rolloutsClient, dynamicClient, kubeClient, metricsClient := initKubeClientsOrDie(config)
98105

99106
backupController := &controller.BackupReconciler{
100107
Client: manager.GetClient(),
@@ -217,6 +224,7 @@ func registerKubeReconcilersOrDie(
217224
Client: manager.GetClient(),
218225
Scheme: manager.GetScheme(),
219226
DiscoveryClient: discoveryClient,
227+
MetricsClient: metricsClient,
220228
}).SetupWithManager(ctx, manager); err != nil {
221229
setupLog.Error(err, "unable to create controller", "controller", "MetricsAggregate")
222230
}

cmd/agent/main.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ import (
1313
"github.com/pluralsh/deployment-operator/pkg/cache"
1414
"github.com/pluralsh/deployment-operator/pkg/client"
1515
consolectrl "github.com/pluralsh/deployment-operator/pkg/controller"
16-
"k8s.io/client-go/discovery"
17-
"k8s.io/client-go/rest"
18-
"k8s.io/klog/v2"
19-
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
20-
2116
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
2217
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2318
"k8s.io/apimachinery/pkg/runtime"
2419
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
20+
"k8s.io/client-go/discovery"
2521
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
22+
"k8s.io/client-go/rest"
23+
"k8s.io/klog/v2"
2624
ctrl "sigs.k8s.io/controller-runtime"
2725
)
2826

@@ -39,7 +37,6 @@ func init() {
3937
utilruntime.Must(constraintstatusv1beta1.AddToScheme(scheme))
4038
utilruntime.Must(templatesv1.AddToScheme(scheme))
4139
utilruntime.Must(rolloutv1alpha1.AddToScheme(scheme))
42-
utilruntime.Must(metricsv1beta1.AddToScheme(scheme))
4340
//+kubebuilder:scaffold:scheme
4441
}
4542

internal/controller/metricsaggregate.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"k8s.io/client-go/discovery"
1515
metricsapi "k8s.io/metrics/pkg/apis/metrics"
1616
"k8s.io/metrics/pkg/apis/metrics/v1beta1"
17+
metricsclientset "k8s.io/metrics/pkg/client/clientset/versioned"
1718
ctrl "sigs.k8s.io/controller-runtime"
1819
k8sClient "sigs.k8s.io/controller-runtime/pkg/client"
1920
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -34,6 +35,7 @@ type MetricsAggregateReconciler struct {
3435
k8sClient.Client
3536
Scheme *runtime.Scheme
3637
DiscoveryClient discovery.DiscoveryInterface
38+
MetricsClient metricsclientset.Interface
3739
}
3840

3941
// Reconcile IngressReplica ensure that stays in sync with Kubernetes cluster.
@@ -94,8 +96,8 @@ func (r *MetricsAggregateReconciler) Reconcile(ctx context.Context, req ctrl.Req
9496
}
9597

9698
nodeDeploymentNodesMetrics := make([]v1beta1.NodeMetrics, 0)
97-
allNodeMetricsList := &v1beta1.NodeMetricsList{}
98-
if err := r.List(ctx, allNodeMetricsList); err != nil {
99+
allNodeMetricsList, err := r.MetricsClient.MetricsV1beta1().NodeMetricses().List(ctx, metav1.ListOptions{})
100+
if err != nil {
99101
return reconcile.Result{}, err
100102
}
101103

@@ -126,12 +128,12 @@ func (r *MetricsAggregateReconciler) Reconcile(ctx context.Context, req ctrl.Req
126128

127129
utils.MarkCondition(metrics.SetCondition, v1alpha1.ReadyConditionType, metav1.ConditionTrue, v1alpha1.ReadyConditionReason, "")
128130

129-
return requeue(requeueAfter, jitter), reterr
131+
return requeue(time.Second*5, jitter), reterr
130132
}
131133

132134
// SetupWithManager sets up the controller with the Manager.
133135
func (r *MetricsAggregateReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
134-
debounceReconciler := NewDebounceReconciler(mgr.GetClient(), debounceDuration, r)
136+
debounceReconciler := NewDebounceReconciler(mgr.GetClient(), time.Second*10, r)
135137
debounceReconciler.Start(ctx)
136138

137139
return ctrl.NewControllerManagedBy(mgr).

0 commit comments

Comments
 (0)