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

Code before:
		}

		helper := resource.NewHelper(target.Client, target.Mapping).WithFieldManager(getManagedFieldsManager())
		if _, err := helper.Get(target.Namespace, target.Name); err != nil {
			if !apierrors.IsNotFound(err) {
				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.Debug("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
		}

Review comment (bug):
Giving the new behavior we should maybe warn about this instead of only printing on debug.

Code after:
		}

		helper := resource.NewHelper(target.Client, target.Mapping).WithFieldManager(getManagedFieldsManager())
		if _, err := helper.Get(target.Namespace, target.Name); err != nil {
			if !apierrors.IsNotFound(err) {
				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
		}