Pārlūkot izejas kodu

Impose service name length limit (#3439)

Feroze Mohideen 2 gadi atpakaļ
vecāks
revīzija
689bd62b37

+ 7 - 0
api/server/handlers/porter_app/create_and_update_events.go

@@ -415,5 +415,12 @@ func getServiceNameFromPodName(podName, porterAppName string) string {
 		return podName[:index]
 	}
 
+	// if the suffix wasn't found, it's possible that the service name was too long to keep the entire suffix. example: postgres-snowflake-connector-postgres-snowflake-service-wk8gnst
+	// if this is the case, find the service name by removing everything after the last dash
+	index = strings.LastIndex(podName, "-")
+	if index != -1 {
+		return podName[:index]
+	}
+
 	return ""
 }

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

@@ -655,6 +655,7 @@ const ExpandedApp: React.FC<Props> = ({ ...props }) => {
               chart={appData.chart}
               addNewText={"Add a new service"}
               setExpandedJob={(x: string) => setExpandedJob(x)}
+              appName={appData.app.name}
             />
             <Spacer y={0.75} />
             <Button

+ 1 - 0
dashboard/src/main/home/app-dashboard/new-app-flow/NewAppFlow.tsx

@@ -502,6 +502,7 @@ const NewAppFlow: React.FC<Props> = ({ ...props }) => {
                   services={formState.serviceList.filter(Service.isNonRelease)}
                   defaultExpanded={true}
                   addNewText={"Add a new service"}
+                  appName={porterApp.name}
                 />
               </>,
               <>

+ 10 - 1
dashboard/src/main/home/app-dashboard/new-app-flow/Services.tsx

@@ -16,6 +16,7 @@ import { Service, ServiceType } from "./serviceTypes";
 
 interface ServicesProps {
   services: Service[];
+  appName: string;
   setServices: (services: Service[]) => void;
   addNewText: string;
   defaultExpanded?: boolean;
@@ -26,6 +27,7 @@ interface ServicesProps {
 }
 
 const Services: React.FC<ServicesProps> = ({
+  appName,
   services,
   setServices,
   addNewText,
@@ -48,6 +50,11 @@ const Services: React.FC<ServicesProps> = ({
     const serviceNames = services.map((service) => service.name);
     return serviceNames.includes(name);
   };
+  const isServiceNameTooLong = (name: string) => {
+    // k8s pod name limit is 63 characters: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set
+    // the pod name is the appName-serviceName-web/wkr/job-random_4_char_string, so the max limit is 53
+    return name.length + appName.length > 53;
+  };
 
   const maybeGetError = (): string | undefined => {
     if (serviceName.length > 30) {
@@ -55,7 +62,9 @@ const Services: React.FC<ServicesProps> = ({
     } else if (serviceName != "" && !isServiceNameValid(serviceName)) {
       return "Lowercase letters, numbers, and '-' only.";
     } else if (isServiceNameDuplicate(serviceName)) {
-      return "Service name is duplicate";
+      return "Service name is duplicate!";
+    } else if (isServiceNameTooLong(serviceName)) {
+      return "Service name is too long!";
     } else {
       return undefined;
     }