Skip to content

Conversation

@bryan-cox
Copy link
Contributor

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

This PR refactors two high-complexity methods to improve code readability and maintainability:

  1. ManagedClusterSpec.Parameters() in azure/services/managedclusters/spec.go

    • Before: 340 lines, cyclomatic complexity 55
    • After: ~45 lines in main method, 20+ focused helper methods
    • Removed the //nolint:gocyclo directive
  2. Service.validateSpec() in azure/services/scalesets/scalesets.go

    • Before: 107 lines, cyclomatic complexity 30
    • After: ~28 lines in main method, 6 focused helper methods

Changes Made

managedclusters/spec.go:

  • Extracted configuration logic into focused methods:
    • convertExistingToHub, initializeManagedCluster
    • configureBasicProperties, configureNetworkProfile, configureServiceCIDR
    • configureLinuxProfile, configureOperatorSpec, configureAADProfile
    • configureAddonProfiles, configureSKU, configureAPIServerAccessProfile
    • configureIdentity, configureHTTPProxyConfig, configureOIDCIssuerProfile
    • configureAutoUpgradeProfile, configureSecurityProfile
    • configureAgentPoolProfiles, convertToTargetVersion
  • Added constants for magic strings (identityTypeMSI, dnsIPLastOctet)

scalesets/scalesets.go:

  • Extracted validation logic into focused methods:
    • validateVMCapabilities: vCPU, memory, ephemeral OS, encryption
    • validateUltraDiskSupport: ultra disk support for location/zones
    • requiresUltraDiskValidation: check if validation needed
    • validateUltraDiskInZone: per-zone validation
    • validateDiagnosticsProfile: diagnostics configuration
    • validateAvailabilityZones: availability zone support

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

This is a pure refactoring PR with no functional changes. All existing tests pass.

The refactoring follows these principles:

  • Single Responsibility Principle: each method does one thing
  • Improved testability: smaller methods are easier to unit test
  • Better readability: code flow is self-documenting
  • Reduced cognitive load: developers can understand one section at a time

TODOs:

  • squashed commits
  • includes documentation
  • adds unit tests
  • cherry-pick candidate

Release note:

NONE

@k8s-ci-robot
Copy link
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Nov 26, 2025
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Nov 26, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign neolit123 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Nov 26, 2025
@bryan-cox
Copy link
Contributor Author

/test all

@codecov
Copy link

codecov bot commented Nov 26, 2025

Codecov Report

❌ Patch coverage is 79.67213% with 62 lines in your changes missing coverage. Please review.
✅ Project coverage is 44.66%. Comparing base (2130510) to head (e18fe2f).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
azure/services/managedclusters/spec.go 80.83% 25 Missing and 21 partials ⚠️
azure/services/scalesets/scalesets.go 75.38% 11 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5984      +/-   ##
==========================================
+ Coverage   44.54%   44.66%   +0.11%     
==========================================
  Files         279      279              
  Lines       25140    25248     +108     
==========================================
+ Hits        11199    11277      +78     
- Misses      13128    13145      +17     
- Partials      813      826      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

bryan-cox and others added 2 commits November 26, 2025 15:16
Split the monolithic Parameters() method (340 lines, complexity 55) into
focused helper methods to improve readability and maintainability.

Extracted methods:
- convertExistingToHub: convert existing object to hub version
- initializeManagedCluster: create or return existing cluster
- configureBasicProperties: set name, owner, identity, location
- configureNetworkProfile: network settings and CIDR configuration
- configureServiceCIDR: service CIDR and DNS IP calculation
- configureLinuxProfile: SSH and Linux settings
- configureOperatorSpec: kubeconfig secrets configuration
- configureAADProfile: Azure Active Directory settings
- configureAddonProfiles: add-on configuration
- configureSKU: SKU tier settings
- configureAPIServerAccessProfile: API server access settings
- configureIdentity: identity and kubelet identity
- configureHTTPProxyConfig: HTTP proxy settings
- configureOIDCIssuerProfile: OIDC issuer settings
- configureAutoUpgradeProfile: auto upgrade settings
- configureSecurityProfile: security profile orchestration
- configureAgentPoolProfiles: agent pool configuration
- convertToTargetVersion: convert to stable/preview API

Also extracted constants for magic strings (identityTypeMSI, dnsIPLastOctet).

No functional changes - all existing tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Bryan Cox <[email protected]>
Split the validateSpec() method (107 lines, complexity 30) into focused
helper methods to improve readability and maintainability.

Extracted methods:
- validateVMCapabilities: validate vCPU, memory, ephemeral OS, encryption
- validateUltraDiskSupport: validate ultra disk support for location/zones
- requiresUltraDiskValidation: check if ultra disk validation is needed
- validateUltraDiskInZone: validate ultra disk support per zone
- validateDiagnosticsProfile: validate diagnostics configuration
- validateAvailabilityZones: validate availability zone support

No functional changes - all existing tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Bryan Cox <[email protected]>
@bryan-cox
Copy link
Contributor Author

/test all

@bryan-cox bryan-cox marked this pull request as ready for review December 1, 2025 15:50
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 1, 2025
@k8s-ci-robot k8s-ci-robot requested a review from Jont828 December 1, 2025 15:50
Copy link
Contributor

@mboersma mboersma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

This diff became challenging to read, but AFAICT all the same validations are present after refactoring, and unit tests still pass. Thanks @bryan-cox!

if err != nil {
return "", fmt.Errorf("failed to parse service cidr: %w", err)
}
// Set the last octet of the IP to .10 to ensure a valid DNS IP within the service CIDR.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 thanks for improving this code comment too

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 1, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: b40f5dbd7425ba137e4ea3874027b23340546bcf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants