Skip to content

Commit

Permalink
Align ingress hostPrefix with pod indices.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytd committed Feb 9, 2024
1 parent d547e06 commit f49b1a5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sync"

oidc_apps_controller "github.com/gardener/oidc-apps-controller/pkg/constants"
"github.com/gardener/oidc-apps-controller/pkg/rand"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -178,7 +179,7 @@ func (c *OIDCAppsControllerConfig) GetHost(object client.Object) string {

prefix := object.GetName() + "-" + object.GetNamespace()
if t.Ingress != nil && t.Ingress.HostPrefix != "" {
prefix = t.Ingress.HostPrefix
prefix = t.Ingress.HostPrefix + "-" + rand.GenerateSha256(object.GetName()+"-"+object.GetNamespace())
}
if t.Ingress != nil && t.Ingress.Host != "" {
prefix, domain, _ = strings.Cut(t.Ingress.Host, ".")
Expand Down
11 changes: 9 additions & 2 deletions pkg/configuration/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/base64"
"os"
"reflect"
"strings"
"testing"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -252,9 +253,15 @@ func TestGetTargetHost(t *testing.T) {
},
}

if extensionConfig.GetHost(deployment) != "my-service.domain.org" {
z := strings.SplitN(extensionConfig.GetHost(deployment), ".", 2)
if len(z) != 2 || z[1] != "domain.org" {
t.Error("getting target host is not as expected",
"expected: ", "my-service.domain.org",
"expected: ", "my-service-[hostPrefix].domain.org.domain.org",
"got", extensionConfig.GetHost(deployment))
}
if !strings.HasPrefix(z[0], "my-service-") {
t.Error("getting target host is not as expected",
"expected: ", "my-service-[hostPrefix].domain.org.domain.org",
"got", extensionConfig.GetHost(deployment))
}
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/controllers/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,14 @@ func reconcileStatefulSetDependencies(ctx context.Context, c client.Client, obje
// There shall be an ingress for each statefulset pod
host, domain, found := strings.Cut(hostPrefix, ".")
if found {
host = fmt.Sprintf("%s-%s.%s", pod.GetName(), pod.GetNamespace(), domain)
// In some envorinments, the pod index is added as a label: apps.kubernetes.io/pod-index
podIndex, present := pod.GetObjectMeta().GetLabels()["statefulset.kubernetes.io/pod-name"]
if present {
l := strings.Split(podIndex, "-")
host = fmt.Sprintf("%s-%s.%s", host, l[len(l)-1], domain)
} else {
host = fmt.Sprintf("%s.%s", host, domain)
}
}
_log.V(9).Info("Set", "host", host)
oauth2Ingress, err = createIngress(host, &pod)
Expand Down
2 changes: 1 addition & 1 deletion pkg/oidc-apps-controller/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"
"path/filepath"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -51,6 +50,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
Expand Down
10 changes: 9 additions & 1 deletion pkg/webhook/pod-webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ func (p *PodMutator) Handle(ctx context.Context, req webhook.AdmissionRequest) w
}
host, domain, found := strings.Cut(hostPrefix, ".")
if found {
host = fmt.Sprintf("%s-%s.%s", pod.GetName(), pod.GetNamespace(), domain)
// In some envorinments, the pod index is added as a label: apps.kubernetes.io/pod-index
podIndex, present := patch.GetObjectMeta().GetLabels()["statefulset.kubernetes.io/pod-name"]
if present {
l := strings.Split(podIndex, "-")
host = fmt.Sprintf("%s-%s.%s", host, l[len(l)-1], domain)
} else {

host = fmt.Sprintf("%s.%s", host, domain)
}
}
_log.Info(fmt.Sprintf("host: %s", host))

Expand Down

0 comments on commit f49b1a5

Please sign in to comment.