mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-04 02:30:45 +00:00
Spec.Replicas is a *int32, with a value of nil occurring when the user doesn't set it. In this case k8s defaults to 1, so we mimic this to show the effective value.
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/weaveworks/scope/report"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
apiv1 "k8s.io/client-go/pkg/api/v1"
|
|
apiv1beta1 "k8s.io/client-go/pkg/apis/extensions/v1beta1"
|
|
)
|
|
|
|
// These constants are keys used in node metadata
|
|
const (
|
|
UpdatedReplicas = "kubernetes_updated_replicas"
|
|
AvailableReplicas = "kubernetes_available_replicas"
|
|
UnavailableReplicas = "kubernetes_unavailable_replicas"
|
|
Strategy = "kubernetes_strategy"
|
|
)
|
|
|
|
// Deployment represents a Kubernetes deployment
|
|
type Deployment interface {
|
|
Meta
|
|
Selector() (labels.Selector, error)
|
|
GetNode(probeID string) report.Node
|
|
}
|
|
|
|
type deployment struct {
|
|
*apiv1beta1.Deployment
|
|
Meta
|
|
Node *apiv1.Node
|
|
}
|
|
|
|
// NewDeployment creates a new Deployment
|
|
func NewDeployment(d *apiv1beta1.Deployment) Deployment {
|
|
return &deployment{Deployment: d, Meta: meta{d.ObjectMeta}}
|
|
}
|
|
|
|
func (d *deployment) Selector() (labels.Selector, error) {
|
|
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return selector, nil
|
|
}
|
|
|
|
func (d *deployment) GetNode(probeID string) report.Node {
|
|
// Spec.Replicas can be omitted, and the pointer will be nil. It defaults to 1.
|
|
desiredReplicas := 1
|
|
if d.Spec.Replicas != nil {
|
|
desiredReplicas = int(*d.Spec.Replicas)
|
|
}
|
|
return d.MetaNode(report.MakeDeploymentNodeID(d.UID())).WithLatests(map[string]string{
|
|
ObservedGeneration: fmt.Sprint(d.Status.ObservedGeneration),
|
|
DesiredReplicas: fmt.Sprint(desiredReplicas),
|
|
Replicas: fmt.Sprint(d.Status.Replicas),
|
|
UpdatedReplicas: fmt.Sprint(d.Status.UpdatedReplicas),
|
|
AvailableReplicas: fmt.Sprint(d.Status.AvailableReplicas),
|
|
UnavailableReplicas: fmt.Sprint(d.Status.UnavailableReplicas),
|
|
Strategy: string(d.Spec.Strategy.Type),
|
|
report.ControlProbeID: probeID,
|
|
NodeType: "Deployment",
|
|
}).WithLatestActiveControls(ScaleUp, ScaleDown)
|
|
}
|