Skip to content
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
44 changes: 44 additions & 0 deletions core/collector/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package collector

import (
"errors"
"time"
)

type (
Config struct {
FeederUrl string `json:"feeder_url"`
ServerUrl string `json:"server_url"`
Timeout time.Duration `json:"timeout"`
Insecure bool `json:"insecure"`

// Hidden fields
Password string `json:"-"`
}
)

var (
ErrConfig = errors.New("collector is not configured: empty configuration keyword node.dbopensvc")
ErrUnregistered = errors.New("this node is not registered. try 'om node register'")
)

func (t *Config) Equal(o *Config) bool {
if t == nil && o != nil {
return false
}
if t != nil && o == nil {
return false
}
if t != nil && o != nil && *t != *o {
return false
}
return true
}

func (t *Config) DeepCopy() *Config {
if t == nil {
return nil
}
n := *t
return &n
}
19 changes: 16 additions & 3 deletions core/collector/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collector
import (
"context"
"crypto/tls"
"encoding/base64"
"fmt"
"net/http"
"net/url"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/ybbus/jsonrpc"

"github.com/opensvc/om3/v3/util/hostname"
"github.com/opensvc/om3/v3/util/httphelper"
"github.com/opensvc/om3/v3/util/plog"
)

Expand Down Expand Up @@ -186,13 +188,24 @@ func BaseURL(s string) (*url.URL, error) {
return u, nil
}

func (c *Config) NewFeedRequester() (*httphelper.T, error) {
if c.FeederUrl == "" {
return nil, ErrConfig
} else if c.Password == "" {
return nil, ErrUnregistered
}

auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(hostname.Hostname()+":"+c.Password))
return NewRequester(c.FeederUrl, auth, c.Insecure)
}

// NewFeedClient returns a Client to call the collector feed app jsonrpc2 methods.
func NewFeedClient(endpoint, secret string) (*Client, error) {
u, err := FeedURL(endpoint)
func (c *Config) NewFeedClient() (*Client, error) {
u, err := FeedURL(c.FeederUrl)
if err != nil {
return nil, err
}
return newClient(u, secret)
return newClient(u, c.Password)
}

// NewComplianceClient returns a Client to call the collector init app jsonrpc2 methods.
Expand Down
5 changes: 3 additions & 2 deletions core/commoncmd/node_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/opensvc/om3/v3/core/output"
"github.com/opensvc/om3/v3/core/rawconfig"
"github.com/opensvc/om3/v3/daemon/msgbus"
"github.com/opensvc/om3/v3/util/flatten"
"github.com/opensvc/om3/v3/util/pubsub"
)

Expand All @@ -36,7 +37,7 @@ type (
NodeSelector string
errC chan error
evC chan *event.Event
diff *output.Delta
diff *flatten.Delta
}

templateHelper struct {
Expand Down Expand Up @@ -156,7 +157,7 @@ func (t *CmdNodeEvents) DoNodes() error {
nodenames []string
)
if t.Output == "diff" {
t.diff = output.NewDiff()
t.diff = flatten.NewDiff()
}

t.evC = make(chan *event.Event)
Expand Down
3 changes: 2 additions & 1 deletion core/commoncmd/text/node-events/flag/filter
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ Receive only events matching the filtering expression formatted as:
NodeAlive, NodeStale
NodeConfigUpdated, NodeDataUpdated, NodeFrozen
NodeFrozenFileRemoved, NodeFrozenFileUpdated
NodeLabelsUpdated, NodeLabelsCommited
NodeMonitorDeleted, NodeMonitorUpdated
NodeOsPathsUpdated
NodePoolStatusUpdated, NodePoolStatusDeleted
NodeRejoin, NodeSplitAction
NodeStatsUpdated
NodeStatusUpdated
NodeStatusArbitratorsUpdated, NodeStatusGenUpdates, NodeStatusLabelsUpdated
NodeStatusArbitratorsUpdated, NodeStatusGenUpdates,
SetNodeMonitor

### Object
Expand Down
77 changes: 76 additions & 1 deletion core/node/config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package node

import (
"encoding/json"
"maps"
"slices"
"time"

"github.com/opensvc/om3/v3/core/collector"
"github.com/opensvc/om3/v3/core/schedule"
"github.com/opensvc/om3/v3/util/flatten"
"github.com/opensvc/om3/v3/util/label"
"github.com/opensvc/om3/v3/util/xmap"
)

type (
Config struct {
Collector *collector.Config `json:"collector,omitempty"`
Env string `json:"env"`
Hooks Hooks `json:"hooks"`
Labels label.M `json:"labels"`
MaintenanceGracePeriod time.Duration `json:"maintenance_grace_period"`
MaxParallel int `json:"max_parallel"`
MaxKeySize int64 `json:"max_key_size"`
Expand All @@ -21,16 +31,27 @@ type (
SSHKey string `json:"sshkey"`
PRKey string `json:"prkey"`
}

Hooks []Hook

Hook struct {
Name string `json:"name"`
Events []string `json:"events"`
Command []string `json:"command"`
}
)

func (cfg *Config) DeepCopy() *Config {
newCfg := *cfg
newCfg.Schedules = append([]schedule.Config{}, cfg.Schedules...)
newCfg.Labels = cfg.Labels.DeepCopy()
newCfg.Hooks = cfg.Hooks.DeepCopy()
newCfg.Collector = cfg.Collector.DeepCopy()
return &newCfg

}

func (c Config) Equals(other Config) bool {
func (c Config) Equal(other Config) bool {
if c.Env != other.Env ||
c.MaintenanceGracePeriod != other.MaintenanceGracePeriod ||
c.MaxParallel != other.MaxParallel ||
Expand All @@ -45,6 +66,13 @@ func (c Config) Equals(other Config) bool {
return false
}

if !c.Collector.Equal(other.Collector) {
return false
}
if !maps.Equal(c.Labels, other.Labels) {
return false
}

// Compare Schedules slice
if len(c.Schedules) != len(other.Schedules) {
return false
Expand All @@ -55,5 +83,52 @@ func (c Config) Equals(other Config) bool {
}
}

// Compare Hook slice
if len(c.Hooks) != len(other.Hooks) {
return false
}
for i := range c.Hooks {
if !c.Hooks[i].Equal(&other.Hooks[i]) {
return false
}
}
return true
}

func (t Hooks) DeepCopy() Hooks {
l := make(Hooks, len(t))
for i, hook := range t {
l[i] = *hook.DeepCopy()
}
return l
}

func (t *Hook) Equal(o *Hook) bool {
if t.Name != o.Name {
return false
} else if !slices.Equal(t.Events, o.Events) {
return false
} else if !slices.Equal(t.Command, o.Command) {
return false
}
return true
}

func (t *Hook) DeepCopy() *Hook {
n := *t
n.Events = append([]string{}, t.Events...)
n.Command = append([]string{}, t.Command...)
return &n
}

func (t *Hook) Diff(other Hook) string {
flattenable := func(hook Hook) map[string]any {
var m map[string]any
b, _ := json.Marshal(hook)
json.Unmarshal(b, &m)
return m
}
m1 := flatten.Flatten(flattenable(*t))
m2 := flatten.Flatten(flattenable(other))
return xmap.Diff(m1, m2)
}
3 changes: 0 additions & 3 deletions core/node/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/opensvc/om3/v3/core/instance"
"github.com/opensvc/om3/v3/core/status"
"github.com/opensvc/om3/v3/util/label"
)

type (
Expand All @@ -18,7 +17,6 @@ type (
Gen Gen `json:"gen"`
IsLeader bool `json:"is_leader"`
IsOverloaded bool `json:"is_overloaded"`
Labels label.M `json:"labels"`
BootedAt time.Time `json:"booted_at"`
}

Expand Down Expand Up @@ -59,7 +57,6 @@ func (t *Status) DeepCopy() *Status {
newGen[n] = v
}
result.Gen = newGen
result.Labels = t.Labels.DeepCopy()

return &result
}
4 changes: 0 additions & 4 deletions core/node/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func Test_TStatus_DeepCopy(t *testing.T) {
},
IsOverloaded: false,
IsLeader: true,
Labels: map[string]string{
"node1": "abc",
"node2": "efg",
},
}

copyValue := value.DeepCopy()
Expand Down
Loading