Skip to content

Commit

Permalink
feat (dns) : Add host.crc.testing to /etc/hosts (#4410)
Browse files Browse the repository at this point in the history
+ 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 <[email protected]>
  • Loading branch information
rohanKanojia authored and praveenkumar committed Nov 25, 2024
1 parent 107c55c commit 504c055
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pkg/crc/machine/bundle/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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)
}

0 comments on commit 504c055

Please sign in to comment.