8000 [deckhouse-controller] New HookInput Snapshot logic [second-iteration] by RottenRat · Pull Request #13941 · deckhouse/deckhouse · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[deckhouse-controller] New HookInput Snapshot logic [second-iteration] #13941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

RottenRat
Copy link
Contributor
@RottenRat RottenRat commented Jun 10, 2025

Description

Second iteration of New Snapshot logic

Refactored access to snapshots by replacing direct indexing (input.Snapshots["snapshot_name"]) with method calls via the new field NewSnapshots. Access to snapshots is now performed using the .Get() method. For example, instead of casting like input.Snapshots["snapshot_name"][0].(NodeInfo) we use:

WARNING

!!We ignore all nil results in new snapshots realization!!!

var node NodeInfo
err := inputs.NewSnapshots.Get("snapshot_name")[0].UnmarshalTo(&node)

Additionally, two new methods provided by the library sdkobjectpatch "github.com/deckhouse/module-sdk/pkg/object-patch" have been introduced:

  • SnapshotIter: Enables catching type cast errors during iteration over objects. Replaces manual checks like policy, ok := policySnap.(EgressGatewayPolicyInfo); if !ok { continue }.
  • UnmarshalToStruct: Allows directly unmarshalling snapshot values into structures without additional steps, eliminating the need for intermediate variables and explicit marshal/unmarshal actions.

Why do we need it, and what problem does it solve?

The previous approach relied heavily on unsafe type assertions and direct index accesses which led to potential runtime panics and inconsistencies. By switching to structured methods and proper error handling, the refactoring reduces the risk of bugs caused by incorrect types or missing elements, thus improving stability and maintainability of the codebase.

Checklist

  • [] The code is covered by unit tests.
  • [] e2e tests passed.
  • [] Documentation updated according to the changes.
  • [] Changes were tested in the Kubernetes cluster manually.

Changelog entries

section: deckhouse-controller
type: chore
summary: change hook input interface, refactor old snapshot logic (part one)
impact_level: low

Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
@RottenRat RottenRat requested a review from ldmonster June 10, 2025 14:34
@RottenRat RottenRat self-assigned this Jun 10, 2025
@github-actions github-actions bot added area/cloud-provider Pull requests that update cloud providers modules area/network Pull requests that update cni and network modules area/cluster-and-infrastructure Pull requests that update infra modules area/core Pull requests that update core modules go Pull requests that update Go code labels Jun 10, 2025
@ldmonster ldmonster force-pushed the refactoring-hooks-snapshots branch from f1d52eb to 57e453b Compare June 10, 2025 15:07
Sinelnikov Michail and others added 20 commits June 11, 2025 15:41
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
Signed-off-by: Michail Sinelnikov <72449391+RottenRat@users.noreply.github.com>
@RottenRat RottenRat added this to the v1.70.7 milestone Jun 18, 2025
@RottenRat RottenRat modified the milestones: v1.70.7, v1.71.0 Jun 18, 2025
Sinelnikov Michail added 3 commits June 18, 2025 18:51
fix
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
@RottenRat RottenRat marked this pull request as ready for review June 19, 2025 08:53
}
legacyMode := snaps[0]
// legacyMode is set in the provider cluster configuration secret
input.Values.Set("cloudProviderVcd.internal.legacyMode", legacyMode)
Copy link
Contributor

Choose a reason for hiding this comment

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

only if legacy mode is true

Suggested change
input.Values.Set("cloudProviderVcd.internal.legacyMode", legacyMode)
input.Values.Set("cloudProviderVcd.internal.legacyMode", legacyMode)

for node, err := range sdkobjectpatch.SnapshotIter[nodeKernelVersion](nodes) {
if err != nil {
input.Logger.Error("failed to iterate over 'nodes' snapshots", log.Err(err))
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

do not continue here. it gives a panic in previous realization. make return

@@ -165,9 +174,10 @@ func handleDiscoveryDataVolumeTypes(
}
}

storageClassSnapshots := make(map[string]*storage.StorageClass)
// TODO: review this, looks like deadcode
storageClassSnapshots := make(map[string]storage.StorageClass)
for _, snapshot := range input.Snapshots["storage_classes"] {
Copy link
Contributor

Choose a reason for hiding this comment

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

@RottenRat refactor this please

for endpoint, err := range sdkobjectpatch.SnapshotIter[etcdInstance](etcdEndpointsSnapshots) {
if err != nil {
input.Logger.Error("failed to iterate over 'etcd_endpoints' snapshot", log.Err(err))
currentQuotaBytes = defaultEtcdMaxSize
Copy link
Contributor

Choose a reason for hiding this comment

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

you must return error here. where did you find this logic?

pod := spod.(controlPlaneManagerPod)
for pod, err := range sdkobjectpatch.SnapshotIter[controlPlaneManagerPod](podsSnaps) {
if err != nil {
input.Logger.Error("failed to iterate over 'cpm_pods' snapshots", log.Err(err))
Copy link
Contributor

Choose a reason for hiding this comment

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

return error here, because of casting


if len(conditions) == 0 {
if len(conditionsSnaps) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

we must check len(conditions) here and len(conditionsSnaps) before

for _, item := range nodeUserSnap {
nuForClear := item.(nodeUsersForClear)
var nuForClear nodeUsersForClear
Copy link
Contributor

Choose a reason for hiding this comment

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

why not iterator?

@@ -68,9 +72,12 @@ func updateControlPlane(input *go_hook.HookInput) error {
"externalManagedControlPlane": true,
},
}
for controlPlane, err := range sdkobjectpatch.SnapshotIter[controlPlane](input.NewSnapshots.Get("control_plane")) {
if err != nil {
input.Logger.Error("failed to iterate over 'control_plane' classes", log.Err(err))
Copy link
Contributor

Choose a reason for hiding this comment

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

return error because of casting

@@ -50,7 +50,7 @@ var _ = sdk.RegisterFunc(&go_hook.HookConfig{
}, discoverCloudProviderHandler)

func discoverCloudProviderHandler(input *go_hook.HookInput) error {
secret := input.Snapshots["cloud_provider_secret"]
secret := input.NewSnapshots.Get("cloud_provider_secret")
Copy link
Contributor

Choose a reason for hiding this comment

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

this is an error.
you must check your PR for this errors.
if you set to values raw snapshot - it will be nested not correct.
try it local

@@ -65,8 +65,8 @@ metadata:

It("`nodeManager.internal.cloudProvider must be filled with data from secret", func() {
Expect(f).To(ExecuteSuccessfully())
Expect(f.ValuesGet("nodeManager.internal.cloudProvider.b64String").String()).To(Equal("abc"))
Expect(f.ValuesGet("nodeManager.internal.cloudProvider.b64JSON.parse").String()).To(Equal("me"))
Expect(f.ValuesGet("nodeManager.internal.cloudProvider.Wrapped.b64String").String()).To(Equal("abc"))
Copy link
Contributor

Choose a reason for hiding this comment

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

it will be not wrapped. fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cloud-provider Pull requests that update cloud providers modules area/cluster-and-infrastructure Pull requests that update infra modules area/core Pull requests that update core modules area/network Pull requests that update cni and network modules go Pull requests that update Go code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0