Skip to content

Commit

Permalink
support context concatenation for self contained wc client certificat…
Browse files Browse the repository at this point in the history
…es (#853)
  • Loading branch information
anvddriesch committed Jul 8, 2022
1 parent 3bf3a00 commit 4af5010
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

## [Unreleased]

### Added

- In the `login` command, allow concatenation of contexts in destination file when creating WC client certificates with `--self-contained` flag.

## [2.17.0] - 2022-07-07

### Added
Expand Down
18 changes: 15 additions & 3 deletions cmd/login/clientcert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/giantswarm/backoff"
"github.com/giantswarm/k8smetadata/pkg/label"
"github.com/giantswarm/microerror"
"github.com/imdario/mergo"
"github.com/spf13/afero"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -354,11 +355,22 @@ func printWCClientCertCredentials(k8sConfigAccess clientcmd.ConfigAccess, fs afe
},
CurrentContext: contextName,
}
if exists, err := afero.Exists(fs, c.filePath); exists {
return "", false, microerror.Maskf(fileExistsError, "The destination file %s already exists. Please specify a different destination.", c.filePath)
} else if err != nil {
// If the destination file exists, we merge the contexts contained in it with the newly created one
exists, err := afero.Exists(fs, c.filePath)
if err != nil {
return "", false, microerror.Mask(err)
}
if exists {
existingKubeConfig, err := clientcmd.LoadFromFile(c.filePath)
if err != nil {
return "", false, microerror.Mask(err)
}
err = mergo.Merge(&kubeconfig, existingKubeConfig, mergo.WithOverride)
if err != nil {
return "", false, microerror.Mask(err)
}
kubeconfig.CurrentContext = contextName
}
err = clientcmd.WriteToFile(kubeconfig, c.filePath)
if err != nil {
return "", false, microerror.Mask(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/giantswarm/micrologger v0.6.0
github.com/giantswarm/release-operator/v3 v3.2.0
github.com/google/go-cmp v0.5.8
github.com/imdario/mergo v0.3.12
github.com/pkg/errors v0.9.1
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
Expand Down Expand Up @@ -79,7 +80,6 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down

0 comments on commit 4af5010

Please sign in to comment.