Skip to content

Commit

Permalink
[Release-1.5] Re-introduce the keep alive flag
Browse files Browse the repository at this point in the history
Thie patch re-introduces the keep alive flag on the CAPV manager. The
flag is by enabled default so there are no breaking changes after
upgrades.
  • Loading branch information
srm09 authored and zhanggbj committed Aug 9, 2023
1 parent da24492 commit 8e9714a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 26 deletions.
1 change: 1 addition & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spec:
- --leader-elect
- --logtostderr
- --v=4
- --enable-keep-alive
- "--feature-gates=NodeAntiAffinity=${EXP_NODE_ANTI_AFFINITY:=false},NodeLabeling=${EXP_NODE_LABELING:=false}"
image: gcr.io/cluster-api-provider-vsphere/release/manager:latest
imagePullPolicy: IfNotPresent
Expand Down
1 change: 1 addition & 0 deletions controllers/vspherecluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ func (r clusterReconciler) reconcileVCenterConnectivity(ctx *context.ClusterCont
WithServer(ctx.VSphereCluster.Spec.Server).
WithThumbprint(ctx.VSphereCluster.Spec.Thumbprint).
WithFeatures(session.Feature{
EnableKeepAlive: r.EnableKeepAlive,
KeepAliveDuration: r.KeepAliveDuration,
})

Expand Down
1 change: 1 addition & 0 deletions controllers/vspheredeploymentzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func (r vsphereDeploymentZoneReconciler) getVCenterSession(ctx *context.VSphereD
WithDatacenter(ctx.VSphereFailureDomain.Spec.Topology.Datacenter).
WithUserInfo(r.ControllerContext.Username, r.ControllerContext.Password).
WithFeatures(session.Feature{
EnableKeepAlive: r.EnableKeepAlive,
KeepAliveDuration: r.KeepAliveDuration,
})

Expand Down
1 change: 1 addition & 0 deletions controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ func (r vmReconciler) retrieveVcenterSession(ctx goctx.Context, vsphereVM *infra
WithUserInfo(r.ControllerContext.Username, r.ControllerContext.Password).
WithThumbprint(vsphereVM.Spec.Thumbprint).
WithFeatures(session.Feature{
EnableKeepAlive: r.EnableKeepAlive,
KeepAliveDuration: r.KeepAliveDuration,
})
cluster, err := clusterutilv1.GetClusterFromMetadata(r.ControllerContext, r.Client, vsphereVM.ObjectMeta)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func InitFlags(fs *pflag.FlagSet) {
&managerOpts.EnableKeepAlive,
"enable-keep-alive",
defaultEnableKeepAlive,
"DEPRECATED: feature to enable keep alive handler in vsphere sessions. This functionality is enabled by default now")
"feature to enable keep alive handler in vsphere sessions. This functionality is enabled by default.")
flag.DurationVar(
&managerOpts.KeepAliveDuration,
"keep-alive-duration",
Expand Down
1 change: 1 addition & 0 deletions pkg/clustermodule/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func newParams(ctx context.ClusterContext) *session.Params {
WithServer(ctx.VSphereCluster.Spec.Server).
WithThumbprint(ctx.VSphereCluster.Spec.Thumbprint).
WithFeatures(session.Feature{
EnableKeepAlive: ctx.EnableKeepAlive,
KeepAliveDuration: ctx.KeepAliveDuration,
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ const (
// cluster are in maintenance mode.
MaintenanceAnnotationLabel = "capv." + v1alpha3.GroupName + "/maintenance"

// DefaultEnableKeepAlive is false by default.
DefaultEnableKeepAlive = false
// DefaultEnableKeepAlive is true by default.
DefaultEnableKeepAlive = true

// KeepaliveDuration unit minutes.
DefaultKeepAliveDuration = time.Minute * 5
Expand Down
54 changes: 31 additions & 23 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"

infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/constants"
)

var (
Expand All @@ -61,11 +62,14 @@ type Session struct {
}

type Feature struct {
EnableKeepAlive bool
KeepAliveDuration time.Duration
}

func DefaultFeature() Feature {
return Feature{}
return Feature{
EnableKeepAlive: constants.DefaultEnableKeepAlive,
}
}

type Params struct {
Expand Down Expand Up @@ -219,15 +223,17 @@ func newClient(ctx context.Context, logger logr.Logger, sessionKey string, url *
SessionManager: session.NewManager(vimClient),
}

vimClient.RoundTripper = session.KeepAliveHandler(vimClient.RoundTripper, feature.KeepAliveDuration, func(tripper soap.RoundTripper) error {
_, err := methods.GetCurrentTime(ctx, tripper)
if err != nil {
logger.Error(err, "failed to keep alive govmomi client")
logger.Info("clearing the session")
sessionCache.Delete(sessionKey)
}
return err
})
if feature.EnableKeepAlive {
vimClient.RoundTripper = session.KeepAliveHandler(vimClient.RoundTripper, feature.KeepAliveDuration, func(tripper soap.RoundTripper) error {
_, err := methods.GetCurrentTime(ctx, tripper)
if err != nil {
logger.Error(err, "failed to keep alive govmomi client")
logger.Info("clearing the session")
sessionCache.Delete(sessionKey)
}
return err
})
}

if err := c.Login(ctx, url.User); err != nil {
return nil, err
Expand All @@ -239,19 +245,21 @@ func newClient(ctx context.Context, logger logr.Logger, sessionKey string, url *
// newManager creates a Manager that encompasses the REST Client for the VSphere tagging API.
func newManager(ctx context.Context, logger logr.Logger, sessionKey string, client *vim25.Client, user *url.Userinfo, feature Feature) (*tags.Manager, error) {
rc := rest.NewClient(client)
rc.Transport = keepalive.NewHandlerREST(rc, feature.KeepAliveDuration, func() error {
s, err := rc.Session(ctx)
if err != nil {
return err
}
if s != nil {
return nil
}

logger.Info("rest client session expired, clearing session")
sessionCache.Delete(sessionKey)
return errors.New("rest client session expired")
})
if feature.EnableKeepAlive {
rc.Transport = keepalive.NewHandlerREST(rc, feature.KeepAliveDuration, func() error {
s, err := rc.Session(ctx)
if err != nil {
return err
}
if s != nil {
return nil
}

logger.Info("rest client session expired, clearing session")
sessionCache.Delete(sessionKey)
return errors.New("rest client session expired")
})
}
if err := rc.Login(ctx, user); err != nil {
return nil, err
}
Expand Down

0 comments on commit 8e9714a

Please sign in to comment.