Add timeout context for controllers (#1990)

This commit is contained in:
Somefive
2021-08-02 19:31:00 +08:00
committed by GitHub
parent d9cd048d79
commit 7a1f95773d
17 changed files with 83 additions and 46 deletions
+31
View File
@@ -0,0 +1,31 @@
/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"context"
"time"
)
const (
reconcileTimeout = time.Minute
)
// NewReconcileContext create context with default timeout (60s)
func NewReconcileContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), reconcileTimeout)
}
@@ -39,6 +39,7 @@ import (
oamcorealpha "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
oamcore "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/clustermanager"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/controller/utils"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
@@ -47,7 +48,6 @@ import (
const (
appDeploymentFinalizer = "finalizers.appdeployment.oam.dev"
reconcileTimeOut = 60 * time.Second
secretKeyConfig = "config"
)
@@ -71,9 +71,9 @@ type Reconciler struct {
// Reconcile is the main logic of appDeployment controller
func (r *Reconciler) Reconcile(req ctrl.Request) (res reconcile.Result, retErr error) {
appDeployment := &oamcore.AppDeployment{}
ctx, cancel := context.WithTimeout(context.TODO(), reconcileTimeOut)
ctx, cancel := common2.NewReconcileContext()
defer cancel()
appDeployment := &oamcore.AppDeployment{}
startTime := time.Now()
defer func() {
@@ -21,8 +21,6 @@ import (
"fmt"
"time"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/pkg/errors"
@@ -35,9 +33,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
velatypes "github.com/oam-dev/kubevela/apis/types"
"github.com/oam-dev/kubevela/pkg/appfile"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
core "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/cue/packages"
"github.com/oam-dev/kubevela/pkg/oam"
@@ -80,7 +80,8 @@ type Reconciler struct {
// Reconcile process app event
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
ctx, cancel := common2.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile application", "application", klog.KRef(req.Namespace, req.Name))
app := new(v1beta1.Application)
@@ -53,8 +53,6 @@ import (
"github.com/oam-dev/kubevela/pkg/utils/apply"
)
const reconcileTimeout = 1 * time.Minute
// Reconcile error strings.
const (
errGetAppConfig = "cannot get application configuration"
@@ -222,10 +220,9 @@ func NewReconciler(m ctrl.Manager, dm discoverymapper.DiscoveryMapper, o ...Reco
// Reconcile an OAM ApplicationConfigurations by rendering and instantiating its
// Components and Traits.
func (r *OAMApplicationReconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
klog.InfoS("Reconcile applicationConfiguration", "applicationConfiguration", klog.KRef(req.Namespace, req.Name))
ctx, cancel := context.WithTimeout(context.Background(), reconcileTimeout)
ctx, cancel := common.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile applicationConfiguration", "applicationConfiguration", klog.KRef(req.Namespace, req.Name))
ac := &v1alpha2.ApplicationConfiguration{}
if err := r.client.Get(ctx, req.NamespacedName, ac); err != nil {
@@ -17,7 +17,6 @@ limitations under the License.
package applicationcontext
import (
"context"
"fmt"
"strings"
"time"
@@ -35,6 +34,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/apis/types"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
core "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
ac "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/applicationconfiguration"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
@@ -48,8 +48,6 @@ const (
errUpdateAppContextStatus = "cannot update application context status"
)
const reconcileTimeout = 1 * time.Minute
// Reconciler reconciles an Application Context by constructing an in-memory
// application configuration and reuse its reconcile logic
type Reconciler struct {
@@ -62,9 +60,9 @@ type Reconciler struct {
// Reconcile reconcile an application context
func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
klog.InfoS("Reconcile", "applicationContext", klog.KRef(request.Namespace, request.Name))
ctx, cancel := context.WithTimeout(context.Background(), reconcileTimeout)
ctx, cancel := common2.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile", "applicationContext", klog.KRef(request.Namespace, request.Name))
// fetch the app context
appContext := &v1alpha2.ApplicationContext{}
if err := r.client.Get(ctx, request.NamespacedName, appContext); err != nil {
@@ -21,8 +21,6 @@ import (
"fmt"
"time"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/pkg/errors"
@@ -36,8 +34,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/apis/standard.oam.dev/v1alpha1"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
"github.com/oam-dev/kubevela/pkg/controller/common/rollout"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
@@ -48,8 +48,6 @@ const (
errUpdateAppRollout = "failed to update the app rollout"
appRolloutFinalizer = "finalizers.approllout.oam.dev"
reconcileTimeOut = 60 * time.Second
)
// Reconciler reconciles an AppRollout object
@@ -69,10 +67,10 @@ type Reconciler struct {
// Reconcile is the main logic of appRollout controller
// nolint:gocyclo
func (r *Reconciler) Reconcile(req ctrl.Request) (res reconcile.Result, retErr error) {
var appRollout v1beta1.AppRollout
ctx, cancel := context.WithTimeout(context.TODO(), reconcileTimeOut)
ctx, cancel := common2.NewReconcileContext()
defer cancel()
ctx = oamutil.SetNamespaceInCtx(ctx, req.Namespace)
var appRollout v1beta1.AppRollout
startTime := time.Now()
defer func() {
@@ -37,6 +37,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/apis/types"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
coredef "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/core"
"github.com/oam-dev/kubevela/pkg/controller/utils"
@@ -59,8 +60,9 @@ type Reconciler struct {
// Reconcile is the main logic for ComponentDefinition controller
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx, cancel := common2.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile componentDefinition", "componentDefinition", klog.KRef(req.Namespace, req.Name))
ctx := context.Background()
var componentDefinition v1beta1.ComponentDefinition
if err := r.Get(ctx, req.NamespacedName, &componentDefinition); err != nil {
@@ -36,6 +36,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
coredef "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/core"
"github.com/oam-dev/kubevela/pkg/controller/utils"
@@ -58,9 +59,10 @@ type Reconciler struct {
// Reconcile is the main logic for PolicyDefinition controller
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx, cancel := common2.NewReconcileContext()
defer cancel()
definitionName := req.NamespacedName.Name
klog.InfoS("Reconciling PolicyDefinition...", "Name", definitionName, "Namespace", req.Namespace)
ctx := context.Background()
var policydefinition v1beta1.PolicyDefinition
if err := r.Get(ctx, req.NamespacedName, &policydefinition); err != nil {
@@ -39,8 +39,7 @@ import (
)
const (
reconcileTimeout = 1 * time.Minute
longWait = 10 * time.Second
longWait = 10 * time.Second
)
// Reconcile error strings.
@@ -130,10 +129,9 @@ func NewReconciler(m ctrl.Manager, o ...ReconcilerOption) *Reconciler {
// Reconcile an OAM HealthScope by keeping track of its health status.
func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
klog.InfoS("Reconcile healthScope", "healthScope", klog.KRef(req.Namespace, req.Name))
ctx, cancel := context.WithTimeout(context.Background(), reconcileTimeout)
ctx, cancel := common.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile healthScope", "healthScope", klog.KRef(req.Namespace, req.Name))
hs := &v1alpha2.HealthScope{}
if err := r.client.Get(ctx, req.NamespacedName, hs); err != nil {
@@ -38,6 +38,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
oamv1alpha2 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/pkg/controller/common"
controller "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
"github.com/oam-dev/kubevela/pkg/oam/util"
@@ -79,7 +80,8 @@ type Reconciler struct {
// +kubebuilder:rbac:groups=core.oam.dev,resources=workloaddefinition,verbs=get;list;
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;update;patch;delete
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
ctx, cancel := common.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile manualscalar trait", "trait", klog.KRef(req.Namespace, req.Name))
var manualScalar oamv1alpha2.ManualScalerTrait
@@ -36,6 +36,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
coredef "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/core"
"github.com/oam-dev/kubevela/pkg/controller/utils"
@@ -58,8 +59,9 @@ type Reconciler struct {
// Reconcile is the main logic for TraitDefinition controller
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx, cancel := common2.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile traitDefinition", "traitDefinition", klog.KRef(req.Namespace, req.Name))
ctx := context.Background()
var traitdefinition v1beta1.TraitDefinition
if err := r.Get(ctx, req.NamespacedName, &traitdefinition); err != nil {
@@ -36,6 +36,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
coredef "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/core"
"github.com/oam-dev/kubevela/pkg/controller/utils"
@@ -58,9 +59,10 @@ type Reconciler struct {
// Reconcile is the main logic for WorkflowStepDefinition controller
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx, cancel := common2.NewReconcileContext()
defer cancel()
definitionName := req.NamespacedName.Name
klog.InfoS("Reconciling WorkflowStepDefinition...", "Name", definitionName, "Namespace", req.Namespace)
ctx := context.Background()
var wfstepdefinition v1beta1.WorkflowStepDefinition
if err := r.Get(ctx, req.NamespacedName, &wfstepdefinition); err != nil {
@@ -37,6 +37,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
controller "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/oam/util"
)
@@ -74,7 +75,8 @@ type Reconciler struct {
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
ctx, cancel := common2.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile containerizedworkload", klog.KRef(req.Namespace, req.Name))
var workload v1alpha2.ContainerizedWorkload
@@ -30,6 +30,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
"github.com/oam-dev/kubevela/pkg/appfile"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/cue/packages"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
@@ -48,9 +49,10 @@ type Reconciler struct {
// Reconcile is the main logic for EnvBinding controller
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx, cancel := common2.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile EnvBinding", "envbinding", klog.KRef(req.Namespace, req.Name))
ctx := context.Background()
envBinding := new(v1alpha1.EnvBinding)
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: req.Name}, envBinding); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
@@ -37,6 +37,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
velatypes "github.com/oam-dev/kubevela/apis/types"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/controller/utils"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
@@ -57,8 +58,9 @@ type Reconciler struct {
// Reconcile is the main logic for Initializer controller
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx, cancel := common2.NewReconcileContext()
defer cancel()
klog.InfoS("Reconcile initializer", "initializer", klog.KRef(req.Namespace, req.Name))
ctx := context.Background()
init := new(v1beta1.Initializer)
if err := r.Client.Get(ctx, req.NamespacedName, init); err != nil {
@@ -22,6 +22,7 @@ import (
"reflect"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
controller "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
@@ -77,7 +78,8 @@ type Reconciler struct {
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=,resources=services,verbs=get;list;watch;create;update;patch;delete
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
ctx, cancel := common2.NewReconcileContext()
defer cancel()
log := r.log.WithValues("podspecworkload", req.NamespacedName)
log.Info("Reconcile podspecworkload workload")
@@ -18,15 +18,9 @@ package rollout
import (
"context"
"time"
"github.com/oam-dev/kubevela/pkg/utils/apply"
"github.com/pkg/errors"
"github.com/oam-dev/kubevela/apis/standard.oam.dev/v1alpha1"
rolloutplan "github.com/oam-dev/kubevela/pkg/controller/common/rollout"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"k8s.io/apimachinery/pkg/runtime"
@@ -36,15 +30,17 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/oam-dev/kubevela/apis/standard.oam.dev/v1alpha1"
common2 "github.com/oam-dev/kubevela/pkg/controller/common"
rolloutplan "github.com/oam-dev/kubevela/pkg/controller/common/rollout"
oamctrl "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/cue/packages"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
oamutil "github.com/oam-dev/kubevela/pkg/oam/util"
"github.com/oam-dev/kubevela/pkg/utils/apply"
)
const (
reconcileTimeOut = 60 * time.Second
rolloutFinalizer = "finalizers.rollout.standard.oam.dev"
errUpdateRollout = "failed to update the rollout"
@@ -61,9 +57,9 @@ type reconciler struct {
}
func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
rollout := new(v1alpha1.Rollout)
ctx, cancel := context.WithTimeout(context.TODO(), reconcileTimeOut)
ctx, cancel := common2.NewReconcileContext()
defer cancel()
rollout := new(v1alpha1.Rollout)
ctx = oamutil.SetNamespaceInCtx(ctx, req.Namespace)
if err := r.Get(ctx, req.NamespacedName, rollout); err != nil {