From 504c05553b3fc30018c4f4eaf96fe7a671b93521 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Wed, 20 Nov 2024 22:32:24 +0530 Subject: [PATCH] feat (dns) : Add `host.crc.testing` to /etc/hosts (#4410) + Add another method `GetFQDN()` to CrcBundleInfo struct in order to provide host domain + Use abovementioned method in `dns.addOpenShiftHosts` in order to add another dns entry for `host.crc.testing` Signed-off-by: Rohan Kumar --- pkg/crc/machine/bundle/metadata.go | 6 +++- pkg/crc/machine/bundle/metadata_test.go | 22 ++++++++++++++ pkg/crc/services/dns/dns.go | 12 ++++++-- pkg/crc/services/dns/dns_test.go | 38 +++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 pkg/crc/services/dns/dns_test.go diff --git a/pkg/crc/machine/bundle/metadata.go b/pkg/crc/machine/bundle/metadata.go index 5ec1c6ff2d..f3cb117866 100644 --- a/pkg/crc/machine/bundle/metadata.go +++ b/pkg/crc/machine/bundle/metadata.go @@ -109,8 +109,12 @@ func (bundle *CrcBundleInfo) GetBundleName() string { return bundle.Name } +func (bundle *CrcBundleInfo) GetFQDN(shortName string) string { + return fmt.Sprintf("%s.%s.%s", shortName, bundle.ClusterInfo.ClusterName, bundle.ClusterInfo.BaseDomain) +} + func (bundle *CrcBundleInfo) GetAPIHostname() string { - return fmt.Sprintf("api.%s.%s", bundle.ClusterInfo.ClusterName, bundle.ClusterInfo.BaseDomain) + return bundle.GetFQDN("api") } func (bundle *CrcBundleInfo) GetAppHostname(appName string) string { diff --git a/pkg/crc/machine/bundle/metadata_test.go b/pkg/crc/machine/bundle/metadata_test.go index f2218103c6..eff5d772eb 100644 --- a/pkg/crc/machine/bundle/metadata_test.go +++ b/pkg/crc/machine/bundle/metadata_test.go @@ -289,3 +289,25 @@ func TestGetBundleInfoFromNameInvalid(t *testing.T) { _, err = GetBundleInfoFromName("crc_nanoshift_libvirt_4.16.7_amd64_232.crcbundle") assert.Error(t, err) } + +func TestGetFQDN(t *testing.T) { + tests := []struct { + name string + shortName string + expectedDomainName string + }{ + {"api host name", "api", "api.crc.testing"}, + {"vm host name", "host", "host.crc.testing"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Given + // When + hostName := parsedReference.GetFQDN(tt.shortName) + + // Then + assert.Equal(t, tt.expectedDomainName, hostName) + }) + } +} diff --git a/pkg/crc/services/dns/dns.go b/pkg/crc/services/dns/dns.go index eb2dec8254..87fff40a83 100644 --- a/pkg/crc/services/dns/dns.go +++ b/pkg/crc/services/dns/dns.go @@ -205,10 +205,18 @@ func matchIP(ips []net.IP, expectedIP string) bool { } func addOpenShiftHosts(serviceConfig services.ServicePostStartConfig) error { - return adminhelper.UpdateHostsFile(serviceConfig.IP, serviceConfig.BundleMetadata.GetAPIHostname(), + hostnames := getApplicableHostnames(serviceConfig) + return adminhelper.UpdateHostsFile(serviceConfig.IP, hostnames...) +} + +func getApplicableHostnames(serviceConfig services.ServicePostStartConfig) []string { + return []string{ + serviceConfig.BundleMetadata.GetAPIHostname(), + serviceConfig.BundleMetadata.GetFQDN("host"), serviceConfig.BundleMetadata.GetAppHostname("oauth-openshift"), serviceConfig.BundleMetadata.GetAppHostname("console-openshift-console"), serviceConfig.BundleMetadata.GetAppHostname("downloads-openshift-console"), serviceConfig.BundleMetadata.GetAppHostname("canary-openshift-ingress-canary"), - serviceConfig.BundleMetadata.GetAppHostname("default-route-openshift-image-registry")) + serviceConfig.BundleMetadata.GetAppHostname("default-route-openshift-image-registry"), + } } diff --git a/pkg/crc/services/dns/dns_test.go b/pkg/crc/services/dns/dns_test.go new file mode 100644 index 0000000000..80d994772b --- /dev/null +++ b/pkg/crc/services/dns/dns_test.go @@ -0,0 +1,38 @@ +package dns + +import ( + "testing" + + "github.com/Masterminds/semver/v3" + "github.com/crc-org/crc/v2/pkg/crc/machine/bundle" + "github.com/crc-org/crc/v2/pkg/crc/services" + "github.com/stretchr/testify/assert" +) + +func TestGetApplicableHostnames(t *testing.T) { + // Given + bundleMetadata := services.ServicePostStartConfig{ + BundleMetadata: bundle.CrcBundleInfo{ + ClusterInfo: bundle.ClusterInfo{ + OpenShiftVersion: semver.MustParse("4.6.1"), + ClusterName: "crc", + BaseDomain: "testing", + AppsDomain: "apps.crc.testing", + SSHPrivateKeyFile: "id_ecdsa_crc", + KubeConfig: "kubeconfig", + }, + }, + } + // When + hostnames := getApplicableHostnames(bundleMetadata) + // Then + assert.Equal(t, []string{ + "api.crc.testing", + "host.crc.testing", + "oauth-openshift.apps.crc.testing", + "console-openshift-console.apps.crc.testing", + "downloads-openshift-console.apps.crc.testing", + "canary-openshift-ingress-canary.apps.crc.testing", + "default-route-openshift-image-registry.apps.crc.testing", + }, hostnames) +}