Repository: vmware/govmomi
Pull request #3391 (2024-03-19 09:03:06)

fix: endpoint url to bypass lookup service if envoy sidecar enabled
EndpointURL returns FQDN:443 as endpoint url even if envoy sidecar enabled. This MR updates to bypass lookup service if envoy sidecar enabled.

## Description

Please include a summary of the change and which issue is fixed. Please also
include relevant motivation and context. List any dependencies that are required
for this change.

Closes: #(issue-number)

## Type of change

Please mark options that are relevant:

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to
  not work as expected)
- [ ] This change requires a documentation update
- [ ] Build related change

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. If applicable, please also list any relevant
details for your test configuration.

- [ ] Test Description 1
- [ ] Test Description 2

## Checklist:

- [ ] My code follows the `CONTRIBUTION` [guidelines] of this project
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

[guidelines]: https://github.com/vmware/govmomi/blob/main/CONTRIBUTING.md


ramineni: cc @dougm @mayankbh 

Patch:
diff --git a/lookup/client.go b/lookup/client.go
index a807c75b..920ce615 100644
--- a/lookup/client.go
+++ b/lookup/client.go
@@ -20,6 +20,7 @@ import (
 	"context"
 	"crypto/x509"
 	"encoding/base64"
+	"fmt"
 	"log"
 	"net/url"
 
@@ -119,6 +120,10 @@ func (c *Client) SiteID(ctx context.Context) (string, error) {
 // If the endpoint is found, its TLS certificate is also added to the vim25.Client's trusted host thumbprints.
 // If the Lookup Service is not available, the given path is returned as the default.
 func EndpointURL(ctx context.Context, c *vim25.Client, path string, filter *types.LookupServiceRegistrationFilter) string {
+	// Services running on vCenter can bypass lookup service.
+	if useSidecar := internal.UsingEnvoySidecar(c); useSidecar {
+		return fmt.Sprintf("http://%s%s", c.URL().Host, path)
+	}
 	if lu, err := NewClient(ctx, c); err == nil {
 		info, _ := lu.List(ctx, filter)
 		if len(info) != 0 && len(info[0].ServiceEndpoints) != 0 {
diff --git a/vapi/library/library_file.go b/vapi/library/library_file.go
index 993163d2..ba71d72d 100644
--- a/vapi/library/library_file.go
+++ b/vapi/library/library_file.go
@@ -31,12 +31,11 @@ type Checksum struct {
 
 // File provides methods to get information on library item files.
 type File struct {
-	Cached           *bool     `json:"cached,omitempty"`
-	Checksum         *Checksum `json:"checksum_info,omitempty"`
-	Name             string    `json:"name,omitempty"`
-	Size             *int64    `json:"size,omitempty"`
-	Version          string    `json:"version,omitempty"`
-	DownloadEndpoint string    `json:"file_download_endpoint,omitempty"`
+	Cached   *bool     `json:"cached,omitempty"`
+	Checksum *Checksum `json:"checksum_info,omitempty"`
+	Name     string    `json:"name,omitempty"`
+	Size     *int64    `json:"size,omitempty"`
+	Version  string    `json:"version,omitempty"`
 }
 
 // ListLibraryItemFiles returns a list of all the files for a library item.


Test patch:
diff --git a/lookup/client_test.go b/lookup/client_test.go
index 8d505e76..849fe920 100644
--- a/lookup/client_test.go
+++ b/lookup/client_test.go
@@ -20,11 +20,16 @@ package lookup_test
 
 import (
 	"context"
+	"fmt"
 	"net/http"
 	"net/url"
+	"os"
 	"strings"
 	"testing"
 
+	"github.com/stretchr/testify/require"
+
+	"github.com/vmware/govmomi/lookup"
 	"github.com/vmware/govmomi/simulator"
 	"github.com/vmware/govmomi/ssoadmin"
 	"github.com/vmware/govmomi/sts"
@@ -69,7 +74,6 @@ func TestEndpointURL(t *testing.T) {
 			}
 		}
 	})
-
 	// these client calls should not fail
 	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
 		{
@@ -96,4 +100,19 @@ func TestEndpointURL(t *testing.T) {
 			}
 		}
 	})
+
+	t.Run("With Envoy sidecar and a malfunctioning lookup service, endpoint url should still succeed", func(t *testing.T) {
+		model := simulator.VPX()
+		model.Create()
+		simulator.Test(func(ctx context.Context, vc *vim25.Client) {
+			lsim.BreakLookupServiceURLs()
+			// Map Envoy sidecar on the same port as the vcsim client.
+			os.Setenv("GOVMOMI_ENVOY_SIDECAR_PORT", vc.Client.URL().Port())
+			os.Setenv("GOVMOMI_ENVOY_SIDECAR_HOST", vc.Client.URL().Hostname())
+			testPath := "/fake/path"
+			expectedUrl := fmt.Sprintf("http://%s%s", vc.Client.URL().Host, testPath)
+			url := lookup.EndpointURL(ctx, vc, testPath, nil)
+			require.Equal(t, expectedUrl, url)
+		}, model)
+	})
 }