-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,22 +8,40 @@ 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 AdditionalCLIArgEnvVarExtensionFlag int | ||
|
|
||
| const ( | ||
| AdditionalCLIArgEnvVarExtensionFlagIgnore AdditionalCLIArgEnvVarExtensionFlag = iota | ||
| AdditionalCLIArgEnvVarExtensionFlagAllow | ||
| ) | ||
|
|
||
| type Provider struct { | ||
| Name string `json:"name"` | ||
| ExecConfig *clientcmdapi.ExecConfig `json:"execConfig"` | ||
| Name string `json:"name"` | ||
| ExecConfig *clientcmdapi.ExecConfig `json:"execConfig"` | ||
| AdditionalCLIArgEnvVarExtensionFlag AdditionalCLIArgEnvVarExtensionFlag `json:"additionalCLIArgEnvVarExtensionFlag,omitempty"` | ||
| } | ||
|
|
||
| type CredentialsProvider struct { | ||
|
|
@@ -68,11 +86,47 @@ func (cp *CredentialsProvider) BuildConfigFromCP(clusterprofile *v1alpha1.Cluste | |
| } | ||
|
|
||
| // 2. Get Exec Config | ||
| execConfig := cp.getExecConfigFromConfig(clusterAccessor.Name) | ||
| execConfig, additionalCLIArgEnvVarsExtFlag := 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 { | ||
|
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. might be if additionalCLIArgEnvVarsExtFlag {
switch ext.Name {
case additionalCLIArgsExtensionKey:
....
}
} |
||
| case additionalCLIArgEnvVarsExtFlag == AdditionalCLIArgEnvVarExtensionFlagAllow && ext.Name == additionalCLIArgsExtensionKey: | ||
| 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 additionalCLIArgEnvVarsExtFlag == AdditionalCLIArgEnvVarExtensionFlagAllow && ext.Name == additionalEnvVarsExtensionKey: | ||
| var additionalEnvs map[string]string | ||
| if err := yaml.Unmarshal(ext.Extension.Raw, &additionalEnvs); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal additional env vars extension: %w", err) | ||
| } | ||
|
|
||
| // Update the value of existing env vars. | ||
| for idx := range execConfig.Env { | ||
| env := &execConfig.Env[idx] | ||
| if _, exists := additionalEnvs[env.Name]; exists { | ||
| env.Value = additionalEnvs[env.Name] | ||
| delete(additionalEnvs, env.Name) | ||
|
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. to make it simpler, how about adding any item in execConfig.Env that does not exist in additionalEnvs into additionalEnvs. So in the loop in line 121, you can construct a new env list. |
||
| } | ||
| } | ||
|
|
||
| // Add new env vars. | ||
| for name, value := range additionalEnvs { | ||
| execConfig.Env = append(execConfig.Env, clientcmdapi.ExecEnvVar{ | ||
| Name: name, | ||
| Value: value, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 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, AdditionalCLIArgEnvVarExtensionFlag) { | ||
| for _, provider := range cp.Providers { | ||
| if provider.Name == providerName { | ||
| return provider.ExecConfig | ||
| return provider.ExecConfig, provider.AdditionalCLIArgEnvVarExtensionFlag | ||
| } | ||
| } | ||
| return nil | ||
| return nil, AdditionalCLIArgEnvVarExtensionFlagIgnore | ||
| } | ||
|
|
||
| // getClusterAccessorFromClusterProfile returns the first AccessProvider from the ClusterProfile | ||
|
|
||
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.
That looks like a verify long name...and if the value is int, I think it would be confusing in json format?