Skip to content

Move datasource/host to pkg #1182

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 7 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"strings"
"time"

"github.com/nginx/agent/v3/pkg/host"

"github.com/nginx/agent/v3/internal/datasource/file"
"github.com/nginx/agent/v3/internal/datasource/host"
"github.com/nginx/agent/v3/internal/logger"

"github.com/goccy/go-yaml"
Expand Down Expand Up @@ -271,7 +272,11 @@ func addDefaultProcessors(collector *Collector) {
}

func addDefaultHostMetricsReceiver(collector *Collector) {
if host.NewInfo().IsContainer() {
isContainer, err := host.NewInfo().IsContainer()
if err != nil {
slog.Warn("Failed to get host info", "error", err)
}
if isContainer {
addDefaultContainerHostMetricsReceiver(collector)
} else {
addDefaultVMHostMetricsReceiver(collector)
Expand Down
10 changes: 7 additions & 3 deletions internal/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"strings"
"sync"

"github.com/nginx/agent/v3/pkg/host"

"github.com/nginx/agent/v3/internal/datasource/file"

"github.com/cenkalti/backoff/v4"
Expand All @@ -27,7 +29,6 @@ import (

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/datasource/host"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
Expand Down Expand Up @@ -88,10 +89,13 @@ func NewGrpcConnection(ctx context.Context, agentConfig *config.Config,

slog.InfoContext(ctx, "Dialing grpc server", "server_addr", serverAddr)

var err error
info := host.NewInfo()
resourceID := info.ResourceID(ctx)
resourceID, err := info.ResourceID(ctx)
if err != nil {
slog.WarnContext(ctx, "Failed to get ResourceID from host info", "error", err.Error())
}

var err error
grpcConnection.mutex.Lock()
grpcConnection.conn, err = grpc.NewClient(serverAddr, DialOptions(agentConfig, commandConfig, resourceID)...)
grpcConnection.mutex.Unlock()
Expand Down
3 changes: 2 additions & 1 deletion internal/resource/nginx_instance_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
"fmt"
"log/slog"

"github.com/nginx/agent/v3/pkg/host/exec"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/datasource/host/exec"
)

type NginxInstanceOperator struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/resource/nginx_instance_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"testing"
"time"

"github.com/nginx/agent/v3/internal/datasource/host/exec/execfakes"
"github.com/nginx/agent/v3/pkg/host/exec/execfakes"

"github.com/nginx/agent/v3/test/helpers"
"github.com/nginx/agent/v3/test/protos"
"github.com/nginx/agent/v3/test/types"
Expand Down
21 changes: 16 additions & 5 deletions internal/resource/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"strings"
"sync"

"github.com/nginx/agent/v3/pkg/host"

parser "github.com/nginx/agent/v3/internal/datasource/config"
datasource "github.com/nginx/agent/v3/internal/datasource/proto"
"github.com/nginx/agent/v3/internal/model"
Expand All @@ -30,8 +32,6 @@ import (

"github.com/nginx/agent/v3/internal/config"

"github.com/nginx/agent/v3/internal/datasource/host"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
)

Expand Down Expand Up @@ -383,12 +383,23 @@ func (r *ResourceService) updateResourceInfo(ctx context.Context) {
r.resourceMutex.Lock()
defer r.resourceMutex.Unlock()

if r.info.IsContainer() {
r.resource.Info = r.info.ContainerInfo(ctx)
isContainer, err := r.info.IsContainer()
if err != nil {
slog.WarnContext(ctx, "Failed to check if resource is container", "error", err)
}

if isContainer {
r.resource.Info, err = r.info.ContainerInfo(ctx)
if err != nil {
slog.ErrorContext(ctx, "Failed to get container info", "error", err)
}
r.resource.ResourceId = r.resource.GetContainerInfo().GetContainerId()
r.resource.Instances = []*mpi.Instance{}
} else {
r.resource.Info = r.info.HostInfo(ctx)
r.resource.Info, err = r.info.HostInfo(ctx)
if err != nil {
slog.ErrorContext(ctx, "Failed to get host info", "error", err)
}
r.resource.ResourceId = r.resource.GetHostInfo().GetHostId()
r.resource.Instances = []*mpi.Instance{}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/resource/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"path/filepath"
"testing"

"github.com/nginx/agent/v3/pkg/host/hostfakes"

"github.com/nginx/agent/v3/internal/model"
"github.com/nginx/agent/v3/internal/watcher/instance/instancefakes"

Expand All @@ -23,8 +25,6 @@ import (
"github.com/nginx/agent/v3/internal/resource/resourcefakes"
"github.com/nginx/agent/v3/test/types"

"github.com/nginx/agent/v3/internal/datasource/host/hostfakes"

"github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/test/protos"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -211,17 +211,17 @@ func TestResourceService_GetResource(t *testing.T) {
mockInfo.ContainerInfoReturns(
&v1.Resource_ContainerInfo{
ContainerInfo: tc.expectedResource.GetContainerInfo(),
},
}, nil,
)
} else {
mockInfo.HostInfoReturns(
&v1.Resource_HostInfo{
HostInfo: tc.expectedResource.GetHostInfo(),
},
}, nil,
)
}

mockInfo.IsContainerReturns(tc.isContainer)
mockInfo.IsContainerReturns(tc.isContainer, nil)

resourceService := NewResourceService(ctx, types.AgentConfig())
resourceService.info = mockInfo
Expand Down
3 changes: 2 additions & 1 deletion internal/watcher/health/nginx_health_watcher_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"context"
"fmt"

"github.com/nginx/agent/v3/pkg/host/exec"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/datasource/host/exec"
processwatcher "github.com/nginx/agent/v3/internal/watcher/process"
"github.com/nginx/agent/v3/pkg/nginxprocess"
)
Expand Down
3 changes: 2 additions & 1 deletion internal/watcher/instance/instance_watcher_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync/atomic"
"time"

"github.com/nginx/agent/v3/pkg/host/exec"

"github.com/nginx/agent/v3/internal/datasource/proto"

parser "github.com/nginx/agent/v3/internal/datasource/config"
Expand All @@ -23,7 +25,6 @@ import (
"github.com/nginx/agent/v3/internal/watcher/process"

"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/datasource/host/exec"
"github.com/nginx/agent/v3/internal/logger"
"github.com/nginx/agent/v3/internal/model"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"context"
"testing"

"github.com/nginx/agent/v3/pkg/host/exec/execfakes"

"github.com/nginx/agent/v3/internal/watcher/instance/instancefakes"
"github.com/nginx/agent/v3/internal/watcher/process/processfakes"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/datasource/host/exec/execfakes"
"github.com/nginx/agent/v3/internal/model"
testModel "github.com/nginx/agent/v3/test/model"
"github.com/nginx/agent/v3/test/protos"
Expand Down
3 changes: 2 additions & 1 deletion internal/watcher/instance/nginx_process_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
"sort"
"strings"

"github.com/nginx/agent/v3/pkg/host/exec"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/datasource/host/exec"
"github.com/nginx/agent/v3/pkg/id"
"github.com/nginx/agent/v3/pkg/nginxprocess"
)
Expand Down
3 changes: 2 additions & 1 deletion internal/watcher/instance/nginx_process_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
"strings"
"testing"

"github.com/nginx/agent/v3/pkg/host/exec/execfakes"

"google.golang.org/protobuf/proto"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/datasource/host/exec/execfakes"
"github.com/nginx/agent/v3/pkg/nginxprocess"
"github.com/nginx/agent/v3/test/helpers"
"github.com/nginx/agent/v3/test/protos"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package exec
import (
"bytes"
"context"
"log/slog"
"errors"
"os"
"os/exec"
"syscall"
Expand All @@ -27,7 +27,7 @@ type ExecInterface interface {
KillProcess(pid int32) error
Hostname() (string, error)
HostID(ctx context.Context) (string, error)
ReleaseInfo(ctx context.Context) (releaseInfo *v1.ReleaseInfo)
ReleaseInfo(ctx context.Context) (releaseInfo *v1.ReleaseInfo, err error)
}

type Exec struct{}
Expand Down Expand Up @@ -67,11 +67,10 @@ func (*Exec) HostID(ctx context.Context) (string, error) {
return host.HostIDWithContext(ctx)
}

func (*Exec) ReleaseInfo(ctx context.Context) (releaseInfo *v1.ReleaseInfo) {
func (*Exec) ReleaseInfo(ctx context.Context) (*v1.ReleaseInfo, error) {
hostInfo, err := host.InfoWithContext(ctx)
if err != nil {
slog.ErrorContext(ctx, "Could not read release information for host", "error", err)
return &v1.ReleaseInfo{}
return &v1.ReleaseInfo{}, errors.New("Could not read release information for host error=" + err.Error())
}

return &v1.ReleaseInfo{
Expand All @@ -80,5 +79,5 @@ func (*Exec) ReleaseInfo(ctx context.Context) (releaseInfo *v1.ReleaseInfo) {
Codename: hostInfo.OS,
Name: hostInfo.PlatformFamily,
Id: hostInfo.Platform,
}
}, nil
}

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

Loading
Loading