瀏覽代碼

Pretty sure env groups don't work for pre-deploy jobs - now they do (#3328)

Feroze Mohideen 2 年之前
父節點
當前提交
029926c6f5

+ 43 - 24
api/server/handlers/porter_app/parse.go

@@ -13,6 +13,7 @@ import (
 	"github.com/porter-dev/porter/internal/kubernetes"
 	"github.com/porter-dev/porter/internal/kubernetes/domain"
 	"github.com/porter-dev/porter/internal/kubernetes/environment_groups"
+	"github.com/porter-dev/porter/internal/kubernetes/porter_app"
 	"github.com/porter-dev/porter/internal/repository"
 	"github.com/porter-dev/porter/internal/templater/utils"
 	"github.com/stefanmcshane/helm/pkg/chart"
@@ -175,28 +176,7 @@ func parse(ctx context.Context, conf ParseConf) (*chart.Chart, map[string]interf
 	}
 
 	for serviceName := range services {
-		if len(conf.EnvironmentGroups) != 0 {
-			if _, ok := services[serviceName].Config["labels"]; !ok {
-				services[serviceName].Config["labels"] = make(map[string]string)
-			}
-			if _, ok := services[serviceName].Config["labels"].(map[string]any); ok {
-				delete(services[serviceName].Config["labels"].(map[string]any), environment_groups.LabelKey_LinkedEnvironmentGroup)
-			}
-			switch services[serviceName].Config["labels"].(type) {
-			case map[string]any:
-				services[serviceName].Config["labels"].(map[string]any)[environment_groups.LabelKey_LinkedEnvironmentGroup] = strings.Join(conf.EnvironmentGroups, ".")
-			case map[string]string:
-				services[serviceName].Config["labels"].(map[string]string)[environment_groups.LabelKey_LinkedEnvironmentGroup] = strings.Join(conf.EnvironmentGroups, ".")
-			case any:
-				if val, ok := services[serviceName].Config["labels"].(string); ok {
-					if val == "" {
-						services[serviceName].Config["labels"] = map[string]string{
-							environment_groups.LabelKey_LinkedEnvironmentGroup: strings.Join(conf.EnvironmentGroups, "."),
-						}
-					}
-				}
-			}
-		}
+		services[serviceName] = addLabelsToService(services[serviceName], conf.EnvironmentGroups, porter_app.LabelKey_PorterApplication)
 	}
 
 	application := &Application{
@@ -206,8 +186,7 @@ func parse(ctx context.Context, conf ParseConf) (*chart.Chart, map[string]interf
 		Release:  parsed.Release,
 	}
 
-
-	values, err := buildUmbrellaChartValues(ctx, application, synced_env, conf.ImageInfo, conf.ExistingHelmValues, conf.SubdomainCreateOpts, conf.InjectLauncherToStartCommand, conf.ShouldValidateHelmValues, conf.UserUpdate, conf.Namespace,  conf.AddCustomNodeSelector)
+	values, err := buildUmbrellaChartValues(ctx, application, synced_env, conf.ImageInfo, conf.ExistingHelmValues, conf.SubdomainCreateOpts, conf.InjectLauncherToStartCommand, conf.ShouldValidateHelmValues, conf.UserUpdate, conf.Namespace, conf.AddCustomNodeSelector)
 	if err != nil {
 		return nil, nil, nil, fmt.Errorf("%s: %w", "error building values", err)
 	}
@@ -221,6 +200,7 @@ func parse(ctx context.Context, conf ParseConf) (*chart.Chart, map[string]interf
 	// return the parsed release values for the release job chart, if they exist
 	var preDeployJobValues map[string]interface{}
 	if application.Release != nil && application.Release.Run != nil {
+		application.Release = addLabelsToService(application.Release, conf.EnvironmentGroups, porter_app.LabelKey_PorterApplicationPreDeploy)
 		preDeployJobValues = buildPreDeployJobChartValues(application.Release, application.Env, synced_env, conf.ImageInfo, conf.InjectLauncherToStartCommand, conf.ExistingHelmValues, strings.TrimSuffix(strings.TrimPrefix(conf.Namespace, "porter-stack-"), "")+"-r", conf.UserUpdate, conf.AddCustomNodeSelector)
 	}
 
@@ -900,3 +880,42 @@ func convertHelmValuesToPorterYaml(helmValues string) (*PorterStackYAML, error)
 		Services: services,
 	}, nil
 }
+
+// addLabelsToService always adds the default label to the service, and if envGroups is not empty, it adds the corresponding environment group label as well.
+func addLabelsToService(service *Service, envGroups []string, defaultLabelKey string) *Service {
+	if _, ok := service.Config["labels"]; !ok {
+		service.Config["labels"] = make(map[string]string)
+	}
+	if len(envGroups) != 0 {
+		// delete the env group label so we can replace it
+		if _, ok := service.Config["labels"].(map[string]any); ok {
+			delete(service.Config["labels"].(map[string]any), environment_groups.LabelKey_LinkedEnvironmentGroup)
+		}
+	}
+
+	switch service.Config["labels"].(type) {
+	case map[string]any:
+		service.Config["labels"].(map[string]any)[defaultLabelKey] = porter_app.LabelValue_PorterApplication
+		if len(envGroups) != 0 {
+			service.Config["labels"].(map[string]any)[environment_groups.LabelKey_LinkedEnvironmentGroup] = strings.Join(envGroups, ".")
+		}
+	case map[string]string:
+		service.Config["labels"].(map[string]string)[defaultLabelKey] = porter_app.LabelValue_PorterApplication
+		if len(envGroups) != 0 {
+			service.Config["labels"].(map[string]string)[environment_groups.LabelKey_LinkedEnvironmentGroup] = strings.Join(envGroups, ".")
+		}
+	case any:
+		if val, ok := service.Config["labels"].(string); ok {
+			if val == "" {
+				service.Config["labels"] = map[string]string{
+					defaultLabelKey: porter_app.LabelValue_PorterApplication,
+				}
+				if len(envGroups) != 0 {
+					service.Config["labels"].(map[string]string)[environment_groups.LabelKey_LinkedEnvironmentGroup] = strings.Join(envGroups, ".")
+				}
+			}
+		}
+	}
+
+	return service
+}

+ 2 - 2
dashboard/src/main/home/app-dashboard/expanded-app/ChangeLogComponent.tsx

@@ -74,7 +74,7 @@ const ChangeLogComponent: FC<Props> = ({ oldYaml, newYaml, appData }) => {
             const commitDiffLink = `https://github.com/${appData.app.repo_name}/compare/${oldCommit}...${newCommit}`;
             changes.push(
               <ChangeBox type="E">
-                {`Tag upated: ${oldCommit} -> ${newCommit}.   `}
+                {`Tag updated: ${oldCommit} -> ${newCommit}.   `}
 
                 <Link
                   target="_blank"
@@ -87,7 +87,7 @@ const ChangeLogComponent: FC<Props> = ({ oldYaml, newYaml, appData }) => {
             );
           } else {
             <ChangeBox type="E">
-              {`Tag upated: ${oldCommit} -> ${newCommit}.   `}
+              {`Tag updated: ${oldCommit} -> ${newCommit}.   `}
             </ChangeBox>
           }
         } else {

+ 0 - 1
dashboard/src/main/home/app-dashboard/expanded-app/logs/LogSection.tsx

@@ -319,7 +319,6 @@ const LogSection: React.FC<Props> = ({
   useEffect(() => {
     // determine if the agent is installed properly - if not, start by render upgrade screen
     checkForAgent();
-    resetSearch();
   }, []);
 
   useEffect(() => {

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/env-groups/CreateEnvGroup.tsx

@@ -250,7 +250,7 @@ export default class CreateEnvGroup extends Component<PropsType, StateType> {
               type="text"
               value={this.state.envGroupName}
               setValue={(x: string) => this.setState({ envGroupName: x })}
-              placeholder="ex: doctor-scientist"
+              placeholder="ex: my-env-group"
               width="100%"
             />
             {!this?.context?.currentProject?.simplified_view_enabled && (<>

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/stacks/components/NewEnvGroupForm.tsx

@@ -99,7 +99,7 @@ const NewEnvGroupForm = (props: {
         setValue={(x: string) => {
           setName(x);
         }}
-        placeholder="ex: doctor-scientist"
+        placeholder="ex: my-env-group"
         width="100%"
       />
 

+ 9 - 0
internal/kubernetes/porter_app/create.go

@@ -0,0 +1,9 @@
+package porter_app
+
+// TODO: migrate all kubernetes-level operations from /api/server/handlers/porter_app/{create,parse}.go to this file
+
+const (
+	LabelKey_PorterApplication          = "porter.run/porter-application"
+	LabelKey_PorterApplicationPreDeploy = "porter.run/porter-application-pre-deploy"
+	LabelValue_PorterApplication        = "true"
+)