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

HostName and SubDomain should be sync to the physical cluster #2501

Open
mayooot opened this issue Feb 20, 2025 · 0 comments
Open

HostName and SubDomain should be sync to the physical cluster #2501

mayooot opened this issue Feb 20, 2025 · 0 comments

Comments

@mayooot
Copy link
Contributor

mayooot commented Feb 20, 2025

Is your feature request related to a problem?

When I use statefulset and headless serivce inside vcluster and access the pod using <pod.hostname>.<pod.subdomain>.<namespace>.svc.cluster.local, it works as expected.

apiVersion: v1
kind: Service
metadata:
  name: nginx-headless
spec:
  clusterIP: None
  selector:
    app: nginx
  ports:
    - port: 80
      targetPort: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx-statefulset
spec:
  serviceName: nginx-headless
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      hostname:
      containers:
      - name: nginx
        image: harbor.zetyun.cn/alaya-bugfix/nginx:latest
        ports:
        - containerPort: 80

However, in the physical cluster using the synced pod and serivce, it does not work, the ip is not resolved. I noticed that the subdomain is set to "" in the physical cluster pod, and by reading I found the following logic:

	// if spec.subdomain is set we have to translate the /etc/hosts
	// because otherwise we could get a different hostname as if the pod
	// would be deployed in a non virtual kubernetes cluster
	if pPod.Spec.Subdomain != "" {
		if t.overrideHosts {
			rewritePodHostnameFQDN(pPod, t.defaultImageRegistry, t.overrideHostsImage, pPod.Spec.Hostname, pPod.Spec.Hostname, pPod.Spec.Hostname+"."+pPod.Spec.Subdomain+"."+vPod.Namespace+".svc."+t.clusterDomain)
		}

		pPod.Spec.Subdomain = ""
	}

I changed the code to this, and it works fine, and I was able to access the vcluster's pods on the phsyical cluster via dns.

	// truncate hostname if needed
	if pPod.Spec.Hostname == "" {
		if len(vPod.Name) > 63 {
			pPod.Spec.Hostname = vPod.Name[0:63]
		} else {
			pPod.Spec.Hostname = vPod.Name
		}

		// Kubernetes does not support setting the hostname to a value that
		// includes a '.', therefore we need to rewrite the hostname. This is really bad
		// and wrong, but unfortunately there is currently no other solution as there is
		// no other way to change the container's hostname.
                // EDIT!
		pPod.Spec.Hostname = translate.Default.PhysicalName(strings.TrimSuffix(strings.Replace(pPod.Spec.Hostname, ".", "-", -1), "-"), vPod.Namespace)
	}

        ....

	// if spec.subdomain is set we have to translate the /etc/hosts
	// because otherwise we could get a different hostname as if the pod
	// would be deployed in a non virtual kubernetes cluster
	if pPod.Spec.Subdomain != "" {
		if t.overrideHosts {
			rewritePodHostnameFQDN(pPod, t.defaultImageRegistry, t.overrideHostsImage, pPod.Spec.Hostname, pPod.Spec.Hostname, pPod.Spec.Hostname+"."+pPod.Spec.Subdomain+"."+vPod.Namespace+".svc."+t.clusterDomain)
		}

		// Translate the subdomain using the same method as other resources
                // EDIT!
		pPod.Spec.Subdomain = translate.Default.PhysicalName(pPod.Spec.Subdomain, vPod.Namespace)
	}
$ ping nginx-statefulset-0-x-default-x-vc9i1h3abk7i.nginx-headless-x-default-x-vc9i1h3abk7i.vcluster-vc9i1h3abk7i.svc.cluster.local

I can't understand why I can't sync out the subdomain and if there is another way for me to access the pods inside StatefuSet via dns in the physical cluster.

Which solution do you suggest?

Set the subdomain and hostname for the synced pods.

Which alternative solutions exist?

No response

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant