diff --git a/src/common/k8s-api/endpoints/stateful-set.api.ts b/src/common/k8s-api/endpoints/stateful-set.api.ts index 1d6895f934..8d8e9553b3 100644 --- a/src/common/k8s-api/endpoints/stateful-set.api.ts +++ b/src/common/k8s-api/endpoints/stateful-set.api.ts @@ -3,6 +3,8 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ +import moment from "moment"; + import type { DerivedKubeApiOptions, IgnoredKubeApiOptions } from "../kube-api"; import { KubeApi } from "../kube-api"; import type { LabelSelector, NamespaceScopedMetadata } from "../kube-object"; @@ -42,6 +44,25 @@ export class StatefulSetApi extends KubeApi { }, }); } + + restart(params: { namespace: string; name: string }) { + return this.request.patch(this.getUrl(params), { + data: { + spec: { + template: { + metadata: { + annotations: { "kubectl.kubernetes.io/restartedAt" : moment.utc().format() }, + }, + }, + }, + }, + }, + { + headers: { + "content-type": "application/strategic-merge-patch+json", + }, + }); + } } export interface StatefulSetSpec { diff --git a/src/renderer/components/+workloads-statefulsets/statefulset-menu.tsx b/src/renderer/components/+workloads-statefulsets/statefulset-menu.tsx new file mode 100644 index 0000000000..942c9674e6 --- /dev/null +++ b/src/renderer/components/+workloads-statefulsets/statefulset-menu.tsx @@ -0,0 +1,73 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import React from "react"; +import type { KubeObjectMenuProps } from "../kube-object-menu"; +import type { StatefulSet, StatefulSetApi } from "../../../common/k8s-api/endpoints"; +import { MenuItem } from "../menu"; +import { Icon } from "../icon"; +import { withInjectables } from "@ogre-tools/injectable-react"; +import statefulSetApiInjectable from "../../../common/k8s-api/endpoints/stateful-set.api.injectable"; +import type { OpenConfirmDialog } from "../confirm-dialog/open.injectable"; +import openConfirmDialogInjectable from "../confirm-dialog/open.injectable"; +import type { ShowCheckedErrorNotification } from "../notifications/show-checked-error.injectable"; +import showCheckedErrorNotificationInjectable from "../notifications/show-checked-error.injectable"; + +export interface StatefulSetMenuProps extends KubeObjectMenuProps {} + +interface Dependencies { + statefulsetApi: StatefulSetApi; + openConfirmDialog: OpenConfirmDialog; + showCheckedErrorNotification: ShowCheckedErrorNotification; +} + +const NonInjectedStatefulSetMenu = ({ + statefulsetApi, + object, + toolbar, + showCheckedErrorNotification, + openConfirmDialog, +}: Dependencies & StatefulSetMenuProps) => ( + <> + openConfirmDialog({ + ok: async () => + { + try { + await statefulsetApi.restart({ + namespace: object.getNs(), + name: object.getName(), + }); + } catch (err) { + showCheckedErrorNotification(err, "Unknown error occured while restarting statefulset"); + } + }, + labelOk: "Restart", + message: ( +

+ {"Are you sure you want to restart statefulset "} + {object.getName()} + ? +

+ ), + })} + > + + Restart +
+ +); + +export const StatefulSetMenu = withInjectables(NonInjectedStatefulSetMenu, { + getProps: (di, props) => ({ + ...props, + showCheckedErrorNotification: di.inject(showCheckedErrorNotificationInjectable), + statefulsetApi: di.inject(statefulSetApiInjectable), + openConfirmDialog: di.inject(openConfirmDialogInjectable), + }), +}); diff --git a/src/renderer/components/kube-object-menu/kube-object-menu-items/statefulset-menu.injectable.ts b/src/renderer/components/kube-object-menu/kube-object-menu-items/statefulset-menu.injectable.ts new file mode 100644 index 0000000000..c4376f81fb --- /dev/null +++ b/src/renderer/components/kube-object-menu/kube-object-menu-items/statefulset-menu.injectable.ts @@ -0,0 +1,25 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import type { KubeObjectMenuItemComponent } from "../kube-object-menu-item-injection-token"; +import { kubeObjectMenuItemInjectionToken } from "../kube-object-menu-item-injection-token"; +import { computed } from "mobx"; +import { StatefulSetMenu } from "../../+workloads-statefulsets/statefulset-menu"; + +const statefulsetMenuInjectable = getInjectable({ + id: "statefulset-menu-kube-object-menu", + + instantiate: () => ({ + kind: "StatefulSet", + apiVersions: ["apps/v1"], + Component: StatefulSetMenu as KubeObjectMenuItemComponent, + enabled: computed(() => true), + orderNumber: 30, + }), + + injectionToken: kubeObjectMenuItemInjectionToken, +}); + +export default statefulsetMenuInjectable;