|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package schedulerextenderenabler |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/base64" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/deckhouse/csi-nfs/api/v1alpha1" |
| 27 | + "github.com/deckhouse/csi-nfs/hooks/go/consts" |
| 28 | + "github.com/deckhouse/module-sdk/pkg" |
| 29 | + "github.com/deckhouse/module-sdk/pkg/registry" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + NFSStorageClassSnapshotName = "nfs-storage-classes" |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + _ = registry.RegisterFunc( |
| 38 | + &pkg.HookConfig{ |
| 39 | + Kubernetes: []pkg.KubernetesConfig{ |
| 40 | + { |
| 41 | + Name: NFSStorageClassSnapshotName, |
| 42 | + APIVersion: "storage.deckhouse.io/v1alpha1", |
| 43 | + Kind: "NFSStorageClass", |
| 44 | + ExecuteHookOnEvents: ptr(true), |
| 45 | + ExecuteHookOnSynchronization: ptr(true), |
| 46 | + JqFilter: ".spec.workloadNodes", |
| 47 | + AllowFailure: ptr(true), |
| 48 | + }, |
| 49 | + }, |
| 50 | + Settings: &pkg.HookConfigSettings{ |
| 51 | + ExecutionMinInterval: time.Second * 3, |
| 52 | + ExecutionBurst: 1, |
| 53 | + }, |
| 54 | + Queue: fmt.Sprintf("modules/%s", consts.ModuleName), |
| 55 | + }, |
| 56 | + mainHook, |
| 57 | + ) |
| 58 | +) |
| 59 | + |
| 60 | +func mainHook(ctx context.Context, input *pkg.HookInput) error { |
| 61 | + fmt.Println("Scheduler extender enabler hook started") |
| 62 | + shouldEnable := false |
| 63 | + |
| 64 | + // Get snapshots using the standard approach |
| 65 | + snapshots := input.Snapshots.Get(NFSStorageClassSnapshotName) |
| 66 | + if len(snapshots) == 0 { |
| 67 | + fmt.Println("No snapshots found") |
| 68 | + // Don't return early - we need to disable the scheduler extender |
| 69 | + } else { |
| 70 | + fmt.Printf("Found %d snapshots\n", len(snapshots)) |
| 71 | + |
| 72 | + for i, snapshot := range snapshots { |
| 73 | + fmt.Printf("Processing snapshot %d: %v\n", i, snapshot) |
| 74 | + |
| 75 | + // Try to access the snapshot data as JSON |
| 76 | + // The JqFilter extracts .spec.workloadNodes, so we should get NFSStorageClassWorkloadNodes directly |
| 77 | + // If workloadNodes is not configured, the JqFilter returns null |
| 78 | + |
| 79 | + // The snapshot contains a base64-encoded JSON string |
| 80 | + // First, marshal the snapshot to get the base64 string |
| 81 | + snapshotBytes, err := json.Marshal(snapshot) |
| 82 | + if err != nil { |
| 83 | + fmt.Printf("Error marshaling snapshot %d: %v\n", i, err) |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + // Remove quotes from the JSON string to get the base64 string |
| 88 | + base64Str := string(snapshotBytes[1 : len(snapshotBytes)-1]) |
| 89 | + fmt.Printf("Snapshot %d base64: %s\n", i, base64Str) |
| 90 | + |
| 91 | + // Decode the base64 string |
| 92 | + jsonBytes, err := base64.StdEncoding.DecodeString(base64Str) |
| 93 | + if err != nil { |
| 94 | + fmt.Printf("Error decoding base64 for snapshot %d: %v\n", i, err) |
| 95 | + continue |
| 96 | + } |
| 97 | + fmt.Printf("Snapshot %d decoded JSON: %s\n", i, string(jsonBytes)) |
| 98 | + |
| 99 | + // Try to unmarshal as NFSStorageClassWorkloadNodes |
| 100 | + workloadNodes := new(v1alpha1.NFSStorageClassWorkloadNodes) |
| 101 | + err = json.Unmarshal(jsonBytes, workloadNodes) |
| 102 | + if err != nil { |
| 103 | + fmt.Printf("Error unmarshaling snapshot %d as NFSStorageClassWorkloadNodes: %v\n", i, err) |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Printf("Successfully unmarshaled workload nodes: %+v\n", workloadNodes) |
| 108 | + |
| 109 | + // Check if NodeSelector is configured |
| 110 | + if workloadNodes.NodeSelector == nil { |
| 111 | + fmt.Println("nodeSelector is nil") |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + // Check if NodeSelector has any actual selectors |
| 116 | + if workloadNodes.NodeSelector.MatchLabels == nil && workloadNodes.NodeSelector.MatchExpressions == nil { |
| 117 | + fmt.Println("nodeSelector has no matchLabels or matchExpressions") |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + fmt.Printf("Found valid nodeSelector: %+v\n", workloadNodes.NodeSelector) |
| 122 | + fmt.Println("NodeSelector is not empty. Should enable scheduler extender") |
| 123 | + shouldEnable = true |
| 124 | + break |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + enableLabel := fmt.Sprintf("%v.internal.shedulerExtenderEnabled", consts.ModuleName) |
| 129 | + |
| 130 | + if shouldEnable { |
| 131 | + fmt.Println("Enable scheduler extender") |
| 132 | + input.Values.Set(enableLabel, true) |
| 133 | + } else { |
| 134 | + fmt.Println("Disable scheduler extender") |
| 135 | + input.Values.Set(enableLabel, false) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func ptr[T any](a T) *T { |
| 142 | + return &a |
| 143 | +} |
0 commit comments