Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send an empty payload if a cluster was not found #227

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions internal/core/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func Md5sumFile(filePath string) (string, error) {
return hex.EncodeToString(hash.Sum(nil)), nil
}

func NewCluster() (Cluster, error) {
func NewCluster() (*Cluster, error) {
return NewClusterWithDiscoveryTools(&DiscoveryTools{
CibAdmPath: cibAdmPath,
CrmmonAdmPath: crmmonAdmPath,
Expand All @@ -71,8 +71,16 @@ func NewCluster() (Cluster, error) {
})
}

func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (Cluster, error) {
var cluster = Cluster{
func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (*Cluster, error) {
commandExecutor := utils.Executor{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you changed the order of the things here?
Was something wrong?
Or is it simply to avoid the declaration of a not needed var?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is it simply to avoid the declaration of a not needed var?

this

cibParser := cib.NewCibAdminParser(discoveryTools.CibAdmPath)

cibConfig, err := cibParser.Parse()
if err != nil {
return nil, err
}

var cluster = &Cluster{
Cib: cib.Root{}, //nolint
Crmmon: crmmon.Root{}, //nolint
SBD: SBD{}, //nolint
Expand All @@ -82,43 +90,35 @@ func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (Cluster, erro
Provider: "",
}

commandExecutor := utils.Executor{}
cibParser := cib.NewCibAdminParser(discoveryTools.CibAdmPath)

cibConfig, err := cibParser.Parse()
if err != nil {
return cluster, err
}

cluster.Cib = cibConfig

crmmonParser := crmmon.NewCrmMonParser(discoveryTools.CrmmonAdmPath)

crmmonConfig, err := crmmonParser.Parse()
if err != nil {
return cluster, err
return nil, err
}

cluster.Crmmon = crmmonConfig

// Set MD5-hashed key based on the corosync auth key
cluster.ID, err = getCorosyncAuthkeyMd5(discoveryTools.CorosyncKeyPath)
if err != nil {
return cluster, err
return nil, err
}

cluster.Name = getName(cluster)

if cluster.IsFencingSBD() {
sbdData, err := NewSBD(commandExecutor, cluster.ID, discoveryTools.SBDPath, discoveryTools.SBDConfigPath)
if err != nil {
return cluster, err
return nil, err
}

cluster.SBD = sbdData
}

cluster.DC = isDC(&cluster)
cluster.DC = isDC(cluster)

cloudIdentifier := cloud.NewIdentifier(commandExecutor)
provider, err := cloudIdentifier.IdentifyCloudProvider()
Expand All @@ -135,7 +135,7 @@ func getCorosyncAuthkeyMd5(corosyncKeyPath string) (string, error) {
return kp, err
}

func getName(c Cluster) string {
func getName(c *Cluster) string {
// Handle not named clusters
for _, prop := range c.Cib.Configuration.CrmConfig.ClusterProperties {
if prop.ID == clusterNameProperty {
Expand Down
9 changes: 8 additions & 1 deletion internal/discovery/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func (c ClusterDiscovery) GetInterval() time.Duration {
// Execute one iteration of a discovery and publish the results to the collector
func (c ClusterDiscovery) Discover() (string, error) {
cluster, err := cluster.NewCluster()

if err != nil {
log.Debugf("Error creating the cluster data object: %s", err)
return "No HA cluster discovered on this host", nil
}

err = c.collectorClient.Publish(c.id, cluster)
Expand All @@ -49,5 +49,12 @@ func (c ClusterDiscovery) Discover() (string, error) {
return "", err
}

// If no cluster is found, the discovery payload is sent anyway.
// This is used by the delta deregstration mechanism to remove a node from a cluster,
// when the node is no longer part of a cluster.
if cluster == nil {
return "No HA cluster discovered on this host", nil
}

return fmt.Sprintf("Cluster with name: %s successfully discovered", cluster.Name), nil
}
2 changes: 1 addition & 1 deletion internal/discovery/mocks/discovered_cluster_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/trento-project/agent/test/helpers"
)

func NewDiscoveredClusterMock() cluster.Cluster {
func NewDiscoveredClusterMock() *cluster.Cluster {
cluster, _ := cluster.NewClusterWithDiscoveryTools(&cluster.DiscoveryTools{
CibAdmPath: helpers.GetFixturePath("discovery/cluster/fake_cibadmin.sh"),
CrmmonAdmPath: helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"),
Expand Down