diff --git a/pkg/virtualkubelet/interlink_client.go b/pkg/virtualkubelet/interlink_client.go new file mode 100644 index 00000000..07dfd117 --- /dev/null +++ b/pkg/virtualkubelet/interlink_client.go @@ -0,0 +1,231 @@ +package virtualkubelet + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "math/rand" + "net/http" + "os" + "strconv" + "strings" + "time" + + "github.com/containerd/containerd/log" + types "github.com/interlink-hq/interlink/pkg/interlink" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" + trace "go.opentelemetry.io/otel/trace" +) + +// InterlinkStatusError is returned when the interLink API answers with a status code other than 200. +type InterlinkStatusError struct { + // Op is the operation that failed (e.g. "create", "delete"). + Op string + // StatusCode is the HTTP status code returned by the interLink API. + StatusCode int + // Body is the response body, which usually carries the reason of the failure. + Body string +} + +func (e *InterlinkStatusError) Error() string { + msg := fmt.Sprintf("interLink %s failed with status code %d", e.Op, e.StatusCode) + if body := strings.TrimSpace(e.Body); body != "" { + msg += ": " + body + } + return msg +} + +// InterlinkClient performs the REST calls from the Virtual Kubelet to the interLink API. +// It is safe for concurrent use. +type InterlinkClient struct { + endpoint string + tokenFile string + jobScriptBuilderURL string + httpClient *http.Client + logHTTPClient *http.Client +} + +// NewInterlinkClient builds a client for the interLink API described by config. +// +// logTransport, when not nil, is cloned and used for log retrieval, with keep-alives disabled because the response body +// is streamed to the kubelet and must not be pooled. When nil, log retrieval uses the same client as the other calls. +func NewInterlinkClient(ctx context.Context, config Config, logTransport *http.Transport) (*InterlinkClient, error) { + endpoint, err := resolveInterlinkEndpoint(config.InterlinkURL, config.InterlinkPort) + if err != nil { + return nil, err + } + + httpClient, err := createTLSHTTPClient(ctx, config.TLS) + if err != nil { + return nil, fmt.Errorf("failed to create TLS HTTP client: %w", err) + } + + logHTTPClient := httpClient + if logTransport != nil { + transport := logTransport.Clone() + transport.DisableKeepAlives = true + transport.MaxIdleConnsPerHost = -1 + logHTTPClient = &http.Client{Transport: transport} + } + + log.G(ctx).Info("InterLink endpoint: ", endpoint) + + return &InterlinkClient{ + endpoint: endpoint, + tokenFile: config.VKTokenFile, + jobScriptBuilderURL: config.JobScriptBuilderURL, + httpClient: httpClient, + logHTTPClient: logHTTPClient, + }, nil +} + +// resolveInterlinkEndpoint returns the base URL used to reach the interLink API. +// Unix sockets are reached through a placeholder host: the transport dials the socket regardless of the host. +func resolveInterlinkEndpoint(interLinkURL string, interLinkPort string) (string, error) { + switch { + case strings.HasPrefix(interLinkURL, "unix://"): + return "http://unix", nil + case strings.HasPrefix(interLinkURL, "http://"), strings.HasPrefix(interLinkURL, "https://"): + if interLinkPort == "" { + return interLinkURL, nil + } + return interLinkURL + ":" + interLinkPort, nil + default: + return "", fmt.Errorf("InterlinkURL %q should either start with unix:// or http(s)://", interLinkURL) + } +} + +// newSessionContext returns an identifier that lets a single call be followed end-to-end, from the VK to the API and +// down to the plugin (e.g. interlink-slurm-plugin). +func newSessionContext(prefix string) string { + return prefix + "#" + strconv.Itoa(rand.Intn(100000)) // #nosec G404 -- not security sensitive, only used to correlate logs +} + +// interlinkCall describes a single REST call to the interLink API. +type interlinkCall struct { + // op is the human readable operation name used in errors. + op string + // spanName is the name of the tracing span wrapping the HTTP call. + spanName string + method string + path string + // body is marshalled to JSON when not nil. + body any + session string + // attrs are extra tracing attributes, e.g. the pod the call is about. + attrs []attribute.KeyValue + httpClient *http.Client +} + +func (c *InterlinkClient) readToken() (string, error) { + if c.tokenFile == "" { + return "", nil + } + // The file is read on every call so that a rotated token is picked up. + token, err := os.ReadFile(c.tokenFile) + if err != nil { + return "", fmt.Errorf("failed to read VK token file: %w", err) + } + return strings.TrimSpace(string(token)), nil +} + +// roundTrip sends the call and returns the response, whose body must be closed by the caller. +// The tracing span covers the time until the response headers are received. +func (c *InterlinkClient) roundTrip(ctx context.Context, call interlinkCall) (*http.Response, error) { + sessionMessage := GetSessionContextMessage(call.session) + + var reader io.Reader + if call.body != nil { + bodyBytes, err := json.Marshal(call.body) + if err != nil { + return nil, fmt.Errorf("%sfailed to marshal %s request: %w", sessionMessage, call.op, err) + } + reader = bytes.NewReader(bodyBytes) + } + + req, err := http.NewRequestWithContext(ctx, call.method, c.endpoint+call.path, reader) + if err != nil { + return nil, fmt.Errorf("%sfailed to build %s request: %w", sessionMessage, call.op, err) + } + + token, err := c.readToken() + if err != nil { + return nil, fmt.Errorf("%s%w", sessionMessage, err) + } + if token != "" { + req.Header.Set("Authorization", "Bearer "+token) + } + req.Header.Set("Content-Type", "application/json") + AddSessionContext(req, call.session) + + startHTTPCall := time.Now().UnixMicro() + attrs := make([]attribute.KeyValue, 0, len(call.attrs)+1) + attrs = append(attrs, call.attrs...) + attrs = append(attrs, attribute.Int64("start.timestamp", startHTTPCall)) + _, span := otel.Tracer("interlink-service").Start(ctx, call.spanName, trace.WithAttributes(attrs...)) + defer span.End() + + resp, err := call.httpClient.Do(req) // #nosec G704 -- the endpoint comes from the operator configuration and is validated in NewInterlinkClient + if err != nil { + types.SetDurationSpan(startHTTPCall, span) + span.RecordError(err) + span.SetStatus(codes.Error, err.Error()) + // The error is not formatted with the request: it would contain the Authorization header. + return nil, fmt.Errorf("%s%s request failed: %w", sessionMessage, call.op, err) + } + + types.SetDurationSpan(startHTTPCall, span, types.WithHTTPReturnCode(resp.StatusCode)) + return resp, nil +} + +// doJSON performs the call and returns the whole response body. +// A status code other than 200 is reported as an *InterlinkStatusError. +func (c *InterlinkClient) doJSON(ctx context.Context, call interlinkCall) ([]byte, error) { + call.httpClient = c.httpClient + + resp, err := c.roundTrip(ctx, call) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + sessionMessage := GetSessionContextMessage(call.session) + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("%sfailed to read %s response: %w", sessionMessage, call.op, err) + } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("%s%w", sessionMessage, &InterlinkStatusError{Op: call.op, StatusCode: resp.StatusCode, Body: string(body)}) + } + + return body, nil +} + +// Ping checks that the interLink API is reachable. A non-200 answer is not an error: it is reported through the returned +// status code, together with the response body. Errors are only returned when no answer could be obtained. +func (c *InterlinkClient) Ping(ctx context.Context) (int, string, error) { + body, err := c.doJSON(ctx, interlinkCall{ + op: "ping", + spanName: "PingHttpCall", + method: http.MethodPost, + path: "/pinglink", + session: newSessionContext("PingInterLink"), + }) + + var statusErr *InterlinkStatusError + switch { + case errors.As(err, &statusErr): + return statusErr.StatusCode, statusErr.Body, nil + case err != nil: + return 0, "", err + } + + return http.StatusOK, string(body), nil +} diff --git a/pkg/virtualkubelet/interlink_client_test.go b/pkg/virtualkubelet/interlink_client_test.go new file mode 100644 index 00000000..f584210e --- /dev/null +++ b/pkg/virtualkubelet/interlink_client_test.go @@ -0,0 +1,331 @@ +package virtualkubelet + +import ( + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const testInterlinkURL = "http://interlink.example.com" + +// newTestInterlinkClient returns a client pointing at srv, bypassing the endpoint resolution and the TLS setup. +func newTestInterlinkClient(srv *httptest.Server) *InterlinkClient { + return &InterlinkClient{ + endpoint: srv.URL, + httpClient: srv.Client(), + logHTTPClient: srv.Client(), + } +} + +func writeTokenFile(t *testing.T, content string) string { + t.Helper() + path := filepath.Join(t.TempDir(), "token") + require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) + return path +} + +// writeBody writes a test response body and fails the test if that is not possible. +func writeBody(t *testing.T, w http.ResponseWriter, body string) { + t.Helper() + _, err := w.Write([]byte(body)) + assert.NoError(t, err) +} + +func TestResolveInterlinkEndpoint(t *testing.T) { + tests := []struct { + name string + url string + port string + want string + wantErr bool + }{ + {name: "http", url: "http://vk.interlink.local", port: "3000", want: "http://vk.interlink.local:3000"}, + {name: "https", url: "https://interlink-api.example.com", port: "8443", want: "https://interlink-api.example.com:8443"}, + {name: "http without port", url: "http://interlink-api.example.com", port: "", want: "http://interlink-api.example.com"}, + {name: "unix socket", url: "unix:///run/interlink/vk.sock", port: "", want: "http://unix"}, + {name: "unsupported scheme", url: "ftp://files.interlink.local", port: "21", wantErr: true}, + {name: "no url", url: "", port: "", wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := resolveInterlinkEndpoint(tt.url, tt.port) + if tt.wantErr { + assert.Error(t, err) + return + } + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} + +func TestNewInterlinkClient(t *testing.T) { + ctx := context.Background() + + t.Run("accepts a remote http endpoint", func(t *testing.T) { + client, err := NewInterlinkClient(ctx, Config{InterlinkURL: testInterlinkURL, InterlinkPort: "3000"}, nil) + require.NoError(t, err) + assert.Equal(t, "http://interlink.example.com:3000", client.endpoint) + assert.Same(t, client.httpClient, client.logHTTPClient, "logs share the client when no transport is given") + }) + + t.Run("accepts a unix socket", func(t *testing.T) { + client, err := NewInterlinkClient(ctx, Config{InterlinkURL: "unix:///run/interlink/vk.sock"}, nil) + require.NoError(t, err) + assert.Equal(t, "http://unix", client.endpoint) + }) + + t.Run("keeps the token file and the job script builder", func(t *testing.T) { + client, err := NewInterlinkClient(ctx, Config{ + InterlinkURL: testInterlinkURL, + VKTokenFile: "/var/run/token", + JobScriptBuilderURL: "http://builder.example.com", + }, nil) + require.NoError(t, err) + assert.Equal(t, "/var/run/token", client.tokenFile) + assert.Equal(t, "http://builder.example.com", client.jobScriptBuilderURL) + }) + + t.Run("rejects an unsupported scheme", func(t *testing.T) { + _, err := NewInterlinkClient(ctx, Config{InterlinkURL: "ftp://files.interlink.local"}, nil) + assert.Error(t, err) + }) + + t.Run("accepts localhost", func(t *testing.T) { + client, err := NewInterlinkClient(ctx, Config{InterlinkURL: "http://localhost", InterlinkPort: "3000"}, nil) + require.NoError(t, err) + assert.Equal(t, "http://localhost:3000", client.endpoint) + }) + + t.Run("reports TLS configuration errors", func(t *testing.T) { + _, err := NewInterlinkClient(ctx, Config{ + InterlinkURL: "https://interlink.example.com", + TLS: TLSConfig{Enabled: true, CACertFile: "/non/existent/ca.crt"}, + }, nil) + assert.ErrorContains(t, err, "TLS HTTP client") + }) + + t.Run("logs use a dedicated transport without keep-alives and the input is left untouched", func(t *testing.T) { + transport := &http.Transport{} + client, err := NewInterlinkClient(ctx, Config{InterlinkURL: testInterlinkURL}, transport) + require.NoError(t, err) + + logTransport, ok := client.logHTTPClient.Transport.(*http.Transport) + require.True(t, ok) + assert.True(t, logTransport.DisableKeepAlives) + assert.NotSame(t, transport, logTransport) + assert.False(t, transport.DisableKeepAlives) + }) +} + +// testCall is a minimal call used to exercise the shared plumbing independently of any API operation. +func testCall() interlinkCall { + return interlinkCall{ + op: "test", + spanName: "TestHttpCall", + method: http.MethodDelete, + path: "/test", + session: newSessionContext("Test"), + } +} + +func TestInterlinkClient_RequestShape(t *testing.T) { + var called atomic.Bool + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + called.Store(true) + assert.Equal(t, http.MethodDelete, r.Method) + assert.Equal(t, "/test", r.URL.Path) + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) + assert.Equal(t, "Bearer secret-token", r.Header.Get("Authorization"), "token must be trimmed") + assert.True(t, strings.HasPrefix(r.Header.Get("InterLink-Http-Session"), "Test#"), r.Header.Get("InterLink-Http-Session")) + + var got map[string]string + assert.NoError(t, json.NewDecoder(r.Body).Decode(&got)) + assert.Equal(t, map[string]string{"name": "pod-a"}, got) + + writeBody(t, w, "ok") + })) + defer srv.Close() + + client := newTestInterlinkClient(srv) + client.tokenFile = writeTokenFile(t, "secret-token\n") + + call := testCall() + call.body = map[string]string{"name": "pod-a"} + body, err := client.doJSON(context.Background(), call) + + require.NoError(t, err) + assert.True(t, called.Load()) + assert.Equal(t, "ok", string(body), "the response body is returned") +} + +func TestInterlinkClient_NoBodyWhenNotGiven(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + assert.NoError(t, err) + assert.Empty(t, body) + })) + defer srv.Close() + + _, err := newTestInterlinkClient(srv).doJSON(context.Background(), testCall()) + require.NoError(t, err) +} + +func TestInterlinkClient_NoAuthorizationHeaderWithoutTokenFile(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Empty(t, r.Header.Get("Authorization")) + w.WriteHeader(http.StatusOK) + })) + defer srv.Close() + + _, err := newTestInterlinkClient(srv).doJSON(context.Background(), testCall()) + require.NoError(t, err) +} + +func TestInterlinkClient_StatusError(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + writeBody(t, w, "plugin exploded") + })) + defer srv.Close() + + _, err := newTestInterlinkClient(srv).doJSON(context.Background(), testCall()) + require.Error(t, err) + + var statusErr *InterlinkStatusError + require.True(t, errors.As(err, &statusErr)) + assert.Equal(t, http.StatusInternalServerError, statusErr.StatusCode) + assert.Equal(t, "test", statusErr.Op) + assert.Equal(t, "plugin exploded", statusErr.Body) + assert.Contains(t, err.Error(), "plugin exploded") + assert.Contains(t, err.Error(), "500") +} + +func TestInterlinkClient_Ping(t *testing.T) { + t.Run("ok", func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + assert.Equal(t, "/pinglink", r.URL.Path) + assert.True(t, strings.HasPrefix(r.Header.Get("InterLink-Http-Session"), "PingInterLink#")) + writeBody(t, w, "pong") + })) + defer srv.Close() + + code, body, err := newTestInterlinkClient(srv).Ping(context.Background()) + require.NoError(t, err) + assert.Equal(t, http.StatusOK, code) + assert.Equal(t, "pong", body) + }) + + t.Run("non-200 is reported through the status code", func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusServiceUnavailable) + writeBody(t, w, "plugin down") + })) + defer srv.Close() + + code, body, err := newTestInterlinkClient(srv).Ping(context.Background()) + require.NoError(t, err) + assert.Equal(t, http.StatusServiceUnavailable, code) + assert.Equal(t, "plugin down", body) + }) + + t.Run("unreachable API is an error", func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) + client := newTestInterlinkClient(srv) + srv.Close() + + _, _, err := client.Ping(context.Background()) + assert.Error(t, err) + }) +} + +func TestInterlinkClient_TokenHandling(t *testing.T) { + t.Run("missing token file fails before any request is sent", func(t *testing.T) { + var called atomic.Bool + srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { + called.Store(true) + })) + defer srv.Close() + + client := newTestInterlinkClient(srv) + client.tokenFile = filepath.Join(t.TempDir(), "does-not-exist") + + _, _, err := client.Ping(context.Background()) + assert.ErrorContains(t, err, "token file") + assert.False(t, called.Load()) + }) + + t.Run("a rotated token is picked up", func(t *testing.T) { + var ( + mu sync.Mutex + seen []string + ) + srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + seen = append(seen, r.Header.Get("Authorization")) + })) + defer srv.Close() + + client := newTestInterlinkClient(srv) + client.tokenFile = writeTokenFile(t, "first") + + _, _, err := client.Ping(context.Background()) + require.NoError(t, err) + require.NoError(t, os.WriteFile(client.tokenFile, []byte("second"), 0o600)) + _, _, err = client.Ping(context.Background()) + require.NoError(t, err) + + mu.Lock() + defer mu.Unlock() + assert.Equal(t, []string{"Bearer first", "Bearer second"}, seen) + }) + + t.Run("errors never contain the token", func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) + client := newTestInterlinkClient(srv) + srv.Close() + client.tokenFile = writeTokenFile(t, "super-secret-token") + + _, _, err := client.Ping(context.Background()) + require.Error(t, err) + assert.NotContains(t, err.Error(), "super-secret-token") + }) +} + +func TestInterlinkClient_ContextCancellation(t *testing.T) { + release := make(chan struct{}) + srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + select { + case <-release: + case <-r.Context().Done(): + } + })) + defer srv.Close() + defer close(release) + + ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) + defer cancel() + + start := time.Now() + _, _, err := newTestInterlinkClient(srv).Ping(ctx) + + require.Error(t, err) + assert.True(t, errors.Is(err, context.DeadlineExceeded), err) + assert.Less(t, time.Since(start), 5*time.Second) +}