Skip to content

fix(kubeconfig): Enforces kubernetes context with helm release namespace #1583

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions helm/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func (m *Meta) NewKubeConfig(ctx context.Context, namespace string) (*KubeConfig
}
if !kubernetesConfig.ConfigContextCluster.IsNull() {
overrides.Context.Cluster = kubernetesConfig.ConfigContextCluster.ValueString()
// Enforces configured release namespace
overrides.Context.Namespace = namespace
}
}

Expand Down
50 changes: 50 additions & 0 deletions helm/resource_helm_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/releaseutil"
"helm.sh/helm/v3/pkg/repo"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

func TestAccResourceRelease_basic(t *testing.T) {
Expand Down Expand Up @@ -1343,6 +1346,50 @@ func testAccCheckHelmReleaseDependencyUpdate(namespace string, name string, expe
}
}

func testAccCheckHelmReleaseResourceNamespace(namespace string, name string) resource.TestCheckFunc {
// Ensures all resources in a release are in the same requested namespace. Including dependency resources.

return func(s *terraform.State) error {
actionConfig := &action.Configuration{}
if err := actionConfig.Init(kube.GetConfig(os.Getenv("KUBE_CONFIG_PATH"), "", namespace), namespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
log.Printf(fmt.Sprintf(format, v...))
}); err != nil {
return err
}

status := action.NewStatus(actionConfig)
status.ShowResources = true

res, err := status.Run(name)

if res == nil {
return fmt.Errorf("release %q not found", name)
}

if err != nil {
return err
}

for _, resources := range res.Info.Resources {
for _, resource := range resources {
innerObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(resource)
if err != nil {
return fmt.Errorf("unable to decode runtime object %s, with error: %s", resource, err)
}

u := unstructured.Unstructured{Object: innerObj}

// Skip check for resources that are not bound to namespaces
if ns := u.GetNamespace(); ns != "" && ns != namespace {
return fmt.Errorf("expected namespace %s, but got %v", namespace, ns)
}
}
}

return nil
}
}

func testAccCheckHelmReleaseDestroy(namespace string) resource.TestCheckFunc {
return func(s *terraform.State) error {
log.Printf("testMeta before checking: %+v\n", testMeta)
Expand Down Expand Up @@ -1522,6 +1569,7 @@ func TestAccResourceRelease_dependency(t *testing.T) {
{
Config: testAccHelmReleaseConfigDependency(testResourceName, namespace, name, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckHelmReleaseResourceNamespace(namespace, name),
resource.TestCheckResourceAttr("helm_release.test", "metadata.revision", "1"),
resource.TestCheckResourceAttr("helm_release.test", "status", release.StatusDeployed.String()),
resource.TestCheckResourceAttr("helm_release.test", "dependency_update", "true"),
Expand All @@ -1535,6 +1583,7 @@ func TestAccResourceRelease_dependency(t *testing.T) {
},
Config: testAccHelmReleaseConfigDependencyUpdate(testResourceName, namespace, name, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckHelmReleaseResourceNamespace(namespace, name),
testAccCheckHelmReleaseDependencyUpdate(namespace, name, 9),
resource.TestCheckResourceAttr("helm_release.test", "metadata.revision", "2"),
resource.TestCheckResourceAttr("helm_release.test", "status", release.StatusDeployed.String()),
Expand All @@ -1549,6 +1598,7 @@ func TestAccResourceRelease_dependency(t *testing.T) {
},
Config: testAccHelmReleaseConfigDependencyUpdateWithLint(testResourceName, namespace, name, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckHelmReleaseResourceNamespace(namespace, name),
resource.TestCheckResourceAttr("helm_release.test", "metadata.revision", "3"),
resource.TestCheckResourceAttr("helm_release.test", "status", release.StatusDeployed.String()),
resource.TestCheckResourceAttr("helm_release.test", "dependency_update", "true"),
Expand Down