mirror of
https://github.com/kubernetes/client-go.git
synced 2025-08-08 10:47:26 +00:00
remove last applied configuration information
Kubernetes-commit: a7fe0f0283765e4970211f7227602e2caa4b3a57
This commit is contained in:
parent
efe378914a
commit
089614c43e
@ -7,8 +7,13 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager"
|
"k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager"
|
||||||
|
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const csaAnnotationName = "kubectl.kubernetes.io/last-applied-configuration"
|
||||||
|
|
||||||
|
var csaAnnotationFieldSet = fieldpath.NewSet(fieldpath.MakePathOrDie("metadata", "annotations", csaAnnotationName))
|
||||||
|
|
||||||
// Upgrades the Manager information for fields managed with CSA
|
// Upgrades the Manager information for fields managed with CSA
|
||||||
// Prepares fields owned by `csaManager` for 'Update' operations for use now
|
// Prepares fields owned by `csaManager` for 'Update' operations for use now
|
||||||
// with the given `ssaManager` for `Apply` operations.
|
// with the given `ssaManager` for `Apply` operations.
|
||||||
@ -60,37 +65,48 @@ func UpgradeManagedFields(
|
|||||||
entry.APIVersion == ssaManager.APIVersion
|
entry.APIVersion == ssaManager.APIVersion
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ssaFieldSet, err := fieldmanager.FieldsToSet(*ssaManager.FieldsV1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert fields to set: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
combinedFieldSet := &ssaFieldSet
|
||||||
|
|
||||||
|
// Union the csa manager with the existing SSA manager
|
||||||
if csaManagerExists {
|
if csaManagerExists {
|
||||||
csaManager := managedFields[csaManagerIndex]
|
csaManager := managedFields[csaManagerIndex]
|
||||||
|
|
||||||
// Union the csa manager with the existing SSA manager
|
|
||||||
ssaFieldSet, err := fieldmanager.FieldsToSet(*ssaManager.FieldsV1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to convert fields to set: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
csaFieldSet, err := fieldmanager.FieldsToSet(*csaManager.FieldsV1)
|
csaFieldSet, err := fieldmanager.FieldsToSet(*csaManager.FieldsV1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert fields to set: %w", err)
|
return nil, fmt.Errorf("failed to convert fields to set: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
combinedFieldSet := ssaFieldSet.Union(&csaFieldSet)
|
combinedFieldSet = combinedFieldSet.Union(&csaFieldSet)
|
||||||
combinedFieldSetEncoded, err := fieldmanager.SetToFields(*combinedFieldSet)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to encode field set: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
managedFields[ssaManagerIndex].FieldsV1 = &combinedFieldSetEncoded
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure that the resultant fieldset does not include the
|
||||||
|
// last applied annotation
|
||||||
|
combinedFieldSet = combinedFieldSet.Difference(csaAnnotationFieldSet)
|
||||||
|
|
||||||
|
combinedFieldSetEncoded, err := fieldmanager.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.
|
||||||
//
|
//
|
||||||
// (find first index, since managed fields are sorted so that most recent is
|
// (find first index, since managed fields are sorted so that most recent is
|
||||||
// first in the list)
|
// first in the list)
|
||||||
csaManagerIndex, csaManagerExists := findFirstIndex(managedFields, func(entry metav1.ManagedFieldsEntry) bool {
|
csaManagerIndex, csaManagerExists := findFirstIndex(managedFields,
|
||||||
return entry.Manager == csaManagerName && entry.Operation == metav1.ManagedFieldsOperationUpdate && entry.Subresource == ""
|
func(entry metav1.ManagedFieldsEntry) bool {
|
||||||
})
|
return entry.Manager == csaManagerName &&
|
||||||
|
entry.Operation == metav1.ManagedFieldsOperationUpdate &&
|
||||||
|
entry.Subresource == ""
|
||||||
|
})
|
||||||
|
|
||||||
if !csaManagerExists {
|
if !csaManagerExists {
|
||||||
// There are no CSA managers that need to be converted. Nothing to do
|
// There are no CSA managers that need to be converted. Nothing to do
|
||||||
@ -98,7 +114,22 @@ func UpgradeManagedFields(
|
|||||||
return obj, nil
|
return obj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
csaManager := managedFields[csaManagerIndex]
|
||||||
|
csaFieldSet, err := fieldmanager.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 := fieldmanager.SetToFields(csaFieldSet)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to encode field set: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Convert the entry to apply operation
|
// 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
|
||||||
}
|
}
|
||||||
@ -116,6 +147,12 @@ func UpgradeManagedFields(
|
|||||||
return nil, fmt.Errorf("failed to get meta accessor for copied object: %w", err)
|
return nil, fmt.Errorf("failed to get meta accessor for copied object: %w", err)
|
||||||
}
|
}
|
||||||
copiedAccessor.SetManagedFields(filteredManagers)
|
copiedAccessor.SetManagedFields(filteredManagers)
|
||||||
|
|
||||||
|
// Wipe out CSA annotation if it exists
|
||||||
|
annotations := copiedAccessor.GetAnnotations()
|
||||||
|
delete(annotations, csaAnnotationName)
|
||||||
|
copiedAccessor.SetAnnotations(annotations)
|
||||||
|
|
||||||
return copied, nil
|
return copied, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,9 +58,7 @@ apiVersion: v1
|
|||||||
data: {}
|
data: {}
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations: {}
|
||||||
kubectl.kubernetes.io/last-applied-configuration: |
|
|
||||||
{"apiVersion":"v1","data":{"key":"value","legacy":"unused"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test","namespace":"default"}}
|
|
||||||
creationTimestamp: "2022-08-22T23:08:23Z"
|
creationTimestamp: "2022-08-22T23:08:23Z"
|
||||||
managedFields:
|
managedFields:
|
||||||
- apiVersion: v1
|
- apiVersion: v1
|
||||||
@ -71,9 +69,7 @@ metadata:
|
|||||||
f:key: {}
|
f:key: {}
|
||||||
f:legacy: {}
|
f:legacy: {}
|
||||||
f:metadata:
|
f:metadata:
|
||||||
f:annotations:
|
f:annotations: {}
|
||||||
.: {}
|
|
||||||
f:kubectl.kubernetes.io/last-applied-configuration: {}
|
|
||||||
manager: kubectl
|
manager: kubectl
|
||||||
operation: Apply
|
operation: Apply
|
||||||
time: "2022-08-22T23:08:23Z"
|
time: "2022-08-22T23:08:23Z"
|
||||||
@ -135,9 +131,7 @@ apiVersion: v1
|
|||||||
data: {}
|
data: {}
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations: {}
|
||||||
kubectl.kubernetes.io/last-applied-configuration: |
|
|
||||||
{"apiVersion":"v1","data":{"key":"value","legacy":"unused"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test","namespace":"default"}}
|
|
||||||
creationTimestamp: "2022-08-22T23:08:23Z"
|
creationTimestamp: "2022-08-22T23:08:23Z"
|
||||||
managedFields:
|
managedFields:
|
||||||
- apiVersion: v1
|
- apiVersion: v1
|
||||||
@ -148,9 +142,7 @@ metadata:
|
|||||||
f:key: {}
|
f:key: {}
|
||||||
f:legacy: {}
|
f:legacy: {}
|
||||||
f:metadata:
|
f:metadata:
|
||||||
f:annotations:
|
f:annotations: {}
|
||||||
.: {}
|
|
||||||
f:kubectl.kubernetes.io/last-applied-configuration: {}
|
|
||||||
manager: kubectl
|
manager: kubectl
|
||||||
operation: Apply
|
operation: Apply
|
||||||
time: "2022-08-23T23:08:23Z"
|
time: "2022-08-23T23:08:23Z"
|
||||||
@ -164,7 +156,7 @@ metadata:
|
|||||||
// CSA entry but no longer present in SSA entry, so it would not be pruned.
|
// CSA entry but no longer present in SSA entry, so it would not be pruned.
|
||||||
// This shows that upgrading such an object results in correct behavior next
|
// This shows that upgrading such an object results in correct behavior next
|
||||||
// time SSA applier
|
// time SSA applier
|
||||||
// Expect final object to have all keys from both entries
|
// Expect final object to have unioned keys from both entries
|
||||||
Name: "csa-combine-with-ssa-additional-keys",
|
Name: "csa-combine-with-ssa-additional-keys",
|
||||||
CSAManager: "kubectl-client-side-apply",
|
CSAManager: "kubectl-client-side-apply",
|
||||||
SSAManager: "kubectl",
|
SSAManager: "kubectl",
|
||||||
@ -213,9 +205,7 @@ apiVersion: v1
|
|||||||
data: {}
|
data: {}
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations: {}
|
||||||
kubectl.kubernetes.io/last-applied-configuration: |
|
|
||||||
{"apiVersion":"v1","data":{"key":"value","legacy":"unused"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test","namespace":"default"}}
|
|
||||||
creationTimestamp: "2022-08-22T23:08:23Z"
|
creationTimestamp: "2022-08-22T23:08:23Z"
|
||||||
managedFields:
|
managedFields:
|
||||||
- apiVersion: v1
|
- apiVersion: v1
|
||||||
@ -226,9 +216,7 @@ metadata:
|
|||||||
f:key: {}
|
f:key: {}
|
||||||
f:legacy: {}
|
f:legacy: {}
|
||||||
f:metadata:
|
f:metadata:
|
||||||
f:annotations:
|
f:annotations: {}
|
||||||
.: {}
|
|
||||||
f:kubectl.kubernetes.io/last-applied-configuration: {}
|
|
||||||
manager: kubectl
|
manager: kubectl
|
||||||
operation: Apply
|
operation: Apply
|
||||||
time: "2022-08-23T23:08:23Z"
|
time: "2022-08-23T23:08:23Z"
|
||||||
@ -319,9 +307,7 @@ apiVersion: v1
|
|||||||
data: {}
|
data: {}
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations: {}
|
||||||
kubectl.kubernetes.io/last-applied-configuration: |
|
|
||||||
{"apiVersion":"v1","data":{"key":"value","legacy":"unused"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test","namespace":"default"}}
|
|
||||||
creationTimestamp: "2022-08-22T23:08:23Z"
|
creationTimestamp: "2022-08-22T23:08:23Z"
|
||||||
managedFields:
|
managedFields:
|
||||||
- apiVersion: v5
|
- apiVersion: v5
|
||||||
@ -332,9 +318,7 @@ metadata:
|
|||||||
f:key: {}
|
f:key: {}
|
||||||
f:legacy: {}
|
f:legacy: {}
|
||||||
f:metadata:
|
f:metadata:
|
||||||
f:annotations:
|
f:annotations: {}
|
||||||
.: {}
|
|
||||||
f:kubectl.kubernetes.io/last-applied-configuration: {}
|
|
||||||
manager: kubectl
|
manager: kubectl
|
||||||
operation: Apply
|
operation: Apply
|
||||||
time: "2022-08-23T23:08:23Z"
|
time: "2022-08-23T23:08:23Z"
|
||||||
@ -425,9 +409,7 @@ apiVersion: v1
|
|||||||
data: {}
|
data: {}
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations: {}
|
||||||
kubectl.kubernetes.io/last-applied-configuration: |
|
|
||||||
{"apiVersion":"v1","data":{"key":"value","legacy":"unused"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test","namespace":"default"}}
|
|
||||||
creationTimestamp: "2022-08-22T23:08:23Z"
|
creationTimestamp: "2022-08-22T23:08:23Z"
|
||||||
managedFields:
|
managedFields:
|
||||||
- apiVersion: v5
|
- apiVersion: v5
|
||||||
@ -442,7 +424,6 @@ metadata:
|
|||||||
f:annotations:
|
f:annotations:
|
||||||
.: {}
|
.: {}
|
||||||
f:hello2: {}
|
f:hello2: {}
|
||||||
f:kubectl.kubernetes.io/last-applied-configuration: {}
|
|
||||||
manager: kubectl
|
manager: kubectl
|
||||||
operation: Apply
|
operation: Apply
|
||||||
time: "2022-08-23T23:08:23Z"
|
time: "2022-08-23T23:08:23Z"
|
||||||
|
Loading…
Reference in New Issue
Block a user