Skip to content

Commit affbf60

Browse files
authored
add detection for CRC to clusterdefaults (#109)
1 parent 362b2ca commit affbf60

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

internal/clusterdefaults/clusterdefaults.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const (
1818
ClusterTypeMinikube
1919
// ClusterTypeK3s represents a K3s cluster
2020
ClusterTypeK3s
21+
// ClusterTypeCRC represents a CRC (CodeReady Containers) cluster
22+
ClusterTypeCRC
2123
)
2224

2325
// String returns the string representation of a ClusterType
@@ -29,6 +31,8 @@ func (ct ClusterType) String() string {
2931
return "minikube"
3032
case ClusterTypeK3s:
3133
return "k3s"
34+
case ClusterTypeCRC:
35+
return "crc"
3236
default:
3337
return "unknown"
3438
}
@@ -139,6 +143,11 @@ func (d *defaultDetector) Detect(kubeContext string) ClusterType {
139143
return ClusterTypeK3s
140144
}
141145

146+
// CRC (CodeReady Containers) contexts start with "crc" or contain "-crc-"/"_crc_" as a segment
147+
if strings.HasPrefix(contextLower, "crc") || strings.Contains(contextLower, "-crc-") || strings.Contains(contextLower, "-crc:") {
148+
return ClusterTypeCRC
149+
}
150+
142151
return ClusterTypeUnknown
143152
}
144153

@@ -198,6 +207,13 @@ func getDefaultsForClusterType(clusterType ClusterType) (DeploymentDefaults, boo
198207
PortForwardEnabled: true,
199208
}, true
200209

210+
case ClusterTypeCRC:
211+
return DeploymentDefaults{
212+
Resources: "small",
213+
Exposure: "none",
214+
PortForwardEnabled: true,
215+
}, true
216+
201217
default:
202218
return DeploymentDefaults{}, false
203219
}

internal/clusterdefaults/clusterdefaults_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ func TestDefaultDetector_Detect(t *testing.T) {
2727
kubeContext: "KIND-test",
2828
want: ClusterTypeKind,
2929
},
30+
{
31+
name: "crc cluster with admin context",
32+
kubeContext: "crc-admin",
33+
want: ClusterTypeCRC,
34+
},
35+
{
36+
name: "crc cluster with api prefix",
37+
kubeContext: "api-crc-testing:6443",
38+
want: ClusterTypeCRC,
39+
},
40+
{
41+
name: "crc cluster with uppercase",
42+
kubeContext: "CRC-admin",
43+
want: ClusterTypeCRC,
44+
},
45+
{
46+
name: "crc cluster bare name",
47+
kubeContext: "crc",
48+
want: ClusterTypeCRC,
49+
},
50+
{
51+
name: "not crc - incidental substring",
52+
kubeContext: "acrc-cluster",
53+
want: ClusterTypeUnknown,
54+
},
55+
{
56+
name: "not crc - encrypted in name",
57+
kubeContext: "my-encrypted-cluster",
58+
want: ClusterTypeUnknown,
59+
},
3060
}
3161

3262
detector := &defaultDetector{}
@@ -108,6 +138,17 @@ func TestDefaultApplicator_Apply(t *testing.T) {
108138
wantPortForward: true,
109139
wantChanged: true,
110140
},
141+
{
142+
name: "crc cluster",
143+
clusterType: ClusterTypeCRC,
144+
resources: "default",
145+
exposure: "loadbalancer",
146+
portForwardEnabled: false,
147+
wantResources: "small",
148+
wantExposure: "none",
149+
wantPortForward: true,
150+
wantChanged: true,
151+
},
111152
}
112153

113154
applicator := &defaultApplicator{}
@@ -158,6 +199,16 @@ func TestManager_ApplyConvenienceDefaults(t *testing.T) {
158199
wantExposure: "none",
159200
wantPortForward: true,
160201
},
202+
{
203+
name: "crc cluster detection and defaults",
204+
kubeContext: "crc-admin",
205+
resources: "default",
206+
exposure: "loadbalancer",
207+
portForwardEnabled: false,
208+
wantResources: "small",
209+
wantExposure: "none",
210+
wantPortForward: true,
211+
},
161212
{
162213
name: "gke cluster no changes",
163214
kubeContext: "gke_project_zone_cluster",
@@ -203,6 +254,7 @@ func TestClusterType_String(t *testing.T) {
203254
{ClusterTypeKind, "kind"},
204255
{ClusterTypeMinikube, "minikube"},
205256
{ClusterTypeK3s, "k3s"},
257+
{ClusterTypeCRC, "crc"},
206258
{ClusterTypeUnknown, "unknown"},
207259
}
208260

0 commit comments

Comments
 (0)