diff --git a/api/applications/applications_handler.go b/api/applications/applications_handler.go index c60a44e5..42499717 100644 --- a/api/applications/applications_handler.go +++ b/api/applications/applications_handler.go @@ -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 } diff --git a/api/kubequery/radixdnsalias.go b/api/kubequery/radixdnsalias.go index 40794c2c..0568cfde 100644 --- a/api/kubequery/radixdnsalias.go +++ b/api/kubequery/radixdnsalias.go @@ -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) + }) } diff --git a/api/kubequery/radixdnsalias_test.go b/api/kubequery/radixdnsalias_test.go index 0d5f3e01..653e4a93 100644 --- a/api/kubequery/radixdnsalias_test.go +++ b/api/kubequery/radixdnsalias_test.go @@ -4,8 +4,8 @@ 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" @@ -13,12 +13,54 @@ import ( ) 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) } diff --git a/api/models/application.go b/api/models/application.go index 01c43136..f3aed0fb 100644 --- a/api/models/application.go +++ b/api/models/application.go @@ -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 } diff --git a/api/models/dns_alias.go b/api/models/dns_alias.go deleted file mode 100644 index 3fb274af..00000000 --- a/api/models/dns_alias.go +++ /dev/null @@ -1,33 +0,0 @@ -package models - -import ( - "fmt" - - 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" -) - -// BuildDNSAlias builds an DNSAliases model for the application -func BuildDNSAlias(ra *radixv1.RadixApplication, radixDNSAliasList []radixv1.RadixDNSAlias, radixDNSZone string) []applicationModels.DNSAlias { - var dnsAliases []applicationModels.DNSAlias - radixDNSAliasMap := slice.Reduce(radixDNSAliasList, make(map[string]*radixv1.RadixDNSAlias), func(acc map[string]*radixv1.RadixDNSAlias, radixDNSAlias radixv1.RadixDNSAlias) map[string]*radixv1.RadixDNSAlias { - acc[radixDNSAlias.GetName()] = &radixDNSAlias - return acc - }) - for _, dnsAlias := range ra.Spec.DNSAlias { - aliasModel := applicationModels.DNSAlias{ - URL: fmt.Sprintf("%s.%s", dnsAlias.Alias, radixDNSZone), - ComponentName: dnsAlias.Component, - EnvironmentName: dnsAlias.Environment, - } - if radixDNSAlias, ok := radixDNSAliasMap[dnsAlias.Alias]; ok { - aliasModel.Status = applicationModels.DNSAliasStatus{ - Condition: string(radixDNSAlias.Status.Condition), - Message: radixDNSAlias.Status.Message, - } - } - dnsAliases = append(dnsAliases, aliasModel) - } - return dnsAliases -}