Repository navigation
feat: add node annotation reconciliation to synchronize cluster specifications #45
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f800341
feat: add node annotation reconciliation to synchronize cluster speci…
havardelnan 3209a22
fix: improve annotation reconciliation logging and enhance patch comp…
havardelnan b1c2b5b
fix: enhance node annotation resolution to prevent clobbering valid a…
havardelnan eef919c
refactor: replace local datacenter info extraction with service metho…
havardelnan 0636b25
test: add unit tests for node annotation reconciliation and patch com…
havardelnan 4860eb2
refactor: replace hardcoded values with constants in node annotation …
havardelnan a4c6c44
refactor: replace hardcoded values with constants in node annotation …
havardelnan 3473d3b
refactor: format comments for consistency in node annotation tests
havardelnan ee33a39
chore: update Go version to 1.26.3 in go.mod
havardelnan 3f596b1
refactor: reorganize exclusions and rules in golangci configuration
havardelnan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module github.com/vitistack/talos-operator | ||
|
|
||
| go 1.26.2 | ||
| go 1.26.3 | ||
|
|
||
| godebug default=go1.26 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
internal/kubernetescluster/talos/reconcile_node_annotations.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| package talos | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/viper" | ||
| "github.com/vitistack/common/pkg/loggers/vlog" | ||
| vitistackv1alpha1 "github.com/vitistack/common/pkg/v1alpha1" | ||
| "github.com/vitistack/talos-operator/internal/services/networknamespaceservice" | ||
| "github.com/vitistack/talos-operator/internal/services/talosconfigservice" | ||
| "github.com/vitistack/talos-operator/internal/services/vitistackservice" | ||
| "github.com/vitistack/talos-operator/pkg/consts" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| k8stypes "k8s.io/apimachinery/pkg/types" | ||
| ) | ||
|
|
||
| // reconcileNodeAnnotations ensures that all nodes in the workload cluster have | ||
| // the expected vitistack annotations. This covers cases where the cluster spec | ||
| // has changed after initial provisioning (e.g. project, environment, region) | ||
| // and keeps annotations in sync without requiring a node re-provision. | ||
| func (t *TalosManager) reconcileNodeAnnotations(ctx context.Context, cluster *vitistackv1alpha1.KubernetesCluster) error { | ||
| clientset, err := t.getWorkloadClusterClient(ctx, cluster) | ||
| if err != nil || clientset == nil { | ||
| return err | ||
| } | ||
|
|
||
| nodeList, err := clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list workload cluster nodes for annotation reconciliation: %w", err) | ||
| } | ||
|
|
||
| machines, err := t.machineService.GetClusterMachines(ctx, cluster) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get cluster machines for annotation reconciliation: %w", err) | ||
| } | ||
| machineMap := make(map[string]*vitistackv1alpha1.Machine, len(machines)) | ||
| for _, m := range machines { | ||
| machineMap[m.Name] = m | ||
| } | ||
|
|
||
| // Resolve shared annotation values once; track keys to skip on lookup failure | ||
| // so we don't clobber valid annotations with empty strings due to transient errors. | ||
| country, az := talosconfigservice.ExtractDatacenterInfo(cluster.Spec.Cluster.Datacenter) | ||
| skipKeys := make(map[string]bool) | ||
|
|
||
| networkNamespaceName, err := resolveNetworkNamespaceName(ctx, cluster) | ||
| if err != nil { | ||
| skipKeys[vitistackv1alpha1.ClusterWorkspaceAnnotation] = true | ||
| } | ||
|
|
||
| infrastructure, err := resolveInfrastructure(ctx) | ||
| if err != nil { | ||
| skipKeys[vitistackv1alpha1.MachineInfrastructureAnnotation] = true | ||
| skipKeys[vitistackv1alpha1.InfrastructureAnnotation] = true //nolint:staticcheck // backward compatibility | ||
| } | ||
|
|
||
| for i := range nodeList.Items { | ||
| node := &nodeList.Items[i] | ||
| m, exists := machineMap[node.Name] | ||
| if !exists { | ||
| continue | ||
| } | ||
|
|
||
| desired := buildDesiredNodeAnnotations(cluster, m, country, az, networkNamespaceName, infrastructure) | ||
| // Remove keys that couldn't be resolved reliably to avoid | ||
| // overwriting valid annotations with empty strings. | ||
| for k := range skipKeys { | ||
| delete(desired, k) | ||
| } | ||
| patch := computeAnnotationPatch(node.Annotations, desired) | ||
| if len(patch) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| patchBytes, err := json.Marshal(map[string]interface{}{ | ||
| "metadata": map[string]interface{}{ | ||
| "annotations": patch, | ||
| }, | ||
| }) | ||
| // Count actual updates (non-nil values) for the log message | ||
| updated, removed := countPatchOps(patch) | ||
| if err != nil { | ||
| vlog.Warn(fmt.Sprintf("Failed to marshal annotation patch for node %s: %v", node.Name, err)) | ||
| continue | ||
| } | ||
|
|
||
| if _, err := clientset.CoreV1().Nodes().Patch(ctx, node.Name, k8stypes.MergePatchType, patchBytes, metav1.PatchOptions{}); err != nil { | ||
| vlog.Warn(fmt.Sprintf("Failed to patch annotations on node %s: %v", node.Name, err)) | ||
| continue | ||
| } | ||
|
|
||
| vlog.Info(fmt.Sprintf("Reconciled annotations on node %s: %d updated, %d removed", node.Name, updated, removed)) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // buildDesiredNodeAnnotations returns the expected annotation map for a node. | ||
| func buildDesiredNodeAnnotations( | ||
| cluster *vitistackv1alpha1.KubernetesCluster, | ||
| m *vitistackv1alpha1.Machine, | ||
| country, az, networkNamespaceName, infrastructure string, | ||
| ) map[string]string { | ||
| annotations := map[string]string{ | ||
| vitistackv1alpha1.ClusterIdAnnotation: cluster.Spec.Cluster.ClusterId, | ||
| vitistackv1alpha1.ClusterNameAnnotation: cluster.Name, | ||
| vitistackv1alpha1.ClusterProjectAnnotation: cluster.Spec.Cluster.Project, | ||
| vitistackv1alpha1.EnvironmentAnnotation: cluster.Spec.Cluster.Environment, | ||
| vitistackv1alpha1.CountryAnnotation: country, | ||
| vitistackv1alpha1.AzAnnotation: az, | ||
| vitistackv1alpha1.RegionAnnotation: cluster.Spec.Cluster.Region, | ||
| vitistackv1alpha1.KubernetesProviderAnnotation: string(cluster.Spec.Cluster.Provider), | ||
| vitistackv1alpha1.MachineProviderAnnotation: string(m.Status.Provider), | ||
| vitistackv1alpha1.MachineClassAnnotation: m.Spec.MachineClass, | ||
| vitistackv1alpha1.MachineIdAnnotation: m.Name, | ||
| vitistackv1alpha1.ClusterWorkspaceAnnotation: networkNamespaceName, | ||
| vitistackv1alpha1.MachineInfrastructureAnnotation: infrastructure, | ||
| vitistackv1alpha1.VMProviderAnnotation: string(m.Status.Provider), //nolint:staticcheck // backward compatibility | ||
| vitistackv1alpha1.InfrastructureAnnotation: infrastructure, //nolint:staticcheck // backward compatibility | ||
| } | ||
|
|
||
| if nodePool, ok := m.Annotations[vitistackv1alpha1.NodePoolAnnotation]; ok && nodePool != "" { | ||
| annotations[vitistackv1alpha1.NodePoolAnnotation] = nodePool | ||
| } | ||
|
|
||
| return annotations | ||
| } | ||
|
|
||
| // computeAnnotationPatch computes a JSON merge-patch for annotations. | ||
| // It sets desired values that differ from current, and sets managed keys | ||
| // (vitistack.io/* annotations) to nil when they are present on the node | ||
| // but absent from the desired set — causing MergePatch to delete them. | ||
| func computeAnnotationPatch(current, desired map[string]string) map[string]interface{} { | ||
| patch := make(map[string]interface{}) | ||
|
|
||
| // Set or update desired annotations | ||
| for k, v := range desired { | ||
| if current[k] != v { | ||
| patch[k] = v | ||
| } | ||
| } | ||
|
|
||
| // Remove managed annotations that are no longer desired | ||
| for _, k := range vitistackv1alpha1.GetAllVitistackAnnotations() { | ||
| if _, isDesired := desired[k]; !isDesired { | ||
| if _, exists := current[k]; exists { | ||
| patch[k] = nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return patch | ||
| } | ||
|
|
||
| // countPatchOps counts the number of updates and removals in a patch. | ||
| func countPatchOps(patch map[string]interface{}) (updated, removed int) { | ||
| for _, v := range patch { | ||
| if v == nil { | ||
| removed++ | ||
| } else { | ||
| updated++ | ||
| } | ||
| } | ||
| return updated, removed | ||
| } | ||
|
|
||
| // resolveNetworkNamespaceName resolves the network namespace name for annotation use. | ||
| // Returns an error when the lookup fails so the caller can skip the key rather than | ||
| // clobbering a valid annotation with an empty string. | ||
| func resolveNetworkNamespaceName(ctx context.Context, cluster *vitistackv1alpha1.KubernetesCluster) (string, error) { | ||
| if cluster.Spec.Cluster.NetworkNamespaceName != "" { | ||
| return cluster.Spec.Cluster.NetworkNamespaceName, nil | ||
| } | ||
| ns, err := networknamespaceservice.FetchFirstNetworkNamespaceByNamespace(ctx, cluster.GetNamespace()) | ||
| if err != nil { | ||
| vlog.Warn(fmt.Sprintf("failed to fetch NetworkNamespaces for namespace %q: %v", cluster.GetNamespace(), err)) | ||
| return "", err | ||
| } | ||
| if ns != nil { | ||
| return ns.Name, nil | ||
| } | ||
| return "", nil | ||
| } | ||
|
|
||
| // resolveInfrastructure resolves the infrastructure name from the Vitistack CR. | ||
| // Returns an error when the lookup fails so the caller can skip the key rather than | ||
| // clobbering a valid annotation with an empty string. | ||
| func resolveInfrastructure(ctx context.Context) (string, error) { | ||
| vitistack, err := vitistackservice.FetchVitistackByName(ctx, viper.GetString(consts.VITISTACK_NAME)) | ||
| if err != nil { | ||
| vlog.Warn(fmt.Sprintf("failed to fetch Vitistack %q: %v", viper.GetString(consts.VITISTACK_NAME), err)) | ||
| return "", err | ||
| } | ||
| if vitistack != nil && vitistack.Spec.Infrastructure != "" { | ||
| return vitistack.Spec.Infrastructure, nil | ||
| } | ||
| return "", nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.