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
4 changes: 4 additions & 0 deletions agent/rpc/auth_client_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func NewAuthGrpcClient(conn *grpc.ClientConn, agentToken string, agentID int64)
return client
}

func (c *AuthClient) AgentID() int64 {
return c.agentID
}

func (c *AuthClient) Auth(ctx context.Context) (string, int64, error) {
ctx, cancel := context.WithTimeout(ctx, authClientTimeout)
defer cancel()
Expand Down
32 changes: 32 additions & 0 deletions agent/rpc/auth_client_grpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rpc

import (
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func TestAuthClientAgentID(t *testing.T) {
conn, err := grpc.NewClient("localhost:0", grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NoError(t, err)
defer conn.Close()

client := NewAuthGrpcClient(conn, "test-token", 42)
assert.Equal(t, int64(42), client.AgentID())
}
9 changes: 9 additions & 0 deletions cmd/agent/core/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
return fmt.Errorf("agent could not auth: %w", err)
}

// Persist the agent ID received during auth so that crashloops reuse the
// same server-side entry instead of creating a new one on every restart.
if agentConfigPath != "" {
agentConfig.AgentID = authClient.AgentID()
if err := writeAgentConfig(agentConfig, agentConfigPath); err == nil {
log.Debug().Msgf("persisted agent ID %d after auth", agentConfig.AgentID)
}
}

conn, err := grpc.NewClient(
c.String("server"),
transport,
Expand Down