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

Fix helm.v3.Release replace behavior #2532

Merged
merged 6 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
@@ -1,6 +1,7 @@
## Unreleased

- fix: ensure CSA does not hit API Server for preview (https://github.com/pulumi/pulumi-kubernetes/pull/2522)
- Fix helm.v3.Release replace behavior (https://github.com/pulumi/pulumi-kubernetes/pull/2532)

## 4.0.3 (July 21, 2023)

Expand Down
3 changes: 2 additions & 1 deletion provider/pkg/provider/helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ func (r *helmReleaseProvider) Diff(ctx context.Context, req *pulumirpc.DiffReque
logger.V(9).Infof("news: %+v", news.Mappable())
logger.V(9).Infof("oldInputs: %+v", oldInputs.Mappable())

if detailedDiff, err = convertPatchToDiff(patchObj, olds.Mappable(), news.Mappable(), oldInputs.Mappable(), ".releaseSpec.name", ".releaseSpec.namespace"); err != nil {
forceNewFields := []string{".name", ".namespace"}
if detailedDiff, err = convertPatchToDiff(patchObj, olds.Mappable(), news.Mappable(), oldInputs.Mappable(), forceNewFields...); err != nil {
return nil, pkgerrors.Wrapf(
err, "Failed to check for changes in helm release %s/%s because of an error "+
"converting JSON patch describing resource changes to a diff",
Expand Down
31 changes: 29 additions & 2 deletions tests/sdk/nodejs/examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/openapi"
"github.com/pulumi/pulumi-kubernetes/tests/v4"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy/providers"
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
Expand Down Expand Up @@ -432,13 +433,39 @@ func TestHelmReleaseNamespace(t *testing.T) {
skipIfShort(t)
test := getBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: filepath.Join(getCwd(t), "helm-release-namespace"),
Dir: filepath.Join(getCwd(t), "helm-release-namespace", "step1"),
SkipRefresh: false,
Verbose: true,
ExpectRefreshChanges: true,
// Ensure that the rule was found in the release's namespace.
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotEmpty(t, stackInfo.Outputs["alertManagerNamespace"].(string))
assert.Equalf(t, stackInfo.Outputs["namespaceName"], stackInfo.Outputs["alertManagerNamespace"],
"expected Helm resources to reside in the provided Namespace")
assert.NotEmptyf(t, stackInfo.Outputs["alertManagerNamespace"].(string),
"Helm resources should not be in the default Namespace")
getResponse, err := tests.Kubectl(fmt.Sprintf("get statefulset -n %s alertmanager",
stackInfo.Outputs["alertManagerNamespace"]))
assert.NoErrorf(t, err, "kubectl command failed")
assert.NotContainsf(t, getResponse, "No resources found",
"kubectl did not find the expected resource")
},
EditDirs: []integration.EditDir{
{
Dir: filepath.Join(getCwd(t), "helm-release-namespace", "step2"),
Additive: true,
// Ensure that the rule was found in the release's namespace.
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.Equalf(t, stackInfo.Outputs["namespaceName"], stackInfo.Outputs["alertManagerNamespace"],
"expected Helm resources to reside in the provided Namespace")
assert.NotEmptyf(t, stackInfo.Outputs["alertManagerNamespace"].(string),
"Helm resources should not be in the default Namespace")
getResponse, err := tests.Kubectl(fmt.Sprintf("get statefulset -n %s alertmanager",
stackInfo.Outputs["alertManagerNamespace"]))
assert.NoErrorf(t, err, "kubectl command failed")
assert.NotContainsf(t, getResponse, "No resources found",
"kubectl did not find the expected resource")
},
},
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

const namespace = new k8s.core.v1.Namespace("release-ns");
export const namespaceName = namespace.metadata.name;

const alertManager = new k8s.helm.v3.Release("alertmanager", {
name: "alertmanager",
namespace: namespace.metadata.name,
namespace: namespaceName,
chart: "alertmanager",
version: "0.12.2",
repositoryOpts: {
Expand Down
34 changes: 34 additions & 0 deletions tests/sdk/nodejs/examples/helm-release-namespace/step2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

const namespace = new k8s.core.v1.Namespace("release-ns-2");
export const namespaceName = namespace.metadata.name;

const alertManager = new k8s.helm.v3.Release("alertmanager", {
name: "alertmanager",
namespace: namespaceName,
chart: "alertmanager",
version: "0.12.2",
repositoryOpts: {
repo: "https://prometheus-community.github.io/helm-charts",
},
});

// Ensure we get the expected namespace for the stateful set.
export const alertManagerNamespace = k8s.apps.v1.StatefulSet.get(
"alertmanager-statefulset",
pulumi.interpolate`${alertManager.status.namespace}/alertmanager`).metadata.namespace;
21 changes: 5 additions & 16 deletions tests/sdk/nodejs/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/pulumi/pulumi-kubernetes/tests/v4"
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -50,13 +49,13 @@ func TestPreview(t *testing.T) {
})

// Create service account and RBAC policies for the service account.
out, err := kubectl("apply -f preview-auth/service-account.yaml")
out, err := tests.Kubectl("apply -f preview-auth/service-account.yaml")
if err != nil {
t.Fatalf("unable to create RBAC policies: %s, out: %s", err, string(out))
}
t.Cleanup(func() {
log.Println("Deleting service-account and rbac")
kubectl("delete -f preview-auth/service-account.yaml")
tests.Kubectl("delete -f preview-auth/service-account.yaml")
})

// Create kubeconfig for service account.
Expand Down Expand Up @@ -96,7 +95,7 @@ func TestPreview(t *testing.T) {
assert.Error(t, err)

// Check that the configmap was not created using kubectl.
out, err = kubectl("get configmap foo")
out, err = tests.Kubectl("get configmap foo")
assert.Error(t, err)
assert.Contains(t, string(out), `Error from server (NotFound): configmaps "foo" not found`)
}
Expand All @@ -107,7 +106,7 @@ func createSAKubeconfig(t *testing.T, saName string) (string, error) {
t.Helper()

// Create token to use for the service account.
token, err := kubectl(fmt.Sprintf("create token %s --duration=1h", saName))
token, err := tests.Kubectl(fmt.Sprintf("create token %s --duration=1h", saName))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -143,13 +142,3 @@ func createSAKubeconfig(t *testing.T, saName string) (string, error) {

return kubeconfigPath, err
}

// kubectl is a helper function to shell out and run kubectl commands.
func kubectl(args ...string) ([]byte, error) {
var fmtArgs []string
for _, arg := range args {
fmtArgs = append(fmtArgs, strings.Fields(arg)...)
}

return exec.Command("kubectl", fmtArgs...).CombinedOutput()
}
12 changes: 12 additions & 0 deletions tests/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tests

import (
"os/exec"
"sort"
"strings"

"github.com/pulumi/pulumi/pkg/v3/testing/integration"
)
Expand All @@ -11,3 +13,13 @@ func SortResourcesByURN(stackInfo integration.RuntimeValidationStackInfo) {
return stackInfo.Deployment.Resources[i].URN < stackInfo.Deployment.Resources[j].URN
})
}

// Kubectl is a helper function to shell out and run kubectl commands.
func Kubectl(args ...string) ([]byte, error) {
var fmtArgs []string
for _, arg := range args {
fmtArgs = append(fmtArgs, strings.Fields(arg)...)
}

return exec.Command("kubectl", fmtArgs...).CombinedOutput()
}
Loading