Skip to content

[duplicate-code] Duplicate Code: Validator Utility Functions in AKS and GKE Packages #129

Description

@github-actions

🔍 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

  1. 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
  2. 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

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

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 ·

  • expires on Jul 5, 2026, 9:40 PM UTC

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions