Skip to content

Commit 97dd48a

Browse files
authored
fix(api, vm): add phase age printer column (#2203)
Add the PhaseAge printer column to VirtualMachine brief output while keeping the existing Age column. The new column is populated directly from the latest entry in .status.stats.phasesTransitions using the JSONPath expression .status.stats.phasesTransitions[-1].timestamp. This shows how long the VM has been in its current phase without introducing additional status fields. --------- Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com>
1 parent ec7af1b commit 97dd48a

5 files changed

Lines changed: 18 additions & 7 deletions

File tree

api/client/kubeclient/async.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (aws *asyncWSRoundTripper) WebsocketCallback(ws *websocket.Conn, resp *http
4949
if resp != nil && resp.StatusCode != http.StatusOK {
5050
return enrichError(err, resp)
5151
}
52-
return fmt.Errorf("Can't connect to websocket: %s\n", err.Error())
52+
return fmt.Errorf("can't connect to websocket: %w", err)
5353
}
5454
aws.Connection <- ws
5555

@@ -105,7 +105,9 @@ func asyncSubresourceHelper(
105105
}
106106

107107
if response != nil {
108-
defer response.Body.Close()
108+
defer func() {
109+
_ = response.Body.Close()
110+
}()
109111
switch response.StatusCode {
110112
case http.StatusOK:
111113
case http.StatusNotFound:
@@ -165,7 +167,7 @@ func enrichError(httpErr error, resp *http.Response) error {
165167
if resp == nil {
166168
return httpErr
167169
}
168-
httpErr = fmt.Errorf("Can't connect to websocket (%d): %s\n", resp.StatusCode, httpErr.Error())
170+
httpErr = fmt.Errorf("can't connect to websocket (%d): %w", resp.StatusCode, httpErr)
169171
status := &metav1.Status{}
170172

171173
if resp.Header.Get("Content-Type") != "application/json" {
@@ -201,7 +203,9 @@ type WebsocketRoundTripper struct {
201203
func (d *WebsocketRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
202204
conn, resp, err := d.Dialer.Dial(r.URL.String(), r.Header)
203205
if err == nil {
204-
defer conn.Close()
206+
defer func() {
207+
_ = conn.Close()
208+
}()
205209
}
206210
return resp, d.Do(conn, resp, err)
207211
}

api/client/kubeclient/streamer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ type wsConn struct {
6969
}
7070

7171
func (c *wsConn) SetDeadline(t time.Time) error {
72-
if err := c.Conn.SetWriteDeadline(t); err != nil {
72+
if err := c.SetWriteDeadline(t); err != nil {
7373
return err
7474
}
75-
return c.Conn.SetReadDeadline(t)
75+
return c.SetReadDeadline(t)
7676
}
7777

7878
func NewWebsocketStreamer(conn *websocket.Conn, done chan struct{}) *wsStreamer {

api/client/kubeclient/websocket.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ func (s *binaryWriter) Write(p []byte) (int, error) {
7676
if err != nil {
7777
return 0, convert(err)
7878
}
79-
defer w.Close()
79+
defer func() {
80+
_ = w.Close()
81+
}()
8082
n, err := w.Write(p)
8183
return n, err
8284
}

api/core/v1alpha2/virtual_machine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
// +kubebuilder:subresource:status
4242
// +kubebuilder:resource:categories={all,virtualization},scope=Namespaced,shortName={vm},singular=virtualmachine
4343
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="The phase of the virtual machine."
44+
// +kubebuilder:printcolumn:name="PhaseAge",type="date",JSONPath=".status.stats.phasesTransitions[-1].timestamp",description="Time since the virtual machine entered the current phase."
4445
// +kubebuilder:printcolumn:name="Cores",priority=1,type="string",JSONPath=".spec.cpu.cores",description="The number of cores of the virtual machine."
4546
// +kubebuilder:printcolumn:name="CoreFraction",priority=1,type="string",JSONPath=".spec.cpu.coreFraction",description="Virtual machine core fraction. The range of available values is set in the `sizePolicy` parameter of the VirtualMachineClass; if it is not set, use values within the 1–100% range."
4647
// +kubebuilder:printcolumn:name="Memory",priority=1,type="string",JSONPath=".spec.memory.size",description="The amount of memory of the virtual machine."

crds/virtualmachines.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,10 @@ spec:
14221422
jsonPath: .status.phase
14231423
name: Phase
14241424
type: string
1425+
- description: Time since the virtual machine entered the current phase.
1426+
jsonPath: .status.stats.phasesTransitions[-1].timestamp
1427+
name: PhaseAge
1428+
type: date
14251429
- description: Real number of the virtual machine cores.
14261430
jsonPath: .status.resources.cpu.cores
14271431
name: Cores

0 commit comments

Comments
 (0)