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

Add a "strict mode" configuration option #2425

Merged
merged 4 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions provider/cmd/pulumi-resource-kubernetes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@
"type": "string",
"description": "BETA FEATURE - If present, render resource manifests to this directory. In this mode, resources will not\nbe created on a Kubernetes cluster, but the rendered manifests will be kept in sync with changes\nto the Pulumi program. This feature is in developer preview, and is disabled by default.\n\nNote that some computed Outputs such as status fields will not be populated\nsince the resources are not created on a Kubernetes cluster. These Output values will remain undefined,\nand may result in an error if they are referenced by other resources. Also note that any secret values\nused in these resources will be rendered in plaintext to the resulting YAML."
},
"strictMode": {
"type": "boolean",
"description": "If present and set to true, the provider will use strict configuration mode. Recommended for production stacks. In this mode, the default Kubernetes provider is disabled, and the `kubeconfig` and `context` settings are required for Provider configuration. These settings unambiguously ensure that every Kubernetes resource is associated with a particular cluster."
},
"suppressDeprecationWarnings": {
"type": "boolean",
"description": "If present and set to true, suppress apiVersion deprecation warnings from the CLI.\n\nThis config can be specified in the following ways, using this precedence:\n1. This `suppressDeprecationWarnings` parameter.\n2. The `PULUMI_K8S_SUPPRESS_DEPRECATION_WARNINGS` environment variable."
Expand Down
4 changes: 4 additions & 0 deletions provider/pkg/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func PulumiSchema(swagger map[string]interface{}) pschema.PackageSpec {
Description: "If present and set to true, suppress unsupported Helm hook warnings from the CLI.\n\nThis config can be specified in the following ways, using this precedence:\n1. This `suppressHelmHookWarnings` parameter.\n2. The `PULUMI_K8S_SUPPRESS_HELM_HOOK_WARNINGS` environment variable.",
TypeSpec: pschema.TypeSpec{Type: "boolean"},
},
"strictMode": {
Description: "If present and set to true, the provider will use strict configuration mode. Recommended for production stacks. In this mode, the default Kubernetes provider is disabled, and the `kubeconfig` and `context` settings are required for Provider configuration. These settings unambiguously ensure that every Kubernetes resource is associated with a particular cluster.",
TypeSpec: pschema.TypeSpec{Type: "boolean"},
},
},
},

Expand Down
61 changes: 61 additions & 0 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/pulumi/pulumi-kubernetes/provider/v3/pkg/openapi"
"github.com/pulumi/pulumi-kubernetes/provider/v3/pkg/ssa"
pulumischema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy/providers"
"github.com/pulumi/pulumi/pkg/v3/resource/provider"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
Expand Down Expand Up @@ -256,6 +257,44 @@ func (k *kubeProvider) CheckConfig(ctx context.Context, req *pulumirpc.CheckRequ
return false
}

strictMode := false
if pConfig, ok := k.loadPulumiConfig(); ok {
if v, ok := pConfig["strictMode"]; ok {
if v, ok := v.(string); ok {
strictMode = v == "true"
}
}
}
if v := news["strictMode"]; v.HasValue() && v.IsString() {
strictMode = v.StringValue() == "true"
}

if strictMode && providers.IsProviderType(urn.Type()) {
var failures []*pulumirpc.CheckFailure

if providers.IsDefaultProvider(urn) {
failures = append(failures, &pulumirpc.CheckFailure{
Reason: fmt.Sprintf("strict mode prohibits default provider"),
})
}
if v := news["kubeconfig"]; !v.HasValue() || v.StringValue() == "" {
failures = append(failures, &pulumirpc.CheckFailure{
Property: "kubeconfig",
Reason: fmt.Sprintf(`strict mode requires Provider "kubeconfig" argument`),
})
}
if v := news["context"]; !v.HasValue() || v.StringValue() == "" {
failures = append(failures, &pulumirpc.CheckFailure{
Property: "context",
Reason: fmt.Sprintf(`strict mode requires Provider "context" argument`),
})
}

if len(failures) > 0 {
return &pulumirpc.CheckResponse{Inputs: req.GetNews(), Failures: failures}, nil
}
}

renderYamlEnabled := truthyValue("renderYamlToDirectory", news)

errTemplate := `%q arg is not compatible with "renderYamlToDirectory" arg`
Expand Down Expand Up @@ -2867,6 +2906,28 @@ func (k *kubeProvider) gvkExists(obj *unstructured.Unstructured) bool {
return true
}

// loadPulumiConfig loads the PULUMI_CONFIG environment variable set by the engine, unmarshals the JSON string into
// a map, and returns the map and a bool indicating if the operation succeeded.
func (k *kubeProvider) loadPulumiConfig() (map[string]interface{}, bool) {
configStr, ok := os.LookupEnv("PULUMI_CONFIG")
// PULUMI_CONFIG is not set on older versions of the engine, so check if the lookup succeeds.
if !ok || configStr == "" {
return nil, false
}

// PULUMI_CONFIG should be a JSON string that looks something like this:
// {"enableServerSideApply":"true","kubeClientSettings":"{\"burst\":120,\"qps\":50}","strictMode":"true"}
// The keys correspond to any project/stack config with a "kubernetes" prefix.
var pConfig map[string]interface{}
err := json.Unmarshal([]byte(configStr), &pConfig)
lblackstone marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.V(3).Infof("failed to load provider config from PULUMI_CONFIG")
lblackstone marked this conversation as resolved.
Show resolved Hide resolved
return nil, false
}

return pConfig, true
}

func mapReplStripSecrets(v resource.PropertyValue) (interface{}, bool) {
if v.IsSecret() {
return v.SecretValue().Element.MapRepl(nil, mapReplStripSecrets), true
Expand Down
10 changes: 10 additions & 0 deletions sdk/dotnet/Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ public void Set(T value)
set => _renderYamlToDirectory.Set(value);
}

private static readonly __Value<bool?> _strictMode = new __Value<bool?>(() => __config.GetBoolean("strictMode"));
/// <summary>
/// If present and set to true, the provider will use strict configuration mode. Recommended for production stacks. In this mode, the default Kubernetes provider is disabled, and the `kubeconfig` and `context` settings are required for Provider configuration. These settings unambiguously ensure that every Kubernetes resource is associated with a particular cluster.
/// </summary>
public static bool? StrictMode
{
get => _strictMode.Get();
set => _strictMode.Set(value);
}

private static readonly __Value<bool?> _suppressDeprecationWarnings = new __Value<bool?>(() => __config.GetBoolean("suppressDeprecationWarnings"));
/// <summary>
/// If present and set to true, suppress apiVersion deprecation warnings from the CLI.
Expand Down
5 changes: 5 additions & 0 deletions sdk/go/kubernetes/config/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions sdk/java/src/main/java/com/pulumi/kubernetes/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public Optional<String> namespace() {
public Optional<String> renderYamlToDirectory() {
return Codegen.stringProp("renderYamlToDirectory").config(config).get();
}
/**
* If present and set to true, the provider will use strict configuration mode. Recommended for production stacks. In this mode, the default Kubernetes provider is disabled, and the `kubeconfig` and `context` settings are required for Provider configuration. These settings unambiguously ensure that every Kubernetes resource is associated with a particular cluster.
*
*/
public Optional<Boolean> strictMode() {
return Codegen.booleanProp("strictMode").config(config).get();
}
/**
* If present and set to true, suppress apiVersion deprecation warnings from the CLI.
*
Expand Down