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 1 commit
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
168 changes: 168 additions & 0 deletions
168
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,168 @@ | ||
| package talos | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "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/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 | ||
| country, az := extractNodeAnnotationDatacenterInfo(cluster.Spec.Cluster.Datacenter) | ||
| networkNamespaceName := resolveNetworkNamespaceName(ctx, cluster) | ||
| infrastructure := resolveInfrastructure(ctx) | ||
|
|
||
| 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) | ||
| patch := computeAnnotationPatch(node.Annotations, desired) | ||
| if len(patch) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| patchBytes, err := json.Marshal(map[string]interface{}{ | ||
| "metadata": map[string]interface{}{ | ||
| "annotations": 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 %d annotations on node %s", len(patch), node.Name)) | ||
| } | ||
|
|
||
| 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 returns only the annotations that differ from current. | ||
| func computeAnnotationPatch(current, desired map[string]string) map[string]string { | ||
| patch := make(map[string]string) | ||
| for k, v := range desired { | ||
| if current[k] != v { | ||
| patch[k] = v | ||
| } | ||
| } | ||
| return patch | ||
| } | ||
|
|
||
|
havardelnan marked this conversation as resolved.
Resolved
|
||
| // extractNodeAnnotationDatacenterInfo splits a datacenter string into country and AZ. | ||
| func extractNodeAnnotationDatacenterInfo(datacenter string) (country string, az string) { | ||
| parts := strings.Split(datacenter, "-") | ||
| switch { | ||
| case len(parts) >= 3: | ||
| country = parts[0] | ||
| az = parts[len(parts)-1] | ||
| case len(parts) == 2: | ||
| country = parts[0] | ||
| az = "" | ||
| default: | ||
| country = datacenter | ||
| az = "" | ||
| } | ||
| return country, az | ||
| } | ||
|
havardelnan marked this conversation as resolved.
Resolved
|
||
|
|
||
| // resolveNetworkNamespaceName resolves the network namespace name for annotation use. | ||
| func resolveNetworkNamespaceName(ctx context.Context, cluster *vitistackv1alpha1.KubernetesCluster) string { | ||
| if cluster.Spec.Cluster.NetworkNamespaceName != "" { | ||
| return cluster.Spec.Cluster.NetworkNamespaceName | ||
| } | ||
| 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 "" | ||
|
havardelnan marked this conversation as resolved.
Resolved
|
||
| } | ||
| if ns != nil { | ||
| return ns.Name | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // resolveInfrastructure resolves the infrastructure name from the Vitistack CR. | ||
| func resolveInfrastructure(ctx context.Context) string { | ||
| 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 "" | ||
| } | ||
| if vitistack != nil && vitistack.Spec.Infrastructure != "" { | ||
| return vitistack.Spec.Infrastructure | ||
| } | ||
| return "" | ||
| } | ||
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.