Skip to content
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

Enh/target cluster by alias #185

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
enh: allow targeting by alias
  • Loading branch information
sven-petersen committed Dec 12, 2022
commit 88d25ed8c76b9d60ab156c87bff9e69372e8804d
26 changes: 22 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Config struct {
type Garden struct {
// Name is a unique identifier of this Garden that can be used to target this Garden
Name string `yaml:"identity" json:"identity"`
// Alias is a unique identifier of this Garden that can be used as an alternate name to target this Garden
// +optional
Alias string `yaml:"alias" json:"alias"`
// Kubeconfig holds the path for the kubeconfig of the garden cluster
Kubeconfig string `yaml:"kubeconfig" json:"kubeconfig"`
// Context overrides the current-context of the garden cluster kubeconfig
Expand Down Expand Up @@ -148,14 +151,29 @@ func (config *Config) GardenNames() []string {
return names
}

// Garden returns a Garden cluster from the list of configured Gardens.
// Garden returns a Garden cluster by name (identity or alias) from the list of configured Gardens.
func (config *Config) Garden(name string) (*Garden, error) {
i, ok := config.IndexOfGarden(name)
if !ok {
var gardenConfig *Garden

for idx := range config.Gardens {
cfg := &config.Gardens[idx]

if name != cfg.Name && name != cfg.Alias {
continue
}

if gardenConfig != nil {
return nil, fmt.Errorf("identity or alias %q must be unique but was found multiple times in gardenctl configuration", cfg.Name)
}

gardenConfig = cfg
}

if gardenConfig == nil {
return nil, fmt.Errorf("garden %q is not defined in gardenctl configuration", name)
}

return &config.Gardens[i], nil
return gardenConfig, nil
}

// ClientConfig returns a deferred loading client config for a configured garden cluster.
Expand Down
19 changes: 13 additions & 6 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var _ = Describe("Config", func() {
var (
clusterIdentity1 = "garden1"
clusterIdentity2 = "garden2"
clusterAlias1 = "gardenalias1"
clusterAlias2 = "gardenalias2"
fooIdentity = "fooGarden"
project = "fooProject"
shoot = "fooShoot"
Expand All @@ -34,14 +36,16 @@ var _ = Describe("Config", func() {
LinkKubeconfig: pointer.Bool(false),
Gardens: []config.Garden{
{
Name: clusterIdentity1,
Name: clusterIdentity1,
Alias: clusterAlias1,
Patterns: []string{
fmt.Sprintf("^%s/shoot--(?P<project>.+)--(?P<shoot>.+)$", clusterIdentity1),
"^shoot--(?P<project>.+)--(?P<shoot>.+)$",
},
},
{
Name: clusterIdentity2,
Name: clusterIdentity2,
Alias: clusterAlias2,
Patterns: []string{
fmt.Sprintf("^(%s/)?shoot--(?P<project>.+)--(?P<shoot>.+)$", clusterIdentity2),
},
Expand Down Expand Up @@ -122,11 +126,14 @@ var _ = Describe("Config", func() {
fmt.Sprintf("garden %q is not defined in gardenctl configuration", fooIdentity)),
)

It("should find garden by identity", func() {
garden, err := cfg.Garden(clusterIdentity1)
DescribeTable("Should find garden by identity and alias", func(name, identityOrAlias string) {
garden, err := cfg.Garden(name)
Expect(err).NotTo(HaveOccurred())
Expect(garden.Name).Should(Equal(clusterIdentity1))
})
Expect(garden.Name).Should(Equal(identityOrAlias))
},
Entry("should find garden by identity", clusterIdentity1, clusterIdentity1),
Entry("should find garden by alias", clusterAlias2, clusterIdentity2),
)

It("should throw an error if garden not found", func() {
_, err := cfg.Garden("foobar")
Expand Down