Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set a field manager name #2271

Merged
merged 4 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Add `PULUMI_K8S_ENABLE_PATCH_FORCE` env var support (https://github.com/pulumi/pulumi-kubernetes/pulls/2260)
- Add link to resolution guide for SSA conflicts (https://github.com/pulumi/pulumi-kubernetes/pulls/2265)
- Always set a field manager name to avoid conflicts in Client-Side Apply mode (https://github.com/pulumi/pulumi-kubernetes/pulls/2271)

## 3.23.0 (December 8, 2022)

Expand Down
8 changes: 6 additions & 2 deletions provider/pkg/await/await.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ func Creation(c CreateConfig) (*unstructured.Unstructured, error) {
ssaConflictDocLink, err)
}
} else {
var options metav1.CreateOptions
options := metav1.CreateOptions{
FieldManager: c.FieldManager,
}
if c.Preview {
options.DryRun = []string{metav1.DryRunAll}
}
Expand Down Expand Up @@ -452,7 +454,9 @@ func Update(c UpdateConfig) (*unstructured.Unstructured, error) {
return nil, err
}

var options metav1.PatchOptions
options := metav1.PatchOptions{
FieldManager: c.FieldManager,
}
if c.Preview {
options.DryRun = []string{metav1.DryRunAll}
}
Expand Down
66 changes: 37 additions & 29 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ func (k *kubeProvider) Diff(ctx context.Context, req *pulumirpc.DiffRequest) (*p
newInputs.GetNamespace(), newInputs.GetName())
}

fieldManager := fieldManagerName(nil, newResInputs, newInputs)
fieldManager := k.fieldManagerName(nil, newResInputs, newInputs)

// Try to compute a server-side patch.
ssPatch, ssPatchBase, ssPatchOk, err := k.tryServerSidePatch(oldInputs, newInputs, gvk, fieldManager)
Expand Down Expand Up @@ -1749,7 +1749,7 @@ func (k *kubeProvider) Create(
}

initialAPIVersion := newInputs.GetAPIVersion()
fieldManager := fieldManagerName(nil, newResInputs, newInputs)
fieldManager := k.fieldManagerName(nil, newResInputs, newInputs)

if k.yamlRenderMode {
if newResInputs.ContainsSecrets() {
Expand Down Expand Up @@ -1974,7 +1974,7 @@ func (k *kubeProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*p
if err != nil {
return nil, err
}
fieldManager := fieldManagerName(nil, oldState, oldInputs)
fieldManager := k.fieldManagerName(nil, oldState, oldInputs)

if k.yamlRenderMode {
// Return a new "checkpoint object".
Expand Down Expand Up @@ -2243,8 +2243,8 @@ func (k *kubeProvider) Update(
return nil, err
}

fieldManagerOld := fieldManagerName(nil, oldState, oldInputs)
fieldManager := fieldManagerName(nil, oldState, newInputs)
fieldManagerOld := k.fieldManagerName(nil, oldState, oldInputs)
fieldManager := k.fieldManagerName(nil, oldState, newInputs)

if k.yamlRenderMode {
if newResInputs.ContainsSecrets() {
Expand Down Expand Up @@ -2421,7 +2421,7 @@ func (k *kubeProvider) Delete(ctx context.Context, req *pulumirpc.DeleteRequest)
if err != nil {
return nil, err
}
fieldManager := fieldManagerName(nil, oldState, oldInputs)
fieldManager := k.fieldManagerName(nil, oldState, oldInputs)
resources, err := k.getResources()
if err != nil {
return nil, pkgerrors.Wrapf(err, "Failed to fetch OpenAPI schema from the API server")
Expand Down Expand Up @@ -2818,6 +2818,37 @@ func (k *kubeProvider) withLastAppliedConfig(config *unstructured.Unstructured)
return config, nil
}

// fieldManagerName returns the name to use for the Server-Side Apply fieldManager. The values are looked up with the
// following precedence:
// 1. Resource annotation (this will likely change to a typed option field in the next major release)
// 2. Value from the Pulumi state
// 3. Randomly generated name
func (k *kubeProvider) fieldManagerName(
randomSeed []byte, state resource.PropertyMap, inputs *unstructured.Unstructured,
) string {
// Always use the same fieldManager name for Client-Side Apply mode to avoid conflicts based on the name of the
// provider executable.
if !k.serverSideApplyMode {
return "pulumi-kubernetes"
}

if v := metadata.GetAnnotationValue(inputs, metadata.AnnotationPatchFieldManager); len(v) > 0 {
return v
}
if v, ok := state[fieldManagerKey]; ok {
return v.StringValue()
}

prefix := "pulumi-kubernetes-"
// This function is called from other provider function apart from Check and so doesn't have a randomSeed
// for those calls, but the field manager name should have already been filled in via Check so this case
// shouldn't actually get hit.
fieldManager, err := resource.NewUniqueName(randomSeed, prefix, 0, 0, nil)
contract.AssertNoError(err)

return fieldManager
}

func mapReplStripSecrets(v resource.PropertyValue) (interface{}, bool) {
if v.IsSecret() {
return v.SecretValue().Element.MapRepl(nil, mapReplStripSecrets), true
Expand Down Expand Up @@ -2898,29 +2929,6 @@ func initialAPIVersion(state resource.PropertyMap, oldConfig *unstructured.Unstr
return oldConfig.GetAPIVersion(), nil
}

// fieldManagerName returns the name to use for the Server-Side Apply fieldManager. The values are looked up with the
// following precedence:
// 1. Resource annotation (this will likely change to a typed option field in the next major release)
// 2. Value from the Pulumi state
// 3. Randomly generated name
func fieldManagerName(randomSeed []byte, state resource.PropertyMap, inputs *unstructured.Unstructured) string {
if v := metadata.GetAnnotationValue(inputs, metadata.AnnotationPatchFieldManager); len(v) > 0 {
return v
}
if v, ok := state[fieldManagerKey]; ok {
return v.StringValue()
}

prefix := "pulumi-kubernetes-"
// This function is called from other provider function apart from Check and so doesn't have a randomSeed
// for those calls, but the field manager name should have already been filled in via Check so this case
// shouldn't actually get hit.
fieldManager, err := resource.NewUniqueName(randomSeed, prefix, 0, 0, nil)
contract.AssertNoError(err)

return fieldManager
}

func checkpointObject(inputs, live *unstructured.Unstructured, fromInputs resource.PropertyMap,
initialAPIVersion, fieldManager string) resource.PropertyMap {
object := resource.NewPropertyMapFromMap(live.Object)
Expand Down