Skip to content

Commit

Permalink
refactor-ingress-path
Browse files Browse the repository at this point in the history
  • Loading branch information
kuritka committed Feb 7, 2025
1 parent 627eff0 commit cfdb80c
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 48 deletions.
18 changes: 10 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ deploy-local-cluster:
helm -n k8gb upgrade -i nginx-ingress nginx-stable/ingress-nginx \
--version 4.0.15 -f $(NGINX_INGRESS_VALUES_PATH)

@echo -e "\n$(YELLOW)Deploy GSLB operator from $(VERSION) $(NC)"
$(MAKE) deploy-k8gb-with-helm

@echo -e "\n$(YELLOW)Install Istio CRDs $(NC)"
kubectl create namespace istio-system --dry-run=client -o yaml | kubectl apply -f -
helm repo add --force-update istio https://istio-release.storage.googleapis.com/charts
Expand All @@ -202,24 +205,24 @@ deploy-local-cluster:
helm upgrade -i istio-ingressgateway istio/gateway -n istio-ingress \
--version "$(ISTIO_VERSION)" -f $(ISTIO_INGRESS_VALUES_PATH)

@echo -e "\n$(YELLOW)Deploy apps $(NC)"
@if [ "$(DEPLOY_APPS)" = true ]; then $(MAKE) deploy-test-apps ; fi

@echo -e "\n$(YELLOW)Deploy GSLB operator from $(VERSION) $(NC)"
$(MAKE) deploy-k8gb-with-helm

@echo -e "\n$(YELLOW)Wait until Ingress controller is ready $(NC)"
@echo -e "\n$(YELLOW)Wait until Ingress controllers are ready $(NC)"
$(call wait-for-ingress)
@echo -e "\n$(YELLOW)Wait until CoreDNS is ready $(NC)"
$(call wait-for-k8gb)

@echo -e "\n$(YELLOW) Installing ingress to fetch IP's $(NC)"
kubectl apply -f deploy/crds/init.yaml

@echo -e "\n$(CYAN)$(CLUSTER_NAME)$(CLUSTER_ID) $(YELLOW)deployed! $(NC)"

.PHONY: deploy-test-apps
deploy-test-apps: ## Deploy Podinfo (example app) and Apply Gslb Custom Resources
@echo -e "\n$(YELLOW)Deploy GSLB cr $(NC)"

kubectl apply -f deploy/crds/init.yaml

kubectl apply -f deploy/crds/test-namespace-ingress.yaml
kubectl apply -f deploy/crds/test-ingress-init.yaml
$(call apply-cr,deploy/crds/k8gb.absa.oss_v1beta1_gslb_cr_roundrobin_ingress_ref.yaml)
$(call apply-cr,deploy/crds/k8gb.absa.oss_v1beta1_gslb_cr_failover_ingress_ref.yaml)

Expand Down Expand Up @@ -581,7 +584,6 @@ define wait-for-k8gb
kubectl -n k8gb wait --for=condition=Ready pod -l app.kubernetes.io/name=coredns --timeout=200s
endef


define generate
$(call install-controller-gen)
@echo -e "\n$(YELLOW)Generating the API code$(NC)"
Expand Down
2 changes: 1 addition & 1 deletion chart/k8gb/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ k8gb:
# If coredns.serviceType is set to LoadBalancer, this value is ignored, and the IP addresses are loaded
# directly from the CoreDNS service.
# ingressPath: "namespace/ingress-to-fetch-ips"
ingressPath: "test-gslb/init-ingress"
ingressPath: "default/init-ingress"
log:
# -- log format (simple,json)
format: simple # log format (simple,json)
Expand Down
8 changes: 8 additions & 0 deletions controllers/depresolver/depresolver_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,11 @@ func getNsName(tag, dnsZone, edgeDNSZone, edgeDNSServer string) string {
func getHeartbeatFQDN(name, geoTag, edgeDNSZone string) string {
return fmt.Sprintf("%s-heartbeat-%s.%s", name, geoTag, edgeDNSZone)
}

func (c *Config) ParseIngressPath() (string, string, error) {
arr := strings.Split(c.IngressPath, "/")
if len(arr) != 2 {
return "", "", fmt.Errorf("path format error (namespace/name): %s", c.IngressPath)
}
return arr[1], arr[0], nil
}
28 changes: 21 additions & 7 deletions controllers/gslb_controller_reconciliation.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ import (
// GslbReconciler reconciles a Gslb object
type GslbReconciler struct {
client.Client
Scheme *runtime.Scheme
Config *depresolver.Config
DepResolver depresolver.GslbResolver
DNSProvider dns.Provider
Recorder record.EventRecorder
Tracer trace.Tracer
Scheme *runtime.Scheme
Config *depresolver.Config
DepResolver depresolver.GslbResolver
DNSProvider dns.Provider
Recorder record.EventRecorder
Tracer trace.Tracer
zoneDelegation []string
}

const (
Expand All @@ -77,8 +78,13 @@ var m = metrics.Metrics()
func (r *GslbReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx, span := r.Tracer.Start(ctx, "Reconcile")
defer span.End()

result := utils.NewReconcileResultHandler(r.Config.ReconcileRequeueSeconds)

if !r.HasZoneDelegationSet() {
log.Error().Msg("No zone delegation set, nothing to reconcile. Expose CoreDNS LoadBalancer service or set INGRESS_PATH")
return result.Stop()
}

// Fetch the Gslb instance
gslb := &k8gbv1beta1.Gslb{}
err := r.Get(ctx, req.NamespacedName, gslb)
Expand Down Expand Up @@ -226,3 +232,11 @@ func (r *GslbReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
m.IncrementReconciliation(gslb)
return result.Requeue()
}

func (r *GslbReconciler) SetIPs(ips []string) {
r.zoneDelegation = ips
}

func (r *GslbReconciler) HasZoneDelegationSet() bool {
return len(r.zoneDelegation) != 0
}
45 changes: 41 additions & 4 deletions controllers/gslb_controller_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
import (
"context"
"fmt"
"github.com/k8gb-io/k8gb/controllers/providers/assistant"
"strconv"

"github.com/k8gb-io/k8gb/controllers/utils"
Expand Down Expand Up @@ -81,20 +82,56 @@ func (r *GslbReconciler) SetupWithManager(mgr ctrl.Manager) error {
})

ingressMapHandler := handler.EnqueueRequestsFromMapFunc(
func(_ context.Context, a client.Object) []reconcile.Request {
func(ctx context.Context, a client.Object) []reconcile.Request {
// create ingress from annotations
annotations := a.GetAnnotations()
if annotationValue, found := annotations[strategyAnnotation]; found {
c := mgr.GetClient()
r.createGSLBFromIngress(c, a, annotationValue)
}

// update delegation zone ingress IPs
ing, isIngress := a.(*netv1.Ingress)
if isIngress && !r.Config.CoreDNSExposed {
name, namespace, _ := r.Config.ParseIngressPath()
if ing.Name == name && ing.Namespace == namespace {
log.Info().Msg("Configure DNS Zones")
ips, err := assistant.NewLoadBalancerService(ctx, r.Client, *r.Config).GetIngressStatusIPs()
if err != nil {
log.Err(err).Msgf("Can't fetch IP addresses from ingress %s", r.Config.IngressPath)
return nil
}
if len(ips) == 0 {
log.Error().Msgf("No IP's bound in ingress %s", r.Config.IngressPath)
return nil
}
r.SetIPs(ips)
log.Info().Msgf("Found IPs %v", ips)
err = r.DNSProvider.CreateZoneDelegationForExternalDNS(ips)
if err != nil {
log.Err(err).Msgf("can't create zone delegation for ingress %s", r.Config.IngressPath)
}
}
}

return nil
})

coreDNSServiceHandler := handler.EnqueueRequestsFromMapFunc(
func(_ context.Context, object client.Object) []reconcile.Request {
if object.GetNamespace() == r.Config.K8gbNamespace && object.GetName() == coreDNSService {
func(ctx context.Context, object client.Object) []reconcile.Request {
if r.Config.CoreDNSExposed && object.GetNamespace() == r.Config.K8gbNamespace && object.GetName() == coreDNSService {
log.Info().Msg("Configure DNS Zones")
err := r.DNSProvider.CreateZoneDelegationForExternalDNS()
ips, err := assistant.NewLoadBalancerService(ctx, r.Client, *r.Config).GetCoreDNSLoadBalancerServiceIPs()
if err != nil {
log.Err(err).Msgf("Can't fetch IP addresses from service %s/%s", object.GetNamespace(), object.GetName())
return nil
}
if len(ips) == 0 {
log.Error().Msgf("No IP's bound in service %s/%s", object.GetNamespace(), object.GetName())
return nil
}
log.Info().Msgf("Found IPs %v", ips)
err = r.DNSProvider.CreateZoneDelegationForExternalDNS(ips)
if err != nil {
log.Err(err).Msg("can't create zone delegation")
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/providers/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

type Provider interface {
// CreateZoneDelegationForExternalDNS handles delegated zone in Edge DNS
CreateZoneDelegationForExternalDNS() error
CreateZoneDelegationForExternalDNS([]string) error
// GetExternalTargets retrieves list of external targets for specified host
GetExternalTargets(string) assistant.Targets
// SaveDNSEndpoint update DNS endpoint in gslb or create new one if doesn't exist
Expand Down
2 changes: 1 addition & 1 deletion controllers/providers/dns/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewEmptyDNS(config depresolver.Config, assistant assistant.Assistant) *Empt
}
}

func (p *EmptyDNSProvider) CreateZoneDelegationForExternalDNS() (err error) {
func (p *EmptyDNSProvider) CreateZoneDelegationForExternalDNS(_ []string) (err error) {
return
}

Expand Down
17 changes: 3 additions & 14 deletions controllers/providers/dns/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewExternalDNS(config depresolver.Config, assistant assistant2.Assistant) *
}
}

func (p *ExternalDNSProvider) CreateZoneDelegationForExternalDNS() error {
func (p *ExternalDNSProvider) CreateZoneDelegationForExternalDNS(nsServerIPs []string) error {
ttl := externaldns.TTL(p.config.NSRecordTTL)
log.Info().
Interface("provider", p).
Expand All @@ -63,17 +63,6 @@ func (p *ExternalDNSProvider) CreateZoneDelegationForExternalDNS() error {
NSServerList = append(NSServerList, v)
}
sort.Strings(NSServerList)
var NSServerIPs []string
var err error
if p.config.CoreDNSExposed {
NSServerIPs, err = p.assistant.GetCoreDNSLoadBalancerServiceIPs()
} else {
NSServerIPs, err = p.assistant.GetIngressStatusIPs()
}
if err != nil {
return err
}
log.Info().Msgf("Found NS Server IPs: %v", NSServerIPs)
NSRecord := &externaldns.DNSEndpoint{
ObjectMeta: metav1.ObjectMeta{
Name: p.endpointName,
Expand All @@ -92,12 +81,12 @@ func (p *ExternalDNSProvider) CreateZoneDelegationForExternalDNS() error {
DNSName: p.config.GetClusterNSName(),
RecordTTL: ttl,
RecordType: "A",
Targets: NSServerIPs,
Targets: nsServerIPs,
},
},
},
}
err = p.assistant.SaveDNSEndpoint(p.config.K8gbNamespace, NSRecord)
err := p.assistant.SaveDNSEndpoint(p.config.K8gbNamespace, NSRecord)
if err != nil {
return err
}
Expand Down
13 changes: 2 additions & 11 deletions controllers/providers/dns/infoblox.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,15 @@ func (p *InfobloxProvider) sanitizeDelegateZone(local, upstream []ibcl.NameServe
return final
}

func (p *InfobloxProvider) CreateZoneDelegationForExternalDNS() error {
func (p *InfobloxProvider) CreateZoneDelegationForExternalDNS(nsServerIPs []string) error {
objMgr, err := p.client.GetObjectManager()
if err != nil {
return err
}

var addresses []string
if p.config.CoreDNSExposed {
addresses, err = p.assistant.GetCoreDNSLoadBalancerServiceIPs()
} else {
addresses, err = p.assistant.GetIngressStatusIPs()
}
if err != nil {
return err
}
var delegateTo []ibcl.NameServer

for _, address := range addresses {
for _, address := range nsServerIPs {
nameServer := ibcl.NameServer{Address: address, Name: p.config.GetClusterNSName()}
delegateTo = append(delegateTo, nameServer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
labels:
app: init-ingress
name: init-ingress
namespace: test-gslb
namespace: default
spec:
ingressClassName: nginx
rules:
Expand Down
84 changes: 84 additions & 0 deletions taskfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# https://taskfile.dev/usage/
version: '3'
env:
VERSION: test008

tasks:

start-*:
vars:
CLUSTER: '{{index .MATCH 0}}'
cmds:
- echo "starting k3d-test-gslb{{ .CLUSTER }}"
- kubectl -n k8gb scale deployment k8gb --replicas=1 --context=k3d-test-gslb{{ .CLUSTER }}
description: "k3d-test-gslb2 = us, k3d-test-gslb1 = eu"
silent: true

stop-*:
vars:
CLUSTER: '{{index .MATCH 0}}'
cmds:
- echo "stopping k3d-test-gslb{{ .CLUSTER }}"
- kubectl -n k8gb scale deployment k8gb --replicas=0 --context=k3d-test-gslb{{ .CLUSTER }}
description: "k3d-test-gslb2 = us, k3d-test-gslb1 = eu"
silent: true

install:
cmds:
- defer: { task: us}
- echo "Installing current k8gb $VERSION"
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w -X main.version=$VERSION -X main.commit=123" -o ./k8gb main.go
- docker build . -t docker.io/kuritka/k8gb:$VERSION
- k3d image import docker.io/kuritka/k8gb:$VERSION -c test-gslb1
- k3d image import docker.io/kuritka/k8gb:$VERSION -c test-gslb2
- kubectl apply -f deploy/crds/init.yaml --context=k3d-test-gslb2 # for INGRESS_PATH=test-gslb/init-ingress
- kubectl apply -f deploy/crds/init.yaml --context=k3d-test-gslb1 # for INGRESS_PATH=test-gslb/init-ingress
- task: stop
- task: patch-deployment
- kubectl -n k8gb set image deployment/k8gb k8gb=kuritka/k8gb:$VERSION --context=k3d-test-gslb2
- kubectl -n k8gb set image deployment/k8gb k8gb=kuritka/k8gb:$VERSION --context=k3d-test-gslb1
- task: start-1
silent: true

stop:
cmds:
- task: stop-1
- task: stop-2

start:
cmds:
- task: start-1
- task: start-2

us:
cmds:
- kubectl config use-context k3d-test-gslb2
eu:
cmds:
- kubectl config use-context k3d-test-gslb1

patch-deployment:
desc: "Patch the deployment to add the environment variable"
cmds:
- |
kubectl patch deployment k8gb -n k8gb \
--patch '
{
"spec": {
"template": {
"spec": {
"containers": [
{
"name": "k8gb",
"env": [
{
"name": "INGRESS_PATH",
"value": "default/init"
}
]
}
]
}
}
}
}'

0 comments on commit cfdb80c

Please sign in to comment.