Skip to content

Commit e2908f4

Browse files
committed
feat: Narc kan nå bruke kubeconfig pakken
1 parent c816723 commit e2908f4

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

Diff for: pkg/kubeconfig/filter.go

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type filterOptions struct {
77
includeOnprem bool
88
overwrite bool
99
verbose bool
10+
prefixWithTenants bool
1011
excludeClusters []string
1112
}
1213

@@ -18,12 +19,14 @@ func WithFromScratch(enabled bool) FilterOption {
1819
}
1920
}
2021

22+
// WithCiClusters is used by Narc
2123
func WithCiClusters(include bool) FilterOption {
2224
return func(options *filterOptions) {
2325
options.includeCi = include
2426
}
2527
}
2628

29+
// WithManagementClusters is used by Narc
2730
func WithManagementClusters(include bool) FilterOption {
2831
return func(options *filterOptions) {
2932
options.includeManagement = include
@@ -48,6 +51,13 @@ func WithOverwriteData(enabled bool) FilterOption {
4851
}
4952
}
5053

54+
// WithPrefixedTenants is used by Narc
55+
func WithPrefixedTenants(prefix bool) FilterOption {
56+
return func(options *filterOptions) {
57+
options.prefixWithTenants = prefix
58+
}
59+
}
60+
5161
func WithVerboseLogging(enabled bool) FilterOption {
5262
return func(options *filterOptions) {
5363
options.verbose = enabled

Diff for: pkg/kubeconfig/gcpcluster.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"slices"
7+
"strings"
88

99
"google.golang.org/api/compute/v1"
1010
"google.golang.org/api/container/v1"
@@ -54,7 +54,7 @@ func getClusters(ctx context.Context, projects []project, options filterOptions)
5454
case kindOnprem:
5555
cluster, err = getOnpremClusters(ctx, project)
5656
default:
57-
cluster, err = getGCPClusters(ctx, project)
57+
cluster, err = getGCPClusters(ctx, project, options)
5858
}
5959

6060
if err != nil {
@@ -66,7 +66,7 @@ func getClusters(ctx context.Context, projects []project, options filterOptions)
6666
return clusters, nil
6767
}
6868

69-
func getGCPClusters(ctx context.Context, project project) ([]k8sCluster, error) {
69+
func getGCPClusters(ctx context.Context, project project, options filterOptions) ([]k8sCluster, error) {
7070
svc, err := container.NewService(ctx)
7171
if err != nil {
7272
return nil, err
@@ -82,12 +82,21 @@ func getGCPClusters(ctx context.Context, project project) ([]k8sCluster, error)
8282
for _, cluster := range response.Clusters {
8383
name := cluster.Name
8484

85-
if project.Tenant == "nav" && cluster.Name == "nais-dev" {
86-
name = "dev-gcp"
85+
if project.Tenant == "nav" {
86+
switch cluster.Name {
87+
case "nais-dev":
88+
name = "dev-gcp"
89+
case "nais-prod":
90+
name = "prod-gcp"
91+
}
8792
}
8893

89-
if project.Tenant == "nav" && cluster.Name == "nais-prod" {
90-
name = "prod-gcp"
94+
if options.prefixWithTenants {
95+
name = project.Tenant + "-" + strings.ReplaceAll(cluster.Name, "nais-", "")
96+
97+
if cluster.Name == "nais-io" {
98+
name = "nais-io" // ReplaceAll vil fjerne 'nais-' fra 'nais-io'
99+
}
91100
}
92101

93102
clusters = append(clusters, k8sCluster{

Diff for: pkg/kubeconfig/gcpproject.go

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ func getProjects(ctx context.Context, options filterOptions) ([]project, error)
5454
}
5555

5656
for _, p := range response.Projects {
57+
if p.State != "ACTIVE" {
58+
// Only check active projects. When a project is deleted,
59+
// it is marked as DELETING for a while before it is removed.
60+
// This results in a 403 when trying to list clusters.
61+
continue
62+
}
63+
5764
projects = append(projects, project{
5865
ID: p.ProjectId,
5966
Tenant: p.Labels["tenant"],

0 commit comments

Comments
 (0)