-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
endpoint.api.ts
129 lines (104 loc) · 2.52 KB
/
endpoint.api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { autobind } from "../../utils";
import { KubeObject } from "../kube-object";
import { KubeApi } from "../kube-api";
export interface IEndpointPort {
name?: string;
protocol: string;
port: number;
}
export interface IEndpointAddress {
hostname: string;
ip: string;
nodeName: string;
}
export interface IEndpointSubset {
addresses: IEndpointAddress[];
notReadyAddresses: IEndpointAddress[];
ports: IEndpointPort[];
}
interface ITargetRef {
kind: string;
namespace: string;
name: string;
uid: string;
resourceVersion: string;
apiVersion: string;
}
export class EndpointAddress implements IEndpointAddress {
hostname: string;
ip: string;
nodeName: string;
targetRef?: {
kind: string;
namespace: string;
name: string;
uid: string;
resourceVersion: string;
};
constructor(data: IEndpointAddress) {
Object.assign(this, data);
}
getId() {
return this.ip;
}
getName() {
return this.hostname;
}
getTargetRef(): ITargetRef {
if (this.targetRef) {
return Object.assign(this.targetRef, {apiVersion: "v1"});
} else {
return null;
}
}
}
export class EndpointSubset implements IEndpointSubset {
addresses: IEndpointAddress[];
notReadyAddresses: IEndpointAddress[];
ports: IEndpointPort[];
constructor(data: IEndpointSubset) {
Object.assign(this, data);
}
getAddresses(): EndpointAddress[] {
const addresses = this.addresses || [];
return addresses.map(a => new EndpointAddress(a));
}
getNotReadyAddresses(): EndpointAddress[] {
const notReadyAddresses = this.notReadyAddresses || [];
return notReadyAddresses.map(a => new EndpointAddress(a));
}
toString(): string {
if(!this.addresses) {
return "";
}
return this.addresses.map(address => {
if (!this.ports) {
return address.ip;
}
return this.ports.map(port => {
return `${address.ip}:${port.port}`;
}).join(", ");
}).join(", ");
}
}
@autobind()
export class Endpoint extends KubeObject {
static kind = "Endpoints";
static namespaced = true;
static apiBase = "/api/v1/endpoints";
subsets: IEndpointSubset[];
getEndpointSubsets(): EndpointSubset[] {
const subsets = this.subsets || [];
return subsets.map(s => new EndpointSubset(s));
}
toString(): string {
if(this.subsets) {
return this.getEndpointSubsets().map(es => es.toString()).join(", ");
} else {
return "<none>";
}
}
}
export const endpointApi = new KubeApi({
objectConstructor: Endpoint,
});