Repository: helm/helm
File: pkg/kube/client.go (Go)

Code before:
				return fmt.Errorf("could not get information about the resource: %w", err)
			}

			// Append the created resource to the results, even if something fails
			res.Created = append(res.Created, target)

			// Since the resource does not exist, create it.
			if err := createResource(target); err != nil {
				return fmt.Errorf("failed to create resource: %w", err)
			}

			kind := target.Mapping.GroupVersionKind.Kind
			slog.Debug("created a new resource", "namespace", target.Namespace, "name", target.Name, "kind", kind)
			return nil
		}

		original := originals.Get(target)
		if original == nil {
			kind := target.Mapping.GroupVersionKind.Kind

			slog.Warn("resource exists on cluster but not in original release, using cluster state as baseline",
				"namespace", target.Namespace, "name", target.Name, "kind", kind)

			currentObj, err := helper.Get(target.Namespace, target.Name)
			if err != nil {
				return fmt.Errorf("failed to get current resource state: %w", err)
			}

			// Create a temporary Info with the current cluster state to use as "original"
			currentInfo := &resource.Info{
				Client:    target.Client,
				Mapping:   target.Mapping,
				Namespace: target.Namespace,
				Name:      target.Name,
				Object:    currentObj,
			}

			if err := updateApplyFunc(currentInfo, target); err != nil {
				updateErrors = append(updateErrors, err)
			}

			// Because we check for errors later, append the info regardless
			res.Updated = append(res.Updated, target)

			return nil
		}

		if err := updateApplyFunc(original, target); err != nil {
			updateErrors = append(updateErrors, err)
		}


Review comment (bug):
I think this error doesn't give context for the problem. Maybe the original error here? `return fmt.Errorf("original object %s with the name %q not found", kind, target.Name)`

Code after:
				return fmt.Errorf("could not get information about the resource: %w", err)
			}

			// Append the created resource to the results, even if something fails
			res.Created = append(res.Created, target)

			// Since the resource does not exist, create it.
			if err := createResource(target); err != nil {
				return fmt.Errorf("failed to create resource: %w", err)
			}

			kind := target.Mapping.GroupVersionKind.Kind
			slog.Debug("created a new resource", "namespace", target.Namespace, "name", target.Name, "kind", kind)
			return nil
		}

		original := originals.Get(target)
		if original == nil {
			kind := target.Mapping.GroupVersionKind.Kind

			slog.Warn("resource exists on cluster but not in original release, using cluster state as baseline",
				"namespace", target.Namespace, "name", target.Name, "kind", kind)

			currentObj, err := helper.Get(target.Namespace, target.Name)
			if err != nil {
				return fmt.Errorf("original object %s with the name %q not found", kind, target.Name)
			}

			// Create a temporary Info with the current cluster state to use as "original"
			currentInfo := &resource.Info{
				Client:    target.Client,
				Mapping:   target.Mapping,
				Namespace: target.Namespace,
				Name:      target.Name,
				Object:    currentObj,
			}

			if err := updateApplyFunc(currentInfo, target); err != nil {
				updateErrors = append(updateErrors, err)
			}

			// Because we check for errors later, append the info regardless
			res.Updated = append(res.Updated, target)

			return nil
		}

		if err := updateApplyFunc(original, target); err != nil {
			updateErrors = append(updateErrors, err)
		}