From 1ea1c5a9da53acf0b8a80c23a09a00e6706d1bae Mon Sep 17 00:00:00 2001 From: Richard Killen Date: Mon, 27 Mar 2023 18:11:32 -0500 Subject: [PATCH] Correct legal k8s name function; use domain UID for rule hosts --- webui/src/js/utils/common-utilities.js | 9 ++++---- .../vz-ingress-trait-rule-edit-dialog.js | 6 +++--- webui/src/test/common-utilities-test.js | 21 ++++++++++--------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/webui/src/js/utils/common-utilities.js b/webui/src/js/utils/common-utilities.js index f3da76183..8478f567a 100644 --- a/webui/src/js/utils/common-utilities.js +++ b/webui/src/js/utils/common-utilities.js @@ -1,6 +1,6 @@ /** * @license - * Copyright (c) 2021, 2022, Oracle and/or its affiliates. + * Copyright (c) 2021, 2023, Oracle and/or its affiliates. * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. */ 'use strict'; @@ -94,11 +94,12 @@ define([], toLegalK8sName(name) { if (this.isLegalK8sName(name)) return name; - let result = name.toLowerCase().replace(/[^-a-z0-9]/, '-'); + let result = name.toLowerCase().replaceAll(/[^a-z0-9-]/g, '-'); while (result.startsWith('-')) result = result.slice(1); - while (result.endsWith('-')) result = result.substring(0, result.length-1); - return result.substring(0, this.k8sMaxNameLength); + result = result.substring(0, this.k8sMaxNameLength); + while (result.endsWith('-')) result = result.slice(0, -1); + return result; }, hashIt(str, seed = 0) { diff --git a/webui/src/js/viewModels/vz-ingress-trait-rule-edit-dialog.js b/webui/src/js/viewModels/vz-ingress-trait-rule-edit-dialog.js index 0d349a6f1..48b648c72 100644 --- a/webui/src/js/viewModels/vz-ingress-trait-rule-edit-dialog.js +++ b/webui/src/js/viewModels/vz-ingress-trait-rule-edit-dialog.js @@ -187,17 +187,17 @@ function(accUtils, ko, i18n, project, props, validationHelper, this.destinationHostNames = ko.computed(() => { let options = []; - const domainName = project.wdtModel.domainName(); + const domainUid = project.k8sDomain.uid.value; const clusters = project.k8sDomain.clusters.observable(); for (const cluster of clusters) { - const clusterHostName = utils.toLegalK8sName(`${domainName}-cluster-${cluster.name}`); + const clusterHostName = utils.toLegalK8sName(`${domainUid}-cluster-${cluster.name}`); options.push( { id : cluster.uid, value: clusterHostName, text: clusterHostName}); } const servers = project.k8sDomain.servers.observable(); for (const server of servers) { - const serverHostName = utils.toLegalK8sName(`${domainName}-${server.name}`); + const serverHostName = utils.toLegalK8sName(`${domainUid}-${server.name}`); options.push( { id : server.uid, value: serverHostName, text: serverHostName}); } diff --git a/webui/src/test/common-utilities-test.js b/webui/src/test/common-utilities-test.js index 4ece9251b..f2c6abf92 100644 --- a/webui/src/test/common-utilities-test.js +++ b/webui/src/test/common-utilities-test.js @@ -1,6 +1,6 @@ /** * @license - * Copyright (c) 2021, Oracle and/or its affiliates. + * Copyright (c) 2021, 2023, Oracle and/or its affiliates. * Licensed under The Universal Permissive License (UPL), Version 1.0 * as shown at https://oss.oracle.com/licenses/upl/ * @ignore @@ -34,42 +34,42 @@ describe('general utilities', function () { expect(utils.equals(1, 'a string')).to.equal(false); expect(utils.equals([1, 2, 3], {one: 'value'})).to.equal(false); }); - + it('returns true when comparing equal scalars', function () { expect(utils.equals(undefined, undefined)).to.equal(true); expect(utils.equals(1, 8 - 7)).to.equal(true); expect(utils.equals(true, true)).to.equal(true); }); - + it('returns false when comparing unequal scalars', function () { expect(utils.equals(true, false)).to.equal(false); expect(utils.equals('abcd', 'xyz')).to.equal(false); expect(utils.equals(1, 5)).to.equal(false); }); - + it('returns false when comparing unequal objects', function () { expect(utils.equals({first: 1, second: 'both'}, {first: 1, second: 'neither'})).to.equal(false); expect(utils.equals({first: 1, second: 'both'}, {second: 'both', first: 1, third: null})).to.equal(false); expect(utils.equals({first: {nest: 1}, second: 'both'}, {second: 'both', first: [1]})).to.equal(false); }); - + it('returns true when comparing equal objects', function () { expect(utils.equals({first: 1, second: 'both'}, {first: 1, second: 'both'})).to.equal(true); expect(utils.equals({first: 1, second: 'both'}, {second: 'both', first: 1})).to.equal(true); expect(utils.equals({first: {nest: 1}, second: 'both'}, {second: 'both', first: {nest: 1}})).to.equal(true); }); - + it('returns true when comparing unequal arrays', function () { expect(utils.equals([1, 2, 3], [3, 1, 2])).to.equal(false); expect(utils.equals([{age: 12, height: 50}], [{age: 12, height: 60}])).to.equal(false); }); - + it('returns true when comparing equal arrays', function () { expect(utils.equals([1, 2, 3], [1, 2, 3])).to.equal(true); expect(utils.equals([{age: 12, height: 50}], [{age: 12, height: 50}])).to.equal(true); }); }); - + // Kubernetes uses a modified version of the DNS-1123 standard. describe('Kubernetes names', function() { it ('recognizes legal Kubernetes names', function() { @@ -77,7 +77,7 @@ describe('general utilities', function () { expect(utils.isLegalK8sName('aa-bb-cc')).to.be.true; expect(utils.isLegalK8sName('aa12-b3')).to.be.true; }); - + it ('recognizes illegal Kubernetes names', function() { expect(utils.isLegalK8sName(7)).to.be.false; expect(utils.isLegalK8sName('Aa')).to.be.false; @@ -97,12 +97,13 @@ describe('general utilities', function () { it ('converts illegal to legal names', function() { expect(utils.toLegalK8sName('AA')).to.equal('aa'); expect(utils.toLegalK8sName('aa_bb-cc')).to.equal('aa-bb-cc'); + expect(utils.toLegalK8sName('aa_bb_cc')).to.equal('aa-bb-cc'); expect(utils.toLegalK8sName('aa12.b3')).to.equal('aa12-b3'); expect(utils.toLegalK8sName('aa12$b3')).to.equal('aa12-b3'); expect(utils.toLegalK8sName('--aa')).to.equal('aa'); expect(utils.toLegalK8sName('aa--')).to.equal('aa'); expect(utils.toLegalK8sName('aa-bb-cc-dd-ee-ff-gg-hh-ii-jj-kk-ll-mm-nn-oo-pp-qq-rr-ss-tt-uu-vv-ww-xx-yy-zz')) - .to.equal('aa-bb-cc-dd-ee-ff-gg-hh-ii-jj-kk-ll-mm-nn-oo-pp-qq-rr-ss-tt-uu-'); + .to.equal('aa-bb-cc-dd-ee-ff-gg-hh-ii-jj-kk-ll-mm-nn-oo-pp-qq-rr-ss-tt-uu'); }); }); });