🔍 Duplicate Code Detected: Validator Utility Functions in AKS and GKE Packages
Analysis of commit 3331497
Assignee: @copilot
Summary
Five validator utility functions (needsValidation, requiredTranslation, requiredInCluster, clusterNameChars, clusterNameStartEnd) are nearly identically duplicated between pkg/aks/util/validators.ts and pkg/gke/util/validators.ts. The requiredTranslation function is 100% identical between both files. The remaining four follow the exact same structure with only minor provider-specific differences (different auth checks, regex patterns, and error message keys).
Duplication Details
Pattern: Near-identical validator factory functions
-
Severity: Medium
-
Occurrences: 2 files, 5 duplicated functions (~30 lines of near-identical code)
-
Locations:
pkg/aks/util/validators.ts (lines 11–42)
pkg/gke/util/validators.ts (lines 7–35)
-
Code Sample (requiredTranslation — 100% identical in both files):
export const requiredTranslation = (ctx: any, labelKey = 'Value'): String => {
return ctx.t('validation.required', { key: ctx.t(labelKey) });
};
- Code Sample (
needsValidation — same structure, different condition):
// AKS (line 11)
export const needsValidation = (ctx: any): Boolean => {
return !!ctx.config.azureCredentialSecret && !!ctx.config.resourceLocation;
};
// GKE (line 7)
export const needsValidation = (ctx: any): Boolean => {
return !!ctx.isAuthenticated;
};
- Code Sample (
requiredInCluster — same structure, different path):
// AKS (line 19)
export const requiredInCluster = (ctx: any, labelKey: string, clusterPath: string) => {
return () :String | undefined => {
return needsValidation(ctx) && clusterPath && !get(ctx, clusterPath) ? requiredTranslation(ctx, labelKey) : undefined;
};
};
// GKE (line 15)
export const requiredInCluster = (ctx: any, labelKey: string, clusterPath: string) => {
return () :String | undefined => {
return needsValidation(ctx) && clusterPath && !get(ctx.normanCluster, clusterPath) ? requiredTranslation(ctx, labelKey) : undefined;
};
};
Impact Analysis
- Maintainability: Any change to the shared validation logic (e.g., translation key format, error return type) must be applied to both files separately, risking divergence.
- Bug Risk: Fixes to one file may not be applied to the other.
requiredTranslation is currently identical — drift is a real risk.
- Code Bloat: ~30 lines of near-duplicate code across 2 files.
Refactoring Recommendations
-
Extract shared validation helpers to a common utility
- Create:
shell/utils/validators/cloudProviderValidators.ts
- Move
requiredTranslation as a shared, provider-agnostic export
- For
needsValidation, requiredInCluster, clusterNameChars, clusterNameStartEnd — create generic factory functions that accept the provider-specific configuration (auth check, regex, error key) as parameters
- Estimated effort: 2–3 hours
- Benefits: Single source of truth for validation logic, easier to test, consistent behavior across providers
-
Parameterize the differing parts
- The differences are: auth check expression, cluster name regex, and i18n error key
- A generic
createClusterNameCharsValidator(ctx, regex, errorKey) factory would eliminate all duplication
Implementation Checklist
Analysis Metadata
- Analyzed Files: 2 (
pkg/aks/util/validators.ts, pkg/gke/util/validators.ts)
- Detection Method: Semantic code analysis
- Commit: 3331497
- Analysis Date: 2026-07-03
Generated by Duplicate Code Detector · ● 961.5K · ◷
🔍 Duplicate Code Detected: Validator Utility Functions in AKS and GKE Packages
Analysis of commit 3331497
Assignee:
@copilotSummary
Five validator utility functions (
needsValidation,requiredTranslation,requiredInCluster,clusterNameChars,clusterNameStartEnd) are nearly identically duplicated betweenpkg/aks/util/validators.tsandpkg/gke/util/validators.ts. TherequiredTranslationfunction is 100% identical between both files. The remaining four follow the exact same structure with only minor provider-specific differences (different auth checks, regex patterns, and error message keys).Duplication Details
Pattern: Near-identical validator factory functions
Severity: Medium
Occurrences: 2 files, 5 duplicated functions (~30 lines of near-identical code)
Locations:
pkg/aks/util/validators.ts(lines 11–42)pkg/gke/util/validators.ts(lines 7–35)Code Sample (
requiredTranslation— 100% identical in both files):needsValidation— same structure, different condition):requiredInCluster— same structure, different path):Impact Analysis
requiredTranslationis currently identical — drift is a real risk.Refactoring Recommendations
Extract shared validation helpers to a common utility
shell/utils/validators/cloudProviderValidators.tsrequiredTranslationas a shared, provider-agnostic exportneedsValidation,requiredInCluster,clusterNameChars,clusterNameStartEnd— create generic factory functions that accept the provider-specific configuration (auth check, regex, error key) as parametersParameterize the differing parts
createClusterNameCharsValidator(ctx, regex, errorKey)factory would eliminate all duplicationImplementation Checklist
Analysis Metadata
pkg/aks/util/validators.ts,pkg/gke/util/validators.ts)