Skip to content

Commit

Permalink
Revert "Elide last applied configuration annotation when SSA is suppo…
Browse files Browse the repository at this point in the history
…rted (pulumi#1863)"

This reverts commit 11e356e.
  • Loading branch information
kaz130 committed Jun 12, 2023
1 parent 5f9eaba commit 87add19
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Note: The `kubernetes:storage.k8s.io/v1alpha1:CSIStorageCapacity` API was remove

- Relax ingress await restrictions (https://github.com/pulumi/pulumi-kubernetes/pull/1832)
- Exclude nil entries from values (https://github.com/pulumi/pulumi-kubernetes/pull/1845)
- Populate inputs from live state for imports (https://github.com/pulumi/pulumi-kubernetes/pull/1846)

## 3.12.1 (December 9, 2021)

Expand Down
61 changes: 38 additions & 23 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ func (k *kubeProvider) Create(
return &pulumirpc.CreateResponse{Id: "", Properties: req.GetProperties()}, nil
}

annotatedInputs, err := k.withLastAppliedConfig(newInputs)
annotatedInputs, err := withLastAppliedConfig(newInputs)
if err != nil {
return nil, pkgerrors.Wrapf(
err, "Failed to create resource %s/%s because of an error generating the %s value in "+
Expand Down Expand Up @@ -1943,14 +1943,7 @@ func (k *kubeProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*p
return nil, err
}

namespace, name := parseFqName(req.GetId())
if name == "" {
return nil, fmt.Errorf(
"failed to read resource because of a failure to parse resource name from request ID: %s",
req.GetId())
}

freshImport := false
noOldInputs := false
oldInputs, oldLive := parseCheckpointObject(oldState)
if oldInputs.GroupVersionKind().Empty() {
if oldLive.GroupVersionKind().Empty() {
Expand All @@ -1959,18 +1952,23 @@ func (k *kubeProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*p
return nil, err
}
oldInputs.SetGroupVersionKind(gvk)
freshImport = true
noOldInputs = true
} else {
oldInputs.SetGroupVersionKind(oldLive.GroupVersionKind())
}
}

if oldInputs.GetName() == "" {
oldInputs.SetName(name)
}

if oldInputs.GetNamespace() == "" {
oldInputs.SetNamespace(namespace)
}
namespace, name := parseFqName(req.GetId())
if name == "" {
return nil, fmt.Errorf(
"failed to read resource because of a failure to parse resource name from request ID: %s",
req.GetId())
}
if oldInputs.GetName() == "" {
oldInputs.SetName(name)
}
if oldInputs.GetNamespace() == "" {
oldInputs.SetNamespace(namespace)
}

initialAPIVersion, err := initialAPIVersion(oldState, oldInputs)
Expand Down Expand Up @@ -2054,8 +2052,7 @@ func (k *kubeProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*p

// Attempt to parse the inputs for this object. If parsing was unsuccessful, retain the old inputs.
liveInputs := parseLiveInputs(liveObj, oldInputs)

if freshImport {
if noOldInputs {
// If no previous inputs were known, this is a fresh import. In which case we want to populate
// the inputs from the live state for the resource by referring to the input properties for the resource.
pkgSpec := pulumischema.PackageSpec{}
Expand All @@ -2077,7 +2074,7 @@ func (k *kubeProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*p
unstructured.RemoveNestedField(liveInputs.Object, "metadata", "managedFields")
unstructured.RemoveNestedField(liveInputs.Object, "metadata", "resourceVersion")
unstructured.RemoveNestedField(liveInputs.Object, "metadata", "uid")
unstructured.RemoveNestedField(liveInputs.Object, "metadata", "annotations", lastAppliedConfigKey)
unstructured.RemoveNestedField(liveInputs.Object, "metadata", "annotations", "kubectl.kubernetes.io/last-applied-configuration")
}

// TODO(lblackstone): not sure why this is needed
Expand Down Expand Up @@ -2233,7 +2230,7 @@ func (k *kubeProvider) Update(
// Ignore old state; we'll get it from Kubernetes later.
oldInputs, _ := parseCheckpointObject(oldState)

annotatedInputs, err := k.withLastAppliedConfig(newInputs)
annotatedInputs, err := withLastAppliedConfig(newInputs)
if err != nil {
return nil, pkgerrors.Wrapf(
err, "Failed to update resource %s/%s because of an error generating the %s value in "+
Expand Down Expand Up @@ -2827,7 +2824,9 @@ func (k *kubeProvider) withLastAppliedConfig(config *unstructured.Unstructured)
// 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.
Expand Down Expand Up @@ -2932,9 +2931,25 @@ func initialAPIVersion(state resource.PropertyMap, oldConfig *unstructured.Unstr
return oldConfig.GetAPIVersion(), nil
}

func checkpointObject(inputs, live *unstructured.Unstructured, fromInputs resource.PropertyMap,
initialAPIVersion, fieldManager string) resource.PropertyMap {
func withLastAppliedConfig(config *unstructured.Unstructured) (*unstructured.Unstructured, error) {
// Serialize the inputs and add the last-applied-configuration annotation.
marshaled, err := config.MarshalJSON()
if err != nil {
return nil, err
}

// Deep copy the config before returning.
config = config.DeepCopy()

annotations := getAnnotations(config)

annotations[lastAppliedConfigKey] = string(marshaled)
config.SetAnnotations(annotations)
return config, nil
}

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

Expand Down
13 changes: 13 additions & 0 deletions tests/sdk/nodejs/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,19 @@ func TestDeploymentRollout(t *testing.T) {
integration.ProgramTest(t, &test)
}

func TestDryRun(t *testing.T) {
test := baseOptions.With(integration.ProgramTestOptions{
Dir: filepath.Join("dry-run", "step1"),
EditDirs: []integration.EditDir{
{
Dir: filepath.Join("dry-run", "step2"),
Additive: true,
},
},
})
integration.ProgramTest(t, &test)
}

func TestEmptyArray(t *testing.T) {
test := baseOptions.With(integration.ProgramTestOptions{
Dir: filepath.Join("empty-array", "step1"),
Expand Down

0 comments on commit 87add19

Please sign in to comment.