Skip to content

Commit eaf953c

Browse files
committed
feat: support service configuration in Kubernetes utilities
1 parent d4a585b commit eaf953c

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@
4242
"typescript": "^5.9.3",
4343
"typescript-eslint": "^8.54.0"
4444
},
45-
"packageManager": "pnpm@10.28.2"
45+
"packageManager": "pnpm@10.28.2",
46+
"volta": {
47+
"node": "24.14.0"
48+
}
4649
}

src/k8s.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,24 @@ export interface KubernetesApplicationArgs {
263263
iap?: Input<Record<string, unknown>>;
264264
securityPolicy?: Input<string>;
265265
}>;
266+
service?: KubernetesServiceArgs;
266267
spot?: {
267268
enabled: boolean;
268269
weight?: number;
269270
required?: boolean;
270271
};
271272
}
272273

274+
export interface KubernetesServiceArgs {
275+
type?: k8s.types.enums.core.v1.ServiceSpecType;
276+
annotations?: Record<string, Input<string>>;
277+
loadBalancerSourceRanges?: Input<string[]>;
278+
gkeInternalLoadBalancer?: {
279+
enabled: boolean;
280+
allowGlobalAccess?: boolean;
281+
};
282+
}
283+
273284
export interface KubernetesApplicationReturn {
274285
labels: Input<Record<string, Input<string>>>;
275286
deployment: k8s.apps.v1.Deployment;
@@ -496,6 +507,7 @@ export const createAutoscaledExposedApplication = ({
496507
shouldCreatePDB = true,
497508
provider,
498509
serviceType = 'ClusterIP',
510+
service,
499511
strategy = {
500512
type: 'RollingUpdate',
501513
rollingUpdate: {
@@ -511,14 +523,34 @@ export const createAutoscaledExposedApplication = ({
511523
serviceType?: k8s.types.enums.core.v1.ServiceSpecType;
512524
}): KubernetesApplicationReturn & { service: k8s.core.v1.Service } => {
513525
const { resourcePrefix = '', name, namespace } = args;
526+
const resolvedServiceType = service?.type ?? serviceType;
527+
if (
528+
service?.loadBalancerSourceRanges &&
529+
resolvedServiceType !== 'LoadBalancer'
530+
) {
531+
throw new Error(
532+
'service.loadBalancerSourceRanges is supported only when service type is LoadBalancer',
533+
);
534+
}
514535
const returnObj = createAutoscaledApplication({
515536
...args,
516537
shouldCreatePDB,
517538
provider,
518539
strategy,
519540
});
520541
const { labels } = returnObj;
521-
const annotations: Record<string, Output<string>> = {};
542+
const annotations: Record<string, Input<string>> = {
543+
...(service?.annotations ?? {}),
544+
};
545+
if (service?.gkeInternalLoadBalancer?.enabled) {
546+
annotations['cloud.google.com/load-balancer-type'] = 'Internal';
547+
annotations['networking.gke.io/load-balancer-type'] = 'Internal';
548+
if (service.gkeInternalLoadBalancer.allowGlobalAccess) {
549+
annotations[
550+
'networking.gke.io/internal-load-balancer-allow-global-access'
551+
] = 'true';
552+
}
553+
}
522554
if (enableCdn || serviceTimeout || backendConfig) {
523555
const rawSpec: Record<string, unknown> = {};
524556
if (enableCdn) {
@@ -579,7 +611,7 @@ export const createAutoscaledExposedApplication = ({
579611
? servicePorts
580612
: [{ port: 80, targetPort: 'http', protocol: 'TCP', name: 'http' }];
581613

582-
const service = new k8s.core.v1.Service(
614+
const serviceResource = new k8s.core.v1.Service(
583615
`${resourcePrefix}service`,
584616
{
585617
metadata: {
@@ -589,14 +621,15 @@ export const createAutoscaledExposedApplication = ({
589621
annotations,
590622
},
591623
spec: {
592-
type: serviceType,
624+
type: resolvedServiceType,
593625
ports,
594626
selector: labels,
627+
loadBalancerSourceRanges: service?.loadBalancerSourceRanges,
595628
},
596629
},
597630
{ provider, dependsOn: [returnObj.deployment] },
598631
);
599-
return { ...returnObj, service };
632+
return { ...returnObj, service: serviceResource };
600633
};
601634

602635
export function createKubernetesSecretFromRecord({

src/suite/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ function deployApplication(
303303
env = [],
304304
createService,
305305
serviceType,
306+
service,
306307
metric,
307308
labels,
308309
command,
@@ -385,6 +386,7 @@ function deployApplication(
385386
return createAutoscaledExposedApplication({
386387
...appArgs,
387388
serviceType,
389+
service,
388390
enableCdn,
389391
serviceTimeout,
390392
backendConfig,

src/suite/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export interface ApplicationArgs {
4444
env?: Input<k8s.types.input.core.v1.EnvVar>[];
4545
createService?: boolean;
4646
isApi?: boolean;
47+
/** @deprecated prefer service.type */
4748
serviceType?: k8s.types.enums.core.v1.ServiceSpecType;
49+
service?: KubernetesApplicationArgs['service'];
4850
metric: CustomMetric;
4951
labels?: Record<string, string>;
5052
command?: Input<Input<string>[]>;

0 commit comments

Comments
 (0)