2022-07-18 13:36:31 +00:00
|
|
|
// Copyright 2016-2022, Pulumi Corporation.
|
2018-05-22 19:43:36 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2017-02-28 23:43:46 +00:00
|
|
|
|
2017-10-16 19:04:35 +00:00
|
|
|
package stack
|
2017-02-28 23:43:46 +00:00
|
|
|
|
|
|
|
import (
|
2022-07-18 13:36:31 +00:00
|
|
|
"context"
|
2017-10-28 02:42:17 +00:00
|
|
|
"encoding/json"
|
2021-11-13 02:37:17 +00:00
|
|
|
"fmt"
|
2018-02-06 17:57:32 +00:00
|
|
|
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/secrets"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype/migrate"
|
2022-05-23 19:13:21 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/encoding"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
2017-02-28 23:43:46 +00:00
|
|
|
)
|
|
|
|
|
2022-05-23 19:13:21 +00:00
|
|
|
func UnmarshalVersionedCheckpointToLatestCheckpoint(m encoding.Marshaler, bytes []byte) (*apitype.CheckpointV3, error) {
|
2018-03-08 18:52:05 +00:00
|
|
|
var versionedCheckpoint apitype.VersionedCheckpoint
|
2022-05-23 19:13:21 +00:00
|
|
|
// Here we are careful to unmarshal `bytes` with the provided unmarshaller `m`.
|
|
|
|
if err := m.Unmarshal(bytes, &versionedCheckpoint); err != nil {
|
|
|
|
return nil, fmt.Errorf("place 1: %w", err)
|
2017-10-28 02:42:17 +00:00
|
|
|
}
|
2018-03-08 18:52:05 +00:00
|
|
|
|
|
|
|
switch versionedCheckpoint.Version {
|
|
|
|
case 0:
|
|
|
|
// The happens when we are loading a checkpoint file from before we started to version things. Go's
|
|
|
|
// json package did not support strict marshalling before 1.10, and we use 1.9 in our toolchain today.
|
|
|
|
// After we upgrade, we could consider rewriting this code to use DisallowUnknownFields() on the decoder
|
|
|
|
// to have the old checkpoint not even deserialize as an apitype.VersionedCheckpoint.
|
2018-08-03 21:06:00 +00:00
|
|
|
var v1checkpoint apitype.CheckpointV1
|
2022-05-23 19:13:21 +00:00
|
|
|
if err := m.Unmarshal(bytes, &v1checkpoint); err != nil {
|
2018-03-08 18:52:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-20 20:31:41 +00:00
|
|
|
|
Implement more precise delete-before-replace semantics. (#2369)
This implements the new algorithm for deciding which resources must be
deleted due to a delete-before-replace operation.
We need to compute the set of resources that may be replaced by a
change to the resource under consideration. We do this by taking the
complete set of transitive dependents on the resource under
consideration and removing any resources that would not be replaced by
changes to their dependencies. We determine whether or not a resource
may be replaced by substituting unknowns for input properties that may
change due to deletion of the resources their value depends on and
calling the resource provider's Diff method.
This is perhaps clearer when described by example. Consider the
following dependency graph:
A
__|__
B C
| _|_
D E F
In this graph, all of B, C, D, E, and F transitively depend on A. It may
be the case, however, that changes to the specific properties of any of
those resources R that would occur if a resource on the path to A were
deleted and recreated may not cause R to be replaced. For example, the
edge from B to A may be a simple dependsOn edge such that a change to
B does not actually influence any of B's input properties. In that case,
neither B nor D would need to be deleted before A could be deleted.
In order to make the above algorithm a reality, the resource monitor
interface has been updated to include a map that associates an input
property key with the list of resources that input property depends on.
Older clients of the resource monitor will leave this map empty, in
which case all input properties will be treated as depending on all
dependencies of the resource. This is probably overly conservative, but
it is less conservative than what we currently implement, and is
certainly correct.
2019-01-28 17:46:30 +00:00
|
|
|
v2checkpoint := migrate.UpToCheckpointV2(v1checkpoint)
|
|
|
|
v3checkpoint := migrate.UpToCheckpointV3(v2checkpoint)
|
|
|
|
return &v3checkpoint, nil
|
2018-03-08 18:52:05 +00:00
|
|
|
case 1:
|
2018-08-03 21:06:00 +00:00
|
|
|
var v1checkpoint apitype.CheckpointV1
|
|
|
|
if err := json.Unmarshal(versionedCheckpoint.Checkpoint, &v1checkpoint); err != nil {
|
2018-03-08 18:52:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-20 20:31:41 +00:00
|
|
|
|
Implement more precise delete-before-replace semantics. (#2369)
This implements the new algorithm for deciding which resources must be
deleted due to a delete-before-replace operation.
We need to compute the set of resources that may be replaced by a
change to the resource under consideration. We do this by taking the
complete set of transitive dependents on the resource under
consideration and removing any resources that would not be replaced by
changes to their dependencies. We determine whether or not a resource
may be replaced by substituting unknowns for input properties that may
change due to deletion of the resources their value depends on and
calling the resource provider's Diff method.
This is perhaps clearer when described by example. Consider the
following dependency graph:
A
__|__
B C
| _|_
D E F
In this graph, all of B, C, D, E, and F transitively depend on A. It may
be the case, however, that changes to the specific properties of any of
those resources R that would occur if a resource on the path to A were
deleted and recreated may not cause R to be replaced. For example, the
edge from B to A may be a simple dependsOn edge such that a change to
B does not actually influence any of B's input properties. In that case,
neither B nor D would need to be deleted before A could be deleted.
In order to make the above algorithm a reality, the resource monitor
interface has been updated to include a map that associates an input
property key with the list of resources that input property depends on.
Older clients of the resource monitor will leave this map empty, in
which case all input properties will be treated as depending on all
dependencies of the resource. This is probably overly conservative, but
it is less conservative than what we currently implement, and is
certainly correct.
2019-01-28 17:46:30 +00:00
|
|
|
v2checkpoint := migrate.UpToCheckpointV2(v1checkpoint)
|
|
|
|
v3checkpoint := migrate.UpToCheckpointV3(v2checkpoint)
|
|
|
|
return &v3checkpoint, nil
|
2018-08-03 21:06:00 +00:00
|
|
|
case 2:
|
|
|
|
var v2checkpoint apitype.CheckpointV2
|
|
|
|
if err := json.Unmarshal(versionedCheckpoint.Checkpoint, &v2checkpoint); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
Implement more precise delete-before-replace semantics. (#2369)
This implements the new algorithm for deciding which resources must be
deleted due to a delete-before-replace operation.
We need to compute the set of resources that may be replaced by a
change to the resource under consideration. We do this by taking the
complete set of transitive dependents on the resource under
consideration and removing any resources that would not be replaced by
changes to their dependencies. We determine whether or not a resource
may be replaced by substituting unknowns for input properties that may
change due to deletion of the resources their value depends on and
calling the resource provider's Diff method.
This is perhaps clearer when described by example. Consider the
following dependency graph:
A
__|__
B C
| _|_
D E F
In this graph, all of B, C, D, E, and F transitively depend on A. It may
be the case, however, that changes to the specific properties of any of
those resources R that would occur if a resource on the path to A were
deleted and recreated may not cause R to be replaced. For example, the
edge from B to A may be a simple dependsOn edge such that a change to
B does not actually influence any of B's input properties. In that case,
neither B nor D would need to be deleted before A could be deleted.
In order to make the above algorithm a reality, the resource monitor
interface has been updated to include a map that associates an input
property key with the list of resources that input property depends on.
Older clients of the resource monitor will leave this map empty, in
which case all input properties will be treated as depending on all
dependencies of the resource. This is probably overly conservative, but
it is less conservative than what we currently implement, and is
certainly correct.
2019-01-28 17:46:30 +00:00
|
|
|
v3checkpoint := migrate.UpToCheckpointV3(v2checkpoint)
|
|
|
|
return &v3checkpoint, nil
|
|
|
|
case 3:
|
|
|
|
var v3checkpoint apitype.CheckpointV3
|
|
|
|
if err := json.Unmarshal(versionedCheckpoint.Checkpoint, &v3checkpoint); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v3checkpoint, nil
|
2018-03-08 18:52:05 +00:00
|
|
|
default:
|
2021-11-13 02:37:17 +00:00
|
|
|
return nil, fmt.Errorf("unsupported checkpoint version %d", versionedCheckpoint.Version)
|
2018-03-08 18:52:05 +00:00
|
|
|
}
|
2017-10-28 02:42:17 +00:00
|
|
|
}
|
2017-02-28 23:43:46 +00:00
|
|
|
|
2023-01-11 11:24:10 +00:00
|
|
|
func MarshalUntypedDeploymentToVersionedCheckpoint(
|
2023-02-10 12:24:28 +00:00
|
|
|
stack tokens.QName, deployment *apitype.UntypedDeployment,
|
2023-03-03 16:36:39 +00:00
|
|
|
) (*apitype.VersionedCheckpoint, error) {
|
2023-01-11 11:24:10 +00:00
|
|
|
chk := struct {
|
|
|
|
Stack tokens.QName
|
|
|
|
Latest json.RawMessage
|
|
|
|
}{
|
2023-02-10 12:24:28 +00:00
|
|
|
Stack: stack,
|
2023-01-11 11:24:10 +00:00
|
|
|
Latest: deployment.Deployment,
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := encoding.JSON.Marshal(chk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshalling checkpoint: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apitype.VersionedCheckpoint{
|
|
|
|
Version: deployment.Version,
|
|
|
|
Checkpoint: bytes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-10-28 02:42:17 +00:00
|
|
|
// SerializeCheckpoint turns a snapshot into a data structure suitable for serialization.
|
2023-02-10 12:24:28 +00:00
|
|
|
func SerializeCheckpoint(stack tokens.QName, snap *deploy.Snapshot,
|
2024-03-25 10:30:14 +00:00
|
|
|
showSecrets bool,
|
2023-03-03 16:36:39 +00:00
|
|
|
) (*apitype.VersionedCheckpoint, error) {
|
2017-02-28 23:43:46 +00:00
|
|
|
// If snap is nil, that's okay, we will just create an empty deployment; otherwise, serialize the whole snapshot.
|
Implement more precise delete-before-replace semantics. (#2369)
This implements the new algorithm for deciding which resources must be
deleted due to a delete-before-replace operation.
We need to compute the set of resources that may be replaced by a
change to the resource under consideration. We do this by taking the
complete set of transitive dependents on the resource under
consideration and removing any resources that would not be replaced by
changes to their dependencies. We determine whether or not a resource
may be replaced by substituting unknowns for input properties that may
change due to deletion of the resources their value depends on and
calling the resource provider's Diff method.
This is perhaps clearer when described by example. Consider the
following dependency graph:
A
__|__
B C
| _|_
D E F
In this graph, all of B, C, D, E, and F transitively depend on A. It may
be the case, however, that changes to the specific properties of any of
those resources R that would occur if a resource on the path to A were
deleted and recreated may not cause R to be replaced. For example, the
edge from B to A may be a simple dependsOn edge such that a change to
B does not actually influence any of B's input properties. In that case,
neither B nor D would need to be deleted before A could be deleted.
In order to make the above algorithm a reality, the resource monitor
interface has been updated to include a map that associates an input
property key with the list of resources that input property depends on.
Older clients of the resource monitor will leave this map empty, in
which case all input properties will be treated as depending on all
dependencies of the resource. This is probably overly conservative, but
it is less conservative than what we currently implement, and is
certainly correct.
2019-01-28 17:46:30 +00:00
|
|
|
var latest *apitype.DeploymentV3
|
2017-02-28 23:43:46 +00:00
|
|
|
if snap != nil {
|
Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929)
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
SerializePropertyValue needed a `context.Context` object to pass to the
`config.Encrypter`. It was using `context.TODO()`, this change instead
accepts a context on the parameters and lifts that up to
SerializeProperties, SerializeResource, SerializeOperation, and
SerializeDeployment.
There were a few call sites for those methods that already had a context
on hand, and they now pass that context. The other calls sites now use
`context.TODO()`, we should continue to iterate in this area to ensure
everywhere that needs a context has one passed in.
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-04-15 07:45:46 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
dep, err := SerializeDeployment(ctx, snap, showSecrets)
|
2019-04-17 20:48:38 +00:00
|
|
|
if err != nil {
|
2021-11-13 02:37:17 +00:00
|
|
|
return nil, fmt.Errorf("serializing deployment: %w", err)
|
2019-04-17 20:48:38 +00:00
|
|
|
}
|
|
|
|
latest = dep
|
2017-02-28 23:43:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 12:27:34 +00:00
|
|
|
b, err := encoding.JSON.Marshal(apitype.CheckpointV3{
|
2023-02-10 12:24:28 +00:00
|
|
|
Stack: stack,
|
2017-02-28 23:43:46 +00:00
|
|
|
Latest: latest,
|
2018-03-08 18:52:05 +00:00
|
|
|
})
|
2019-04-17 20:48:38 +00:00
|
|
|
if err != nil {
|
2021-11-13 02:37:17 +00:00
|
|
|
return nil, fmt.Errorf("marshalling checkpoint: %w", err)
|
2019-04-17 20:48:38 +00:00
|
|
|
}
|
2018-03-08 18:52:05 +00:00
|
|
|
|
|
|
|
return &apitype.VersionedCheckpoint{
|
2018-05-25 20:29:59 +00:00
|
|
|
Version: apitype.DeploymentSchemaVersionCurrent,
|
2018-03-08 18:52:05 +00:00
|
|
|
Checkpoint: json.RawMessage(b),
|
2019-04-17 20:48:38 +00:00
|
|
|
}, nil
|
2017-02-28 23:43:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 21:06:00 +00:00
|
|
|
// DeserializeCheckpoint takes a serialized deployment record and returns its associated snapshot. Returns nil
|
|
|
|
// if there have been no deployments performed on this checkpoint.
|
2022-07-18 13:36:31 +00:00
|
|
|
func DeserializeCheckpoint(
|
|
|
|
ctx context.Context,
|
2023-01-11 13:47:59 +00:00
|
|
|
secretsProvider secrets.Provider,
|
2023-03-03 16:36:39 +00:00
|
|
|
chkpoint *apitype.CheckpointV3,
|
|
|
|
) (*deploy.Snapshot, error) {
|
2023-02-17 18:46:23 +00:00
|
|
|
contract.Requiref(chkpoint != nil, "chkpoint", "must not be nil")
|
2018-08-03 21:06:00 +00:00
|
|
|
if chkpoint.Latest != nil {
|
2023-01-09 19:10:46 +00:00
|
|
|
return DeserializeDeploymentV3(ctx, *chkpoint.Latest, secretsProvider)
|
2017-02-28 23:43:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 21:06:00 +00:00
|
|
|
return nil, nil
|
2017-02-28 23:43:46 +00:00
|
|
|
}
|
2017-12-14 00:09:14 +00:00
|
|
|
|
2019-04-19 17:41:08 +00:00
|
|
|
// GetRootStackResource returns the root stack resource from a given snapshot, or nil if not found.
|
|
|
|
func GetRootStackResource(snap *deploy.Snapshot) (*resource.State, error) {
|
2017-12-14 00:09:14 +00:00
|
|
|
if snap != nil {
|
|
|
|
for _, res := range snap.Resources {
|
2023-12-04 10:36:51 +00:00
|
|
|
if res.Type == resource.RootStackType && res.Parent == "" {
|
2019-04-19 17:41:08 +00:00
|
|
|
return res, nil
|
2017-12-14 00:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 17:41:08 +00:00
|
|
|
return nil, nil
|
2017-12-14 00:09:14 +00:00
|
|
|
}
|
2024-06-28 09:43:19 +00:00
|
|
|
|
|
|
|
// CreateRootStackResource creates a new root stack resource with the given name
|
|
|
|
func CreateRootStackResource(stackName tokens.QName, projectName tokens.PackageName) *resource.State {
|
|
|
|
typ, name := resource.RootStackType, fmt.Sprintf("%s-%s", projectName, stackName)
|
|
|
|
urn := resource.NewURN(stackName, projectName, "", typ, name)
|
|
|
|
state := resource.NewState(typ, urn, false, false, "", resource.PropertyMap{}, nil, "", false, false, nil, nil, "",
|
|
|
|
nil, false, nil, nil, nil, "", false, "", nil, nil, "", nil)
|
|
|
|
return state
|
|
|
|
}
|