Skip to content

Commit

Permalink
Get only existing DNS aliases (#577)
Browse files Browse the repository at this point in the history
* Get only existing DNS aliases

* Removed outdated argument
  • Loading branch information
satr authored Dec 28, 2023
1 parent 3c4d5b6 commit 6386b85
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 55 deletions.
7 changes: 2 additions & 5 deletions api/applications/applications_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,8 @@ func (ah *ApplicationHandler) GetApplication(ctx context.Context, appName string
return nil, err
}

radixDNSAliasList, err := kubequery.GetRadixDNSAliases(ctx, ah.accounts.UserAccount.RadixClient, appName)
if err != nil {
return nil, err
}
application := apimodels.BuildApplication(rr, ra, reList, rdList, rjList, ingressList, userIsAdmin, radixDNSAliasList, ah.config.DNSZone)
dnsAliases := kubequery.GetDNSAliases(ctx, ah.accounts.UserAccount.RadixClient, ra, ah.config.DNSZone)
application := apimodels.BuildApplication(rr, ra, reList, rdList, rjList, ingressList, userIsAdmin, dnsAliases)
return application, nil
}

Expand Down
35 changes: 28 additions & 7 deletions api/kubequery/radixdnsalias.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,39 @@ package kubequery

import (
"context"
"fmt"

"github.com/equinor/radix-api/api/utils/labelselector"
applicationModels "github.com/equinor/radix-api/api/applications/models"
"github.com/equinor/radix-common/utils/slice"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
radixclient "github.com/equinor/radix-operator/pkg/client/clientset/versioned"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// GetRadixDNSAliases returns all RadixDNSAliases for the specified application.
func GetRadixDNSAliases(ctx context.Context, client radixclient.Interface, appName string) ([]radixv1.RadixDNSAlias, error) {
res, err := client.RadixV1().RadixDNSAliases().List(ctx, metav1.ListOptions{LabelSelector: labelselector.ForApplication(appName).String()})
if err != nil {
return nil, err
// GetDNSAliases returns all RadixDNSAliases for the specified application.
func GetDNSAliases(ctx context.Context, client radixclient.Interface, radixApplication *radixv1.RadixApplication, dnsZone string) []applicationModels.DNSAlias {
if radixApplication == nil {
return nil
}
return res.Items, nil
return slice.Reduce(radixApplication.Spec.DNSAlias, []applicationModels.DNSAlias{}, func(acc []applicationModels.DNSAlias, dnsAlias radixv1.DNSAlias) []applicationModels.DNSAlias {
radixDNSAlias, err := client.RadixV1().RadixDNSAliases().Get(ctx, dnsAlias.Alias, metav1.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) && !errors.IsForbidden(err) {
log.Errorf("failed to get DNS alias %s: %v", dnsAlias.Alias, err)
}
return acc
}
aliasModel := applicationModels.DNSAlias{
URL: fmt.Sprintf("%s.%s", dnsAlias.Alias, dnsZone),
ComponentName: dnsAlias.Component,
EnvironmentName: dnsAlias.Environment,
Status: applicationModels.DNSAliasStatus{
Condition: string(radixDNSAlias.Status.Condition),
Message: radixDNSAlias.Status.Message,
},
}
return append(acc, aliasModel)
})
}
58 changes: 50 additions & 8 deletions api/kubequery/radixdnsalias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,63 @@ import (
"context"
"testing"

applicationModels "github.com/equinor/radix-api/api/applications/models"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils/labels"
radixfake "github.com/equinor/radix-operator/pkg/client/clientset/versioned/fake"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Test_GetRadixDNSAliases(t *testing.T) {
matched1 := radixv1.RadixDNSAlias{ObjectMeta: metav1.ObjectMeta{Name: "matched1", Labels: labels.ForApplicationName("app1")}}
matched2 := radixv1.RadixDNSAlias{ObjectMeta: metav1.ObjectMeta{Name: "matched2", Labels: labels.ForApplicationName("app1")}}
unmatched1 := radixv1.RadixDNSAlias{ObjectMeta: metav1.ObjectMeta{Name: "unmatched1", Labels: labels.ForApplicationName("app2")}}
client := radixfake.NewSimpleClientset(&matched1, &matched2, &unmatched1)
expected := []radixv1.RadixDNSAlias{matched1, matched2}
actual, err := GetRadixDNSAliases(context.Background(), client, "app1")
require.NoError(t, err)
matched1 := radixv1.RadixDNSAlias{
ObjectMeta: metav1.ObjectMeta{Name: "matched1"},
Spec: radixv1.RadixDNSAliasSpec{
AppName: "app1", Environment: "env1", Component: "comp1",
},
Status: radixv1.RadixDNSAliasStatus{
Condition: "Success",
Message: "",
},
}
matched2 := radixv1.RadixDNSAlias{ObjectMeta: metav1.ObjectMeta{Name: "matched2"}, Spec: radixv1.RadixDNSAliasSpec{
AppName: "app1", Environment: "env1", Component: "comp1",
},
Status: radixv1.RadixDNSAliasStatus{
Condition: "Failed",
Message: "Some error",
},
}
unmatched := radixv1.RadixDNSAlias{ObjectMeta: metav1.ObjectMeta{Name: "unmatched"}, Spec: radixv1.RadixDNSAliasSpec{
AppName: "app2", Environment: "env1", Component: "comp1",
}}
client := radixfake.NewSimpleClientset(&matched1, &matched2, &unmatched)
expected := []applicationModels.DNSAlias{
{
URL: "matched1.test.radix.equinor.com",
EnvironmentName: "env1",
ComponentName: "comp1",
Status: applicationModels.DNSAliasStatus{
Condition: "Success",
Message: "",
},
}, {
URL: "matched2.test.radix.equinor.com",
EnvironmentName: "env1",
ComponentName: "comp2",
Status: applicationModels.DNSAliasStatus{
Condition: "Failed",
Message: "Some error",
},
}}
ra := &radixv1.RadixApplication{ObjectMeta: metav1.ObjectMeta{Name: "app1"},
Spec: radixv1.RadixApplicationSpec{
DNSAlias: []radixv1.DNSAlias{
{Alias: "matched1", Environment: "env1", Component: "comp1"},
{Alias: "matched2", Environment: "env1", Component: "comp2"},
}},
}
actual := GetDNSAliases(context.Background(), client, ra, "test.radix.equinor.com")
require.Len(t, actual, 2, "unexpected amount of actual DNS aliases")
assert.ElementsMatch(t, expected, actual)
}
4 changes: 2 additions & 2 deletions api/models/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
)

// BuildApplication builds an Application model.
func BuildApplication(rr *radixv1.RadixRegistration, ra *radixv1.RadixApplication, reList []radixv1.RadixEnvironment, rdList []radixv1.RadixDeployment, rjList []radixv1.RadixJob, ingressList []networkingv1.Ingress, userIsAdmin bool, radixDNSAliasList []radixv1.RadixDNSAlias, radixDNSZone string) *applicationModels.Application {
func BuildApplication(rr *radixv1.RadixRegistration, ra *radixv1.RadixApplication, reList []radixv1.RadixEnvironment, rdList []radixv1.RadixDeployment, rjList []radixv1.RadixJob, ingressList []networkingv1.Ingress, userIsAdmin bool, dnsAliases []applicationModels.DNSAlias) *applicationModels.Application {
application := applicationModels.Application{
Name: rr.Name,
Registration: *BuildApplicationRegistration(rr),
Jobs: BuildJobSummaryList(rjList),
AppAlias: BuildApplicationAlias(ingressList, reList),
UserIsAdmin: userIsAdmin,
DNSAliases: dnsAliases,
}
if ra != nil {
application.Environments = BuildEnvironmentSummaryList(rr, ra, reList, rdList, rjList)
application.DNSAliases = BuildDNSAlias(ra, radixDNSAliasList, radixDNSZone)
}
return &application
}
33 changes: 0 additions & 33 deletions api/models/dns_alias.go

This file was deleted.

0 comments on commit 6386b85

Please sign in to comment.