Skip to content
Open
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
21 changes: 21 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Go Checks

on:
pull_request: {}
push:
branches:
- main

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: golangci-lint
uses: golangci/golangci-lint-action@v8
with:
version: v2.4
2 changes: 1 addition & 1 deletion client/cmd/data_blameConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var dataBlameConfig = &cobra.Command{
}
b, err := opts.Marshal(rsp.ConfigTree)
if err != nil {
fmt.Fprintf(os.Stdout, "%v", err)
_, _ = fmt.Fprintf(os.Stdout, "%v", err)
Copy link
Author

Choose a reason for hiding this comment

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

Since you're not using a simple printf, you may want to check the return error of the function at least.

}
fmt.Println(string(b))
}
Expand Down
4 changes: 2 additions & 2 deletions client/cmd/datastore_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func printDataStoreTable(rsp *sdcpb.GetDataStoreResponse) {
table := tablewriter.NewWriter(os.Stdout)
table.Header([]string{"Name", "Schema", "Protocol", "Address", "State"})
table.Options(tablewriter.WithAlignment(tw.Alignment{tw.AlignLeft}))
table.Bulk(toTableData(rsp))
table.Render()
_ = table.Bulk(toTableData(rsp))
Copy link
Author

Choose a reason for hiding this comment

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

Here again, maybe you want to check the returned error here.

_ = table.Render()
Copy link
Author

Choose a reason for hiding this comment

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

Same here (error check).

}

func toTableData(rsp *sdcpb.GetDataStoreResponse) [][]string {
Expand Down
4 changes: 2 additions & 2 deletions client/cmd/datastore_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ func printDataStoresTable(rsp *sdcpb.ListDataStoreResponse) {
table := tablewriter.NewWriter(os.Stdout)
table.Header([]string{"Name", "Schema", "Protocol", "Address", "State", "Candidate (C/O/P)"})
table.Options(tablewriter.WithAlignment(tw.Alignment{tw.AlignLeft}))
table.Bulk(tableData)
table.Render()
_ = table.Bulk(tableData)
Copy link
Author

Choose a reason for hiding this comment

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

Error check

_ = table.Render()
Copy link
Author

Choose a reason for hiding this comment

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

Error check

}
4 changes: 2 additions & 2 deletions client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func init() {
func createDataClient(ctx context.Context, addr string) (sdcpb.DataServerClient, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, addr,
grpc.WithBlock(),
cc, err := grpc.DialContext(ctx, addr, //nolint:staticcheck
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have added nolint for this deprecated call for now. At the same time I've opened the issue #377 that is about changing this to the NewClient() call.

Copy link
Author

Choose a reason for hiding this comment

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

Alright, works for me.

grpc.WithBlock(), //nolint:staticcheck
grpc.WithTransportCredentials(
insecure.NewCredentials(),
),
Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/sdcio/data-server/pkg/config"
"github.com/sdcio/data-server/pkg/server"
"github.com/sdcio/logger"
logf "github.com/sdcio/logger"
"github.com/spf13/pflag"
)

Expand All @@ -58,7 +57,7 @@ func main() {

slogOpts := &slog.HandlerOptions{
Level: slog.LevelInfo,
ReplaceAttr: logf.ReplaceTimeAttr,
ReplaceAttr: logger.ReplaceTimeAttr,
}
if debug {
slogOpts.Level = slog.Level(-logger.VDebug)
Expand All @@ -68,8 +67,8 @@ func main() {
}

log := logr.FromSlogHandler(slog.NewJSONHandler(os.Stdout, slogOpts))
logf.SetDefaultLogger(log)
ctx := logf.IntoContext(context.Background(), log)
logger.SetDefaultLogger(log)
ctx := logger.IntoContext(context.Background(), log)

go func() {
log.Info("pprof server started", "address", "localhost:6060")
Expand Down
16 changes: 9 additions & 7 deletions pkg/datastore/datastore_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

"github.com/sdcio/logger"
logf "github.com/sdcio/logger"
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
"google.golang.org/grpc"

Expand Down Expand Up @@ -77,11 +76,11 @@ func New(ctx context.Context, c *config.DatastoreConfig, sc schema.Client, cc ca

ctx, cancel := context.WithCancel(ctx)

log := logf.FromContext(ctx)
log := logger.FromContext(ctx)
log = log.WithName("datastore").WithValues(
"datastore-name", c.Name,
)
ctx = logf.IntoContext(ctx, log)
ctx = logger.IntoContext(ctx, log)

log.Info("new datastore",
"target-name", c.Name,
Expand Down Expand Up @@ -144,11 +143,11 @@ func (d *Datastore) IntentsList(ctx context.Context) ([]string, error) {
}

func (d *Datastore) initCache(ctx context.Context) {
log := logf.FromContext(ctx)
log := logger.FromContext(ctx)

exists := d.cacheClient.InstanceExists(ctx)
if exists {
log.V(logf.VDebug).Info("cache already exists")
log.V(logger.VDebug).Info("cache already exists")
return
}
log.Info("creating cache instance")
Expand All @@ -162,7 +161,7 @@ CREATE:
}

func (d *Datastore) connectSBI(ctx context.Context, opts ...grpc.DialOption) error {
log := logf.FromContext(ctx)
log := logger.FromContext(ctx)

var err error
d.sbi, err = target.New(ctx, d.config.Name, d.config.SBI, d.schemaClient, d, d.config.Sync.Config, d.taskPool, opts...)
Expand Down Expand Up @@ -222,7 +221,7 @@ func (d *Datastore) Stop(ctx context.Context) error {
}
err := d.sbi.Close(ctx)
if err != nil {
logf.DefaultLogger.Error(err, "datastore failed to close the target connection", "datastore-name", d.Name())
logger.DefaultLogger.Error(err, "datastore failed to close the target connection", "datastore-name", d.Name())
}
return nil
}
Expand All @@ -248,6 +247,9 @@ func (d *Datastore) BlameConfig(ctx context.Context, includeDefaults bool) (*sdc
bcp := tree.NewBlameConfigProcessor(tree.NewBlameConfigProcessorConfig(includeDefaults))

bte, err := bcp.Run(ctx, root.GetRoot(), blamePool)
if err != nil {
return nil, err
}

// set the root level elements name to the target name
bte.Name = d.config.Name
Expand Down
23 changes: 11 additions & 12 deletions pkg/datastore/deviations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"github.com/sdcio/data-server/pkg/config"
treetypes "github.com/sdcio/data-server/pkg/tree/types"
"github.com/sdcio/logger"
logf "github.com/sdcio/logger"
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
status "google.golang.org/grpc/status"
)

func (d *Datastore) WatchDeviations(req *sdcpb.WatchDeviationRequest, stream sdcpb.DataServer_WatchDeviationsServer) error {
log := logf.FromContext(d.ctx)
log := logger.FromContext(d.ctx)

ctx := stream.Context()
p, ok := peer.FromContext(ctx)
Expand All @@ -33,7 +32,7 @@ func (d *Datastore) WatchDeviations(req *sdcpb.WatchDeviationRequest, stream sdc
}

func (d *Datastore) StopDeviationsWatch(stream sdcpb.DataServer_WatchDeviationsServer) {
log := logf.FromContext(d.ctx)
log := logger.FromContext(d.ctx)
d.m.Lock()
defer d.m.Unlock()
peer := d.deviationClients[stream]
Expand All @@ -42,8 +41,8 @@ func (d *Datastore) StopDeviationsWatch(stream sdcpb.DataServer_WatchDeviationsS
}

func (d *Datastore) DeviationMgr(ctx context.Context, c *config.DeviationConfig) {
log := logf.FromContext(ctx).WithName("DeviationManager").WithValues("target-name", d.config.Name)
ctx = logf.IntoContext(ctx, log)
log := logger.FromContext(ctx).WithName("DeviationManager").WithValues("target-name", d.config.Name)
ctx = logger.IntoContext(ctx, log)

log.Info("starting deviation manager")
ticker := time.NewTicker(c.Interval)
Expand All @@ -57,7 +56,7 @@ func (d *Datastore) DeviationMgr(ctx context.Context, c *config.DeviationConfig)
log.Info("datastore context done, stopping deviation manager")
return
case <-ticker.C:
log.V(logf.VDebug).Info("deviation calc run - start")
log.V(logger.VDebug).Info("deviation calc run - start")

deviationClients := map[string]sdcpb.DataServer_WatchDeviationsServer{}
deviationClientNames := make([]string, 0, len(d.deviationClients))
Expand All @@ -76,10 +75,10 @@ func (d *Datastore) DeviationMgr(ctx context.Context, c *config.DeviationConfig)
}
}()
if len(deviationClients) == 0 {
log.V(logf.VDebug).Info("no deviation clients present")
log.V(logger.VDebug).Info("no deviation clients present")
continue
}
log.V(logf.VDebug).Info("deviation clients", "clients", deviationClientNames)
log.V(logger.VDebug).Info("deviation clients", "clients", deviationClientNames)
for clientIdentifier, dc := range deviationClients {
err := dc.Send(&sdcpb.WatchDeviationResponse{
Name: d.config.Name,
Expand All @@ -95,7 +94,7 @@ func (d *Datastore) DeviationMgr(ctx context.Context, c *config.DeviationConfig)
log.Error(err, "failed to calculate deviations")
continue
}
log.V(logf.VDebug).Info("calculate deviations", "duration", time.Since(start))
log.V(logger.VDebug).Info("calculate deviations", "duration", time.Since(start))
d.SendDeviations(ctx, deviationChan, deviationClients)
log.Info("Before sending DeviationEvent_END")
for clientIdentifier, dc := range deviationClients {
Expand All @@ -110,13 +109,13 @@ func (d *Datastore) DeviationMgr(ctx context.Context, c *config.DeviationConfig)
log.Error(err, "error sending deviation", "client-identifier", clientIdentifier)
}
}
log.V(logf.VDebug).Info("deviation calc run - finished")
log.V(logger.VDebug).Info("deviation calc run - finished")
}
}
}

func (d *Datastore) SendDeviations(ctx context.Context, ch <-chan *treetypes.DeviationEntry, deviationClients map[string]sdcpb.DataServer_WatchDeviationsServer) {
log := logf.FromContext(ctx)
log := logger.FromContext(ctx)
for deviation := range ch {
for clientIdentifier, dc := range deviationClients {
if dc.Context().Err() != nil {
Expand All @@ -141,7 +140,7 @@ func (d *Datastore) SendDeviations(ctx context.Context, ch <-chan *treetypes.Dev
if err != nil {
// ignore client-side cancellation (context closed) as it's expected when a client disconnects
if dc.Context().Err() != nil || status.Code(err) == codes.Canceled {
log.V(logf.VDebug).Info("client context closed, skipping send", "client-identifier", clientIdentifier, "err", err)
log.V(logger.VDebug).Info("client context closed, skipping send", "client-identifier", clientIdentifier, "err", err)
continue
}
log.Error(err, "error sending deviation", "client-identifier", clientIdentifier)
Expand Down
5 changes: 2 additions & 3 deletions pkg/datastore/target/gnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
targetTypes "github.com/sdcio/data-server/pkg/datastore/target/types"
"github.com/sdcio/data-server/pkg/pool"
"github.com/sdcio/data-server/pkg/utils"
dsutils "github.com/sdcio/data-server/pkg/utils"
logf "github.com/sdcio/logger"
sdcpb "github.com/sdcio/sdc-protos/sdcpb"

Expand All @@ -47,11 +46,11 @@ type gnmiTarget struct {
cfg *config.SBI
syncs map[string]GnmiSync
runningStore targetTypes.RunningStore
schemaClient dsutils.SchemaClientBound
schemaClient utils.SchemaClientBound
taskpoolFactory pool.VirtualPoolFactory
}

func NewTarget(ctx context.Context, name string, cfg *config.SBI, runningStore targetTypes.RunningStore, schemaClient dsutils.SchemaClientBound, taskpoolFactory pool.VirtualPoolFactory, opts ...grpc.DialOption) (*gnmiTarget, error) {
func NewTarget(ctx context.Context, name string, cfg *config.SBI, runningStore targetTypes.RunningStore, schemaClient utils.SchemaClientBound, taskpoolFactory pool.VirtualPoolFactory, opts ...grpc.DialOption) (*gnmiTarget, error) {
tc := &types.TargetConfig{
Name: name,
Address: fmt.Sprintf("%s:%d", cfg.Address, cfg.Port),
Expand Down
3 changes: 2 additions & 1 deletion pkg/datastore/target/gnmi/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gnmi
import (
"context"
"errors"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -209,7 +210,7 @@ func (s *StreamSync) gnmiSubscribe(subReq *gnmi.SubscribeRequest, updChan chan<-
syncResponse <- struct{}{}

case *gnmi.SubscribeResponse_Error:
log.Error(nil, "gnmi subscription error", "error", r.Error.Message)
log.Error(fmt.Errorf("%s", r.Error.Message), "gnmi subscription error") //nolint:staticcheck
Copy link
Author

Choose a reason for hiding this comment

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

Why is linting disabled?
Something similar to what's in other files isn't possible?

logger.DefaultLogger.Error(err, "datastore failed to close the target connection", "datastore-name", d.Name())

}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/datastore/target/netconf/nc.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (t *ncTarget) internalGet(ctx context.Context, req *sdcpb.GetDataRequest) (
ncResponse, err := t.driver.GetConfig(source, filterDoc)
if err != nil {
if strings.Contains(err.Error(), "EOF") {
t.Close(ctx)
_ = t.Close(ctx)
Copy link
Author

Choose a reason for hiding this comment

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

If the underlying channel and transport can not be closed cleanly, this may lead to other problems. You should check (or at least log) whether there was an error here and process it.

go t.reconnect(ctx)
}
return nil, err
Expand Down Expand Up @@ -261,7 +261,7 @@ func (t *ncTarget) setToDevice(ctx context.Context, commitDatastore string, sour
if err != nil {
log.Error(err, "failed during edit-config")
if strings.Contains(err.Error(), "EOF") {
t.Close(ctx)
_ = t.Close(ctx)
Copy link
Author

Choose a reason for hiding this comment

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

Again check the returned error and log/process it.

go t.reconnect(ctx)
return nil, err
}
Expand All @@ -288,7 +288,7 @@ func (t *ncTarget) setToDevice(ctx context.Context, commitDatastore string, sour
err = t.driver.Commit()
if err != nil {
if strings.Contains(err.Error(), "EOF") {
t.Close(ctx)
_ = t.Close(ctx)
Copy link
Author

Choose a reason for hiding this comment

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

Again: log/process returned error.

go t.reconnect(ctx)
}
return nil, err
Expand Down
7 changes: 5 additions & 2 deletions pkg/datastore/target/netconf/nc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestLeafList(t *testing.T) {
HonorNamespace: true,
})

xmlBuilder.AddValue(ctx, &sdcpb.Path{
_ = xmlBuilder.AddValue(ctx, &sdcpb.Path{
Copy link
Author

Choose a reason for hiding this comment

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

AddValue seems to be a pretty difficult function that can return different errors. You may process/log them here.

Elem: []*sdcpb.PathElem{
{
Name: "leaflist",
Expand Down Expand Up @@ -367,7 +367,10 @@ func Test_filterRPCErrors(t *testing.T) {
`

doc := etree.NewDocument()
doc.ReadFromString(xml)
err := doc.ReadFromString(xml)
if err != nil {
t.Error(err)
}

type args struct {
xml *etree.Document
Expand Down
7 changes: 6 additions & 1 deletion pkg/datastore/target/netconf/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ func (s *NetconfSyncImpl) Start() error {
return nil
}

go s.internalSync(req)
go func() {
err := s.internalSync(req)
if err != nil {
log.Error(err, "failed syncing")
}
}()

go func() {
ticker := time.NewTicker(s.config.Interval)
Expand Down
4 changes: 2 additions & 2 deletions pkg/datastore/target/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (t *noopTarget) AddSyncs(ctx context.Context, sps ...*config.SyncProtocol)
}

func (t *noopTarget) Get(ctx context.Context, req *sdcpb.GetDataRequest) (*sdcpb.GetDataResponse, error) {
log := logf.FromContext(ctx).WithName("Get")
ctx = logf.IntoContext(ctx, log)
// log := logf.FromContext(ctx).WithName("Get")
// ctx = logf.IntoContext(ctx, log)
Copy link
Author

Choose a reason for hiding this comment

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

Can those comments be removed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes


result := &sdcpb.GetDataResponse{
Notification: make([]*sdcpb.Notification, 0, len(req.GetPath())),
Expand Down
Loading
Loading