Merge branch 'hpa'
commit
c456fb1cfb
1
go.sum
1
go.sum
|
|
@ -156,6 +156,7 @@ github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+v
|
||||||
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
|
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
|
||||||
github.com/petergtz/pegomock v0.0.0-20181206220228-b113d17a7e81 h1:MhSbvsIs4KvpPYr4taOvb6j+r9VNbj/08AfjsKi+Ui0=
|
github.com/petergtz/pegomock v0.0.0-20181206220228-b113d17a7e81 h1:MhSbvsIs4KvpPYr4taOvb6j+r9VNbj/08AfjsKi+Ui0=
|
||||||
github.com/petergtz/pegomock v0.0.0-20181206220228-b113d17a7e81/go.mod h1:nuBLWZpVyv/fLo56qTwt/AUau7jgouO1h7bEvZCq82o=
|
github.com/petergtz/pegomock v0.0.0-20181206220228-b113d17a7e81/go.mod h1:nuBLWZpVyv/fLo56qTwt/AUau7jgouO1h7bEvZCq82o=
|
||||||
|
github.com/petergtz/pegomock v2.5.0+incompatible h1:NgwX1/qc+tsl7I45OkDxYZ1mIonYWbOESnpZcd20sR0=
|
||||||
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
|
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
|
||||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
|
|
|
||||||
|
|
@ -361,7 +361,7 @@ func (a *APIClient) SupportsRes(group string, versions []string) (string, bool,
|
||||||
if grp.Name != group {
|
if grp.Name != group {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return grp.PreferredVersion.Version, true, nil
|
return grp.Versions[len(grp.Versions)-1].Version, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", false, nil
|
return "", false, nil
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package k8s
|
package k8s
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -34,6 +35,16 @@ func (h *HorizontalPodAutoscalerV1) List(ns string) (Collection, error) {
|
||||||
for i, r := range rr.Items {
|
for i, r := range rr.Items {
|
||||||
cc[i] = r
|
cc[i] = r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rr1, err := h.DialOrDie().AutoscalingV2beta2().HorizontalPodAutoscalers(ns).List(opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, r := range rr1.Items {
|
||||||
|
log.Debug().Msgf("MX %#v", len(r.Spec.Metrics))
|
||||||
|
log.Debug().Msgf("HPA:%#v -- %s", r.TypeMeta.Kind, r.Name)
|
||||||
|
}
|
||||||
|
|
||||||
return cc, nil
|
return cc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package k8s
|
package k8s
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -17,7 +18,7 @@ func NewHorizontalPodAutoscalerV2Beta1(c Connection) *HorizontalPodAutoscalerV2B
|
||||||
|
|
||||||
// Get a HorizontalPodAutoscaler.
|
// Get a HorizontalPodAutoscaler.
|
||||||
func (h *HorizontalPodAutoscalerV2Beta1) Get(ns, n string) (interface{}, error) {
|
func (h *HorizontalPodAutoscalerV2Beta1) Get(ns, n string) (interface{}, error) {
|
||||||
return h.DialOrDie().AutoscalingV2beta2().HorizontalPodAutoscalers(ns).Get(n, metav1.GetOptions{})
|
return h.DialOrDie().AutoscalingV2beta1().HorizontalPodAutoscalers(ns).Get(n, metav1.GetOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// List all HorizontalPodAutoscalers in a given namespace.
|
// List all HorizontalPodAutoscalers in a given namespace.
|
||||||
|
|
@ -26,8 +27,9 @@ func (h *HorizontalPodAutoscalerV2Beta1) List(ns string) (Collection, error) {
|
||||||
LabelSelector: h.labelSelector,
|
LabelSelector: h.labelSelector,
|
||||||
FieldSelector: h.fieldSelector,
|
FieldSelector: h.fieldSelector,
|
||||||
}
|
}
|
||||||
rr, err := h.DialOrDie().AutoscalingV2beta2().HorizontalPodAutoscalers(ns).List(opts)
|
rr, err := h.DialOrDie().AutoscalingV2beta1().HorizontalPodAutoscalers(ns).List(opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Beta1 Failed!")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
cc := make(Collection, len(rr.Items))
|
cc := make(Collection, len(rr.Items))
|
||||||
|
|
|
||||||
|
|
@ -40,5 +40,11 @@ func (s *StatefulSet) List(ns string) (Collection, error) {
|
||||||
|
|
||||||
// Delete a StatefulSet.
|
// Delete a StatefulSet.
|
||||||
func (s *StatefulSet) Delete(ns, n string, cascade, force bool) error {
|
func (s *StatefulSet) Delete(ns, n string, cascade, force bool) error {
|
||||||
return s.DialOrDie().AppsV1().StatefulSets(ns).Delete(n, nil)
|
p := metav1.DeletePropagationOrphan
|
||||||
|
if cascade {
|
||||||
|
p = metav1.DeletePropagationBackground
|
||||||
|
}
|
||||||
|
return s.DialOrDie().AppsV1().StatefulSets(ns).Delete(n, &metav1.DeleteOptions{
|
||||||
|
PropagationPolicy: &p,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,9 +97,12 @@ func (r *Endpoints) toEPs(ss []v1.EndpointSubset) string {
|
||||||
for _, s := range ss {
|
for _, s := range ss {
|
||||||
pp := make([]string, len(s.Ports))
|
pp := make([]string, len(s.Ports))
|
||||||
portsToStrs(s.Ports, pp)
|
portsToStrs(s.Ports, pp)
|
||||||
proccessIPs(aa, pp, s.Addresses)
|
log.Debug().Msgf("Ports %#v", pp)
|
||||||
|
a := make([]string, len(s.Addresses))
|
||||||
|
proccessIPs(a, pp, s.Addresses)
|
||||||
|
aa = append(aa, strings.Join(a, ","))
|
||||||
}
|
}
|
||||||
|
log.Debug().Msgf("AA %#v", aa)
|
||||||
return strings.Join(aa, ",")
|
return strings.Join(aa, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,18 +114,19 @@ func portsToStrs(pp []v1.EndpointPort, ss []string) {
|
||||||
|
|
||||||
func proccessIPs(aa []string, pp []string, addrs []v1.EndpointAddress) {
|
func proccessIPs(aa []string, pp []string, addrs []v1.EndpointAddress) {
|
||||||
const maxIPs = 3
|
const maxIPs = 3
|
||||||
|
var i int
|
||||||
for _, a := range addrs {
|
for _, a := range addrs {
|
||||||
if len(a.IP) == 0 {
|
if len(a.IP) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(pp) == 0 {
|
if len(pp) == 0 {
|
||||||
aa = append(aa, a.IP)
|
aa[i], i = a.IP, i+1
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(pp) > maxIPs {
|
if len(pp) > maxIPs {
|
||||||
aa = append(aa, a.IP+":"+strings.Join(pp[:maxIPs], ",")+"...")
|
aa[i], i = a.IP+":"+strings.Join(pp[:maxIPs], ",")+"...", i+1
|
||||||
} else {
|
} else {
|
||||||
aa = append(aa, a.IP+":"+strings.Join(pp, ","))
|
aa[i], i = a.IP+":"+strings.Join(pp, ","), i+1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ func (r *HorizontalPodAutoscalerV1) Marshal(path string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
hpa := i.(*autoscalingv1.HorizontalPodAutoscaler)
|
hpa := i.(*autoscalingv1.HorizontalPodAutoscaler)
|
||||||
hpa.TypeMeta.APIVersion = "autoscaling/v1"
|
hpa.TypeMeta.APIVersion = extractVersion(hpa.Annotations)
|
||||||
hpa.TypeMeta.Kind = "HorizontalPodAutoscaler"
|
hpa.TypeMeta.Kind = "HorizontalPodAutoscaler"
|
||||||
|
|
||||||
return r.marshalObject(hpa)
|
return r.marshalObject(hpa)
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ func (r *HorizontalPodAutoscalerV2Beta1) Marshal(path string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
hpa := i.(*autoscalingv2beta1.HorizontalPodAutoscaler)
|
hpa := i.(*autoscalingv2beta1.HorizontalPodAutoscaler)
|
||||||
hpa.TypeMeta.APIVersion = "autoscaling/v2beta1"
|
hpa.TypeMeta.APIVersion = extractVersion(hpa.Annotations)
|
||||||
hpa.TypeMeta.Kind = "HorizontalPodAutoscaler"
|
hpa.TypeMeta.Kind = "HorizontalPodAutoscaler"
|
||||||
|
|
||||||
return r.marshalObject(hpa)
|
return r.marshalObject(hpa)
|
||||||
|
|
@ -153,7 +153,7 @@ func (r *HorizontalPodAutoscalerV2Beta1) checkHPAType(i int, spec autoscalingv2b
|
||||||
|
|
||||||
func (*HorizontalPodAutoscalerV2Beta1) externalMetrics(i int, spec autoscalingv2beta1.MetricSpec, statuses []autoscalingv2beta1.MetricStatus) string {
|
func (*HorizontalPodAutoscalerV2Beta1) externalMetrics(i int, spec autoscalingv2beta1.MetricSpec, statuses []autoscalingv2beta1.MetricStatus) string {
|
||||||
current := "<unknown>"
|
current := "<unknown>"
|
||||||
|
log.Debug().Msg("YO!")
|
||||||
if spec.External.TargetAverageValue != nil {
|
if spec.External.TargetAverageValue != nil {
|
||||||
if len(statuses) > i && statuses[i].External != nil && &statuses[i].External.CurrentAverageValue != nil {
|
if len(statuses) > i && statuses[i].External != nil && &statuses[i].External.CurrentAverageValue != nil {
|
||||||
current = statuses[i].External.CurrentAverageValue.String()
|
current = statuses[i].External.CurrentAverageValue.String()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -58,12 +59,23 @@ func (r *HorizontalPodAutoscaler) Marshal(path string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
hpa := i.(*autoscalingv2beta2.HorizontalPodAutoscaler)
|
hpa := i.(*autoscalingv2beta2.HorizontalPodAutoscaler)
|
||||||
hpa.TypeMeta.APIVersion = "autoscaling/v2beta2"
|
hpa.TypeMeta.APIVersion = extractVersion(hpa.Annotations)
|
||||||
hpa.TypeMeta.Kind = "HorizontalPodAutoscaler"
|
hpa.TypeMeta.Kind = "HorizontalPodAutoscaler"
|
||||||
|
|
||||||
return r.marshalObject(hpa)
|
return r.marshalObject(hpa)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractVersion(a map[string]string) string {
|
||||||
|
ann := a["kubectl.kubernetes.io/last-applied-configuration"]
|
||||||
|
rx := regexp.MustCompile(`\A{"apiVersion":"([\w|/]+)",`)
|
||||||
|
found := rx.FindAllStringSubmatch(ann, 1)
|
||||||
|
if len(found) == 0 || len(found[0]) < 1 {
|
||||||
|
return "autoscaling/v2beta2"
|
||||||
|
}
|
||||||
|
|
||||||
|
return found[0][1]
|
||||||
|
}
|
||||||
|
|
||||||
// Header return resource header.
|
// Header return resource header.
|
||||||
func (*HorizontalPodAutoscaler) Header(ns string) Row {
|
func (*HorizontalPodAutoscaler) Header(ns string) Row {
|
||||||
hh := Row{}
|
hh := Row{}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package resource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestVersionFromAnnotation(t *testing.T) {
|
||||||
|
ann := map[string]string{
|
||||||
|
"kubectl.kubernetes.io/last-applied-configuration": `{"apiVersion":"autoscaling/v1","kind":"HorizontalPodAutoscaler","metadata":{"annotations":{},"name":"nginx","namespace":"default"},"spec":{"maxReplicas":10,"minReplicas":1,"scaleTargetRef":{"apiVersion":"apps/v1","kind":"Deployment","name":"nginx"},"targetCPUUtilizationPercentage":10}}`,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "autoscaling/v1", extractVersion(ann))
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue