/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package source

import (
	"testing"

	"sigs.k8s.io/external-dns/internal/testutils"

	"github.com/stretchr/testify/require"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/client-go/kubernetes/fake"

	"sigs.k8s.io/external-dns/endpoint"
	templatetest "sigs.k8s.io/external-dns/source/template/testutil"
)

func TestNodeSourceFqdnTemplatingExamples(t *testing.T) {
	for _, tt := range []struct {
		title        string
		nodes        []*v1.Node
		fqdnTemplate string
		expected     []*endpoint.Endpoint
		combineFQDN  bool
	}{
		{
			title: "templating expansion with multiple domains",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "ip-10-1-176-5.internal",
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeInternalIP, Address: "10.1.176.1"},
							{Type: v1.NodeInternalIP, Address: "fc00:f853:ccd:e793::1"},
						},
					},
				},
			},
			fqdnTemplate: "{{.Name}}.domainA.com,{{.Name}}.domainB.com",
			expected: []*endpoint.Endpoint{
				{DNSName: "ip-10-1-176-5.internal.domainA.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.1.176.1"}},
				{DNSName: "ip-10-1-176-5.internal.domainA.com", RecordType: endpoint.RecordTypeAAAA, Targets: endpoint.Targets{"fc00:f853:ccd:e793::1"}},
				{DNSName: "ip-10-1-176-5.internal.domainB.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.1.176.1"}},
				{DNSName: "ip-10-1-176-5.internal.domainB.com", RecordType: endpoint.RecordTypeAAAA, Targets: endpoint.Targets{"fc00:f853:ccd:e793::1"}},
			},
		},
		{
			title: "templating contains namespace when node namespace is not a valid variable",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "node-name",
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeInternalIP, Address: "10.1.176.1"},
						},
					},
				},
			},
			fqdnTemplate: "{{.Name}}.domainA.com,{{ .Name }}.{{ .Namespace }}.example.tld",
			expected: []*endpoint.Endpoint{
				{DNSName: "node-name.domainA.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.1.176.1"}},
				{DNSName: "node-name..example.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.1.176.1"}},
			},
		},
		{
			title: "templating with external IP and range of addresses",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "ip-10-1-176-1",
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeExternalIP, Address: "243.186.136.160"},
							{Type: v1.NodeInternalIP, Address: "fc00:f853:ccd:e793::1"},
						},
					},
				},
			},
			fqdnTemplate: "{{ range .Status.Addresses }}{{if and (eq .Type \"ExternalIP\") (isIPv4 .Address)}}ip-{{ .Address | replace \".\" \"-\" }}{{ break }}{{ end }}{{ end }}.example.com",
			expected: []*endpoint.Endpoint{
				{DNSName: "ip-243-186-136-160.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160"}},
				{DNSName: "ip-243-186-136-160.example.com", RecordType: endpoint.RecordTypeAAAA, Targets: endpoint.Targets{"fc00:f853:ccd:e793::1"}},
			},
		},
		{
			title: "templating with name definition and ipv4 check",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "node-name-ip",
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeExternalIP, Address: "243.186.136.160"},
							{Type: v1.NodeInternalIP, Address: "fc00:f853:ccd:e793::1"},
						},
					},
				},
			},
			fqdnTemplate: "{{ $name := .Name }}{{ range .Status.Addresses }}{{if (isIPv4 .Address)}}{{ $name }}.ipv4{{ break }}{{ end }}{{ end }}.example.com",
			expected: []*endpoint.Endpoint{
				{DNSName: "node-name-ip.ipv4.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160"}},
				{DNSName: "node-name-ip.ipv4.example.com", RecordType: endpoint.RecordTypeAAAA, Targets: endpoint.Targets{"fc00:f853:ccd:e793::1"}},
			},
		},
		{
			title: "templating with hostname annotation",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "ip-10-1-176-1",
						Annotations: map[string]string{
							"external-dns.alpha.kubernetes.io/hostname": "ip-10-1-176-1.internal.domain.com",
						},
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeExternalIP, Address: "243.186.136.160"},
							{Type: v1.NodeInternalIP, Address: "fc00:f853:ccd:e793::1"},
						},
					},
				},
			},
			fqdnTemplate: "{{.Name}}.example.com",
			expected: []*endpoint.Endpoint{
				{DNSName: "ip-10-1-176-1.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160"}},
				{DNSName: "ip-10-1-176-1.example.com", RecordType: endpoint.RecordTypeAAAA, Targets: endpoint.Targets{"fc00:f853:ccd:e793::1"}},
			},
		},
		{
			title: "templating when target annotation and no external IP",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name:   "node-name",
						Labels: nil,
						Annotations: map[string]string{
							"external-dns.alpha.kubernetes.io/target": "203.2.45.22",
						},
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeExternalIP, Address: "243.186.136.160"},
							{Type: v1.NodeInternalIP, Address: "fc00:f853:ccd:e793::1"},
						},
					},
				},
			},
			fqdnTemplate: "{{.Name}}.example.com",
			expected: []*endpoint.Endpoint{
				{DNSName: "node-name.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"203.2.45.22"}},
			},
		},
		{
			title: "templating with simple annotation expansion",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "node-name",
						Annotations: map[string]string{
							"workload": "cluster-resources",
						},
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeExternalIP, Address: "243.186.136.160"},
						},
					},
				},
			},
			fqdnTemplate: "{{ .Name }}.{{ .Annotations.workload }}.domain.tld",
			expected: []*endpoint.Endpoint{
				{DNSName: "node-name.cluster-resources.domain.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160"}},
			},
		},
		{
			title: "templating with complex labels expansion",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "node-name",
						Labels: map[string]string{
							"topology.kubernetes.io/region": "eu-west-1",
						},
						Annotations: nil,
					},
					Spec: v1.NodeSpec{
						Unschedulable: false,
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeExternalIP, Address: "243.186.136.160"},
						},
					},
				},
			},
			fqdnTemplate: "{{ .Name }}.{{ index .ObjectMeta.Labels \"topology.kubernetes.io/region\" }}.domain.tld",
			expected: []*endpoint.Endpoint{
				{DNSName: "node-name.eu-west-1.domain.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160"}},
			},
		},
		{
			title: "templating with shared all domain",
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "node-name-1",
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeExternalIP, Address: "243.186.136.160"},
						},
					},
				},
				{
					ObjectMeta: metav1.ObjectMeta{
						Name: "node-name-2",
					},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{
							{Type: v1.NodeExternalIP, Address: "243.186.136.178"},
						},
					},
				},
			},
			fqdnTemplate: "{{ .Name }}.domain.tld,all.example.com",
			expected: []*endpoint.Endpoint{
				{DNSName: "all.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160", "243.186.136.178"}},
				{DNSName: "node-name-1.domain.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160"}},
				{DNSName: "node-name-2.domain.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.178"}},
			},
		},
		{
			title:       "templating with shared all domain and fqdn combination annotation",
			combineFQDN: true,
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{Name: "node-name-1"},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{{Type: v1.NodeExternalIP, Address: "243.186.136.160"}},
					},
				},
				{
					ObjectMeta: metav1.ObjectMeta{Name: "node-name-2"},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{{Type: v1.NodeExternalIP, Address: "243.186.136.178"}},
					},
				},
			},
			fqdnTemplate: "{{ .Name }}.domain.tld,all.example.com",
			expected: []*endpoint.Endpoint{
				{DNSName: "all.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160", "243.186.136.178"}},
				{DNSName: "node-name-1.domain.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160"}},
				{DNSName: "node-name-2.domain.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.178"}},
				{DNSName: "node-name-1", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.160"}},
				{DNSName: "node-name-2", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"243.186.136.178"}},
			},
		},
		{
			title: "templating with kind-based FQDNs",
			fqdnTemplate: `{{ if eq .Kind "Pod" }}{{.Name}}.pod.tld{{ end }}
				{{ if eq .Kind "Node" }}{{.Name}}.{{.Status.NodeInfo.Architecture}}.node.tld{{ end }}`,
			expected: []*endpoint.Endpoint{
				{DNSName: "node-name-1.arm64.node.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.0.0.1"}},
				{DNSName: "node-name-2.x86_64.node.tld", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.0.0.2"}},
			},
			combineFQDN: false,
			nodes: []*v1.Node{
				{
					ObjectMeta: metav1.ObjectMeta{Name: "node-name-1"},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{{Type: v1.NodeExternalIP, Address: "10.0.0.1"}},
						NodeInfo: v1.NodeSystemInfo{
							Architecture: "arm64",
						},
					},
					Spec: v1.NodeSpec{},
				},
				{
					ObjectMeta: metav1.ObjectMeta{Name: "node-name-2"},
					Status: v1.NodeStatus{
						Addresses: []v1.NodeAddress{{Type: v1.NodeExternalIP, Address: "10.0.0.2"}},
						NodeInfo: v1.NodeSystemInfo{
							Architecture: "x86_64",
						},
					},
				},
			},
		},
	} {
		t.Run(tt.title, func(t *testing.T) {
			kubeClient := fake.NewClientset()

			for _, node := range tt.nodes {
				_, err := kubeClient.CoreV1().Nodes().Create(t.Context(), node, metav1.CreateOptions{})
				require.NoError(t, err)
			}

			src, err := NewNodeSource(
				t.Context(),
				kubeClient,
				&Config{
					TemplateEngine:       templatetest.MustEngine(t, tt.fqdnTemplate, "", "", tt.combineFQDN),
					ExcludeUnschedulable: true,
					ExposeInternalIPv6:   true,
					LabelFilter:          labels.Everything(),
				},
			)
			require.NoError(t, err)

			endpoints, err := src.Endpoints(t.Context())
			require.NoError(t, err)

			testutils.ValidateEndpoints(t, endpoints, tt.expected)
		})
	}
}
