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 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 15, 2021
1 parent fe34099 commit c761843
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 103 deletions.
2 changes: 2 additions & 0 deletions controllers/depresolver/depresolver.go
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
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
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
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
6 changes: 3 additions & 3 deletions controllers/dnsupdate.go
Expand Up @@ -373,7 +373,7 @@ func Dig(edgeDNSServer, fqdn string) ([]string, error) {
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 Down Expand Up @@ -411,7 +411,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 +431,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
2 changes: 1 addition & 1 deletion controllers/finalize.go
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
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

0 comments on commit c761843

Please sign in to comment.