Skip to content

Commit

Permalink
Related to #255
Browse files Browse the repository at this point in the history
This is first of PR

 -  move Dig to utils
 -  move prettyPrint to utils and rename to `ToJSON()`
 -  cover `ToJSON()` by tests
 -  move environment variable `POD_NAMESPACE` to `depresolver`
 -  extend depresolver tests
 -  extend depresolver validators for namespace validator
 -  Extract Prometheus metrics into standalone module
 -  Extend prometheus metrics
 -  Fix controller tests
 -  remove dead registry/k3s.yaml file (pushed by mistake)

TODO:
  - dnsupdate refactor to provider pattern in next PR
  - fianally, some of utils will go into [gopkg](https://github.com/AbsaOSS/gopkg)
  - IMetrics introduction (#124)
  • Loading branch information
kuritka committed Jan 14, 2021
1 parent fe34099 commit 51cd8c8
Show file tree
Hide file tree
Showing 17 changed files with 378 additions and 154 deletions.
2 changes: 2 additions & 0 deletions controllers/depresolver/depresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type Config struct {
EdgeDNSZone string
// DNSZone controlled by gslb; e.g. cloud.example.com
DNSZone string
// K8gbNamespace k8gb namespace
K8gbNamespace string
// Infoblox configuration
Infoblox Infoblox
// Override the behavior of GSLB in the test environments
Expand Down
6 changes: 6 additions & 0 deletions controllers/depresolver/depresolver_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
InfobloxPasswordKey = "EXTERNAL_DNS_INFOBLOX_WAPI_PASSWORD"
OverrideWithFakeDNSKey = "OVERRIDE_WITH_FAKE_EXT_DNS"
OverrideFakeInfobloxKey = "FAKE_INFOBLOX"
K8gbNamespaceKey = "POD_NAMESPACE"
)

// ResolveOperatorConfig executes once. It reads operator's configuration
Expand All @@ -39,6 +40,7 @@ func (dr *DependencyResolver) ResolveOperatorConfig() (*Config, error) {
dr.config.EdgeDNSServer = env.GetEnvAsStringOrFallback(EdgeDNSServerKey, "")
dr.config.EdgeDNSZone = env.GetEnvAsStringOrFallback(EdgeDNSZoneKey, "")
dr.config.DNSZone = env.GetEnvAsStringOrFallback(DNSZoneKey, "")
dr.config.K8gbNamespace = env.GetEnvAsStringOrFallback(K8gbNamespaceKey, "")
dr.config.Infoblox.Host = env.GetEnvAsStringOrFallback(InfobloxGridHostKey, "")
dr.config.Infoblox.Version = env.GetEnvAsStringOrFallback(InfobloxVersionKey, "")
dr.config.Infoblox.Port, _ = env.GetEnvAsIntOrFallback(InfobloxPortKey, 0)
Expand All @@ -53,6 +55,10 @@ func (dr *DependencyResolver) ResolveOperatorConfig() (*Config, error) {
}

func (dr *DependencyResolver) validateConfig(config *Config) (err error) {
err = field("k8gbNamespace", config.K8gbNamespace).isNotEmpty().matchRegexp(k8sNamespaceRegex).err
if err != nil {
return err
}
err = field("reconcileRequeueSeconds", config.ReconcileRequeueSeconds).isHigherThanZero().err
if err != nil {
return err
Expand Down
35 changes: 34 additions & 1 deletion controllers/depresolver/depresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var predefinedConfig = Config{
EdgeDNSServer: "cloud.example.com",
EdgeDNSZone: "8.8.8.8",
DNSZone: "example.com",
K8gbNamespace: "k8gb",
Infoblox: Infoblox{
"Infoblox.host.com",
"0.0.3",
Expand Down Expand Up @@ -466,6 +467,37 @@ func TestResolveConfigWithoutDnsZone(t *testing.T) {
arrangeVariablesAndAssert(t, expected, assert.Error, DNSZoneKey)
}

func TestResolveConfigWithEmptyK8gbNamespace(t *testing.T) {
// arrange
defer cleanup()
expected := predefinedConfig
expected.K8gbNamespace = ""
// act,assert
arrangeVariablesAndAssert(t, expected, assert.Error, K8gbNamespaceKey)
}

func TestResolveConfigWithInvalidK8gbNamespace(t *testing.T) {
// arrange
defer cleanup()
for _, ns := range []string{"-","Op.","kube/netes","my-ns???","123-MY","MY-123"} {
expected := predefinedConfig
expected.K8gbNamespace = ns
// act,assert
arrangeVariablesAndAssert(t, expected, assert.Error)
}
}

func TestResolveConfigWithValidK8gbNamespace(t *testing.T) {
// arrange
defer cleanup()
for _, ns := range []string{"k8gb","my-123","123-my","n"} {
expected := predefinedConfig
expected.K8gbNamespace = ns
// act,assert
arrangeVariablesAndAssert(t, expected, assert.NoError)
}
}

func TestResolveEmptyExtGeoTags(t *testing.T) {
// arrange
defer cleanup()
Expand Down Expand Up @@ -928,7 +960,7 @@ func arrangeVariablesAndAssert(t *testing.T, expected Config,
func cleanup() {
for _, s := range []string{ReconcileRequeueSecondsKey, ClusterGeoTagKey, ExtClustersGeoTagsKey, EdgeDNSZoneKey, DNSZoneKey, EdgeDNSServerKey,
Route53EnabledKey, InfobloxGridHostKey, InfobloxVersionKey, InfobloxPortKey, InfobloxUsernameKey, InfobloxPasswordKey,
OverrideWithFakeDNSKey, OverrideFakeInfobloxKey} {
OverrideWithFakeDNSKey, OverrideFakeInfobloxKey, K8gbNamespaceKey} {
if os.Unsetenv(s) != nil {
panic(fmt.Errorf("cleanup %s", s))
}
Expand All @@ -942,6 +974,7 @@ func configureEnvVar(config Config) {
_ = os.Setenv(EdgeDNSServerKey, config.EdgeDNSServer)
_ = os.Setenv(EdgeDNSZoneKey, config.EdgeDNSZone)
_ = os.Setenv(DNSZoneKey, config.DNSZone)
_ = os.Setenv(K8gbNamespaceKey, config.K8gbNamespace)
_ = os.Setenv(Route53EnabledKey, strconv.FormatBool(config.route53Enabled))
_ = os.Setenv(NS1EnabledKey, strconv.FormatBool(config.ns1Enabled))
_ = os.Setenv(InfobloxGridHostKey, config.Infoblox.Host)
Expand Down
2 changes: 2 additions & 0 deletions controllers/depresolver/depresolver_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
ipAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
// versionNumberRegex matches version in formats 0.1.2, v0.1.2, v0.1.2-alpha
versionNumberRegex = "^(v){0,1}(0|(?:[1-9]\\d*))(?:\\.(0|(?:[1-9]\\d*))(?:\\.(0|(?:[1-9]\\d*)))?(?:\\-([\\w][\\w\\.\\-_]*))?)?$"
// k8sNamespaceRegex matches valid kubernetes namespace
k8sNamespaceRegex = "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
)

// validator wrapper against field to be verified
Expand Down
50 changes: 10 additions & 40 deletions controllers/dnsupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package controllers

import (
"context"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"time"

"github.com/AbsaOSS/k8gb/controllers/internal/utils"

"github.com/AbsaOSS/k8gb/controllers/depresolver"

coreerrors "errors"

k8gbv1beta1 "github.com/AbsaOSS/k8gb/api/v1beta1"
ibclient "github.com/infobloxopen/infoblox-go-client"
"github.com/lixiangzhong/dnsutil"
"github.com/miekg/dns"
corev1 "k8s.io/api/core/v1"
v1beta1 "k8s.io/api/extensions/v1beta1"
Expand Down Expand Up @@ -52,8 +52,9 @@ func (r *GslbReconciler) getGslbIngressIPs(gslb *k8gbv1beta1.Gslb) ([]string, er
gslbIngressIPs = append(gslbIngressIPs, ip.IP)
}
if len(ip.Hostname) > 0 {
IPs, err := Dig(r.Config.EdgeDNSServer, ip.Hostname)
IPs, err := utils.Dig(r.Config.EdgeDNSServer, ip.Hostname)
if err != nil {
log.Info("Dig error: %s", err.Error())
return nil, err
}
gslbIngressIPs = append(gslbIngressIPs, IPs...)
Expand Down Expand Up @@ -346,34 +347,10 @@ func filterOutDelegateTo(delegateTo []ibclient.NameServer, fqdn string) []ibclie
return delegateTo
}

// Dig digs
func Dig(edgeDNSServer, fqdn string) ([]string, error) {
var dig dnsutil.Dig
if edgeDNSServer == "" {
return nil, fmt.Errorf("empty edgeDNSServer")
}
err := dig.SetDNS(edgeDNSServer)
if err != nil {
log.Info(fmt.Sprintf("Can't set query dns (%s) with error(%s)", edgeDNSServer, err))
return nil, err
}
a, err := dig.A(fqdn)
if err != nil {
log.Info(fmt.Sprintf("Can't dig fqdn(%s) with error(%s)", fqdn, err))
return nil, err
}
var IPs []string
for _, ip := range a {
IPs = append(IPs, fmt.Sprint(ip.A))
}
sort.Strings(IPs)
return IPs, nil
}

func (r *GslbReconciler) coreDNSExposedIPs() ([]string, error) {
coreDNSService := &corev1.Service{}

err := r.Get(context.TODO(), types.NamespacedName{Namespace: k8gbNamespace, Name: coreDNSExtServiceName}, coreDNSService)
err := r.Get(context.TODO(), types.NamespacedName{Namespace: r.Config.K8gbNamespace, Name: coreDNSExtServiceName}, coreDNSService)
if err != nil {
if errors.IsNotFound(err) {
log.Info(fmt.Sprintf("Can't find %s service", coreDNSExtServiceName))
Expand All @@ -389,8 +366,9 @@ func (r *GslbReconciler) coreDNSExposedIPs() ([]string, error) {
err := coreerrors.New(errMessage)
return nil, err
}
IPs, err := Dig(r.Config.EdgeDNSServer, lbHostname)
IPs, err := utils.Dig(r.Config.EdgeDNSServer, lbHostname)
if err != nil {
log.Info("Dig error: %s", err.Error())
log.Info(fmt.Sprintf("Can't dig k8gb-coredns-lb service loadbalancer fqdn %s", lbHostname))
return nil, err
}
Expand All @@ -411,7 +389,7 @@ func (r *GslbReconciler) createZoneDelegationRecordsForExternalDNS(gslb *k8gbv1b
NSRecord := &externaldns.DNSEndpoint{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("k8gb-ns-%s", dnsProvider),
Namespace: k8gbNamespace,
Namespace: r.Config.K8gbNamespace,
Annotations: map[string]string{"k8gb.absa.oss/dnstype": dnsProvider},
},
Spec: externaldns.DNSEndpointSpec{
Expand All @@ -431,7 +409,7 @@ func (r *GslbReconciler) createZoneDelegationRecordsForExternalDNS(gslb *k8gbv1b
},
},
}
res, err := r.ensureDNSEndpoint(k8gbNamespace, NSRecord)
res, err := r.ensureDNSEndpoint(r.Config.K8gbNamespace, NSRecord)
if err != nil {
return res, err
}
Expand Down Expand Up @@ -538,7 +516,7 @@ func (r *GslbReconciler) ensureDNSEndpoint(
if err != nil && errors.IsNotFound(err) {

// Create the DNSEndpoint
log.Info(fmt.Sprintf("Creating a new DNSEndpoint:\n %s", prettyPrint(i)))
log.Info(fmt.Sprintf("Creating a new DNSEndpoint:\n %s", utils.ToJSON(i)))
err = r.Create(context.TODO(), i)

if err != nil {
Expand Down Expand Up @@ -583,11 +561,3 @@ func overrideWithFakeDNS(fakeDNSEnabled bool, server string) (ns string) {
}
return
}

func prettyPrint(s interface{}) string {
prettyStruct, err := json.MarshalIndent(s, "", "\t")
if err != nil {
fmt.Println("can't convert struct to json")
}
return string(prettyStruct)
}
2 changes: 1 addition & 1 deletion controllers/finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (r *GslbReconciler) finalizeGslb(gslb *k8gbv1beta1.Gslb) error {
if r.Config.EdgeDNSType == depresolver.DNSTypeRoute53 {
log.Info("Removing Zone Delegation entries...")
dnsEndpointRoute53 := &externaldns.DNSEndpoint{}
err := r.Get(context.Background(), client.ObjectKey{Namespace: k8gbNamespace, Name: "k8gb-ns-route53"}, dnsEndpointRoute53)
err := r.Get(context.Background(), client.ObjectKey{Namespace: r.Config.K8gbNamespace, Name: "k8gb-ns-route53"}, dnsEndpointRoute53)
if err != nil {
if errors.IsNotFound(err) {
log.Info(fmt.Sprint(err))
Expand Down
5 changes: 3 additions & 2 deletions controllers/gslb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package controllers
import (
"context"
"fmt"
"os"
"time"

"github.com/AbsaOSS/k8gb/controllers/metrics"

"github.com/AbsaOSS/k8gb/controllers/depresolver"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand All @@ -41,7 +42,6 @@ import (
k8gbv1beta1 "github.com/AbsaOSS/k8gb/api/v1beta1"
)

var k8gbNamespace = os.Getenv("POD_NAMESPACE")
var log = logf.Log.WithName("controller_gslb")

// GslbReconciler reconciles a Gslb object
Expand All @@ -51,6 +51,7 @@ type GslbReconciler struct {
Scheme *runtime.Scheme
Config *depresolver.Config
DepResolver *depresolver.DependencyResolver
Metrics *metrics.PrometheusMetrics
}

const (
Expand Down
Loading

0 comments on commit 51cd8c8

Please sign in to comment.