Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle different namespaces for server-side diff #1631

Merged
merged 5 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Update pulumi dependencies v3.5.1 (https://github.com/pulumi/pulumi-kubernetes/pull/1623)
- Skip cluster connectivity check in yamlRenderMode (https://github.com/pulumi/pulumi-kubernetes/pull/1629)
- Handle different namespaces for server-side diff (https://github.com/pulumi/pulumi-kubernetes/pull/1631)

## 3.4.0 (June 17, 2021)

Expand Down
17 changes: 14 additions & 3 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2201,14 +2201,25 @@ func (k *kubeProvider) serverSidePatch(
var newObject *unstructured.Unstructured
_, err = client.Get(context.TODO(), newInputs.GetName(), metav1.GetOptions{})
switch {
case err == nil:
newObject, err = client.Patch(context.TODO(), newInputs.GetName(), patchType, patch, metav1.PatchOptions{
case errors.IsNotFound(err):
newObject, err = client.Create(context.TODO(), newInputs, metav1.CreateOptions{
DryRun: []string{metav1.DryRunAll},
})
case errors.IsNotFound(err):
case newInputs.GetNamespace() != oldInputs.GetNamespace():
client, err := k.clientSet.ResourceClient(newInputs.GroupVersionKind(), newInputs.GetNamespace())
if err != nil {
return nil, nil, err
}
newObject, err = client.Create(context.TODO(), newInputs, metav1.CreateOptions{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just trying to wrap my head around this... What happens with the object in the old namespace? Don't we need to delete it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method just computes the patch. The resource replacement logic happens separately from this.

DryRun: []string{metav1.DryRunAll},
})
if err != nil {
return nil, nil, err
}
case err == nil:
newObject, err = client.Patch(context.TODO(), newInputs.GetName(), patchType, patch, metav1.PatchOptions{
DryRun: []string{metav1.DryRunAll},
})
default:
return nil, nil, err
}
Expand Down
17 changes: 11 additions & 6 deletions tests/sdk/nodejs/dry-run/step1/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2019, Pulumi Corporation.
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -13,13 +13,18 @@
// limitations under the License.

import * as k8s from "@pulumi/kubernetes";
import * as random from "@pulumi/random";
import * as pulumi from "@pulumi/pulumi";

const appLabels = { app: "nginx" };
// TODO(levi): Use auto-naming for namespace once https://github.com/pulumi/pulumi-kubernetes/issues/1632 is fixed.
const suffix = new random.RandomString("suffix", {length: 7, special: false, upper: false}).result;
const ns = new k8s.core.v1.Namespace("test", {metadata: {name: pulumi.interpolate`test-${suffix}`}});
const provider = new k8s.Provider("k8s", {enableDryRun: true, namespace: ns.metadata.name});

const appLabels = { app: "nginx" };
const deployment = new k8s.apps.v1.Deployment("nginx", {
spec: {
selector: { matchLabels: appLabels },
replicas: 1,
template: {
metadata: { labels: appLabels },
spec: {
Expand All @@ -28,14 +33,14 @@ const deployment = new k8s.apps.v1.Deployment("nginx", {
image: "nginx",
resources: {
limits: {
// This value will be normalized by the API server to "2Gi" in the returned state. Without
// This value will be normalized by the API server to "1Gi" in the returned state. Without
// dry-run support, Pulumi will detect a spurious diff due to the normalization.
memory: "2048Mi",
memory: "1024Mi",
},
},
}],
}
}
}
});
}, { provider });
export const name = deployment.metadata.name;
2 changes: 1 addition & 1 deletion tests/sdk/nodejs/dry-run/step1/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "steps",
"name": "dry-run",
"version": "0.1.0",
"dependencies": {
"@pulumi/pulumi": "latest",
Expand Down
46 changes: 46 additions & 0 deletions tests/sdk/nodejs/dry-run/step2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2016-2021, Pulumi Corporation.
//
// 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.

import * as k8s from "@pulumi/kubernetes";
import * as random from "@pulumi/random";
import * as pulumi from "@pulumi/pulumi";

// TODO(levi): Use auto-naming for namespace once https://github.com/pulumi/pulumi-kubernetes/issues/1632 is fixed.
const suffix = new random.RandomString("suffix", {length: 7, special: false, upper: false}).result;
const ns = new k8s.core.v1.Namespace("test", {metadata: {name: pulumi.interpolate`test-${suffix}`}});
const provider = new k8s.Provider("k8s", {enableDryRun: true}); // Use the default namespace.
lblackstone marked this conversation as resolved.
Show resolved Hide resolved

const appLabels = { app: "nginx" };
const deployment = new k8s.apps.v1.Deployment("nginx", {
spec: {
selector: { matchLabels: appLabels },
template: {
metadata: { labels: appLabels },
spec: {
containers: [{
name: "nginx",
image: "nginx",
resources: {
limits: {
// This value will be normalized by the API server to "1Gi" in the returned state. Without
// dry-run support, Pulumi will detect a spurious diff due to the normalization.
memory: "1024Mi",
},
},
}],
}
}
}
}, { provider });
export const name = deployment.metadata.name;
6 changes: 6 additions & 0 deletions tests/sdk/nodejs/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ func TestDeploymentRollout(t *testing.T) {
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)
}
Expand Down