diff --git a/cmd/kubeconfigwriter/main.go b/cmd/kubeconfigwriter/main.go index 915186bff41..8ad316e88fa 100644 --- a/cmd/kubeconfigwriter/main.go +++ b/cmd/kubeconfigwriter/main.go @@ -32,7 +32,8 @@ import ( ) var ( - clusterConfig = flag.String("clusterConfig", "", "json string with the configuration of a cluster based on values from a cluster resource. Only required for external clusters.") + clusterConfig = flag.String("clusterConfig", "", "json string with the configuration of a cluster based on values from a cluster resource. Only required for external clusters.") + destinationDir = flag.String("destinationDir", "", "destination directory where generated kubeconfig file will be stored.") ) func main() { @@ -48,10 +49,10 @@ func main() { if err != nil { logger.Fatalf("Error reading cluster config: %v", err) } - createKubeconfigFile(&cr, logger) + createKubeconfigFile(&cr, logger, destinationDir) } -func createKubeconfigFile(resource *cluster.Resource, logger *zap.SugaredLogger) { +func createKubeconfigFile(resource *cluster.Resource, logger *zap.SugaredLogger, destinationDir *string) { cluster := &clientcmdapi.Cluster{ Server: resource.URL, InsecureSkipTLSVerify: resource.Insecure, @@ -72,14 +73,18 @@ func createKubeconfigFile(resource *cluster.Resource, logger *zap.SugaredLogger) //only one authentication technique per user is allowed in a kubeconfig, so clear out the password if a token is provided user := resource.Username pass := resource.Password + clientKeyData := resource.ClientKeyData + clientCertificateData := resource.ClientCertificateData if resource.Token != "" { user = "" pass = "" } auth := &clientcmdapi.AuthInfo{ - Token: resource.Token, - Username: user, - Password: pass, + Token: resource.Token, + Username: user, + Password: pass, + ClientKeyData: clientKeyData, + ClientCertificateData: clientCertificateData, } context := &clientcmdapi.Context{ Cluster: resource.Name, @@ -95,7 +100,17 @@ func createKubeconfigFile(resource *cluster.Resource, logger *zap.SugaredLogger) c.APIVersion = "v1" c.Kind = "Config" - destinationFile := fmt.Sprintf("/workspace/%s/kubeconfig", resource.Name) + // kubeconfig file location + var destinationFile string + + // If the destination Directory is provided, kubeconfig will be written to the given directory. + // otherwise it will use default location i.e. "/workspace// + if *destinationDir != "" { + destinationFile = fmt.Sprintf("%s/kubeconfig", *destinationDir) + } else { + destinationFile = fmt.Sprintf("/workspace/%s/kubeconfig", resource.Name) + } + if err := clientcmd.WriteToFile(*c, destinationFile); err != nil { logger.Fatalf("Error writing kubeconfig to file: %v", err) } diff --git a/docs/resources.md b/docs/resources.md index 2a3e95bfad9..7fba1ac1071 100644 --- a/docs/resources.md +++ b/docs/resources.md @@ -648,11 +648,18 @@ The Cluster resource has the following parameters: certificate. - `cadata` (required): holds PEM-encoded bytes (typically read from a root certificates bundle). +- `clientKeyData`: contains PEM-encoded data from a client key file + for TLS +- `clientCertificateData`: contains PEM-encoded data from a client cert file for TLS + Note: Since only one authentication technique is allowed per user, either a `token` or a `password` should be provided, if both are provided, the `password` will be ignored. +`clientKeyData` and `clientCertificateData` are only required if `token` or +`password` is not provided for authentication to cluster. + The following example shows the syntax and structure of a `cluster` resource: ```yaml diff --git a/docs/variables.md b/docs/variables.md index 101bb2e5617..4662def16c1 100644 --- a/docs/variables.md +++ b/docs/variables.md @@ -98,6 +98,8 @@ Each variable is accessible via `resources.inputs..