Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (dns) : Add host.crc.testing to /etc/hosts (#4410) #4471

Merged
merged 1 commit into from
Nov 25, 2024
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
6 changes: 5 additions & 1 deletion pkg/crc/machine/bundle/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,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 {
Expand Down
22 changes: 22 additions & 0 deletions pkg/crc/machine/bundle/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
12 changes: 10 additions & 2 deletions pkg/crc/services/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
38 changes: 38 additions & 0 deletions pkg/crc/services/dns/dns_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading