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 19, 2023
1 parent 5f9eaba commit 492ac6b
Showing 1 changed file with 35 additions and 54 deletions.
89 changes: 35 additions & 54 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 @@ -2788,39 +2785,6 @@ func (k *kubeProvider) tryServerSidePatch(
return nil, nil, false, err
}

func (k *kubeProvider) withLastAppliedConfig(config *unstructured.Unstructured) (*unstructured.Unstructured, error) {
if k.serverSideApplyMode || k.supportsDryRun(config.GroupVersionKind()) {
// Skip last-applied-config annotation if the resource supports server-side apply.
return config, nil
}

// CRDs are updated using a separate mechanism, so skip the last-applied-configuration annotation, and delete it
// if it was present from a previous update.
if clients.IsCRD(config) {
// Deep copy the config before returning.
config = config.DeepCopy()

annotations := getAnnotations(config)
delete(annotations, lastAppliedConfigKey)
config.SetAnnotations(annotations)
return config, nil
}

// 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
}

// 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)
Expand Down Expand Up @@ -2932,6 +2896,23 @@ func initialAPIVersion(state resource.PropertyMap, oldConfig *unstructured.Unstr
return oldConfig.GetAPIVersion(), nil
}

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 {

Expand Down

0 comments on commit 492ac6b

Please sign in to comment.