Skip to content

Commit

Permalink
session with round tripper keep alive
Browse files Browse the repository at this point in the history
add command line args for keep alive feature

go fmt

change deafults update keepalivehandler

use session from func

keepalive default to false specify unit

duration unit changes

fix go-lint

refactor params

gofmt

constant refactor to pkg/constant

gofmt

use ctrl logger

code comment changes

fix import order

make rebase fix
  • Loading branch information
sadysnaat committed Jun 27, 2021
1 parent f9baaa9 commit caf0d81
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 42 deletions.
18 changes: 14 additions & 4 deletions controllers/vspherecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,29 @@ func (r clusterReconciler) reconcileIdentitySecret(ctx *context.ClusterContext)
}

func (r clusterReconciler) reconcileVCenterConnectivity(ctx *context.ClusterContext) error {
params := session.NewParams().
WithServer(ctx.VSphereCluster.Spec.Server).
WithDatacenter(ctx.VSphereCluster.Spec.CloudProviderConfiguration.Workspace.Datacenter).
WithThumbprint(ctx.VSphereCluster.Spec.Thumbprint).
WithFeatures(session.Feature{
EnableKeepAlive: r.EnableKeepAlive,
KeepAliveDuration: r.KeepAliveDuration,
})

if ctx.VSphereCluster.Spec.IdentityRef != nil {
creds, err := identity.GetCredentials(ctx, r.Client, ctx.VSphereCluster, r.Namespace)
if err != nil {
return err
}

_, err = session.GetOrCreate(ctx, ctx.VSphereCluster.Spec.Server,
ctx.VSphereCluster.Spec.CloudProviderConfiguration.Workspace.Datacenter, creds.Username, creds.Password, ctx.VSphereCluster.Spec.Thumbprint)
params = params.WithUserInfo(creds.Username, creds.Password)
_, err = session.GetOrCreate(ctx, params)
return err
}

_, err := session.GetOrCreate(ctx, ctx.VSphereCluster.Spec.Server,
ctx.VSphereCluster.Spec.CloudProviderConfiguration.Workspace.Datacenter, ctx.Username, ctx.Password, ctx.VSphereCluster.Spec.Thumbprint)
params = params.WithUserInfo(ctx.Username, ctx.Password)
_, err := session.GetOrCreate(ctx,
params)
return err
}

Expand Down
26 changes: 19 additions & 7 deletions controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,21 @@ func (r *vmReconciler) clusterToVSphereVMs(a ctrlclient.Object) []reconcile.Requ

func (r *vmReconciler) retrieveVcenterSession(ctx goctx.Context, vsphereVM *infrav1.VSphereVM) (*session.Session, error) {
// Get cluster object and then get VSphereCluster object

params := session.NewParams().
WithServer(vsphereVM.Spec.Server).
WithDatacenter(vsphereVM.Spec.Datacenter).
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)
if err != nil {
r.Logger.Info("VsphereVM is missing cluster label or cluster does not exist")
return session.GetOrCreate(r.Context, vsphereVM.Spec.Server, vsphereVM.Spec.Datacenter,
r.ControllerManagerContext.Username, r.ControllerManagerContext.Password, vsphereVM.Spec.Thumbprint)
return session.GetOrCreate(r.Context,
params)
}

key := client.ObjectKey{
Expand All @@ -430,19 +440,21 @@ func (r *vmReconciler) retrieveVcenterSession(ctx goctx.Context, vsphereVM *infr
err = r.Client.Get(r, key, vsphereCluster)
if err != nil {
r.Logger.Info("VSphereCluster couldn't be retrieved")
return session.GetOrCreate(r.Context, vsphereVM.Spec.Server, vsphereVM.Spec.Datacenter,
r.ControllerManagerContext.Username, r.ControllerManagerContext.Password, vsphereVM.Spec.Thumbprint)
return session.GetOrCreate(r.Context,
params)
}

if vsphereCluster.Spec.IdentityRef != nil {
creds, err := identity.GetCredentials(ctx, r.Client, vsphereCluster, r.Namespace)
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve credentials from IdentityRef")
}
return session.GetOrCreate(ctx, vsphereVM.Spec.Server, vsphereVM.Spec.Datacenter, creds.Username, creds.Password, vsphereVM.Spec.Thumbprint)
params = params.WithUserInfo(creds.Username, creds.Password)
return session.GetOrCreate(r.Context,
params)
}

// Fallback to using credentials provided to the manager
return session.GetOrCreate(r.Context, vsphereVM.Spec.Server, vsphereVM.Spec.Datacenter,
r.ControllerManagerContext.Username, r.ControllerManagerContext.Password, vsphereVM.Spec.Thumbprint)
return session.GetOrCreate(r.Context,
params)
}
27 changes: 19 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import (
"os"
"time"

"sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha4"

"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
"sigs.k8s.io/controller-runtime/pkg/healthz"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
ctrlsig "sigs.k8s.io/controller-runtime/pkg/manager/signals"

"sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha4"
"sigs.k8s.io/cluster-api-provider-vsphere/controllers"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/constants"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/manager"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/version"
Expand All @@ -42,12 +42,14 @@ import (
var (
setupLog = ctrllog.Log.WithName("entrypoint")

managerOpts manager.Options
defaultProfilerAddr = os.Getenv("PROFILER_ADDR")
defaultSyncPeriod = manager.DefaultSyncPeriod
defaultLeaderElectionID = manager.DefaultLeaderElectionID
defaultPodName = manager.DefaultPodName
defaultWebhookPort = manager.DefaultWebhookServiceContainerPort
managerOpts manager.Options
defaultProfilerAddr = os.Getenv("PROFILER_ADDR")
defaultSyncPeriod = manager.DefaultSyncPeriod
defaultLeaderElectionID = manager.DefaultLeaderElectionID
defaultPodName = manager.DefaultPodName
defaultWebhookPort = manager.DefaultWebhookServiceContainerPort
defaultEnableKeepAlive = constants.DefaultEnableKeepAlive
defaultKeepAliveDuration = constants.DefaultKeepAliveDuration
)

// nolint:gocognit
Expand Down Expand Up @@ -116,6 +118,15 @@ func main() {
"/etc/capv/credentials.yaml",
"path to CAPV's credentials file",
)
flag.BoolVar(&managerOpts.EnableKeepAlive,
"enable-keep-alive",
defaultEnableKeepAlive,
"feature to enable keep alive handler in vsphere sessions")

flag.DurationVar(&managerOpts.KeepAliveDuration,
"keep-alive-duration",
defaultKeepAliveDuration,
"idle time interval(minutes) in between send() requests in keepalive handler")

flag.Parse()

Expand Down
8 changes: 8 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package constants

import (
"time"

"sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha3"
)

Expand Down Expand Up @@ -48,4 +50,10 @@ const (
// MaintenanceAnnotationLabel is the annotation used to indicate a machine and/or
// cluster are in maintenance mode.
MaintenanceAnnotationLabel = "capv." + v1alpha3.GroupName + "/maintenance"

// DefaultEnableKeepAlive is false by default
DefaultEnableKeepAlive = false

// KeepaliveDuration unit minutes
DefaultKeepAliveDuration = time.Minute * 5
)
9 changes: 9 additions & 0 deletions pkg/context/controller_manager_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package context
import (
"context"
"sync"
"time"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -77,6 +78,14 @@ type ControllerManagerContext struct {
// endpoints.
Password string

// EnableKeepAlive is a session feature to enable keep alive handler
// for better load management on vSphere api server
EnableKeepAlive bool

// KeepAliveDuration is the idle time interval in between send() requests
// in keepalive handler
KeepAliveDuration time.Duration

genericEventCache sync.Map
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func New(opts Options) (Manager, error) {
Scheme: opts.Scheme,
Username: opts.Username,
Password: opts.Password,
EnableKeepAlive: opts.EnableKeepAlive,
KeepAliveDuration: opts.KeepAliveDuration,
}

// Add the requested items to the manager.
Expand Down
8 changes: 8 additions & 0 deletions pkg/manager/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type Options struct {
// LeaderElectionEnabled is a flag that enables leader election.
LeaderElectionEnabled bool

// EnableKeepAlive is a session feature to enable keep alive handler
// for better load management on vSphere api server
EnableKeepAlive bool

// LeaderElectionID is the name of the config map to use as the
// locking resource when configuring leader election.
LeaderElectionID string
Expand Down Expand Up @@ -98,6 +102,10 @@ type Options struct {
// endpoints.
Password string

// KeepAliveDuration is the idle time interval in between send() requests
// in keepalive handler
KeepAliveDuration time.Duration

// WebhookPort is the port that the webhook server serves at.
WebhookPort int

Expand Down
7 changes: 4 additions & 3 deletions pkg/services/govmomi/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ func TestCreate(t *testing.T) {
vmContext.VSphereVM.Spec.Server = s.URL.Host

authSession, err := session.GetOrCreate(
vmContext,
vmContext.VSphereVM.Spec.Server, "",
s.URL.User.Username(), pass, "")
vmContext.Context,
session.NewParams().
WithServer(vmContext.VSphereVM.Spec.Server).
WithUserInfo(s.URL.User.Username(), pass))
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/services/govmomi/vcenter/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ func initSimulator(t *testing.T) (*simulator.Model, *session.Session, *simulator

authSession, err := session.GetOrCreate(
ctx.TODO(),
server.URL.Host, "",
server.URL.User.Username(), pass, "")
session.NewParams().
WithServer(server.URL.Host).
WithUserInfo(server.URL.User.Username(), pass))
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit caf0d81

Please sign in to comment.