Skip to content

Commit

Permalink
Load default kubeconfig if not specified in provider (#2180)
Browse files Browse the repository at this point in the history
The default kubeconfig behavior was previously inconsistent across SDKs, and required the kubeconfig path to be specified manually in some cases. This change unifies the behavior and will load the ambient kubeconfig if unspecified for a provider.
  • Loading branch information
lblackstone committed Sep 22, 2022
1 parent 23c2390 commit ad66026
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

- Fix Helm Chart preview with unconfigured provider (C#) (https://github.com/pulumi/pulumi-kubernetes/issues/2162)
- Load default kubeconfig if not specified in provider (https://github.com/pulumi/pulumi-kubernetes/issues/2180)
- Skip computing a preview for Patch resources (https://github.com/pulumi/pulumi-kubernetes/issues/2182)
- [sdk/python] Handle CRDs with status field input (https://github.com/pulumi/pulumi-kubernetes/issues/2183)
- Upgrade Kubernetes and Helm dependencies (https://github.com/pulumi/pulumi-kubernetes/issues/2186)
Expand Down
3 changes: 2 additions & 1 deletion provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ func (k *kubeProvider) Configure(_ context.Context, req *pulumirpc.ConfigureRequ
usr, _ := user.Current()
return usr.HomeDir
}
if pathOrContents, ok := vars["kubernetes:config:kubeconfig"]; ok {
// Note: the Python SDK was setting the kubeconfig value to "" by default, so explicitly check for empty string.
if pathOrContents, ok := vars["kubernetes:config:kubeconfig"]; ok && pathOrContents != "" {
var contents string

// Handle the '~' character if it is set in the config string. Normally, this would be expanded by the shell
Expand Down
7 changes: 2 additions & 5 deletions tests/sdk/go/kustomize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import (

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
provider, err := k8s.NewProvider(ctx, "k8s", &k8s.ProviderArgs{
Kubeconfig: pulumi.String("~/.kube/config"),
})
provider, err := k8s.NewProvider(ctx, "k8s", &k8s.ProviderArgs{})
if err != nil {
return err
}
Expand All @@ -34,8 +32,7 @@ func main() {
return err
}
nsProvider, err := k8s.NewProvider(ctx, "k8s-ns", &k8s.ProviderArgs{
Kubeconfig: pulumi.String("~/.kube/config"),
Namespace: ns.Metadata.Name(),
Namespace: ns.Metadata.Name(),
})
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion tests/sdk/go/server-side-apply/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
provider, err := kubernetes.NewProvider(ctx, "k8s", &kubernetes.ProviderArgs{
EnableServerSideApply: pulumi.BoolPtr(true),
Kubeconfig: pulumi.String("~/.kube/config"),
})
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion tests/sdk/go/server-side-apply/step2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
provider, err := kubernetes.NewProvider(ctx, "k8s", &kubernetes.ProviderArgs{
EnableServerSideApply: pulumi.BoolPtr(true),
Kubeconfig: pulumi.String("~/.kube/config"),
})
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions tests/sdk/go/yaml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import (

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
provider, err := k8s.NewProvider(ctx, "k8s", &k8s.ProviderArgs{
Kubeconfig: pulumi.String("~/.kube/config"),
})
provider, err := k8s.NewProvider(ctx, "k8s", &k8s.ProviderArgs{})
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions tests/sdk/nodejs/examples/helm-no-default-provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

import * as k8s from '@pulumi/kubernetes'

const k8sProvider = new k8s.Provider(`k8s-provider`, {
kubeconfig: '~/.kube/config',
})
const k8sProvider = new k8s.Provider(`k8s-provider`, {})

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

Expand Down
27 changes: 11 additions & 16 deletions tests/sdk/python/provider-old/__main__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
# Copyright 2016-2019, Pulumi Corporation.
# Copyright 2016-2022, 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
# 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
# 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.
from os import path
# 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.

from pulumi import ResourceOptions
from pulumi_kubernetes import Provider
from pulumi_kubernetes.core.v1 import Pod, Namespace

kubeconfig_file = path.join(path.expanduser("~"), ".kube", "config")
with open(kubeconfig_file) as f:
kubeconfig = f.read()

my_k8s = Provider("myk8s", kubeconfig=kubeconfig)
my_k8s = Provider("myk8s")

namespace = Namespace("test")

Expand Down
27 changes: 11 additions & 16 deletions tests/sdk/python/provider/__main__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
# Copyright 2016-2020, Pulumi Corporation.
# Copyright 2016-2022, 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
# 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
# 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.
from os import path
# 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.

from pulumi import ResourceOptions
from pulumi_kubernetes import Provider
from pulumi_kubernetes.core.v1 import Pod, Namespace, PodSpecArgs, ContainerArgs, ContainerPortArgs
from pulumi_kubernetes.meta.v1 import ObjectMetaArgs

kubeconfig_file = path.join(path.expanduser("~"), ".kube", "config")
with open(kubeconfig_file) as f:
kubeconfig = f.read()

my_k8s = Provider("myk8s", kubeconfig=kubeconfig)
my_k8s = Provider("myk8s")

namespace = Namespace("test")

Expand Down
7 changes: 1 addition & 6 deletions tests/sdk/python/server-side-apply/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@
# 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.
from os import path

import pulumi
import pulumi_kubernetes as k8s

kubeconfig_file = path.join(path.expanduser("~"), ".kube", "config")
with open(kubeconfig_file) as f:
kubeconfig = f.read()

provider = k8s.Provider("myk8s", kubeconfig=kubeconfig, enable_server_side_apply=True)
provider = k8s.Provider("myk8s", enable_server_side_apply=True)

ns = k8s.core.v1.Namespace("test", opts=pulumi.ResourceOptions(provider=provider))

Expand Down

0 comments on commit ad66026

Please sign in to comment.