diff --git a/internal/core/cluster/cluster.go b/internal/core/cluster/cluster.go index af7db189..3097a1df 100644 --- a/internal/core/cluster/cluster.go +++ b/internal/core/cluster/cluster.go @@ -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, @@ -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{} + 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 @@ -82,21 +90,13 @@ 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 @@ -104,7 +104,7 @@ func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (Cluster, erro // 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) @@ -112,13 +112,13 @@ func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (Cluster, erro 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() @@ -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 { diff --git a/internal/discovery/cluster.go b/internal/discovery/cluster.go index 0f398443..64c202a2 100644 --- a/internal/discovery/cluster.go +++ b/internal/discovery/cluster.go @@ -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) @@ -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 } diff --git a/internal/discovery/mocks/discovered_cluster_mock.go b/internal/discovery/mocks/discovered_cluster_mock.go index d8140038..43e4e3d0 100644 --- a/internal/discovery/mocks/discovered_cluster_mock.go +++ b/internal/discovery/mocks/discovered_cluster_mock.go @@ -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"),