@@ -2,13 +2,17 @@ import '../styles.scss'
2
2
import { Client , KubeClientBuilder } from '@ccremer/kubernetes-client/fetch'
3
3
import { newSelfSubjectRulesReview } from './types'
4
4
import { createAlert } from './alerts'
5
+ import { WatchEvent } from '@ccremer/kubernetes-client/api'
6
+ import { KubeObject } from '@ccremer/kubernetes-client/types/core'
5
7
6
8
console . debug ( 'Starting up...' )
7
9
8
10
window . onload = function ( ) {
9
11
document . getElementById ( 'createClient' ) ?. addEventListener ( 'click' , createClient )
10
12
document . getElementById ( 'listBtn' ) ?. addEventListener ( 'click' , listObjects )
11
13
document . getElementById ( 'getBtn' ) ?. addEventListener ( 'click' , getObject )
14
+ document . getElementById ( 'watchBtn' ) ?. addEventListener ( 'click' , watchObjects )
15
+ document . getElementById ( 'clearOutputBtn' ) ?. addEventListener ( 'click' , clearOutput )
12
16
const tokenElement = document . getElementById ( 'token' )
13
17
if ( tokenElement instanceof HTMLInputElement ) {
14
18
tokenElement . value = localStorage . getItem ( 'token' ) ?? ''
@@ -62,7 +66,7 @@ function getObject(): void {
62
66
const name = getName ( )
63
67
64
68
if ( name ) {
65
- console . debug ( 'Fetching Object' , `${ kind } ${ namespace } /${ name } ` )
69
+ console . debug ( 'Fetching Object' , `${ kind } / ${ namespace } /${ name } ` )
66
70
kubeClient
67
71
. getById ( 'v1' , kind , name , namespace , { hideManagedFields : hideManagedFields ( ) } )
68
72
. then ( ( cm ) => fillTextArea ( cm ) )
@@ -72,6 +76,53 @@ function getObject(): void {
72
76
}
73
77
}
74
78
79
+ let abortController : AbortController | undefined
80
+
81
+ function watchObjects ( ) : void {
82
+ if ( ! kubeClient ) return
83
+ if ( abortController ) {
84
+ abortController . abort ( 'Stop' )
85
+ abortController = undefined
86
+ toggleWatchButton ( false )
87
+ return
88
+ }
89
+
90
+ const kind = getKind ( )
91
+ const namespace = getNamespace ( )
92
+ const name = getName ( )
93
+
94
+ const events : WatchEvent < KubeObject > [ ] = [ ]
95
+
96
+ console . debug ( 'Watching Objects in' , `${ kind } /${ namespace } ` )
97
+ kubeClient
98
+ . watchByID (
99
+ {
100
+ onUpdate : ( event ) => {
101
+ if ( event ) {
102
+ events . push ( event )
103
+ fillTextArea ( events )
104
+ }
105
+ } ,
106
+ onError : ( err , effect ) => {
107
+ console . log ( 'received err' , err )
108
+ if ( err instanceof Error ) createAlert ( `Watch failed: ${ err . message } ` , 'danger' )
109
+ if ( typeof err === 'string' && err === 'Stop' ) createAlert ( 'Watch stopped' , 'warning' , 3000 )
110
+ if ( effect ?. closed ) toggleWatchButton ( false )
111
+ } ,
112
+ } ,
113
+ 'v1' ,
114
+ kind ,
115
+ name ,
116
+ namespace ,
117
+ { hideManagedFields : hideManagedFields ( ) }
118
+ )
119
+ . then ( ( result ) => {
120
+ abortController = result . abortController
121
+ toggleWatchButton ( true )
122
+ } )
123
+ . catch ( ( err ) => createAlert ( err . message , 'danger' ) )
124
+ }
125
+
75
126
function getNamespace ( ) : string {
76
127
const namespaceInput = document . getElementById ( 'namespace' )
77
128
return namespaceInput instanceof HTMLInputElement ? namespaceInput . value : 'default'
@@ -100,9 +151,22 @@ function fillTextArea(value: unknown): void {
100
151
}
101
152
}
102
153
154
+ function clearOutput ( ) : void {
155
+ const textArea = document . getElementById ( 'kubeobject' )
156
+ if ( textArea instanceof HTMLTextAreaElement ) {
157
+ textArea . value = ''
158
+ }
159
+ }
160
+
103
161
function enableDemo ( ) : void {
104
162
const container = document . getElementById ( 'demo-container' )
105
163
if ( container ) {
106
164
container . className = container . className . replace ( 'visually-hidden' , '' )
107
165
}
108
166
}
167
+
168
+ function toggleWatchButton ( isWatching : boolean ) : void {
169
+ const btn = document . getElementById ( 'watchBtn' )
170
+ if ( ! btn ) return
171
+ btn . innerText = isWatching ? 'Stop' : 'Watch'
172
+ }
0 commit comments