Release/v5.3.2 (#4479)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-12-02 09:01:29 -05:00
1 parent 63256dcaf9
commit e79e8eb76c
15 files changed
+429 -171

No files matched your search

+139 -1
View File
@@ -33,7 +33,7 @@ class TestKubeObject extends KubeObject {
static apiBase = "/api/v1/pods";
}
class TestKubeApi extends KubeApi<TestKubeObject> {}
class TestKubeApi extends KubeApi<TestKubeObject> { }
describe("forRemoteCluster", () => {
it("builds api client for KubeObject", async () => {
@@ -474,4 +474,142 @@ describe("KubeApi", () => {
});
});
});
describe("create", () => {
let api: TestKubeApi;
beforeEach(() => {
api = new TestKubeApi({
request,
objectConstructor: TestKubeObject,
});
});
it("should add kind and apiVersion", async () => {
expect.hasAssertions();
(fetch as any).mockResponse(async (request: Request) => {
expect(request.method).toEqual("POST");
expect(JSON.parse(request.body.toString())).toEqual({
kind: "Pod",
apiVersion: "v1",
metadata: {
name: "foobar",
namespace: "default",
},
spec: {
containers: [
{
name: "web",
image: "nginx",
ports: [
{
name: "web",
containerPort: 80,
protocol: "TCP",
},
],
},
],
},
});
return {};
});
await api.create({
name: "foobar",
namespace: "default",
}, {
spec: {
containers: [
{
name: "web",
image: "nginx",
ports: [
{
name: "web",
containerPort: 80,
protocol: "TCP",
},
],
},
],
},
});
});
it("doesn't override metadata.labels", async () => {
expect.hasAssertions();
(fetch as any).mockResponse(async (request: Request) => {
expect(request.method).toEqual("POST");
expect(JSON.parse(request.body.toString())).toEqual({
kind: "Pod",
apiVersion: "v1",
metadata: {
name: "foobar",
namespace: "default",
labels: {
foo: "bar",
},
},
});
return {};
});
await api.create({
name: "foobar",
namespace: "default",
}, {
metadata: {
labels: {
foo: "bar",
},
},
});
});
});
describe("update", () => {
let api: TestKubeApi;
beforeEach(() => {
api = new TestKubeApi({
request,
objectConstructor: TestKubeObject,
});
});
it("doesn't override metadata.labels", async () => {
expect.hasAssertions();
(fetch as any).mockResponse(async (request: Request) => {
expect(request.method).toEqual("PUT");
expect(JSON.parse(request.body.toString())).toEqual({
metadata: {
name: "foobar",
namespace: "default",
labels: {
foo: "bar",
},
},
});
return {};
});
await api.update({
name: "foobar",
namespace: "default",
}, {
metadata: {
labels: {
foo: "bar",
},
},
});
});
});
});
@@ -27,6 +27,7 @@ import { metricsApi } from "./metrics.api";
import type { KubeJsonApiData } from "../kube-json-api";
import type { IPodContainer, IPodMetrics } from "./pods.api";
import { isClusterPageContext } from "../../utils/cluster-id-url-parsing";
import type { LabelSelector } from "../kube-object";
export class DaemonSet extends WorkloadKubeObject {
static kind = "DaemonSet";
@@ -39,11 +40,7 @@ export class DaemonSet extends WorkloadKubeObject {
}
declare spec: {
selector: {
matchLabels: {
[name: string]: string;
};
};
selector: LabelSelector;
template: {
metadata: {
creationTimestamp?: string;
@@ -28,6 +28,7 @@ import { metricsApi } from "./metrics.api";
import type { IPodMetrics } from "./pods.api";
import type { KubeJsonApiData } from "../kube-json-api";
import { isClusterPageContext } from "../../utils/cluster-id-url-parsing";
import type { LabelSelector } from "../kube-object";
export class DeploymentApi extends KubeApi<Deployment> {
protected getScaleApiUrl(params: { namespace: string; name: string }) {
@@ -122,7 +123,7 @@ export class Deployment extends WorkloadKubeObject {
declare spec: {
replicas: number;
selector: { matchLabels: { [app: string]: string }};
selector: LabelSelector;
template: {
metadata: {
creationTimestamp?: string;
+2 -5
View File
@@ -27,6 +27,7 @@ import { metricsApi } from "./metrics.api";
import type { KubeJsonApiData } from "../kube-json-api";
import type { IPodContainer, IPodMetrics } from "./pods.api";
import { isClusterPageContext } from "../../utils/cluster-id-url-parsing";
import type { LabelSelector } from "../kube-object";
export class Job extends WorkloadKubeObject {
static kind = "Job";
@@ -42,11 +43,7 @@ export class Job extends WorkloadKubeObject {
parallelism?: number;
completions?: number;
backoffLimit?: number;
selector?: {
matchLabels: {
[name: string]: string;
};
};
selector?: LabelSelector;
template: {
metadata: {
creationTimestamp?: string;
@@ -19,7 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { KubeObject } from "../kube-object";
import { KubeObject, LabelSelector } from "../kube-object";
import { autoBind } from "../../utils";
import { KubeApi } from "../kube-api";
import type { KubeJsonApiData } from "../kube-json-api";
@@ -30,46 +30,94 @@ export interface IPolicyIpBlock {
except?: string[];
}
export interface IPolicySelector {
matchLabels: {
[label: string]: string;
};
/**
* @deprecated Use `LabelSelector` instead
*/
export type IPolicySelector = LabelSelector;
export interface NetworkPolicyPort {
/**
* The protocol which network traffic must match.
*
* One of:
* - `"TCP"`
* - `"UDP"`
* - `"SCTP"`
*
* @default "TCP"
*/
protocol?: string;
/**
* The port on the given protocol. This can either be a numerical or named
* port on a pod. If this field is not provided, this matches all port names and
* numbers.
*
* If present, only traffic on the specified protocol AND port will be matched.
*/
port?: number | string;
/**
* If set, indicates that the range of ports from port to endPort, inclusive,
* should be allowed by the policy. This field cannot be defined if the port field
* is not defined or if the port field is defined as a named (string) port.
*
* The endPort must be equal or greater than port.
*/
endPort?: number;
}
export interface NetworkPolicyPeer {
/**
* IPBlock defines policy on a particular IPBlock. If this field is set then
* neither of the other fields can be.
*/
ipBlock?: IPolicyIpBlock;
/**
* Selects Namespaces using cluster-scoped labels. This field follows standard label
* selector semantics; if present but empty, it selects all namespaces.
*
* If PodSelector is also set, then the NetworkPolicyPeer as a whole selects
* the Pods matching PodSelector in the Namespaces selected by NamespaceSelector.
*
* Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector.
*/
namespaceSelector?: LabelSelector;
/**
* This is a label selector which selects Pods. This field follows standard label
* selector semantics; if present but empty, it selects all pods.
*
* If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects
* the Pods matching PodSelector in the Namespaces selected by NamespaceSelector.
*
* Otherwise it selects the Pods matching PodSelector in the policy's own Namespace.
*/
podSelector?: LabelSelector;
}
export interface IPolicyIngress {
from: {
ipBlock?: IPolicyIpBlock;
namespaceSelector?: IPolicySelector;
podSelector?: IPolicySelector;
}[];
ports: {
protocol: string;
port: number;
}[];
from?: NetworkPolicyPeer[];
ports?: NetworkPolicyPort[];
}
export interface IPolicyEgress {
to: {
ipBlock: IPolicyIpBlock;
}[];
ports: {
protocol: string;
port: number;
}[];
to?: NetworkPolicyPeer[];
ports?: NetworkPolicyPort[];
}
export type PolicyType = "Ingress" | "Egress";
export interface NetworkPolicySpec {
podSelector: LabelSelector;
policyTypes?: PolicyType[];
ingress?: IPolicyIngress[];
egress?: IPolicyEgress[];
}
export interface NetworkPolicy {
spec: {
podSelector: {
matchLabels: {
[label: string]: string;
role: string;
};
};
policyTypes: string[];
ingress: IPolicyIngress[];
egress: IPolicyEgress[];
};
spec: NetworkPolicySpec;
}
export class NetworkPolicy extends KubeObject {
@@ -19,7 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { KubeObject } from "../kube-object";
import { KubeObject, LabelSelector } from "../kube-object";
import { autoBind } from "../../utils";
import { IMetrics, metricsApi } from "./metrics.api";
import type { Pod } from "./pods.api";
@@ -51,16 +51,7 @@ export interface PersistentVolumeClaim {
spec: {
accessModes: string[];
storageClassName: string;
selector: {
matchLabels: {
release: string;
};
matchExpressions: {
key: string; // environment,
operator: string; // In,
values: string[]; // [dev]
}[];
};
selector: LabelSelector;
resources: {
requests: {
storage: string; // 8Gi
@@ -20,7 +20,7 @@
*/
import { autoBind } from "../../utils";
import { KubeObject } from "../kube-object";
import { KubeObject, LabelSelector } from "../kube-object";
import { KubeApi } from "../kube-api";
import type { KubeJsonApiData } from "../kube-json-api";
import { isClusterPageContext } from "../../utils/cluster-id-url-parsing";
@@ -29,7 +29,7 @@ export interface PodDisruptionBudget {
spec: {
minAvailable: string;
maxUnavailable: string;
selector: { matchLabels: { [app: string]: string }};
selector: LabelSelector;
};
status: {
currentHealthy: number
@@ -27,6 +27,7 @@ import { metricsApi } from "./metrics.api";
import type { IPodContainer, IPodMetrics, Pod } from "./pods.api";
import type { KubeJsonApiData } from "../kube-json-api";
import { isClusterPageContext } from "../../utils/cluster-id-url-parsing";
import type { LabelSelector } from "../kube-object";
export class ReplicaSetApi extends KubeApi<ReplicaSet> {
protected getScaleApiUrl(params: { namespace: string; name: string }) {
@@ -78,7 +79,7 @@ export class ReplicaSet extends WorkloadKubeObject {
declare spec: {
replicas?: number;
selector: { matchLabels: { [app: string]: string }};
selector: LabelSelector;
template?: {
metadata: {
labels: {
@@ -26,6 +26,7 @@ import { metricsApi } from "./metrics.api";
import type { IPodMetrics } from "./pods.api";
import type { KubeJsonApiData } from "../kube-json-api";
import { isClusterPageContext } from "../../utils/cluster-id-url-parsing";
import type { LabelSelector } from "../kube-object";
export class StatefulSetApi extends KubeApi<StatefulSet> {
protected getScaleApiUrl(params: { namespace: string; name: string }) {
@@ -82,11 +83,7 @@ export class StatefulSet extends WorkloadKubeObject {
declare spec: {
serviceName: string;
replicas: number;
selector: {
matchLabels: {
[key: string]: string;
};
};
selector: LabelSelector;
template: {
metadata: {
labels: {
+15 -11
View File
@@ -21,7 +21,7 @@
// Base class for building all kubernetes apis
import { isFunction } from "lodash";
import { isFunction, merge } from "lodash";
import { stringify } from "querystring";
import { apiKubePrefix, isDevelopment } from "../../common/vars";
import logger from "../../main/logger";
@@ -102,7 +102,11 @@ export type PropagationPolicy = undefined | "Orphan" | "Foreground" | "Backgroun
/**
* @deprecated
*/
export interface IKubeApiCluster extends ILocalKubeApiConfig {}
export interface IKubeApiCluster extends ILocalKubeApiConfig { }
export type PartialKubeObject<T extends KubeObject> = Partial<Omit<T, "metadata">> & {
metadata?: Partial<T["metadata"]>,
};
export interface IRemoteKubeApiConfig {
cluster: {
@@ -478,18 +482,19 @@ export class KubeApi<T extends KubeObject> {
return parsed;
}
async create({ name, namespace }: Partial<ResourceDescriptor>, data?: Partial<T>): Promise<T | null> {
async create({ name, namespace }: Partial<ResourceDescriptor>, data?: PartialKubeObject<T>): Promise<T | null> {
await this.checkPreferredVersion();
const apiUrl = this.getUrl({ namespace });
const res = await this.request.post(apiUrl, {
data: {
...data,
data: merge(data, {
kind: this.kind,
apiVersion: this.apiVersionWithGroup,
metadata: {
name,
namespace,
},
},
}),
});
const parsed = this.parseResponse(res);
@@ -500,18 +505,17 @@ export class KubeApi<T extends KubeObject> {
return parsed;
}
async update({ name, namespace }: ResourceDescriptor, data: Partial<T>): Promise<T | null> {
async update({ name, namespace }: ResourceDescriptor, data: PartialKubeObject<T>): Promise<T | null> {
await this.checkPreferredVersion();
const apiUrl = this.getUrl({ namespace, name });
const res = await this.request.put(apiUrl, {
data: {
...data,
data: merge(data, {
metadata: {
name,
namespace,
},
},
}),
});
const parsed = this.parseResponse(res);
@@ -580,7 +584,7 @@ export class KubeApi<T extends KubeObject> {
clearTimeout(timedRetry);
});
const requestParams = timeout ? { query: { timeoutSeconds: timeout }}: {};
const requestParams = timeout ? { query: { timeoutSeconds: timeout }} : {};
const watchUrl = this.getWatchUrl(namespace);
const responsePromise = this.request.getResponse(watchUrl, requestParams, {
signal: abortController.signal,
+28
View File
@@ -104,6 +104,34 @@ export class KubeCreationError extends Error {
}
}
export type LabelMatchExpression = {
/**
* The label key that the selector applies to.
*/
key: string;
} & (
{
/**
* This represents the key's relationship to a set of values.
*/
operator: "Exists" | "DoesNotExist";
values?: undefined;
}
|
{
operator: "In" | "NotIn";
/**
* The set of values for to match according to the operator for the label.
*/
values: string[];
}
);
export interface LabelSelector {
matchLabels?: Record<string, string | undefined>;
matchExpressions?: LabelMatchExpression[];
}
export class KubeObject<Metadata extends KubeObjectMetadata = KubeObjectMetadata, Status = any, Spec = any> implements ItemObject {
static readonly kind?: string;
static readonly namespaced?: boolean;