-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add scripts to create N number of components
- Loading branch information
Showing
6 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ e2e-tests/* | |
commitlint.config.js | ||
**.config.js | ||
config/* | ||
scripts/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env zx | ||
|
||
import { | ||
checkSafetyThresholds, | ||
createResource, | ||
getCurrentNamespace, | ||
promptForNamespace, | ||
} from './utils.mjs'; | ||
|
||
const NumberOfComponent = 2; | ||
|
||
const baseApplicationConfig = { | ||
apiVersion: 'appstudio.redhat.com/v1alpha1', | ||
kind: 'Application', | ||
metadata: { | ||
name: 'test-application-n-components', | ||
namespace: 'NAMESPACE', | ||
annotations: { 'application.thumbnail': '9' }, | ||
}, | ||
spec: { displayName: 'Testing Application (100 components)' }, | ||
}; | ||
|
||
const baseComponentConfig = { | ||
apiVersion: 'appstudio.redhat.com/v1alpha1', | ||
kind: 'Component', | ||
metadata: { | ||
annotations: { | ||
'build.appstudio.openshift.io/pipeline': '{"name":"docker-build","bundle":"latest"}', | ||
'build.appstudio.openshift.io/request': 'configure-pac', | ||
'image.redhat.com/generate': '{"visibility": "public"}', | ||
}, | ||
name: 'COMPONENT_METADATA_NAME_PLACEHOLDER', | ||
}, | ||
spec: { | ||
componentName: 'COMPONENT_NAME_PLACEHOLDER', | ||
application: 'test-application-n-components', | ||
source: { | ||
git: { | ||
url: 'https://github.com/sahil143/devfile-sample-code-with-quarkus', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const allConfigs = []; | ||
|
||
// Safety check | ||
const shouldProceed = await checkSafetyThresholds(NumberOfComponent); | ||
if (!shouldProceed) { | ||
console.log(chalk.blue('Operation cancelled by user')); | ||
process.exit(0); | ||
} | ||
|
||
// Get and confirm namespace | ||
const currentNs = await getCurrentNamespace(); | ||
const targetNs = await promptForNamespace(currentNs); | ||
|
||
console.log(chalk.green('Using namespace', targetNs)); | ||
|
||
// Create application | ||
console.log( | ||
chalk.grey( | ||
`Creating application '${baseApplicationConfig.spec.displayName}'(${baseApplicationConfig.metadata.name})`, | ||
allConfigs, | ||
), | ||
); | ||
baseApplicationConfig.metadata['namespace'] = targetNs; | ||
|
||
await createResource(baseApplicationConfig, 'application'); | ||
|
||
console.log(chalk.grey(`Creating ${NumberOfComponent} component`, allConfigs)); | ||
|
||
for (let i = 1; i <= NumberOfComponent; i++) { | ||
// Create a deep copy of the base config | ||
const config = JSON.parse(JSON.stringify(baseComponentConfig)); | ||
|
||
// Update with unique names | ||
config.metadata.name = `devfile-sample-code-with-quarkus-longer-name-new-${i}`; | ||
config.metadata['namespace'] = targetNs; | ||
config.spec.componentName = `devfile-sample-code-with-quarkus-longer-name-new-${i}`; | ||
|
||
allConfigs.push(config); | ||
} | ||
|
||
// Apply each config using kubectl with JSON | ||
for (const config of allConfigs) { | ||
await createResource(config, 'component'); | ||
} | ||
|
||
console.log(chalk.blue(`✨ Successfully processed ${allConfigs.length} components`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const DELAY = 10000; | ||
|
||
// Function to get current namespace | ||
export async function getCurrentNamespace() { | ||
try { | ||
const result = await $`kubectl config view --minify -o jsonpath='{..namespace}'`; | ||
return result.stdout.trim() || 'default'; | ||
} catch (error) { | ||
return 'default'; | ||
} | ||
} | ||
|
||
// Function to prompt for namespace | ||
export async function promptForNamespace(currentNs) { | ||
console.log(chalk.blue(`Current namespace is: ${currentNs}`)); | ||
const useCurrentNs = await question( | ||
chalk.yellow(`Do you want to use the current namespace "${currentNs}"? (y/n) `), | ||
); | ||
|
||
if (useCurrentNs.toLowerCase() === 'y') { | ||
return currentNs; | ||
} | ||
|
||
const newNs = await question(chalk.yellow('Enter the namespace to use: ')); | ||
return newNs.trim(); | ||
} | ||
|
||
// sleep function | ||
export async function sleep(time) { | ||
return await new Promise((resolve) => setTimeout(resolve, time)); | ||
} | ||
|
||
export async function createResource(config, type = 'resource') { | ||
try { | ||
await $`kubectl apply --validate=false -f - <<< ${JSON.stringify(config)}`; | ||
console.log(chalk.green(`✓ Created ${type}: ${config.metadata.name}`)); | ||
// Sleep for 10 second. Disclaimer: Do not create too many component wi | ||
await sleep(DELAY); | ||
} catch (error) { | ||
console.log(chalk.red(`✗ Failed to create ${type} ${config.metadata.name}: ${error.message}`)); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
export async function checkSafetyThresholds(numberOfComponents) { | ||
if (numberOfComponents > 10) { | ||
console.log(chalk.yellow('\n CAUTION:')); | ||
console.log(chalk.yellow(`You are about to create ${numberOfComponents} components.`)); | ||
console.log(chalk.yellow('Creating too many components in quick succession might:')); | ||
console.log(chalk.yellow(' - Overload the API server')); | ||
console.log(chalk.yellow(' - Trigger rate limiting')); | ||
console.log(chalk.yellow(' - Cause failed deployments')); | ||
console.log(chalk.yellow(`\nCurrent delay between components: ${DELAY / 1000} seconds`)); | ||
|
||
if (DELAY < 10000) { | ||
console.log( | ||
chalk.yellow(`\nRecommended: Use a delay of at least 10 seconds between components`), | ||
); | ||
} | ||
|
||
const proceed = await question(chalk.yellow('\nDo you want to proceed? (y/N) ')); | ||
return proceed.toLowerCase() === 'y'; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters