Skip to content

Commit

Permalink
Merge pull request #669 from cgalibern/dev
Browse files Browse the repository at this point in the history
Add support for om,ox node event --wait ...
  • Loading branch information
cgalibern authored Jan 20, 2025
2 parents 7f43073 + 96fcb5d commit bbb5413
Show file tree
Hide file tree
Showing 68 changed files with 1,176 additions and 312 deletions.
7 changes: 7 additions & 0 deletions core/client/api/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type GetEvents struct {
relatives *bool
Limit *uint64
Filters []string
Wait bool
Duration *time.Duration
}

Expand All @@ -42,6 +43,11 @@ func (t *GetEvents) SetFilters(filters []string) *GetEvents {
return t
}

func (t *GetEvents) SetWait(wait bool) *GetEvents {
t.Wait = wait
return t
}

func (t *GetEvents) SetNamespace(s string) *GetEvents {
t.namespace = &s
return t
Expand Down Expand Up @@ -179,6 +185,7 @@ func (t GetEvents) eventsBase() (*http.Response, error) {
params := api.GetDaemonEventsParams{
Filter: &t.Filters,
Selector: t.selector,
Cache: &t.Wait,
}
if t.Limit != nil {
i := int64(*t.Limit)
Expand Down
4 changes: 2 additions & 2 deletions core/instance/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func (c *Data[T]) DropNode(nodename string) {
delete(c.nodeToPath, nodename)
}

// Get returns an instance data or nil if data is not found
func (c *Data[T]) Get(p naming.Path, nodename string) *T {
// GetByPathAndNode returns an instance data or nil if data is not found
func (c *Data[T]) GetByPathAndNode(p naming.Path, nodename string) *T {
c.RLock()
v := c.data[InstanceString(p, nodename)]
c.RUnlock()
Expand Down
14 changes: 7 additions & 7 deletions core/instance/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func Test_Mapper(t *testing.T) {
mapper.Set(p, "node2", &Status{Avail: status.Warn})
mapper.Set(p2, "node2", &Status{Avail: status.Down})

require.Equal(t, status.Up, mapper.Get(p, "node1").Avail)
require.Equal(t, status.Warn, mapper.Get(p, "node2").Avail)
require.Equal(t, status.Down, mapper.Get(p2, "node2").Avail)
require.Nil(t, mapper.Get(p, "node3"))
require.Equal(t, status.Up, mapper.GetByPathAndNode(p, "node1").Avail)
require.Equal(t, status.Warn, mapper.GetByPathAndNode(p, "node2").Avail)
require.Equal(t, status.Down, mapper.GetByPathAndNode(p2, "node2").Avail)
require.Nil(t, mapper.GetByPathAndNode(p, "node3"))
require.Len(t, mapper.GetAll(), 3)
require.Len(t, mapper.GetByPath(p), 2)
require.Len(t, mapper.GetByNode("node1"), 1)
Expand All @@ -29,9 +29,9 @@ func Test_Mapper(t *testing.T) {
mapper.Unset(p, "node3")
mapper.Unset(p, "node2")

require.Equal(t, status.Up, mapper.Get(p, "node1").Avail)
require.Nil(t, mapper.Get(p, "node2"))
require.Nil(t, mapper.Get(p, "node3"))
require.Equal(t, status.Up, mapper.GetByPathAndNode(p, "node1").Avail)
require.Nil(t, mapper.GetByPathAndNode(p, "node2"))
require.Nil(t, mapper.GetByPathAndNode(p, "node3"))
require.Len(t, mapper.GetAll(), 2)
require.Len(t, mapper.GetByPath(p), 1)
require.Len(t, mapper.GetByNode("node1"), 1)
Expand Down
4 changes: 2 additions & 2 deletions core/node/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func (c *Data[T]) Unset(nodename string) {
delete(c.data, nodename)
}

// Get return the stored value for nodename or nil if not found
func (c *Data[T]) Get(nodename string) *T {
// GetByNode return the stored value for nodename or nil if not found
func (c *Data[T]) GetByNode(nodename string) *T {
c.RLock()
v := c.data[nodename]
c.RUnlock()
Expand Down
2 changes: 1 addition & 1 deletion core/object/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *Data[T]) Unset(p naming.Path) {
c.Unlock()
}

func (c *Data[T]) Get(p naming.Path) *T {
func (c *Data[T]) GetByPath(p naming.Path) *T {
c.RLock()
v := c.data[p]
c.RUnlock()
Expand Down
3 changes: 1 addition & 2 deletions core/om/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
_ "embed"
"fmt"
"os"

"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -1165,7 +1164,7 @@ func newCmdNodeEvents() *cobra.Command {
addFlagEventTemplate(flags, &options.Template)
addFlagWait(flags, &options.Wait)
addFlagNodeSelector(flags, &options.NodeSelector)
flags.Uint64Var(&options.Limit, "limit", 0, "limit event count to fetch")
flags.Uint64Var(&options.Limit, "limit", 0, "stop listening when <limit> events are received, the default is 0 (unlimited) or 1 if --wait is set")
return cmd
}

Expand Down
4 changes: 4 additions & 0 deletions core/omcmd/node_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func hasInstanceLabel(labels []pubsub.Label, expected ...string) bool {
}

func (t *CmdNodeEvents) Run() error {
if t.Wait && t.Limit == 0 {
t.Limit = 1
}
if t.Local {
t.NodeSelector = hostname.Hostname()
}
Expand Down Expand Up @@ -320,6 +323,7 @@ func (t *CmdNodeEvents) getEvReader(nodename string) (event.ReadCloser, error) {
return t.cli.NewGetEvents().
SetRelatives(false).
SetLimit(t.Limit).
SetWait(t.Wait).
SetFilters(t.Filters).
SetDuration(t.Duration).
SetNodename(nodename).
Expand Down
2 changes: 1 addition & 1 deletion core/ox/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ func newCmdNodeEvents() *cobra.Command {
addFlagEventTemplate(flags, &options.Template)
addFlagWait(flags, &options.Wait)
addFlagNodeSelector(flags, &options.NodeSelector)
flags.Uint64Var(&options.Limit, "limit", 0, "limit event count to fetch")
flags.Uint64Var(&options.Limit, "limit", 0, "stop listening when <limit> events are received, the default is 0 (unlimited) or 1 if --wait is set")
return cmd
}

Expand Down
12 changes: 8 additions & 4 deletions core/oxcmd/node_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package oxcmd

import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"text/template"
"time"

"encoding/json"

"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/clientcontext"
"github.com/opensvc/om3/core/event"
Expand All @@ -30,8 +29,9 @@ type (
Limit uint64
Template string
Wait bool
templ *template.Template
helper *templateHelper

templ *template.Template
helper *templateHelper

cli *client.T
NodeSelector string
Expand Down Expand Up @@ -136,6 +136,9 @@ func hasInstanceLabel(labels []pubsub.Label, expected ...string) bool {
}

func (t *CmdNodeEvents) Run() error {
if t.Wait && t.Limit == 0 {
t.Limit = 1
}
if !clientcontext.IsSet() && t.NodeSelector == "" {
t.NodeSelector = hostname.Hostname()
}
Expand Down Expand Up @@ -313,6 +316,7 @@ func (t *CmdNodeEvents) getEvReader(nodename string) (event.ReadCloser, error) {
return t.cli.NewGetEvents().
SetRelatives(false).
SetLimit(t.Limit).
SetWait(t.Wait).
SetFilters(t.Filters).
SetDuration(t.Duration).
SetNodename(nodename).
Expand Down
2 changes: 1 addition & 1 deletion core/schedule/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *Data[T]) Unset(p naming.Path) {
c.Unlock()
}

func (c *Data[T]) Get(p naming.Path) *T {
func (c *Data[T]) GetByPath(p naming.Path) *T {
c.RLock()
v := c.data[p]
c.RUnlock()
Expand Down
12 changes: 10 additions & 2 deletions daemon/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.0

info:
title: opensvc agent api
version: 3.12.2
version: 3.12.3

paths:
/auth/info:
Expand Down Expand Up @@ -405,6 +405,7 @@ paths:
- $ref: '#/components/parameters/Duration'
- $ref: '#/components/parameters/Limit'
- $ref: '#/components/parameters/EventFilter'
- $ref: '#/components/parameters/EventCache'
- $ref: '#/components/parameters/SelectorOptional'
responses:
200:
Expand Down Expand Up @@ -6841,6 +6842,13 @@ components:
description: report this number of past last log entries
schema:
type: integer
EventCache:
name: cache
in: query
description: start the event feed using the latest past events from the cache that match the specified kind and labels.
schema:
type: boolean
default: false
EventFilter:
name: filter
in: query
Expand All @@ -6850,7 +6858,7 @@ components:
items:
type: string
description: |
filter expression: [kind][,label=value]*
filter expression: [kind][,label=value]*[.dataxxx=value]
example: ObjectStatusUpdated,path=foo
DRBDConfigName:
name: name
Expand Down
16 changes: 16 additions & 0 deletions daemon/api/codegen_client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bbb5413

Please sign in to comment.