Skip to content

Commit

Permalink
Restructuring to put different in different packages to make the
Browse files Browse the repository at this point in the history
architecture more clear
  • Loading branch information
ErikEngerd committed Dec 8, 2024
1 parent 0618c30 commit 7cac33e
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 130 deletions.
29 changes: 17 additions & 12 deletions cmd/dns-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,32 @@ import (
"k8s.io/klog/v2"
"os"
"time"
"wamblee.org/kubedock/dns/internal/admissioncontroller"
"wamblee.org/kubedock/dns/internal/config"
"wamblee.org/kubedock/dns/internal/dns"
"wamblee.org/kubedock/dns/internal/model"
"wamblee.org/kubedock/dns/internal/support"
"wamblee.org/kubedock/dns/internal/watcher"
)

func createDns(config Config) *KubeDockDns {
func createDns(config config.Config) *dns.KubeDockDns {
clientConfig := support.GetClientConfig()
clientConfig.Timeout = int(config.DnsTimeout.Seconds())
clientConfig.Attempts = config.DnsRetries

upstreamDnsServer := NewExternalDNSServer(clientConfig.Servers[0] + ":53")
upstreamDnsServer := dns.NewExternalDNSServer(clientConfig.Servers[0] + ":53")
klog.Infof("Upstream DNS server %s", upstreamDnsServer)
kubedocDns := NewKubeDockDns(upstreamDnsServer, ":1053", clientConfig.Search[0],
kubedocDns := dns.NewKubeDockDns(upstreamDnsServer, ":1053", clientConfig.Search[0],
config.InternalDomains)
return kubedocDns
}

type DnsWatcherIntegration struct {
pods *Pods
dns *KubeDockDns
pods *model.Pods
dns *dns.KubeDockDns
}

func (integrator *DnsWatcherIntegration) AddOrUpdate(pod *Pod) {
func (integrator *DnsWatcherIntegration) AddOrUpdate(pod *model.Pod) {
klog.V(2).Infof("%v/%v: Pod added or updated", pod.Namespace, pod.Name)
if integrator.pods.AddOrUpdate(pod) {
integrator.updateDns()
Expand All @@ -52,7 +57,7 @@ func (integrator *DnsWatcherIntegration) updateDns() {
}
}

func execute(cmd *cobra.Command, args []string, config Config) error {
func execute(cmd *cobra.Command, args []string, config config.Config) error {

klog.Info("Starting DNS server and mutator")
klog.V(2).Info("Verbose logging enabled")
Expand All @@ -79,25 +84,25 @@ func execute(cmd *cobra.Command, args []string, config Config) error {
dns := createDns(config)
sourceIp := os.Getenv("KUBEDOCK_DNS_SOURCE_IP")
if sourceIp != "" {
dns.OverrideSourceIP(IPAddress(sourceIp))
dns.OverrideSourceIP(model.IPAddress(sourceIp))
}
go func() {
dns.Serve()
}()

// pod administration
pods := NewPods()
pods := model.NewPods()
dnsWatcherIntegration := &DnsWatcherIntegration{
pods: pods,
dns: dns,
}

// Watching Pods
go WatchPods(clientset, namespace, dnsWatcherIntegration, config.PodConfig)
go watcher.WatchPods(clientset, namespace, dnsWatcherIntegration, config.PodConfig)

// Admission controller

if err := runAdmisstionController(ctx, pods, clientset, namespace, "dns-server",
if err := admissioncontroller.RunAdmisstionController(ctx, pods, clientset, namespace, "dns-server",
config.CrtFile, config.KeyFile, config.PodConfig); err != nil {
return fmt.Errorf("Could not start admission controler: %+v", err)
}
Expand All @@ -108,7 +113,7 @@ func main() {
klogFlags := goflags.NewFlagSet("", goflags.PanicOnError)
klog.InitFlags(klogFlags)

config := Config{}
config := config.Config{}
cmd := &cobra.Command{
Use: "kubedock-dns",
Short: "Run a DNS server and mutator for test containers",
Expand Down
11 changes: 6 additions & 5 deletions cmd/dns-server/performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ import (
"k8s.io/klog/v2"
"strconv"
"testing"
"wamblee.org/kubedock/dns/internal/model"
)

func BenchmarkCreateNetworks(b *testing.B) {
nPodsPerTest := 3
nTests := 300

pods := NewPods()
pods := model.NewPods()

for i := range nTests {
for j := range nPodsPerTest {
ipod := i*nPodsPerTest + j
pod, err := NewPod(
IPAddress(strconv.Itoa(ipod)),
pod, err := model.NewPod(
model.IPAddress(strconv.Itoa(ipod)),
"kubedock",
fmt.Sprintf("pod%d", ipod),
[]Hostname{Hostname(fmt.Sprintf("host%d", j))},
[]NetworkId{NetworkId(fmt.Sprintf("network%d", i))},
[]model.Hostname{model.Hostname(fmt.Sprintf("host%d", j))},
[]model.NetworkId{model.NetworkId(fmt.Sprintf("network%d", i))},
)
assert.Nil(b, err)
pods.AddOrUpdate(pod)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main
package admissioncontroller

import (
"context"
"fmt"
"github.com/miekg/dns"
"gomodules.xyz/jsonpatch/v2"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/kubernetes"
Expand All @@ -14,11 +16,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"strconv"
"time"
"wamblee.org/kubedock/dns/internal/config"
"wamblee.org/kubedock/dns/internal/model"
"wamblee.org/kubedock/dns/internal/support"

admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"encoding/json"
controllerlog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
Expand All @@ -29,8 +30,8 @@ const (
)

type DnsMutator struct {
podConfig PodConfig
pods *Pods
podConfig config.PodConfig
pods *model.Pods
dnsServiceIP string
clientConfig *dns.ClientConfig
}
Expand All @@ -41,8 +42,8 @@ type PatchOperation struct {
Value interface{} `json:"value,omitempty"`
}

func NewDnsMutator(pods *Pods, dnsServiceIP string, clientConfig *dns.ClientConfig,
podConfig PodConfig) *DnsMutator {
func NewDnsMutator(pods *model.Pods, dnsServiceIP string, clientConfig *dns.ClientConfig,
podConfig config.PodConfig) *DnsMutator {
mutator := DnsMutator{
podConfig: podConfig,
pods: pods,
Expand Down Expand Up @@ -76,15 +77,15 @@ func (mutator *DnsMutator) validateK8sPod(k8spod corev1.Pod, operation admission
// later when the IP becomes known during deployment.
podIpOverride := k8spod.Status.PodIP
if podIpOverride == "" {
podIpOverride = UNKNOWN_IP_PREFIX + strconv.Itoa(time.Now().Nanosecond()) +
podIpOverride = model.UNKNOWN_IP_PREFIX + strconv.Itoa(time.Now().Nanosecond()) +
strconv.Itoa(rand.Int())
}
pod, err := getPodEssentials(&k8spod, podIpOverride, mutator.podConfig)
pod, err := model.GetPodEssentials(&k8spod, podIpOverride, mutator.podConfig)
if err != nil {
klog.Infof("%v", err)
return err
}
var networks *Networks
var networks *model.Networks
networks, err = mutator.validatePod(operation, pod)
if err != nil {
klog.Warningf("%s/%s invalid", pod.Namespace, pod.Name)
Expand All @@ -96,7 +97,7 @@ func (mutator *DnsMutator) validateK8sPod(k8spod corev1.Pod, operation admission
return nil
}

func (mutator *DnsMutator) validatePod(operation admissionv1.Operation, pod *Pod) (*Networks, error) {
func (mutator *DnsMutator) validatePod(operation admissionv1.Operation, pod *model.Pod) (*model.Networks, error) {
if operation == admissionv1.Update {
oldpod := mutator.pods.Get(pod.Namespace, pod.Name)
if !oldpod.Equal(pod) {
Expand Down Expand Up @@ -191,14 +192,14 @@ func (mutator *DnsMutator) rejectPod(request admission.Request,
return response
}

func runAdmisstionController(ctx context.Context,
pods *Pods,
func RunAdmisstionController(ctx context.Context,
pods *model.Pods,
clientset *kubernetes.Clientset,
namespace string,
dnsServiceName string,
crtFile string,
keyFile string,
podConfig PodConfig) error {
podConfig config.PodConfig) error {

svc, err := clientset.CoreV1().Services(namespace).Get(ctx, dnsServiceName, v1.GetOptions{})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package admissioncontroller

import (
"context"
Expand All @@ -17,23 +17,25 @@ import (
"strconv"
"strings"
"testing"
config2 "wamblee.org/kubedock/dns/internal/config"
"wamblee.org/kubedock/dns/internal/model"
)

type MutatorTestSuite struct {
suite.Suite

config PodConfig
config config2.PodConfig

ctx context.Context
dnsip string
pods *Pods
pods *model.Pods
mutator *DnsMutator
clientConfig dns.ClientConfig
stdlabels map[string]string
}

func (s *MutatorTestSuite) SetupSuite() {
s.config = PodConfig{
s.config = config2.PodConfig{
LabelName: "kubedock",
HostAliasPrefix: "kubedock.host/",
NetworkIdPrefix: "kubedock.network/",
Expand All @@ -43,7 +45,7 @@ func (s *MutatorTestSuite) SetupSuite() {
func (s *MutatorTestSuite) SetupTest() {
s.ctx = context.Background()
s.dnsip = "10.11.12.13"
s.pods = NewPods()
s.pods = model.NewPods()
s.clientConfig = dns.ClientConfig{
Servers: []string{"11.12.13.14"},
Search: []string{"a.b.c", "b.c", "c"},
Expand All @@ -70,7 +72,7 @@ func (s *MutatorTestSuite) createPod(namespace string, name string,
labels map[string]string,
ip string) v1.Pod {
if ip == "" {
ip = UNKNOWN_IP_PREFIX + strconv.Itoa(rand.Int())
ip = model.UNKNOWN_IP_PREFIX + strconv.Itoa(rand.Int())
}
pod := v1.Pod{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -201,8 +203,8 @@ func (s *MutatorTestSuite) Test_SingleHostAndNetwork() {

pod := s.pods.Get("kubedock", "db")
s.NotNil(pod)
s.Equal([]Hostname{"db"}, pod.HostAliases)
s.Equal([]NetworkId{"test"}, pod.Networks)
s.Equal([]model.Hostname{"db"}, pod.HostAliases)
s.Equal([]model.NetworkId{"test"}, pod.Networks)
}

func (s *MutatorTestSuite) Test_DuplicateHost() {
Expand Down Expand Up @@ -325,8 +327,8 @@ func (s *MutatorTestSuite) Test_UpdateAllowedWhenNetworkNotModified() {

pod := s.pods.Get("kubedock", "db")
s.NotNil(pod)
s.Equal([]Hostname{"db"}, pod.HostAliases)
s.Equal([]NetworkId{"test"}, pod.Networks)
s.Equal([]model.Hostname{"db"}, pod.HostAliases)
s.Equal([]model.NetworkId{"test"}, pod.Networks)
}

func (s *MutatorTestSuite) Test_UpdateDeniedWhenHostModified() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dns-server/config.go → internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import "time"

Expand Down
Loading

0 comments on commit 7cac33e

Please sign in to comment.