mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
refactor if statement
This commit is contained in:
parent
e8d8eb4381
commit
f7defeecce
@ -59,48 +59,11 @@ func UpgradeManagedFields(
|
|||||||
})
|
})
|
||||||
|
|
||||||
if ssaManagerExists {
|
if ssaManagerExists {
|
||||||
ssaManager := managedFields[ssaManagerIndex]
|
err = unionManagerIntoIndex(managedFields, ssaManagerIndex, csaManagerName)
|
||||||
|
|
||||||
// find Update manager of same APIVersion, union ssa fields with it.
|
|
||||||
// discard all other Update managers of the same name
|
|
||||||
csaManagerIndex, csaManagerExists := findFirstIndex(managedFields,
|
|
||||||
func(entry metav1.ManagedFieldsEntry) bool {
|
|
||||||
return entry.Manager == csaManagerName &&
|
|
||||||
entry.Operation == metav1.ManagedFieldsOperationUpdate &&
|
|
||||||
entry.Subresource == "" &&
|
|
||||||
entry.APIVersion == ssaManager.APIVersion
|
|
||||||
})
|
|
||||||
|
|
||||||
ssaFieldSet, err := fieldsToSet(*ssaManager.FieldsV1)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert fields to set: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
combinedFieldSet := &ssaFieldSet
|
|
||||||
|
|
||||||
// Union the csa manager with the existing SSA manager
|
|
||||||
if csaManagerExists {
|
|
||||||
csaManager := managedFields[csaManagerIndex]
|
|
||||||
|
|
||||||
csaFieldSet, err := fieldsToSet(*csaManager.FieldsV1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to convert fields to set: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
combinedFieldSet = combinedFieldSet.Union(&csaFieldSet)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that the resultant fieldset does not include the
|
|
||||||
// last applied annotation
|
|
||||||
combinedFieldSet = combinedFieldSet.Difference(csaAnnotationFieldSet)
|
|
||||||
|
|
||||||
combinedFieldSetEncoded, err := setToFields(*combinedFieldSet)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to encode field set: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
managedFields[ssaManagerIndex].FieldsV1 = &combinedFieldSetEncoded
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// SSA manager does not exist. Find the most recent matching CSA manager,
|
// SSA manager does not exist. Find the most recent matching CSA manager,
|
||||||
// convert it to an SSA manager.
|
// convert it to an SSA manager.
|
||||||
@ -120,24 +83,14 @@ func UpgradeManagedFields(
|
|||||||
return obj, nil
|
return obj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
csaManager := managedFields[csaManagerIndex]
|
// Convert CSA manager into SSA manager
|
||||||
csaFieldSet, err := fieldsToSet(*csaManager.FieldsV1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to convert fields to set: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove last applied configuration from owned fields, if necessary
|
|
||||||
csaFieldSet = *csaFieldSet.Difference(csaAnnotationFieldSet)
|
|
||||||
|
|
||||||
csaFieldSetEncoded, err := setToFields(csaFieldSet)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to encode field set: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert the entry to apply operation
|
|
||||||
managedFields[csaManagerIndex].FieldsV1 = &csaFieldSetEncoded
|
|
||||||
managedFields[csaManagerIndex].Operation = metav1.ManagedFieldsOperationApply
|
managedFields[csaManagerIndex].Operation = metav1.ManagedFieldsOperationApply
|
||||||
managedFields[csaManagerIndex].Manager = ssaManagerName
|
managedFields[csaManagerIndex].Manager = ssaManagerName
|
||||||
|
|
||||||
|
err = unionManagerIntoIndex(managedFields, csaManagerIndex, csaManagerName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create version of managed fields which has no CSA managers with the given name
|
// Create version of managed fields which has no CSA managers with the given name
|
||||||
@ -162,6 +115,56 @@ func UpgradeManagedFields(
|
|||||||
return copied, nil
|
return copied, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Locates an Update manager entry named `csaManagerName` with the same APIVersion
|
||||||
|
// as the manager at the targetIndex. Unions both manager's fields together
|
||||||
|
// into the manager specified by `targetIndex`. No other managers are modified.
|
||||||
|
func unionManagerIntoIndex(entries []metav1.ManagedFieldsEntry, targetIndex int, csaManagerName string) error {
|
||||||
|
ssaManager := entries[targetIndex]
|
||||||
|
|
||||||
|
// find Update manager of same APIVersion, union ssa fields with it.
|
||||||
|
// discard all other Update managers of the same name
|
||||||
|
csaManagerIndex, csaManagerExists := findFirstIndex(entries,
|
||||||
|
func(entry metav1.ManagedFieldsEntry) bool {
|
||||||
|
return entry.Manager == csaManagerName &&
|
||||||
|
entry.Operation == metav1.ManagedFieldsOperationUpdate &&
|
||||||
|
entry.Subresource == "" &&
|
||||||
|
entry.APIVersion == ssaManager.APIVersion
|
||||||
|
})
|
||||||
|
|
||||||
|
targetFieldSet, err := fieldsToSet(*ssaManager.FieldsV1)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to convert fields to set: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
combinedFieldSet := &targetFieldSet
|
||||||
|
|
||||||
|
// Union the csa manager with the existing SSA manager. Do nothing if
|
||||||
|
// there was no good candidate found
|
||||||
|
if csaManagerExists {
|
||||||
|
csaManager := entries[csaManagerIndex]
|
||||||
|
|
||||||
|
csaFieldSet, err := fieldsToSet(*csaManager.FieldsV1)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to convert fields to set: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
combinedFieldSet = combinedFieldSet.Union(&csaFieldSet)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that the resultant fieldset does not include the
|
||||||
|
// last applied annotation
|
||||||
|
combinedFieldSet = combinedFieldSet.Difference(csaAnnotationFieldSet)
|
||||||
|
|
||||||
|
// Encode the fields back to the serialized format
|
||||||
|
combinedFieldSetEncoded, err := setToFields(*combinedFieldSet)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to encode field set: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries[targetIndex].FieldsV1 = &combinedFieldSetEncoded
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func findFirstIndex[T any](
|
func findFirstIndex[T any](
|
||||||
collection []T,
|
collection []T,
|
||||||
predicate func(T) bool,
|
predicate func(T) bool,
|
||||||
|
Loading…
Reference in New Issue
Block a user