|
|
@@ -9,6 +9,8 @@ import (
|
|
|
|
|
|
"github.com/porter-dev/porter/internal/kubernetes"
|
|
|
"github.com/porter-dev/porter/internal/telemetry"
|
|
|
+ appsv1 "k8s.io/api/apps/v1"
|
|
|
+ batchv1 "k8s.io/api/batch/v1"
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
)
|
|
|
|
|
|
@@ -22,6 +24,9 @@ const (
|
|
|
// Namespace_EnvironmentGroups is the base namespace for storing all environment groups.
|
|
|
// The configmaps and secrets here should be considered the source's of truth for a given version
|
|
|
Namespace_EnvironmentGroups = "porter-env-group"
|
|
|
+
|
|
|
+ // LabelKey_AppName is the label key for the app name
|
|
|
+ LabelKey_AppName = "porter.run/app-name"
|
|
|
)
|
|
|
|
|
|
// EnvironmentGroup represents a ConfigMap in the porter-env-group namespace
|
|
|
@@ -228,26 +233,48 @@ type LinkedPorterApplication struct {
|
|
|
Namespace string
|
|
|
}
|
|
|
|
|
|
-// LinkedApplications lists all applications that are linked to a given environment group. Since there can be multiple linked environment groups we must check by the presence of a label on the deployment and job
|
|
|
-func LinkedApplications(ctx context.Context, a *kubernetes.Agent, environmentGroupName string) ([]LinkedPorterApplication, error) {
|
|
|
- ctx, span := telemetry.NewSpan(ctx, "list-linked-applications")
|
|
|
- defer span.End()
|
|
|
+func listLinkedAppsByUniqueAppLabel(environmentGroupName string, deployments []appsv1.Deployment, cronJobs []batchv1.CronJob) []LinkedPorterApplication {
|
|
|
+ appsByName := make(map[string]LinkedPorterApplication)
|
|
|
|
|
|
- if environmentGroupName == "" {
|
|
|
- return nil, telemetry.Error(ctx, span, nil, "environment group cannot be empty")
|
|
|
+ for _, d := range deployments {
|
|
|
+ applicationsLinkedEnvironmentGroups := strings.Split(d.Labels[LabelKey_LinkedEnvironmentGroup], ".")
|
|
|
+ appName := d.Labels[LabelKey_AppName]
|
|
|
+
|
|
|
+ for _, linkedEnvironmentGroup := range applicationsLinkedEnvironmentGroups {
|
|
|
+ if linkedEnvironmentGroup == environmentGroupName && appName != "" {
|
|
|
+ appsByName[appName] = LinkedPorterApplication{
|
|
|
+ Name: appName,
|
|
|
+ Namespace: d.Namespace,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "environment-group-name", Value: environmentGroupName})
|
|
|
|
|
|
- deployListResp, err := a.Clientset.AppsV1().Deployments(metav1.NamespaceAll).List(ctx,
|
|
|
- metav1.ListOptions{
|
|
|
- LabelSelector: LabelKey_LinkedEnvironmentGroup,
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return nil, telemetry.Error(ctx, span, err, "unable to list linked deployment applications")
|
|
|
+ for _, d := range cronJobs {
|
|
|
+ applicationsLinkedEnvironmentGroups := strings.Split(d.Labels[LabelKey_LinkedEnvironmentGroup], ".")
|
|
|
+ appName := d.Labels[LabelKey_AppName]
|
|
|
+
|
|
|
+ for _, linkedEnvironmentGroup := range applicationsLinkedEnvironmentGroups {
|
|
|
+ if linkedEnvironmentGroup == environmentGroupName && appName != "" {
|
|
|
+ appsByName[appName] = LinkedPorterApplication{
|
|
|
+ Name: appName,
|
|
|
+ Namespace: d.Namespace,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
var apps []LinkedPorterApplication
|
|
|
- for _, d := range deployListResp.Items {
|
|
|
+ for _, app := range appsByName {
|
|
|
+ apps = append(apps, app)
|
|
|
+ }
|
|
|
+
|
|
|
+ return apps
|
|
|
+}
|
|
|
+
|
|
|
+func listLinkedAppsByUniqueNamespace(environmentGroupName string, deployments []appsv1.Deployment, cronJobs []batchv1.CronJob) []LinkedPorterApplication {
|
|
|
+ var apps []LinkedPorterApplication
|
|
|
+ for _, d := range deployments {
|
|
|
applicationsLinkedEnvironmentGroups := strings.Split(d.Labels[LabelKey_LinkedEnvironmentGroup], ".")
|
|
|
|
|
|
for _, linkedEnvironmentGroup := range applicationsLinkedEnvironmentGroups {
|
|
|
@@ -260,15 +287,7 @@ func LinkedApplications(ctx context.Context, a *kubernetes.Agent, environmentGro
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- cronListResp, err := a.Clientset.BatchV1().CronJobs(metav1.NamespaceAll).List(ctx,
|
|
|
- metav1.ListOptions{
|
|
|
- LabelSelector: LabelKey_LinkedEnvironmentGroup,
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return nil, telemetry.Error(ctx, span, err, "unable to list linked cronjob applications")
|
|
|
- }
|
|
|
-
|
|
|
- for _, d := range cronListResp.Items {
|
|
|
+ for _, d := range cronJobs {
|
|
|
applicationsLinkedEnvironmentGroups := strings.Split(d.Labels[LabelKey_LinkedEnvironmentGroup], ".")
|
|
|
for _, linkedEnvironmentGroup := range applicationsLinkedEnvironmentGroups {
|
|
|
if linkedEnvironmentGroup == environmentGroupName {
|
|
|
@@ -280,5 +299,40 @@ func LinkedApplications(ctx context.Context, a *kubernetes.Agent, environmentGro
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ return apps
|
|
|
+}
|
|
|
+
|
|
|
+// LinkedApplications lists all applications that are linked to a given environment group. Since there can be multiple linked environment groups we must check by the presence of a label on the deployment and job
|
|
|
+func LinkedApplications(ctx context.Context, a *kubernetes.Agent, environmentGroupName string, byUniqueNamespace bool) ([]LinkedPorterApplication, error) {
|
|
|
+ ctx, span := telemetry.NewSpan(ctx, "list-linked-applications")
|
|
|
+ defer span.End()
|
|
|
+
|
|
|
+ if environmentGroupName == "" {
|
|
|
+ return nil, telemetry.Error(ctx, span, nil, "environment group cannot be empty")
|
|
|
+ }
|
|
|
+ telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "environment-group-name", Value: environmentGroupName})
|
|
|
+
|
|
|
+ deployListResp, err := a.Clientset.AppsV1().Deployments(metav1.NamespaceAll).List(ctx,
|
|
|
+ metav1.ListOptions{
|
|
|
+ LabelSelector: LabelKey_LinkedEnvironmentGroup,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return nil, telemetry.Error(ctx, span, err, "unable to list linked deployment applications")
|
|
|
+ }
|
|
|
+ cronListResp, err := a.Clientset.BatchV1().CronJobs(metav1.NamespaceAll).List(ctx,
|
|
|
+ metav1.ListOptions{
|
|
|
+ LabelSelector: LabelKey_LinkedEnvironmentGroup,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return nil, telemetry.Error(ctx, span, err, "unable to list linked cronjob applications")
|
|
|
+ }
|
|
|
+
|
|
|
+ var apps []LinkedPorterApplication
|
|
|
+ if byUniqueNamespace {
|
|
|
+ apps = listLinkedAppsByUniqueNamespace(environmentGroupName, deployListResp.Items, cronListResp.Items)
|
|
|
+ return apps, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ apps = listLinkedAppsByUniqueAppLabel(environmentGroupName, deployListResp.Items, cronListResp.Items)
|
|
|
return apps, nil
|
|
|
}
|