Просмотр исходного кода

handle edge cases for env group updating

Alexander Belanger 5 лет назад
Родитель
Сommit
db8a0da89d

+ 2 - 0
dashboard/src/components/values-form/KeyValueArray.tsx

@@ -109,6 +109,7 @@ export default class KeyValueArray extends Component<PropsType, StateType> {
                   this.props.setValues(obj);
                 }}
                 disabled={this.props.disabled || entry?.value?.includes("PORTERSECRET") }
+                spellCheck={false}
               />
               <Spacer />
               <Input
@@ -124,6 +125,7 @@ export default class KeyValueArray extends Component<PropsType, StateType> {
                 }}
                 disabled={this.props.disabled || entry?.value?.includes("PORTERSECRET") }
                 type={entry?.value?.includes("PORTERSECRET") ? "password" : "text"}
+                spellCheck={false}
               />
               {this.renderDeleteButton(i)}
               {this.renderHiddenOption(entry?.value?.includes("PORTERSECRET"), i)}

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

@@ -56,14 +56,33 @@ export default class CreateEnvGroup extends Component<PropsType, StateType> {
     let apiEnvVariables : Record<string, string> = {}
     let secretEnvVariables : Record<string, string> = {}
 
-    this.state.envVariables.forEach((envVar: KeyValueType) => {
-      if (envVar.hidden) {
-        secretEnvVariables[envVar.key] = envVar.value
+    let envVariables = this.state.envVariables
+
+    envVariables.filter((envVar: KeyValueType, index : number, self : KeyValueType[]) => {
+      // remove any collisions that are marked as deleted and are duplicates
+      let numCollisions = self.reduce((n, _envVar : KeyValueType) => {
+        return n + (_envVar.key === envVar.key ? 1 : 0);
+      }, 0)
+
+      if (numCollisions == 1) {
+        return true
       } else {
-        apiEnvVariables[envVar.key] = envVar.value
+        return index === self.findIndex((_envVar : KeyValueType) => (
+          _envVar.key === envVar.key && !_envVar.deleted
+        ))
+      }
+    }).forEach((envVar: KeyValueType) => {
+      if (!envVar.deleted) {
+        if (envVar.hidden) {
+          secretEnvVariables[envVar.key] = envVar.value
+        } else {
+          apiEnvVariables[envVar.key] = envVar.value
+        }
       }
     })
 
+    console.log("API ENV VARIABLES ARE", apiEnvVariables, secretEnvVariables)
+
     api
       .createConfigMap(
         "<token>",

+ 2 - 0
dashboard/src/main/home/cluster-dashboard/env-groups/EnvGroupArray.tsx

@@ -104,6 +104,7 @@ export default class EnvGroupArray extends Component<PropsType, StateType> {
                           this.props.setValues(_values);
                         }}
                         disabled={this.props.disabled || entry.locked}
+                        spellCheck={false}
                       />
                       <Spacer />
                       <Input
@@ -117,6 +118,7 @@ export default class EnvGroupArray extends Component<PropsType, StateType> {
                         }}
                         disabled={this.props.disabled || entry.locked}
                         type={entry.hidden ? "password" : "text"}
+                        spellCheck={false}
                       />
                       {this.renderHiddenOption(entry.hidden, entry.locked, i)}
                       {this.renderDeleteButton(i)}

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

@@ -74,7 +74,35 @@ export default class ExpandedEnvGroup extends Component<PropsType, StateType> {
     let apiEnvVariables : Record<string, string> = {}
     let secretEnvVariables : Record<string, string> = {}
 
-    this.state.envVariables.forEach((envVar: KeyValueType) => {
+    let envVariables = this.state.envVariables
+
+    envVariables.filter((envVar: KeyValueType, index : number, self : KeyValueType[]) => {
+      // remove any collisions that are marked as deleted and are duplicates, unless they are 
+      // all delete collisions
+      let numDeleteCollisions = self.reduce((n, _envVar : KeyValueType) => {
+        return n + (_envVar.key === envVar.key && envVar.deleted ? 1 : 0);
+      }, 0)
+
+      let numCollisions = self.reduce((n, _envVar : KeyValueType) => {
+        return n + (_envVar.key === envVar.key ? 1 : 0);
+      }, 0)
+
+      if (numCollisions == numDeleteCollisions) {
+        // if all collisions are delete collisions, just remove duplicates
+        return index === self.findIndex((_envVar : KeyValueType) => (
+          _envVar.key === envVar.key
+        ))
+      } else if (numCollisions == 1) {
+        // if there's just one collision (self), keep the object
+        return true
+      } else {
+        // if there are more collisions than delete collisions, remove all duplicates that
+        // are deletions
+        return index === self.findIndex((_envVar : KeyValueType) => (
+          _envVar.key === envVar.key && !_envVar.deleted
+        ))
+      }
+    }).forEach((envVar: KeyValueType) => {
       if (envVar.hidden) {
         if (envVar.deleted) {
           secretEnvVariables[envVar.key] = null

+ 4 - 4
server/api/k8s_handler.go

@@ -372,11 +372,11 @@ func (app *App) HandleUpdateConfigMap(w http.ResponseWriter, r *http.Request) {
 
 	// add all secret env variables to configmap with value PORTERSECRET_${configmap_name}
 	for key, val := range configMap.SecretEnvVariables {
-		configMap.EnvVariables[key] = fmt.Sprintf("PORTERSECRET_%s", configMap.Name)
-
-		// if val is empty, set to empty
-		if val == "" {
+		// if val is empty and key does not exist in configmap already, set to empty
+		if _, found := configMap.EnvVariables[key]; val == "" && !found {
 			configMap.EnvVariables[key] = ""
+		} else if val != "" {
+			configMap.EnvVariables[key] = fmt.Sprintf("PORTERSECRET_%s", configMap.Name)
 		}
 	}