github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/static/hostname.go (about)

     1  package static
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/caos/orbos/internal/operator/common"
     7  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
     8  	"github.com/caos/orbos/mntr"
     9  )
    10  
    11  func desireHostname(poolsSpec map[string][]*Machine, nodeagents *common.DesiredNodeAgents, nodeagentsCurr *common.CurrentNodeAgents, monitor mntr.Monitor) func(machine infra.Machine, pool string) (bool, error) {
    12  	return func(machine infra.Machine, pool string) (bool, error) {
    13  		for _, machineSpec := range poolsSpec[pool] {
    14  			if machineSpec.ID == machine.ID() {
    15  				machineMonitor := monitor.WithFields(map[string]interface{}{
    16  					"machine": machine.ID(),
    17  				})
    18  
    19  				nodeagent, _ := nodeagents.Get(machineSpec.ID)
    20  				if nodeagent.Software.Hostname.Config == nil || nodeagent.Software.Hostname.Config["hostname"] != machineSpec.ID {
    21  					nodeagent.Software.Hostname = common.Package{Config: map[string]string{"hostname": machineSpec.ID}}
    22  					machineMonitor.Changed("Hostname desired")
    23  				}
    24  				logWaiting := func() {
    25  					machineMonitor.Info("Awaiting hostname")
    26  				}
    27  				curr, ok := nodeagentsCurr.Get(machine.ID())
    28  				if !ok {
    29  					logWaiting()
    30  					return false, nil
    31  				}
    32  				if curr.Software.Hostname.Config == nil || curr.Software.Hostname.Config["hostname"] != machineSpec.ID {
    33  					logWaiting()
    34  					return false, nil
    35  				}
    36  				return true, nil
    37  			}
    38  		}
    39  		return false, fmt.Errorf("machine %s is not configured in pool %s", machine.ID(), pool)
    40  	}
    41  }