55namespace Icinga \Module \Kubernetes \ProvidedHook \Notifications ;
66
77use Generator ;
8+ use Icinga \Module \Kubernetes \Common \Database ;
89use Icinga \Module \Kubernetes \Common \Factory ;
10+ use Icinga \Module \Kubernetes \Model \Cluster ;
911use Icinga \Module \Kubernetes \Web \ItemList \ResourceList ;
1012use Icinga \Module \Kubernetes \Web \Widget \KIcon ;
1113use Icinga \Module \Notifications \Hook \ObjectsRendererHook ;
1214use ipl \Html \Attributes ;
1315use ipl \Html \FormattedString ;
14- use ipl \Html \Html ;
16+ use ipl \Html \HtmlDocument ;
1517use ipl \Html \HtmlElement ;
1618use ipl \Html \Text ;
1719use ipl \Html \ValidHtml ;
@@ -45,10 +47,15 @@ public function createObjectLink(array $objectIdTag): ?ValidHtml
4547 return null ;
4648 }
4749
48- return ( new ResourceList (
50+ $ html = new ResourceList (
4951 Factory::fetchResource ($ objectIdTag ['resource ' ])
5052 ->filter (Filter::equal ('uuid ' , Uuid::fromString ($ objectIdTag ['uuid ' ])->getBytes ()))
51- ));
53+ );
54+ // TODO(el): Icinga Notifications Web now forcefully adds the target, which results in having it twice,
55+ // ultimately leading to JS errors.
56+ $ html ->removeAttribute ('data-base-target ' );
57+
58+ return $ html ;
5259 }
5360
5461 /**
@@ -61,147 +68,71 @@ public function createObjectLink(array $objectIdTag): ?ValidHtml
6168 */
6269 protected function yieldObjectsResults (array $ objectIdTags , bool $ asHtml ): Generator
6370 {
71+ $ clusterNames = [];
72+
6473 foreach ($ objectIdTags as $ idTags ) {
6574 if (! isset ($ idTags ['resource ' ]) || ! isset ($ idTags ['name ' ])) {
6675 continue ;
6776 }
6877
69- switch ($ idTags ['resource ' ]) {
70- case 'pod ' :
71- if (! $ asHtml ) {
72- yield $ idTags => sprintf (
73- $ this ->translate ('%s - %s ' , '<namespace> - <name> ' ),
74- $ idTags ['namespace ' ],
75- $ idTags ['name ' ]
76- );
77- } else {
78- yield $ idTags => Html::sprintf (
79- $ this ->translate ('%s ' , '<pod> ' ),
80- [
81- new HtmlElement (
82- 'span ' ,
83- new Attributes (['class ' => 'namespace-badge ' ]),
84- new KIcon ('namespace ' ),
85- new Text ($ idTags ['namespace ' ])
86- ),
87- new HtmlElement (
88- 'span ' ,
89- new Attributes (['class ' => 'subject ' ]),
90- new KIcon ('pod ' ),
91- new Text ($ idTags ['name ' ])
92- )
93- ]
94- );
95- }
78+ $ clusterName = 'default ' ;
79+ if (isset ($ idTags ['cluster_uuid ' ])) {
80+ $ clusterNames [$ idTags ['cluster_uuid ' ]] ??= Cluster::on (Database::connection ())
81+ ->columns ('name ' )
82+ ->filter (Filter::equal ('uuid ' , Uuid::fromString ($ idTags ['cluster_uuid ' ])->getBytes ()))
83+ ->first ()
84+ ?->name ?? $ idTags ['cluster_uuid ' ];
9685
97- break ;
98- case 'deployment ' :
99- if (! $ asHtml ) {
100- yield $ idTags => sprintf ($ this ->translate ('%s ' , '<deployment> ' ), $ idTags ['name ' ]);
101- } else {
102- yield $ idTags => Html::sprintf (
103- $ this ->translate ('%s ' , '<deployment> ' ),
104- [
105- new HtmlElement (
106- 'span ' ,
107- new Attributes (['class ' => 'namespace-badge ' ]),
108- new KIcon ('namespace ' ),
109- new Text ($ idTags ['namespace ' ])
110- ),
111- new HtmlElement (
112- 'span ' ,
113- new Attributes (['class ' => 'subject ' ]),
114- new KIcon ('deployment ' ),
115- new Text ($ idTags ['name ' ])
116- )
117- ]
118- );
119- }
86+ $ clusterName = $ clusterNames [$ idTags ['cluster_uuid ' ]] ?? $ idTags ['cluster_uuid ' ];
87+ }
12088
121- break ;
89+ switch ( $ idTags [ ' resource ' ]) {
12290 case 'node ' :
12391 if (! $ asHtml ) {
124- yield $ idTags => sprintf ($ this -> translate ( '%s ' , ' <node> ' ), $ idTags ['name ' ]);
92+ yield $ idTags => sprintf ('%s@%s ' , $ idTags ['name ' ], $ clusterName );
12593 } else {
126- yield $ idTags => Html::sprintf (
127- $ this ->translate ('%s ' , '<node> ' ),
128- new HtmlElement (
94+ yield $ idTags => (new HtmlDocument ())
95+ ->addHtml (new HtmlElement (
12996 'span ' ,
13097 new Attributes (['class ' => 'subject ' ]),
13198 new Icon ('share-nodes ' ),
13299 new Text ($ idTags ['name ' ])
133- )
134- );
135- }
136- break ;
137- case 'daemon_set ' :
138- if (! $ asHtml ) {
139- yield $ idTags => sprintf ($ this ->translate ('%s ' , '<daemon_set> ' ), $ idTags ['name ' ]);
140- } else {
141- yield $ idTags => Html::sprintf (
142- $ this ->translate ('%s ' , '<daemon_set> ' ),
143- [
144- new HtmlElement (
145- 'span ' ,
146- new Attributes (['class ' => 'namespace-badge ' ]),
147- new KIcon ('namespace ' ),
148- new Text ($ idTags ['namespace ' ])
149- ),
150- new HtmlElement (
151- 'span ' ,
152- new Attributes (['class ' => 'subject ' ]),
153- new KIcon ('daemonset ' ),
154- new Text ($ idTags ['name ' ])
155- )
156- ]
157- );
100+ ))
101+ ->addHtml (new HtmlElement (
102+ 'span ' ,
103+ new Attributes (['class ' => 'cluster-name ' ]),
104+ new Icon ('circle-nodes ' ),
105+ new Text ($ clusterName )
106+ ));
158107 }
108+
159109 break ;
160- case ' replica_set ' :
110+ default :
161111 if (! $ asHtml ) {
162- yield $ idTags => sprintf ($ this -> translate ( '%s ' , ' <replica_set> ' ) , $ idTags ['name ' ]);
112+ yield $ idTags => sprintf ('%s/%s@%s ' , $ idTags [ ' namespace ' ] , $ idTags ['name ' ], $ clusterName );
163113 } else {
164- yield $ idTags => Html::sprintf (
165- $ this ->translate ('%s ' , '<replica_set> ' ),
166- [
167- new HtmlElement (
168- 'span ' ,
169- new Attributes (['class ' => 'namespace-badge ' ]),
170- new KIcon ('namespace ' ),
171- new Text ($ idTags ['namespace ' ])
172- ),
173- new HtmlElement (
174- 'span ' ,
175- new Attributes (['class ' => 'subject ' ]),
176- new KIcon ('replicaset ' ),
177- new Text ($ idTags ['name ' ])
178- )
179- ]
180- );
114+ yield $ idTags => (new HtmlDocument ())
115+ ->addHtml (new HtmlElement (
116+ 'span ' ,
117+ new Attributes (['class ' => 'namespace-badge ' ]),
118+ new KIcon ('namespace ' ),
119+ new Text ($ idTags ['namespace ' ])
120+ ))
121+ ->addHtml (new HtmlElement (
122+ 'span ' ,
123+ new Attributes (['class ' => 'subject ' ]),
124+ Factory::createIcon ($ idTags ['resource ' ]),
125+ new Text ($ idTags ['name ' ])
126+ ))
127+ ->addHtml (new HtmlElement (
128+ 'span ' ,
129+ new Attributes (['class ' => 'cluster-name ' ]),
130+ new Icon ('circle-nodes ' ),
131+ new Text ($ clusterName )
132+ ));
181133 }
134+
182135 break ;
183- case 'stateful_set ' :
184- if (! $ asHtml ) {
185- yield $ idTags => sprintf ($ this ->translate ('%s ' , '<stateful_set> ' ), $ idTags ['name ' ]);
186- } else {
187- yield $ idTags => Html::sprintf (
188- $ this ->translate ('%s is %s ' , '<stateful_set> is <icinga_state> ' ),
189- [
190- new HtmlElement (
191- 'span ' ,
192- new Attributes (['class ' => 'namespace-badge ' ]),
193- new KIcon ('namespace ' ),
194- new Text ($ idTags ['namespace ' ])
195- ),
196- new HtmlElement (
197- 'span ' ,
198- new Attributes (['class ' => 'subject ' ]),
199- new KIcon ('statefulset ' ),
200- new Text ($ idTags ['name ' ])
201- )
202- ]
203- );
204- }
205136 }
206137 }
207138 }
0 commit comments