Skip to content

Commit

Permalink
[kdoctor-io#303]fix-Failed_to_get_the_dns_service_IP_list
Browse files Browse the repository at this point in the history
  • Loading branch information
carryaimp committed Nov 6, 2023
1 parent 8f17d25 commit 51a6347
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/agentDnsServer/appDnsServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func SetupAppDnsServer(rootLogger *zap.Logger, tlsCert, tlsKey string) {
if err != nil {
logger.Sugar().Fatalf("failed get kube dns service,err: %v", err)
}

// If the ip address list is empty, the corresponding service does not exist or the selected label is incorrect
if len(dnsServiceIPs) == 0 {
logger.Sugar().Fatalf("failed get kube dns service: %v", "the corresponding service does not exist or the selected label is incorrect")
}

logger.Sugar().Infof("kube dns service %s ", dnsServiceIPs)
coreDnsAddr = fmt.Sprintf("%s:53", dnsServiceIPs[0])
}
Expand Down
15 changes: 14 additions & 1 deletion pkg/k8ObjManager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -98,8 +99,20 @@ func (nm *k8sObjManager) ListServicesDnsIP(ctx context.Context) ([]string, error
serviceList := new(corev1.ServiceList)
var err error
var result []string
// default dns service selector key:
selectorLabelKey := os.Getenv("DNS_SERVICE_SELECT_LABEL_KEY")
if len(selectorLabelKey) == 0 {
selectorLabelKey = "kubernetes.io/name"
}

// default dns service selector value: CoreDNS
selectorLabelValue := os.Getenv("DNS_SERVICE_SELECT_LABEL_VALUE")
if len(selectorLabelValue) == 0 {
selectorLabelValue = "CoreDNS"
}

label := map[string]string{
"k8s-app": "kube-dns",
selectorLabelKey: selectorLabelValue,
}
ListOption := &client.ListOptions{
Namespace: "kube-system",
Expand Down

0 comments on commit 51a6347

Please sign in to comment.