Skip to content

Commit

Permalink
Merge pull request #4881 from k0sproject/backport-4860-to-release-1.28
Browse files Browse the repository at this point in the history
[Backport release-1.28] Detect --hostname-override flag in Autopilot for controller+worker nodes
  • Loading branch information
makhov authored Aug 21, 2024
2 parents 5957888 + deb9f76 commit 2c70d4f
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 21 deletions.
1 change: 1 addition & 0 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ func (c *command) start(ctx context.Context) error {

clusterComponents.Add(ctx, &controller.Autopilot{
K0sVars: c.K0sVars,
KubeletExtraArgs: c.KubeletExtraArgs,
AdminClientFactory: adminClientFactory,
EnableWorker: c.EnableWorker,
})
Expand Down
4 changes: 4 additions & 0 deletions inttest/ap-controllerworker/controllerworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ spec:
peerAddress: %s
`

// TODO: Update this test after the https://github.com/k0sproject/k0s/pull/4860 is merged, backported and released.
// Apply this commit to properly test controller+worker update process:
// https://github.com/makhov/k0s/commit/bf702a829f958b04b7a6119ff03960e90100d4c9

// SetupTest prepares the controller and filesystem, getting it into a consistent
// state which we can run tests against.
func (s *controllerworkerSuite) SetupTest() {
Expand Down
14 changes: 14 additions & 0 deletions pkg/autopilot/common/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package common
import (
"os"

"github.com/k0sproject/k0s/internal/pkg/flags"
"github.com/k0sproject/k0s/pkg/node"
)

Expand All @@ -30,3 +31,16 @@ const (
func FindEffectiveHostname() (string, error) {
return node.GetNodename(os.Getenv(envAutopilotHostname))
}

func FindKubeletHostname(kubeletExtraArgs string) string {
defaultNodename, _ := node.GetNodename("")
if kubeletExtraArgs != "" {
extras := flags.Split(kubeletExtraArgs)
nodeName, ok := extras["--hostname-override"]
if ok {
return nodeName
}
}

return defaultNodename
}
1 change: 1 addition & 0 deletions pkg/autopilot/controller/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type RootConfig struct {
KubeConfig string
K0sDataDir string
KubeletExtraArgs string
Mode string
ManagerPort int
MetricsBindAddr string
Expand Down
2 changes: 1 addition & 1 deletion pkg/autopilot/controller/root_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewRootController(cfg aproot.RootConfig, logger *logrus.Entry, enableWorker
c.stopSubHandler = c.stopSubControllers
c.leaseWatcherCreator = NewLeaseWatcher
c.setupHandler = func(ctx context.Context, cf apcli.FactoryInterface) error {
setupController := NewSetupController(c.log, cf, cfg.K0sDataDir, enableWorker)
setupController := NewSetupController(c.log, cf, cfg.K0sDataDir, cfg.KubeletExtraArgs, enableWorker)
return setupController.Run(ctx)
}

Expand Down
43 changes: 25 additions & 18 deletions pkg/autopilot/controller/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,23 @@ type SetupController interface {
}

type setupController struct {
log *logrus.Entry
clientFactory apcli.FactoryInterface
k0sDataDir string
enableWorker bool
log *logrus.Entry
clientFactory apcli.FactoryInterface
k0sDataDir string
enableWorker bool
kubeletExtraArgs string
}

var _ SetupController = (*setupController)(nil)

// NewSetupController creates a `SetupController`
func NewSetupController(logger *logrus.Entry, cf apcli.FactoryInterface, k0sDataDir string, enableWorker bool) SetupController {
func NewSetupController(logger *logrus.Entry, cf apcli.FactoryInterface, k0sDataDir, kubeletExtraArgs string, enableWorker bool) SetupController {
return &setupController{
log: logger.WithField("controller", "setup"),
clientFactory: cf,
k0sDataDir: k0sDataDir,
enableWorker: enableWorker,
log: logger.WithField("controller", "setup"),
clientFactory: cf,
k0sDataDir: k0sDataDir,
kubeletExtraArgs: kubeletExtraArgs,
enableWorker: enableWorker,
}
}

Expand All @@ -74,23 +76,28 @@ func (sc *setupController) Run(ctx context.Context) error {
}
}

hostname, err := apcomm.FindEffectiveHostname()
controlNodeName, err := apcomm.FindEffectiveHostname()
if err != nil {
return fmt.Errorf("unable to determine hostname for signal node setup: %w", err)
}

logger.Infof("Using effective hostname = '%v'", hostname)
kubeletNodeName := controlNodeName
if sc.enableWorker {
kubeletNodeName = apcomm.FindKubeletHostname(sc.kubeletExtraArgs)
}

logger.Infof("Using effective hostname = '%v', kubelet hostname = '%v'", controlNodeName, kubeletNodeName)

if err := retry.Do(func() error {
logger.Infof("Attempting to create controlnode '%s'", hostname)
if err := sc.createControlNode(ctx, sc.clientFactory, hostname); err != nil {
return fmt.Errorf("create controlnode '%s' attempt failed, retrying: %w", hostname, err)
logger.Infof("Attempting to create controlnode '%s'", controlNodeName)
if err := sc.createControlNode(ctx, sc.clientFactory, controlNodeName, kubeletNodeName); err != nil {
return fmt.Errorf("create controlnode '%s' attempt failed, retrying: %w", controlNodeName, err)
}

return nil

}); err != nil {
return fmt.Errorf("failed to create controlnode '%s' after max attempts: %w", hostname, err)
return fmt.Errorf("failed to create controlnode '%s' after max attempts: %w", controlNodeName, err)
}

return nil
Expand All @@ -114,7 +121,7 @@ func createNamespace(ctx context.Context, cf apcli.FactoryInterface, name string

// createControlNode creates a new control node, ignoring errors if one already exists
// for this physical host.
func (sc *setupController) createControlNode(ctx context.Context, cf apcli.FactoryInterface, name string) error {
func (sc *setupController) createControlNode(ctx context.Context, cf apcli.FactoryInterface, name, nodeName string) error {
logger := sc.log.WithField("component", "setup")
client, err := sc.clientFactory.GetAutopilotClient()
if err != nil {
Expand All @@ -141,7 +148,7 @@ func (sc *setupController) createControlNode(ctx context.Context, cf apcli.Facto
Name: name,
// Create the usual os and arch labels as this describes a controller node
Labels: map[string]string{
corev1.LabelHostname: name,
corev1.LabelHostname: nodeName,
corev1.LabelOSStable: runtime.GOOS,
corev1.LabelArchStable: runtime.GOARCH,
},
Expand All @@ -160,7 +167,7 @@ func (sc *setupController) createControlNode(ctx context.Context, cf apcli.Facto
return err
}

addresses, err := getControlNodeAddresses(name)
addresses, err := getControlNodeAddresses(nodeName)
if err != nil {
return err
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/autopilot/controller/signal/k0s/cordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"time"

autopilotv1beta2 "github.com/k0sproject/k0s/pkg/apis/autopilot/v1beta2"
apcomm "github.com/k0sproject/k0s/pkg/autopilot/common"
apdel "github.com/k0sproject/k0s/pkg/autopilot/controller/delegate"
apsigpred "github.com/k0sproject/k0s/pkg/autopilot/controller/signal/common/predicate"
Expand Down Expand Up @@ -157,8 +158,19 @@ func (r *cordoning) drainNode(ctx context.Context, signalNode crcli.Object) erro
return fmt.Errorf("failed to cast signalNode to *corev1.Node")
}
} else {
nodeName := signalNode.GetName()
controlNode, ok := signalNode.(*autopilotv1beta2.ControlNode)
if ok {
for _, addr := range controlNode.Status.Addresses {
if addr.Type == corev1.NodeHostName {
nodeName = addr.Address
break
}
}
}

//otherwise get node from client
if err := r.client.Get(ctx, crcli.ObjectKey{Name: signalNode.GetName()}, node); err != nil {
if err := r.client.Get(ctx, crcli.ObjectKey{Name: nodeName}, node); err != nil {
return fmt.Errorf("failed to get node: %w", err)
}
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/autopilot/controller/signal/k0s/uncordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"time"

autopilotv1beta2 "github.com/k0sproject/k0s/pkg/apis/autopilot/v1beta2"
apcomm "github.com/k0sproject/k0s/pkg/autopilot/common"
apdel "github.com/k0sproject/k0s/pkg/autopilot/controller/delegate"
apsigcomm "github.com/k0sproject/k0s/pkg/autopilot/controller/signal/common"
Expand Down Expand Up @@ -157,8 +158,19 @@ func (r *uncordoning) unCordonNode(ctx context.Context, signalNode crcli.Object)
return fmt.Errorf("failed to convert signalNode to Node")
}
} else {
nodeName := signalNode.GetName()
controlNode, ok := signalNode.(*autopilotv1beta2.ControlNode)
if ok {
for _, addr := range controlNode.Status.Addresses {
if addr.Type == corev1.NodeHostName {
nodeName = addr.Address
break
}
}
}

//otherwise get node from client
if err := r.client.Get(ctx, crcli.ObjectKey{Name: signalNode.GetName()}, node); err != nil {
if err := r.client.Get(ctx, crcli.ObjectKey{Name: nodeName}, node); err != nil {
return fmt.Errorf("failed to get node: %w", err)
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/component/controller/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var _ manager.Component = (*Autopilot)(nil)

type Autopilot struct {
K0sVars *config.CfgVars
KubeletExtraArgs string
AdminClientFactory kubernetes.ClientFactoryInterface
EnableWorker bool
}
Expand All @@ -53,6 +54,7 @@ func (a *Autopilot) Start(ctx context.Context) error {
autopilotRoot, err := apcont.NewRootController(aproot.RootConfig{
KubeConfig: a.K0sVars.AdminKubeConfigPath,
K0sDataDir: a.K0sVars.DataDir,
KubeletExtraArgs: a.KubeletExtraArgs,
Mode: "controller",
ManagerPort: 8899,
MetricsBindAddr: "0",
Expand Down

0 comments on commit 2c70d4f

Please sign in to comment.