generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add support for additional CLI arg/env var extensions + add azure/kubelogin based example #27
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
michaelawyu
wants to merge
4
commits into
kubernetes-sigs:main
Choose a base branch
from
michaelawyu:feat/add-support-for-additional-cli-arg-env-var-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+307
−18
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,22 +8,34 @@ import ( | |
| "net/url" | ||
| "os" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| "k8s.io/client-go/rest" | ||
| clientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||
| clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest" | ||
| "k8s.io/klog/v2" | ||
| "sigs.k8s.io/cluster-inventory-api/apis/v1alpha1" | ||
| ) | ||
|
|
||
| // client.authentication.k8s.io/exec is a reserved extension key defined by the Kubernetes | ||
| // client authentication API (SIG Auth), not by the ClusterProfile API. | ||
| // Reference: | ||
| // https://kubernetes.io/docs/reference/config-api/client-authentication.v1beta1/#client-authentication-k8s-io-v1beta1-Cluster | ||
| const clusterExtensionKey = "client.authentication.k8s.io/exec" | ||
| const ( | ||
| // client.authentication.k8s.io/exec is a reserved extension key defined by the Kubernetes | ||
| // client authentication API (SIG Auth), not by the ClusterProfile API. | ||
| // Reference: | ||
| // https://kubernetes.io/docs/reference/config-api/client-authentication.v1beta1/#client-authentication-k8s-io-v1beta1-Cluster | ||
| clusterExecExtensionKey = "client.authentication.k8s.io/exec" | ||
|
|
||
| // additionalCLIArgsExtensionKey and additionalEnvVarsExtensionKey are | ||
| // two reserved extensions defined in KEP 5339, which allows users to pass in (usually cluster-specific) | ||
| // additional command-line arguments and environment variables to the exec plugin from | ||
| // the ClusterProfile API side. | ||
| additionalCLIArgsExtensionKey = "multicluster.x-k8s.io/clusterprofiles/auth/exec/additional-args" | ||
| additionalEnvVarsExtensionKey = "multicluster.x-k8s.io/clusterprofiles/auth/exec/additional-envs" | ||
| ) | ||
|
|
||
| type Provider struct { | ||
| Name string `json:"name"` | ||
| ExecConfig *clientcmdapi.ExecConfig `json:"execConfig"` | ||
| Name string `json:"name"` | ||
| ExecConfig *clientcmdapi.ExecConfig `json:"execConfig"` | ||
| AllowProfileSourcedCLIArgs bool `json:"allowProfileSourcedCLIArgs,omitempty"` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of boolean, how do you think if we can set a this as |
||
| AllowProfileSourcedEnvVars bool `json:"allowProfileSourcedEnvVars,omitempty"` | ||
| } | ||
|
|
||
| type CredentialsProvider struct { | ||
|
|
@@ -68,11 +80,53 @@ func (cp *CredentialsProvider) BuildConfigFromCP(clusterprofile *v1alpha1.Cluste | |
| } | ||
|
|
||
| // 2. Get Exec Config | ||
| execConfig := cp.getExecConfigFromConfig(clusterAccessor.Name) | ||
| execConfig, allowProfileSourcedCLIArgs, allowProfileSourcedEnvVars := cp.getExecConfigAndFlagsFromConfig(clusterAccessor.Name) | ||
| if execConfig == nil { | ||
| return nil, fmt.Errorf("no exec credentials found for provider %q", clusterAccessor.Name) | ||
| } | ||
|
|
||
| // 3. Add additional CLI arguments and environment variables from cluster extensions if allowed. | ||
| for idx := range clusterAccessor.Cluster.Extensions { | ||
| ext := &clusterAccessor.Cluster.Extensions[idx] | ||
|
|
||
| switch ext.Name { | ||
| case additionalCLIArgsExtensionKey: | ||
| if allowProfileSourcedCLIArgs { | ||
| var additionalArgs []string | ||
| if err := yaml.Unmarshal(ext.Extension.Raw, &additionalArgs); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal additional CLI args extension: %w", err) | ||
| } | ||
| execConfig.Args = append(execConfig.Args, additionalArgs...) | ||
| } | ||
| case additionalEnvVarsExtensionKey: | ||
| if allowProfileSourcedEnvVars { | ||
| var envVars map[string]string | ||
| if err := yaml.Unmarshal(ext.Extension.Raw, &envVars); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal additional env vars extension: %w", err) | ||
| } | ||
|
|
||
| // Add existing environment variables. Note that if the same variable is specified twice | ||
| // in both the extension data and the execConfig data, the value in the extension data takes precedence. | ||
| for idx := range execConfig.Env { | ||
| env := &execConfig.Env[idx] | ||
| if _, exists := envVars[env.Name]; !exists { | ||
| envVars[env.Name] = env.Value | ||
| } | ||
| } | ||
|
|
||
| // Write the merged list back to the execConfig. | ||
| envVarList := make([]clientcmdapi.ExecEnvVar, 0, len(envVars)) | ||
| for name, value := range envVars { | ||
| envVarList = append(envVarList, clientcmdapi.ExecEnvVar{ | ||
| Name: name, | ||
| Value: value, | ||
| }) | ||
| } | ||
| execConfig.Env = envVarList | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 3. build resulting rest.Config | ||
| config := &rest.Config{ | ||
| Host: clusterAccessor.Cluster.Server, | ||
|
|
@@ -94,25 +148,28 @@ func (cp *CredentialsProvider) BuildConfigFromCP(clusterprofile *v1alpha1.Cluste | |
| Env: execConfig.Env, | ||
| InteractiveMode: "Never", | ||
| ProvideClusterInfo: execConfig.ProvideClusterInfo, | ||
| Config: execConfig.Config, | ||
| } | ||
|
|
||
| // Propagate reserved extension into ExecCredential.Spec.Cluster.Config if present | ||
| internalCluster := clientcmdapi.NewCluster() | ||
| if err := clientcmdlatest.Scheme.Convert(&clusterAccessor.Cluster, internalCluster, nil); err != nil { | ||
| return nil, fmt.Errorf("failed to convert v1 Cluster to internal: %w", err) | ||
| } | ||
| config.ExecProvider.Config = internalCluster.Extensions[clusterExtensionKey] | ||
| if extData, ok := internalCluster.Extensions[clusterExecExtensionKey]; ok { | ||
| config.ExecProvider.Config = extData | ||
| } | ||
|
|
||
| return config, nil | ||
| } | ||
|
|
||
| func (cp *CredentialsProvider) getExecConfigFromConfig(providerName string) *clientcmdapi.ExecConfig { | ||
| func (cp *CredentialsProvider) getExecConfigAndFlagsFromConfig(providerName string) (*clientcmdapi.ExecConfig, bool, bool) { | ||
| for _, provider := range cp.Providers { | ||
| if provider.Name == providerName { | ||
| return provider.ExecConfig | ||
| return provider.ExecConfig, provider.AllowProfileSourcedCLIArgs, provider.AllowProfileSourcedEnvVars | ||
| } | ||
| } | ||
| return nil | ||
| return nil, false, false | ||
| } | ||
|
|
||
| // getClusterAccessorFromClusterProfile returns the first AccessProvider from the ClusterProfile | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest