Skip to content

Commit

Permalink
Adds DrainConnectionsTimeout microcluster configuration (#867)
Browse files Browse the repository at this point in the history
In microcluster, there is now a configuration that allows us to drain
any current connections before shutting down the unix / http servers.

Note that the servers will also be shut down when removing a node, or if
bootstrapping or joining a node fails. If we're not waiting for the
connection to drain, we may close current connections before managing to
write a response to the clients, resulting in them getting EOF errors.

Adds the --disable-update-node-config-controller k8sd flag, with 10s as
the default value.
  • Loading branch information
claudiubelu authored Feb 13, 2025
1 parent 7cc5bbb commit 23fc18c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/k8s/cmd/k8sd/k8sd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package k8sd

import (
"time"

cmdutil "github.com/canonical/k8s/cmd/util"
"github.com/canonical/k8s/pkg/k8sd/app"
"github.com/canonical/k8s/pkg/log"
Expand All @@ -19,6 +21,7 @@ var rootCmdOpts struct {
disableFeatureController bool
disableUpdateNodeConfigController bool
disableCSRSigningController bool
drainConnectionsTimeout time.Duration
}

func addCommands(root *cobra.Command, group *cobra.Group, commands ...*cobra.Command) {
Expand Down Expand Up @@ -55,6 +58,7 @@ func NewRootCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
DisableUpdateNodeConfigController: rootCmdOpts.disableUpdateNodeConfigController,
DisableFeatureController: rootCmdOpts.disableFeatureController,
DisableCSRSigningController: rootCmdOpts.disableCSRSigningController,
DrainConnectionsTimeout: rootCmdOpts.drainConnectionsTimeout,
})
if err != nil {
cmd.PrintErrf("Error: Failed to initialize k8sd: %v", err)
Expand Down Expand Up @@ -88,6 +92,7 @@ func NewRootCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {

cmd.Flags().Uint("port", 0, "Default port for the HTTP API")
cmd.Flags().MarkDeprecated("port", "this flag does not have any effect, and will be removed in a future version")
cmd.Flags().DurationVar(&rootCmdOpts.drainConnectionsTimeout, "drain-connection-timeout", 10*time.Second, "amount of time to allow for all connections to drain when shutting down")

cmd.AddCommand(newSqlCmd(env))

Expand Down
4 changes: 3 additions & 1 deletion src/k8s/pkg/k8sd/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api

import (
"context"
"time"

apiv1 "github.com/canonical/k8s-snap-api/api/v1"
"github.com/canonical/microcluster/v2/rest"
Expand All @@ -15,7 +16,7 @@ type Endpoints struct {

// New creates a new API server instance.
// Context is the context to use for the API servers endpoints.
func New(ctx context.Context, provider Provider) map[string]rest.Server {
func New(ctx context.Context, provider Provider, drainConnectionsTimeout time.Duration) map[string]rest.Server {
k8sd := &Endpoints{
context: ctx,
provider: provider,
Expand All @@ -31,6 +32,7 @@ func New(ctx context.Context, provider Provider) map[string]rest.Server {
Endpoints: k8sd.Endpoints(),
},
},
DrainConnectionsTimeout: drainConnectionsTimeout,
},
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/k8s/pkg/k8sd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Config struct {
DisableFeatureController bool
// DisableCSRSigningController is a bool flag to disable csrsigning controller.
DisableCSRSigningController bool
// DrainConnectionsTimeout is the amount of time to allow for all connections to drain when shutting down.
DrainConnectionsTimeout time.Duration
}

// App is the k8sd microcluster instance.
Expand Down Expand Up @@ -232,12 +234,13 @@ func (a *App) Run(ctx context.Context, customHooks *state.Hooks) error {
}

err := a.cluster.Start(ctx, microcluster.DaemonArgs{
Version: string(apiv1.K8sdAPIVersion),
Verbose: a.config.Verbose,
Debug: a.config.Debug,
Hooks: hooks,
ExtensionServers: api.New(ctx, a),
ExtensionsSchema: database.SchemaExtensions,
Version: string(apiv1.K8sdAPIVersion),
Verbose: a.config.Verbose,
Debug: a.config.Debug,
Hooks: hooks,
ExtensionServers: api.New(ctx, a, a.config.DrainConnectionsTimeout),
ExtensionsSchema: database.SchemaExtensions,
DrainConnectionsTimeout: a.config.DrainConnectionsTimeout,
})
if err != nil {
return fmt.Errorf("failed to run microcluster: %w", err)
Expand Down

0 comments on commit 23fc18c

Please sign in to comment.