operator: fix gRPC dialing over UDS (#3201)

* operator: add test for gRPC connection over UDS
This commit is contained in:
Markus Rudy 2024-06-25 10:11:57 +02:00 committed by GitHub
parent e0c5acf2f3
commit 3db3db3bf2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 7 deletions

View File

@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//bazel/go:go_test.bzl", "go_test")
go_library(
name = "upgrade",
@ -13,3 +14,15 @@ go_library(
"@org_golang_google_grpc//credentials/insecure",
],
)
go_test(
name = "upgrade_test",
srcs = ["upgrade_test.go"],
embed = [":upgrade"],
deps = [
"//internal/versions/components",
"//upgrade-agent/upgradeproto",
"@com_github_stretchr_testify//require",
"@org_golang_google_grpc//:grpc",
],
)

View File

@ -21,24 +21,25 @@ import (
// Client is a client for the upgrade agent.
type Client struct {
addr string
dialer Dialer
}
// NewClient creates a new upgrade agent client.
// NewClient creates a new upgrade agent client connecting to the default upgrade-agent Unix socket.
func NewClient() *Client {
return newClientWithAddress(mainconstants.UpgradeAgentMountPath)
}
func newClientWithAddress(addr string) *Client {
return &Client{
addr: "unix:" + addr,
dialer: &net.Dialer{},
}
}
// Upgrade upgrades the Constellation node to the given Kubernetes version.
func (c *Client) Upgrade(ctx context.Context, kubernetesComponents components.Components, WantedKubernetesVersion string) error {
conn, err := grpc.NewClient(mainconstants.UpgradeAgentMountPath, grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(
func(ctx context.Context, addr string) (net.Conn, error) {
return c.dialer.DialContext(ctx, "unix", addr)
},
))
conn, err := grpc.NewClient(c.addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return fmt.Errorf("failed to dial: %w", err)
}

View File

@ -0,0 +1,52 @@
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package upgrade
import (
"context"
"net"
"os"
"path/filepath"
"testing"
"github.com/edgelesssys/constellation/v2/internal/versions/components"
"github.com/edgelesssys/constellation/v2/upgrade-agent/upgradeproto"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
// TestGRPCDialer is a regression test to ensure the upgrade client can connect to a UDS.
func TestGRPCDialer(t *testing.T) {
require := require.New(t)
dir := t.TempDir()
sockAddr := filepath.Join(dir, "test.socket")
upgradeAgent := &fakeUpgradeAgent{}
grpcServer := grpc.NewServer()
upgradeproto.RegisterUpdateServer(grpcServer, upgradeAgent)
listener, err := net.Listen("unix", sockAddr)
require.NoError(err)
go grpcServer.Serve(listener)
t.Cleanup(grpcServer.Stop)
fileInfo, err := os.Stat(sockAddr)
require.NoError(err)
require.Equal(os.ModeSocket, fileInfo.Mode()&os.ModeType)
upgradeClient := newClientWithAddress(sockAddr)
require.NoError(upgradeClient.Upgrade(context.Background(), []*components.Component{}, "v1.29.6"))
}
type fakeUpgradeAgent struct {
upgradeproto.UnimplementedUpdateServer
}
func (s *fakeUpgradeAgent) ExecuteUpdate(_ context.Context, _ *upgradeproto.ExecuteUpdateRequest) (*upgradeproto.ExecuteUpdateResponse, error) {
return &upgradeproto.ExecuteUpdateResponse{}, nil
}