fix: clean managedFields and metadata from resource YAML to prevent update conflicts

This commit is contained in:
loveuer
2025-11-13 11:40:36 +08:00
parent fa0298b4d8
commit 23add6447d

View File

@@ -517,6 +517,11 @@ func K8sResourceFetch(ctx context.Context, db *gorm.DB, store store.Store) fiber
if err != nil { if err != nil {
return resp.R500(c, "", nil, fmt.Errorf("failed to get deployment: %w", err)) return resp.R500(c, "", nil, fmt.Errorf("failed to get deployment: %w", err))
} }
// Clean up managed fields and other metadata
deployment.ManagedFields = nil
deployment.ResourceVersion = ""
deployment.UID = ""
deployment.CreationTimestamp = metav1.Time{}
yamlData, err = yaml.Marshal(deployment) yamlData, err = yaml.Marshal(deployment)
case "StatefulSet": case "StatefulSet":
var statefulset *appsv1.StatefulSet var statefulset *appsv1.StatefulSet
@@ -528,6 +533,11 @@ func K8sResourceFetch(ctx context.Context, db *gorm.DB, store store.Store) fiber
if err != nil { if err != nil {
return resp.R500(c, "", nil, fmt.Errorf("failed to get statefulset: %w", err)) return resp.R500(c, "", nil, fmt.Errorf("failed to get statefulset: %w", err))
} }
// Clean up managed fields and other metadata
statefulset.ManagedFields = nil
statefulset.ResourceVersion = ""
statefulset.UID = ""
statefulset.CreationTimestamp = metav1.Time{}
yamlData, err = yaml.Marshal(statefulset) yamlData, err = yaml.Marshal(statefulset)
case "Service": case "Service":
var service *corev1.Service var service *corev1.Service
@@ -539,6 +549,11 @@ func K8sResourceFetch(ctx context.Context, db *gorm.DB, store store.Store) fiber
if err != nil { if err != nil {
return resp.R500(c, "", nil, fmt.Errorf("failed to get service: %w", err)) return resp.R500(c, "", nil, fmt.Errorf("failed to get service: %w", err))
} }
// Clean up managed fields and other metadata
service.ManagedFields = nil
service.ResourceVersion = ""
service.UID = ""
service.CreationTimestamp = metav1.Time{}
yamlData, err = yaml.Marshal(service) yamlData, err = yaml.Marshal(service)
case "ConfigMap": case "ConfigMap":
var configmap *corev1.ConfigMap var configmap *corev1.ConfigMap
@@ -550,6 +565,11 @@ func K8sResourceFetch(ctx context.Context, db *gorm.DB, store store.Store) fiber
if err != nil { if err != nil {
return resp.R500(c, "", nil, fmt.Errorf("failed to get configmap: %w", err)) return resp.R500(c, "", nil, fmt.Errorf("failed to get configmap: %w", err))
} }
// Clean up managed fields and other metadata
configmap.ManagedFields = nil
configmap.ResourceVersion = ""
configmap.UID = ""
configmap.CreationTimestamp = metav1.Time{}
yamlData, err = yaml.Marshal(configmap) yamlData, err = yaml.Marshal(configmap)
default: default:
return resp.R400(c, "", nil, fmt.Errorf("unsupported resource kind: %s", kind)) return resp.R400(c, "", nil, fmt.Errorf("unsupported resource kind: %s", kind))