fix: improve resource field cleaning to prevent YAML parsing errors

This commit is contained in:
loveuer
2025-11-13 11:48:14 +08:00
parent 23add6447d
commit c2bde4a0ff

View File

@@ -517,11 +517,13 @@ 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 // Clean up managed fields and other metadata that cause conflicts
deployment.ManagedFields = nil deployment.ManagedFields = nil
deployment.ResourceVersion = "" deployment.ResourceVersion = ""
deployment.UID = "" deployment.UID = ""
deployment.CreationTimestamp = metav1.Time{} deployment.CreationTimestamp = metav1.Time{}
deployment.SelfLink = ""
deployment.Generation = 0
yamlData, err = yaml.Marshal(deployment) yamlData, err = yaml.Marshal(deployment)
case "StatefulSet": case "StatefulSet":
var statefulset *appsv1.StatefulSet var statefulset *appsv1.StatefulSet
@@ -533,11 +535,13 @@ 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 // Clean up managed fields and other metadata that cause conflicts
statefulset.ManagedFields = nil statefulset.ManagedFields = nil
statefulset.ResourceVersion = "" statefulset.ResourceVersion = ""
statefulset.UID = "" statefulset.UID = ""
statefulset.CreationTimestamp = metav1.Time{} statefulset.CreationTimestamp = metav1.Time{}
statefulset.SelfLink = ""
statefulset.Generation = 0
yamlData, err = yaml.Marshal(statefulset) yamlData, err = yaml.Marshal(statefulset)
case "Service": case "Service":
var service *corev1.Service var service *corev1.Service
@@ -549,11 +553,13 @@ 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 // Clean up managed fields and other metadata that cause conflicts
service.ManagedFields = nil service.ManagedFields = nil
service.ResourceVersion = "" service.ResourceVersion = ""
service.UID = "" service.UID = ""
service.CreationTimestamp = metav1.Time{} service.CreationTimestamp = metav1.Time{}
service.SelfLink = ""
service.Generation = 0
yamlData, err = yaml.Marshal(service) yamlData, err = yaml.Marshal(service)
case "ConfigMap": case "ConfigMap":
var configmap *corev1.ConfigMap var configmap *corev1.ConfigMap
@@ -565,11 +571,13 @@ 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 // Clean up managed fields and other metadata that cause conflicts
configmap.ManagedFields = nil configmap.ManagedFields = nil
configmap.ResourceVersion = "" configmap.ResourceVersion = ""
configmap.UID = "" configmap.UID = ""
configmap.CreationTimestamp = metav1.Time{} configmap.CreationTimestamp = metav1.Time{}
configmap.SelfLink = ""
configmap.Generation = 0
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))