Skip to content

Commit

Permalink
[FEATURE] Operator: DNS_TARGET via env. supported (#68)
Browse files Browse the repository at this point in the history
Allow consumers to configure a DNS_TARGET via env. on controller.
  • Loading branch information
Pavan-SAP committed Mar 7, 2024
1 parent 3ff2bf3 commit f90aa29
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
6 changes: 5 additions & 1 deletion internal/controller/reconcile-domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,11 @@ func (c *Controller) getIngressGatewayInfo(ctx context.Context, ca *v1alpha1.CAP
// Get dnsTarget
// First try to use dnsTarget --> if it is set
dnsTarget := ca.Spec.Domains.DnsTarget
// Attempt to get dnsTarget from Service via annotation(s)
// Attempt to get dnsTarget from Env
if dnsTarget == "" {
dnsTarget = envDNSTarget()
}
// Finally attempt to get dnsTarget from Service via annotation(s)
if dnsTarget == "" {
ingressGWSvc, err := c.getIngressGatewayService(ctx, namespace, relevantPodsNames, ca)
if err != nil {
Expand Down
35 changes: 33 additions & 2 deletions internal/controller/reconcile-domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ import (
"k8s.io/apimachinery/pkg/labels"
)

const envDNS = "env-ingress.some.cluster.sap"

func TestController_reconcileOperatorDomains(t *testing.T) {
tests := []struct {
name string
createCA bool
createCA2 bool
updateCA bool
createIngress bool
withoutDNSNames bool
useEnvDNS bool
cleanUpDomains bool
wantErr bool
expectDomainResources bool
Expand Down Expand Up @@ -64,6 +68,25 @@ func TestController_reconcileOperatorDomains(t *testing.T) {
wantErr: false,
expectDomainResources: true,
},
{
name: "Test with multiple CAPApplications and Ingress GW without DNS names",
createCA: true,
createCA2: true,
createIngress: true,
withoutDNSNames: true,
wantErr: true, // ingress gateway service not annotated with dns target name for CAPApplication default.ca-test-name
expectDomainResources: false,
},
{
name: "Test with multiple CAPApplications and Ingress GW without DNS names but DNS_TARGET env",
createCA: true,
createCA2: true,
createIngress: true,
withoutDNSNames: true,
useEnvDNS: true,
wantErr: false,
expectDomainResources: true, // Creates resources because of DNS_TARGET env
},
// {
// name: "Test cleanup with multiple CAPApplications and Ingress GW",
// createCA: true,
Expand Down Expand Up @@ -127,14 +150,18 @@ func TestController_reconcileOperatorDomains(t *testing.T) {
expectDomainResources: true,
},
}
defer os.Setenv(certManagerEnv, "")
defer os.Unsetenv(certManagerEnv)
defer os.Unsetenv(dnsTargetEnv)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.enableCertManagerEnv {
os.Setenv(certManagerEnv, certManagerCertManagerIO)
} else {
os.Setenv(certManagerEnv, certManagerGardener)
}
if tt.useEnvDNS {
os.Setenv(dnsTargetEnv, envDNS)
}
var c *Controller
var ca *v1alpha1.CAPApplication
var ca2 *v1alpha1.CAPApplication
Expand All @@ -150,7 +177,11 @@ func TestController_reconcileOperatorDomains(t *testing.T) {
}

if tt.createIngress {
ingressRes = createIngressResource(ingressGWName, ca, dnsTarget)
dns := dnsTarget
if tt.withoutDNSNames {
dns = ""
}
ingressRes = createIngressResource(ingressGWName, ca, dns)
}

c = getTestController(testResources{
Expand Down
11 changes: 11 additions & 0 deletions internal/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
const (
certManagerEnv = "CERT_MANAGER"
dnsManagerEnv = "DNS_MANAGER"
dnsTargetEnv = "DNS_TARGET"
)

type ownerInfo struct {
Expand Down Expand Up @@ -157,6 +158,16 @@ func dnsManager() string {
return mgr
}

func envDNSTarget() string {
target := ""
env := os.Getenv(dnsTargetEnv)
if env != "" {
// convert to lower case
target = strings.ToLower(env)
}
return target
}

func updateResourceAnnotation(object *metav1.ObjectMeta, hash string) {
if object.Annotations == nil {
object.Annotations = map[string]string{}
Expand Down
5 changes: 5 additions & 0 deletions website/includes/chart-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| affinity | object | `{}` | Default affinity settings (can be overwritten on component level) |
| tolerations | list | `[]` | Default tolerations (can be overwritten on component level) |
| priorityClassName | string | `""` | Default priority class (can be overwritten on component level) |
| topologySpreadConstraints | list | `[]` | Default topology spread constraints (can be overwritten on component level) |
| controller.replicas | int | `1` | Replicas |
| controller.image.repository | string | `"ghcr.io/sap/cap-operator/controller"` | Image repository |
| controller.image.tag | string | `""` | Image tag |
Expand All @@ -20,11 +21,13 @@
| controller.affinity | object | `{}` | Affinity settings |
| controller.tolerations | list | `[]` | Tolerations |
| controller.priorityClassName | string | `""` | Priority class |
| controller.topologySpreadConstraints | list | `[]` | Topology spread constraints |
| controller.securityContext | object | `{}` | Security context |
| controller.resources.limits.memory | string | `"500Mi"` | Memory limit |
| controller.resources.limits.cpu | float | `0.2` | CPU limit |
| controller.resources.requests.memory | string | `"50Mi"` | Memory request |
| controller.resources.requests.cpu | float | `0.02` | CPU request |
| controller.dnsTarget | string | `""` | The dns target mentioned on the public ingress gateway service used in the cluster |
| subscriptionServer.replicas | int | `1` | Replicas |
| subscriptionServer.image.repository | string | `"ghcr.io/sap/cap-operator/server"` | Image repository |
| subscriptionServer.image.tag | string | `""` | Image tag |
Expand All @@ -35,6 +38,7 @@
| subscriptionServer.affinity | object | `{}` | Affinity settings |
| subscriptionServer.tolerations | list | `[]` | Tolerations |
| subscriptionServer.priorityClassName | string | `""` | Priority class |
| subscriptionServer.topologySpreadConstraints | list | `[]` | Topology spread constraints |
| subscriptionServer.securityContext | object | `{}` | Security context |
| subscriptionServer.resources.limits.memory | string | `"200Mi"` | Memory limit |
| subscriptionServer.resources.limits.cpu | float | `0.1` | CPU limit |
Expand All @@ -56,6 +60,7 @@
| webhook.affinity | object | `{}` | Affinity settings |
| webhook.tolerations | list | `[]` | Tolerations |
| webhook.priorityClassName | string | `""` | Priority class |
| webhook.topologySpreadConstraints | list | `[]` | Topology spread constraints |
| webhook.securityContext | object | `{}` | Security context |
| webhook.resources.limits.memory | string | `"200Mi"` | Memory limit |
| webhook.resources.limits.cpu | float | `0.1` | CPU limit |
Expand Down

0 comments on commit f90aa29

Please sign in to comment.