Skip to content

Commit 15446a8

Browse files
authored
Merge pull request #792 from n2h9/chore-take-into-account-insecure-setting-from-kubeconfig-during-helm-apply
chore: helm: use setting from kubeconfig
2 parents aa58e5a + 251209a commit 15446a8

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

utils/kubernetes/apply-helm-chart.go

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,16 +412,47 @@ func createHelmActionConfig(c *Client, cfg ApplyHelmChartConfig) (*action.Config
412412
os.Setenv("HELM_DRIVER_SQL_CONNECTION_STRING", cfg.SQLConnectionString)
413413

414414
// KubeConfig setup
415-
cafile, err := setDataAndReturnFileHandler(c.RestConfig.CAData)
416-
if err != nil {
417-
return nil, err
418-
}
419-
cafilename := cafile.Name()
420-
421415
kubeConfig := genericclioptions.NewConfigFlags(false)
422416
kubeConfig.APIServer = &c.RestConfig.Host
423-
kubeConfig.CAFile = &cafilename
424417
kubeConfig.BearerToken = &c.RestConfig.BearerToken
418+
kubeConfig.Insecure = &c.RestConfig.TLSClientConfig.Insecure
419+
420+
// Set username and password for basic auth if available
421+
if c.RestConfig.Username != "" {
422+
kubeConfig.Username = &c.RestConfig.Username
423+
}
424+
if c.RestConfig.Password != "" {
425+
kubeConfig.Password = &c.RestConfig.Password
426+
}
427+
428+
// Only set CA file if not running in insecure mode
429+
if !c.RestConfig.TLSClientConfig.Insecure {
430+
if len(c.RestConfig.CAData) > 0 {
431+
caFileName, err := setDataAndReturnFilename(c.RestConfig.CAData)
432+
if err != nil {
433+
return nil, err
434+
}
435+
kubeConfig.CAFile = &caFileName
436+
}
437+
}
438+
439+
// Set client certificate data if available
440+
if len(c.RestConfig.CertData) > 0 {
441+
certFileName, err := setDataAndReturnFilename(c.RestConfig.CertData)
442+
if err != nil {
443+
return nil, err
444+
}
445+
kubeConfig.CertFile = &certFileName
446+
}
447+
448+
// Set client key data if available
449+
if len(c.RestConfig.KeyData) > 0 {
450+
keyFileName, err := setDataAndReturnFilename(c.RestConfig.KeyData)
451+
if err != nil {
452+
return nil, err
453+
}
454+
kubeConfig.KeyFile = &keyFileName
455+
}
425456

426457
actionConfig := new(action.Configuration)
427458
if err := actionConfig.Init(kubeConfig, cfg.Namespace, string(cfg.HelmDriver), cfg.Logger); err != nil {
@@ -430,17 +461,21 @@ func createHelmActionConfig(c *Client, cfg ApplyHelmChartConfig) (*action.Config
430461
return actionConfig, nil
431462
}
432463

433-
// Populates a file in temp directory with the passed data and returns the data handler
434-
func setDataAndReturnFileHandler(data []byte) (*os.File, error) {
464+
// Populates a file in temp directory with the passed data and returns the filename
465+
func setDataAndReturnFilename(data []byte) (string, error) {
435466
f, err := os.CreateTemp("", "")
436467
if err != nil {
437-
return nil, err
468+
return "", err
438469
}
470+
defer f.Close() // Close file immediately after writing
471+
439472
_, err = f.Write(data)
440473
if err != nil {
441-
return nil, err
474+
os.Remove(f.Name()) // Clean up on write error
475+
return "", err
442476
}
443-
return f, nil
477+
478+
return f.Name(), nil
444479
}
445480

446481
// generateAction generates an action function using action.Configuration

0 commit comments

Comments
 (0)