Controller to not touch osup if possible
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
monov1alpha1 "example.com/monok8s/pkg/apis/monok8s/v1alpha1"
|
||||
"example.com/monok8s/pkg/buildinfo"
|
||||
"example.com/monok8s/pkg/kube"
|
||||
)
|
||||
|
||||
@@ -39,34 +38,7 @@ func EnsureOSUpgradeProgressForNode(
|
||||
return fmt.Errorf("osupgrade is nil")
|
||||
}
|
||||
|
||||
// Keep using your existing helper if it already encapsulates
|
||||
// selector matching / parent linkage / create-or-update semantics.
|
||||
osup, err := ensureProgressHeartbeat(ctx, clients, namespace, nodeName, osu)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
klog.InfoS("ensured OSUpgradeProgress from admission",
|
||||
"osupgrade", osu.Name,
|
||||
"osupgradeProgress", osup.Name,
|
||||
"node", nodeName,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureProgressHeartbeat(ctx context.Context, clients *kube.Clients,
|
||||
namespace string, nodeName string,
|
||||
osu *monov1alpha1.OSUpgrade,
|
||||
) (*monov1alpha1.OSUpgradeProgress, error) {
|
||||
name := fmt.Sprintf("%s-%s", osu.Name, nodeName)
|
||||
now := metav1.Now()
|
||||
|
||||
currentVersion := buildinfo.KubeVersion
|
||||
targetVersion := ""
|
||||
if osu.Status != nil {
|
||||
targetVersion = osu.Status.ResolvedVersion
|
||||
}
|
||||
|
||||
progress := &monov1alpha1.OSUpgradeProgress{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
@@ -83,84 +55,35 @@ func ensureProgressHeartbeat(ctx context.Context, clients *kube.Clients,
|
||||
Name: osu.Name,
|
||||
},
|
||||
},
|
||||
Status: &monov1alpha1.OSUpgradeProgressStatus{
|
||||
CurrentVersion: currentVersion,
|
||||
TargetVersion: targetVersion,
|
||||
Phase: monov1alpha1.OSUpgradeProgressPhasePending,
|
||||
LastUpdatedAt: &now,
|
||||
Message: "acknowledged",
|
||||
},
|
||||
}
|
||||
|
||||
created, err := createProgress(ctx, clients, osup_gvr, progress)
|
||||
if err == nil {
|
||||
klog.InfoS("created osupgradeprogress", "name", created.Name, "namespace", created.Namespace)
|
||||
return created, nil
|
||||
return nil
|
||||
}
|
||||
if !apierrors.IsAlreadyExists(err) {
|
||||
return nil, fmt.Errorf("create OSUpgradeProgress %s/%s: %w", namespace, name, err)
|
||||
return fmt.Errorf("create OSUpgradeProgress %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
|
||||
var out *monov1alpha1.OSUpgradeProgress
|
||||
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
||||
existing, err := getProgress(ctx, clients, osup_gvr, namespace, name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get existing OSUpgradeProgress %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
|
||||
// Keep spec aligned with source and node.
|
||||
existing.Spec.NodeName = nodeName
|
||||
existing.Spec.SourceRef.Name = osu.Name
|
||||
|
||||
existing, err = updateProgressSpec(ctx, clients, osup_gvr, existing)
|
||||
if err != nil {
|
||||
if isUnknownUpdateResult(err) {
|
||||
latest, getErr := getProgress(ctx, clients, osup_gvr, namespace, name)
|
||||
if getErr == nil {
|
||||
out = latest
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("update OSUpgradeProgress spec %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
|
||||
if existing.Status == nil {
|
||||
existing.Status = &monov1alpha1.OSUpgradeProgressStatus{}
|
||||
}
|
||||
|
||||
existing.Status.CurrentVersion = currentVersion
|
||||
existing.Status.TargetVersion = targetVersion
|
||||
existing.Status.LastUpdatedAt = &now
|
||||
|
||||
if existing.Status.Phase == "" {
|
||||
existing.Status.Phase = monov1alpha1.OSUpgradeProgressPhasePending
|
||||
}
|
||||
if existing.Status.Message == "" {
|
||||
existing.Status.Message = "acknowledged"
|
||||
}
|
||||
|
||||
existing, err = updateProgressStatus(ctx, clients, osup_gvr, existing)
|
||||
if err != nil {
|
||||
if isUnknownUpdateResult(err) {
|
||||
latest, getErr := getProgress(ctx, clients, osup_gvr, namespace, name)
|
||||
if getErr == nil {
|
||||
out = latest
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("update OSUpgradeProgress status %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
|
||||
out = existing
|
||||
return nil
|
||||
})
|
||||
existing, err := getProgress(ctx, clients, osup_gvr, namespace, name)
|
||||
if err != nil {
|
||||
if out != nil {
|
||||
return out, nil
|
||||
}
|
||||
return nil, err
|
||||
return fmt.Errorf("get existing OSUpgradeProgress %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
|
||||
klog.InfoS("updated osupgradeprogress", "name", out.Name, "namespace", out.Namespace)
|
||||
return out, nil
|
||||
if existing.Spec.NodeName != nodeName || existing.Spec.SourceRef.Name != osu.Name {
|
||||
return fmt.Errorf(
|
||||
"conflicting OSUpgradeProgress %s/%s already exists: got spec.nodeName=%q spec.sourceRef.name=%q, want nodeName=%q sourceRef.name=%q",
|
||||
namespace,
|
||||
name,
|
||||
existing.Spec.NodeName,
|
||||
existing.Spec.SourceRef.Name,
|
||||
nodeName,
|
||||
osu.Name,
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateProgressRobust(
|
||||
|
||||
@@ -276,13 +276,7 @@ func listTargetNodeNames(
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid nodeSelector: %w", err)
|
||||
}
|
||||
|
||||
reqs, selectable := sel.Requirements()
|
||||
if !selectable {
|
||||
return nil, fmt.Errorf("nodeSelector is not selectable")
|
||||
}
|
||||
|
||||
selector = selector.Add(reqs...)
|
||||
selector = sel
|
||||
}
|
||||
|
||||
list, err := clients.Kubernetes.CoreV1().
|
||||
@@ -306,7 +300,6 @@ func listTargetNodeNames(
|
||||
}
|
||||
|
||||
func shouldUseNode(node *corev1.Node) bool {
|
||||
// Keep this conservative for now. Tighten if you want only Ready nodes.
|
||||
return node != nil && node.Name != ""
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user