Skip to content

Commit

Permalink
Use real plugin installation path and state directory path
Browse files Browse the repository at this point in the history
  • Loading branch information
XuechunHou committed Jan 24, 2025
1 parent e0a2e32 commit 9045ae7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
4 changes: 2 additions & 2 deletions builds/ops_agent_plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

set -x -e
DESTDIR=$1
mkdir -p "$DESTDIR/opt/google-cloud-ops-agent/libexec"
go build -buildvcs=false -ldflags "-s -w" -o "$DESTDIR/opt/google-cloud-ops-agent/libexec/google_cloud_ops_agent_uap_plugin" \
mkdir -p "$DESTDIR/opt/google-cloud-ops-agent"
go build -buildvcs=false -ldflags "-s -w" -o "$DESTDIR/opt/google-cloud-ops-agent/plugin" \
github.com/GoogleCloudPlatform/ops-agent/cmd/ops_agent_uap_plugin
7 changes: 2 additions & 5 deletions cmd/ops_agent_uap_plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os/exec"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

pb "github.com/GoogleCloudPlatform/ops-agent/cmd/ops_agent_uap_plugin/google_guest_agent/plugin"
)
Expand Down Expand Up @@ -84,11 +85,7 @@ func main() {
// offered mean Guest Agent was successful in installing/launching the plugin
// & will manage the lifecycle (start, stop, or revision change) here onwards.
pb.RegisterGuestAgentPluginServer(server, ps)

ctx := context.Background()
ps.GetStatus(ctx, &pb.GetStatusRequest{})
ps.Start(ctx, &pb.StartRequest{})

reflection.Register(server)
if err := server.Serve(listener); err != nil {
fmt.Fprintf(os.Stderr, "Exiting, cannot continue serving: %v\n", err)
os.Exit(1)
Expand Down
57 changes: 35 additions & 22 deletions cmd/ops_agent_uap_plugin/service_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os/exec"
"os/signal"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
Expand All @@ -46,7 +47,7 @@ const (
FluentBitStateDiectory = "state/fluent-bit"
FluentBitRuntimeDirectory = "run/google-cloud-ops-agent-fluent-bit"
OtelRuntimeDirectory = "run/google-cloud-ops-agent-opentelemetry-collector"
DefaultPluginStateDirectory = "/var/lib/google-guest-agent/plugins/ops-agent-plugin"
DefaultPluginStateDirectory = "/var/lib/google-guest-agent/agent_state/plugins/ops-agent-plugin"
)

var (
Expand Down Expand Up @@ -80,6 +81,18 @@ func (ps *OpsAgentPluginServer) Start(ctx context.Context, msg *pb.StartRequest)
pContext, cancel := context.WithCancel(context.Background())
ps.cancel = cancel

pluginInstallaPath, err := os.Executable()
if err != nil {
log.Printf("Start() failed, because it cannot determine the plugin install location: %s", err)
return nil, status.Error(1, err.Error())
}
pluginInstallaPath, err = filepath.EvalSymlinks(pluginInstallaPath)
if err != nil {
log.Printf("Start() failed, because it cannot determine the plugin install location: %s", err)
status.Error(1, err.Error())
}
pluginInstallDir := filepath.Dir(pluginInstallaPath)

pluginStateDir := msg.GetConfig().GetStateDirectoryPath()
if pluginStateDir == "" {
pluginStateDir = DefaultPluginStateDirectory
Expand All @@ -95,22 +108,22 @@ func (ps *OpsAgentPluginServer) Start(ctx context.Context, msg *pb.StartRequest)
}

// Ops Agent config validation
if err := validateOpsAgentConfig(pContext, ps.runCommand, pluginStateDir); err != nil {
if err := validateOpsAgentConfig(pContext, pluginInstallDir, ps.runCommand); err != nil {
log.Printf("Start() failed: %s", err)
ps.cancel()
ps.cancel = nil
return nil, status.Errorf(1, "failed to validate Ops Agent config: %s", err)
}
// Subagent config generation
if err := generateSubagentConfigs(pContext, ps.runCommand, pluginStateDir); err != nil {
if err := generateSubagentConfigs(pContext, ps.runCommand, pluginInstallDir, pluginStateDir); err != nil {
log.Printf("Start() failed: %s", err)
ps.cancel()
ps.cancel = nil
return nil, status.Errorf(1, "failed to generate subagent configs: %s", err)
}

// the diagnostics service and subagent startups
go runSubagents(pContext, cancel, pluginStateDir, restartCommand, ps.runCommand)
go runSubagents(pContext, cancel, pluginInstallDir, pluginStateDir, restartCommand, ps.runCommand)
return &pb.StartResponse{}, nil
}

Expand Down Expand Up @@ -155,7 +168,7 @@ func (ps *OpsAgentPluginServer) GetStatus(ctx context.Context, msg *pb.GetStatus
//
// cancel: the cancel function for the parent context. By calling this function, the parent context is canceled,
// and GetStatus() returns a non-healthy status, signaling UAP to re-trigger Start().
func runSubagents(ctx context.Context, cancel context.CancelFunc, pluginBaseLocation string, restartCommand RestartCommandFunc, runCommand RunCommandFunc) {
func runSubagents(ctx context.Context, cancel context.CancelFunc, pluginInstallDirectory string, pluginStateDirectory string, restartCommand RestartCommandFunc, runCommand RunCommandFunc) {
// Register signal handler and implements its callback.
sigHandler(ctx, func(_ os.Signal) {
cancel()
Expand All @@ -164,29 +177,29 @@ func runSubagents(ctx context.Context, cancel context.CancelFunc, pluginBaseLoca
var wg sync.WaitGroup
// Starting the diagnostics service
runDiagnosticsCmd := exec.CommandContext(ctx,
path.Join(pluginBaseLocation, DiagnosticsBinary),
path.Join(pluginInstallDirectory, DiagnosticsBinary),
"-config", OpsAgentConfigLocationLinux,
)
wg.Add(1)
go restartCommand(ctx, cancel, runDiagnosticsCmd, runCommand, &wg)

// Starting Otel
runOtelCmd := exec.CommandContext(ctx,
path.Join(pluginBaseLocation, OtelBinary),
"--config", path.Join(pluginBaseLocation, OtelRuntimeDirectory, "otel.yaml"),
path.Join(pluginInstallDirectory, OtelBinary),
"--config", path.Join(pluginStateDirectory, OtelRuntimeDirectory, "otel.yaml"),
)
wg.Add(1)
go restartCommand(ctx, cancel, runOtelCmd, runCommand, &wg)

// Starting FluentBit
runFluentBitCmd := exec.CommandContext(ctx,
path.Join(pluginBaseLocation, AgentWrapperBinary),
path.Join(pluginInstallDirectory, AgentWrapperBinary),
"-config_path", OpsAgentConfigLocationLinux,
"-log_path", path.Join(pluginBaseLocation, LogsDirectory, "subagents/logging-module.log"),
path.Join(pluginBaseLocation, FluentbitBinary),
"--config", path.Join(pluginBaseLocation, FluentBitRuntimeDirectory, "fluent_bit_main.conf"),
"--parser", path.Join(pluginBaseLocation, FluentBitRuntimeDirectory, "fluent_bit_parser.conf"),
"--storage_path", path.Join(pluginBaseLocation, FluentBitStateDiectory, "buffers"),
"-log_path", path.Join(pluginStateDirectory, LogsDirectory, "subagents/logging-module.log"),
path.Join(pluginInstallDirectory, FluentbitBinary),
"--config", path.Join(pluginStateDirectory, FluentBitRuntimeDirectory, "fluent_bit_main.conf"),
"--parser", path.Join(pluginStateDirectory, FluentBitRuntimeDirectory, "fluent_bit_parser.conf"),
"--storage_path", path.Join(pluginStateDirectory, FluentBitStateDiectory, "buffers"),
)
wg.Add(1)
go restartCommand(ctx, cancel, runFluentBitCmd, runCommand, &wg)
Expand Down Expand Up @@ -256,9 +269,9 @@ func runCommand(cmd *exec.Cmd) (string, error) {
return string(out), err
}

func validateOpsAgentConfig(ctx context.Context, runCommand RunCommandFunc, pluginBaseLocation string) error {
func validateOpsAgentConfig(ctx context.Context, pluginInstallDirectory string, runCommand RunCommandFunc) error {
configValidationCmd := exec.CommandContext(ctx,
path.Join(pluginBaseLocation, ConfGeneratorBinary),
path.Join(pluginInstallDirectory, ConfGeneratorBinary),
"-in", OpsAgentConfigLocationLinux,
)
if output, err := runCommand(configValidationCmd); err != nil {
Expand All @@ -267,14 +280,14 @@ func validateOpsAgentConfig(ctx context.Context, runCommand RunCommandFunc, plug
return nil
}

func generateSubagentConfigs(ctx context.Context, runCommand RunCommandFunc, pluginBaseLocation string) error {
confGeneratorBinaryFullPath := path.Join(pluginBaseLocation, ConfGeneratorBinary)
func generateSubagentConfigs(ctx context.Context, runCommand RunCommandFunc, pluginInstallDirectory string, pluginStateDirectory string) error {
confGeneratorBinaryFullPath := path.Join(pluginInstallDirectory, ConfGeneratorBinary)
otelConfigGenerationCmd := exec.CommandContext(ctx,
confGeneratorBinaryFullPath,
"-service", "otel",
"-in", OpsAgentConfigLocationLinux,
"-out", path.Join(pluginBaseLocation, OtelRuntimeDirectory),
"-logs", path.Join(pluginBaseLocation, LogsDirectory))
"-out", path.Join(pluginStateDirectory, OtelRuntimeDirectory),
"-logs", path.Join(pluginStateDirectory, LogsDirectory))

if output, err := runCommand(otelConfigGenerationCmd); err != nil {
return fmt.Errorf("failed to generate Otel config:\ncommand output: %s\ncommand error: %s", output, err)
Expand All @@ -284,8 +297,8 @@ func generateSubagentConfigs(ctx context.Context, runCommand RunCommandFunc, plu
confGeneratorBinaryFullPath,
"-service", "fluentbit",
"-in", OpsAgentConfigLocationLinux,
"-out", path.Join(pluginBaseLocation, FluentBitRuntimeDirectory),
"-logs", path.Join(pluginBaseLocation, LogsDirectory), "-state", path.Join(pluginBaseLocation, FluentBitStateDiectory))
"-out", path.Join(pluginStateDirectory, FluentBitRuntimeDirectory),
"-logs", path.Join(pluginStateDirectory, LogsDirectory), "-state", path.Join(pluginStateDirectory, FluentBitStateDiectory))

if output, err := runCommand(fluentBitConfigGenerationCmd); err != nil {
return fmt.Errorf("failed to generate Fluntbit config:\ncommand output: %s\ncommand error: %s", output, err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/ops_agent_uap_plugin/service_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Test_validateOpsAgentConfig(t *testing.T) {
}

ctx := context.Background()
err := validateOpsAgentConfig(ctx, mockRunCommand, "")
err := validateOpsAgentConfig(ctx, "", mockRunCommand)
gotSuccess := (err == nil)
if gotSuccess != tc.wantSuccess {
t.Errorf("%s: validateOpsAgentConfig() failed to valide Ops Agent config: %v, want successful config validation: %v, error:%v", tc.name, gotSuccess, tc.wantSuccess, err)
Expand Down Expand Up @@ -153,7 +153,7 @@ func Test_generateSubagentConfigs(t *testing.T) {
}

ctx := context.Background()
err := generateSubagentConfigs(ctx, mockRunCommand, "")
err := generateSubagentConfigs(ctx, mockRunCommand, "", "")
gotSuccess := (err == nil)
if gotSuccess != tc.wantSuccess {
t.Errorf("%s: generateSubagentConfigs() failed to generate subagents configs: %v, want successful config validation: %v, error:%v", tc.name, gotSuccess, tc.wantSuccess, err)
Expand Down Expand Up @@ -329,7 +329,7 @@ func Test_runSubagents_TerminatesWhenSpawnedGoRoutinesReturn(t *testing.T) {
}
cancel() // child go routines return immediately, because the parent context has been cancelled.
// the test times out and fails if runSubagents does not returns
runSubagents(ctx, cancel, "", mockRestartCommandFunc, runCommand)
runSubagents(ctx, cancel, "", "", mockRestartCommandFunc, runCommand)
}

// TestHelperProcess isn't a real test. It's used as a helper process to mock
Expand Down

0 comments on commit 9045ae7

Please sign in to comment.