This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 481
/
replicaset.go
67 lines (55 loc) · 2.08 KB
/
replicaset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package objectstatus
import (
"context"
"fmt"
"github.com/vmware-tanzu/octant/internal/link"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"github.com/vmware-tanzu/octant/pkg/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
// replicaSetAppsV1 creates status for an v1/apps replica set. This is
// not the final implementation. It is included to generate output.
func replicaSetAppsV1(_ context.Context, object runtime.Object, _ store.Store, _ link.Interface) (ObjectStatus, error) {
if object == nil {
return ObjectStatus{}, errors.Errorf("replica set is nil")
}
replicaSet := &appsv1.ReplicaSet{}
if err := scheme.Scheme.Convert(object, replicaSet, 0); err != nil {
return ObjectStatus{}, errors.Wrap(err, "convert object to apps/v1 replica set")
}
status := replicaSet.Status
specReplicas := int32(0)
if r := replicaSet.Spec.Replicas; r != nil {
specReplicas = *r
}
current := fmt.Sprintf("%d", replicaSet.Status.ReadyReplicas)
desired := fmt.Sprintf("%d", specReplicas)
properties := []component.Property{{Label: "Current Replicas", Value: component.NewText(current)},
{Label: "Desired Replicas", Value: component.NewText(desired)}}
switch {
case status.Replicas == 0 && specReplicas != 0:
return ObjectStatus{
NodeStatus: component.NodeStatusError,
Details: []component.Component{component.NewText("Replica Set has no replicas available")},
Properties: properties,
}, nil
case status.Replicas == status.AvailableReplicas:
return ObjectStatus{NodeStatus: component.NodeStatusOK,
Details: []component.Component{component.NewText("Replica Set is OK")},
Properties: properties,
}, nil
default:
return ObjectStatus{
NodeStatus: component.NodeStatusWarning,
Details: []component.Component{component.NewText(fmt.Sprintf("Expected %d replicas, but %d are available", status.Replicas, status.AvailableReplicas))},
Properties: properties,
}, nil
}
}