diff --git a/go.sum b/go.sum index 0f4ac7d8..23f1046d 100644 --- a/go.sum +++ b/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/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 v2.5.0+incompatible h1:NgwX1/qc+tsl7I45OkDxYZ1mIonYWbOESnpZcd20sR0= 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.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= diff --git a/internal/k8s/api.go b/internal/k8s/api.go index 961bf5c6..479073b6 100644 --- a/internal/k8s/api.go +++ b/internal/k8s/api.go @@ -361,7 +361,7 @@ func (a *APIClient) SupportsRes(group string, versions []string) (string, bool, if grp.Name != group { continue } - return grp.PreferredVersion.Version, true, nil + return grp.Versions[len(grp.Versions)-1].Version, true, nil } return "", false, nil diff --git a/internal/k8s/hpa_v1.go b/internal/k8s/hpa_v1.go index 7bcddd09..d641baa9 100644 --- a/internal/k8s/hpa_v1.go +++ b/internal/k8s/hpa_v1.go @@ -1,6 +1,7 @@ package k8s import ( + "github.com/rs/zerolog/log" 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 { 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 } diff --git a/internal/k8s/hpa_v2beta1.go b/internal/k8s/hpa_v2beta1.go index 65b0259c..b2b07d56 100644 --- a/internal/k8s/hpa_v2beta1.go +++ b/internal/k8s/hpa_v2beta1.go @@ -1,6 +1,7 @@ package k8s import ( + "github.com/rs/zerolog/log" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -17,7 +18,7 @@ func NewHorizontalPodAutoscalerV2Beta1(c Connection) *HorizontalPodAutoscalerV2B // Get a HorizontalPodAutoscaler. 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. @@ -26,8 +27,9 @@ func (h *HorizontalPodAutoscalerV2Beta1) List(ns string) (Collection, error) { LabelSelector: h.labelSelector, FieldSelector: h.fieldSelector, } - rr, err := h.DialOrDie().AutoscalingV2beta2().HorizontalPodAutoscalers(ns).List(opts) + rr, err := h.DialOrDie().AutoscalingV2beta1().HorizontalPodAutoscalers(ns).List(opts) if err != nil { + log.Error().Err(err).Msg("Beta1 Failed!") return nil, err } cc := make(Collection, len(rr.Items)) diff --git a/internal/k8s/sts.go b/internal/k8s/sts.go index 1dcb63ed..2b64412c 100644 --- a/internal/k8s/sts.go +++ b/internal/k8s/sts.go @@ -40,5 +40,11 @@ func (s *StatefulSet) List(ns string) (Collection, error) { // Delete a StatefulSet. 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, + }) } diff --git a/internal/resource/ep.go b/internal/resource/ep.go index f4da04ec..b463fd8d 100644 --- a/internal/resource/ep.go +++ b/internal/resource/ep.go @@ -97,9 +97,12 @@ func (r *Endpoints) toEPs(ss []v1.EndpointSubset) string { for _, s := range ss { pp := make([]string, len(s.Ports)) 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, ",") } @@ -111,18 +114,19 @@ func portsToStrs(pp []v1.EndpointPort, ss []string) { func proccessIPs(aa []string, pp []string, addrs []v1.EndpointAddress) { const maxIPs = 3 + var i int for _, a := range addrs { if len(a.IP) == 0 { continue } if len(pp) == 0 { - aa = append(aa, a.IP) + aa[i], i = a.IP, i+1 continue } 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 { - aa = append(aa, a.IP+":"+strings.Join(pp, ",")) + aa[i], i = a.IP+":"+strings.Join(pp, ","), i+1 } } } diff --git a/internal/resource/hpa_v1.go b/internal/resource/hpa_v1.go index fe8ba712..dd434088 100644 --- a/internal/resource/hpa_v1.go +++ b/internal/resource/hpa_v1.go @@ -57,7 +57,7 @@ func (r *HorizontalPodAutoscalerV1) Marshal(path string) (string, error) { } hpa := i.(*autoscalingv1.HorizontalPodAutoscaler) - hpa.TypeMeta.APIVersion = "autoscaling/v1" + hpa.TypeMeta.APIVersion = extractVersion(hpa.Annotations) hpa.TypeMeta.Kind = "HorizontalPodAutoscaler" return r.marshalObject(hpa) diff --git a/internal/resource/hpa_test.go b/internal/resource/hpa_v1_test.go similarity index 100% rename from internal/resource/hpa_test.go rename to internal/resource/hpa_v1_test.go diff --git a/internal/resource/hpa_v2beta1.go b/internal/resource/hpa_v2beta1.go index b4de4b68..39a5c52a 100644 --- a/internal/resource/hpa_v2beta1.go +++ b/internal/resource/hpa_v2beta1.go @@ -58,7 +58,7 @@ func (r *HorizontalPodAutoscalerV2Beta1) Marshal(path string) (string, error) { } hpa := i.(*autoscalingv2beta1.HorizontalPodAutoscaler) - hpa.TypeMeta.APIVersion = "autoscaling/v2beta1" + hpa.TypeMeta.APIVersion = extractVersion(hpa.Annotations) hpa.TypeMeta.Kind = "HorizontalPodAutoscaler" 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 { current := "" - + log.Debug().Msg("YO!") if spec.External.TargetAverageValue != nil { if len(statuses) > i && statuses[i].External != nil && &statuses[i].External.CurrentAverageValue != nil { current = statuses[i].External.CurrentAverageValue.String() diff --git a/internal/resource/hpa_v2beta2.go b/internal/resource/hpa_v2beta2.go index e7938ec3..06335ec2 100644 --- a/internal/resource/hpa_v2beta2.go +++ b/internal/resource/hpa_v2beta2.go @@ -1,6 +1,7 @@ package resource import ( + "regexp" "strconv" "strings" @@ -58,12 +59,23 @@ func (r *HorizontalPodAutoscaler) Marshal(path string) (string, error) { } hpa := i.(*autoscalingv2beta2.HorizontalPodAutoscaler) - hpa.TypeMeta.APIVersion = "autoscaling/v2beta2" + hpa.TypeMeta.APIVersion = extractVersion(hpa.Annotations) hpa.TypeMeta.Kind = "HorizontalPodAutoscaler" 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. func (*HorizontalPodAutoscaler) Header(ns string) Row { hh := Row{} diff --git a/internal/resource/hpa_v2beta2_int_test.go b/internal/resource/hpa_v2beta2_int_test.go new file mode 100644 index 00000000..4003e19f --- /dev/null +++ b/internal/resource/hpa_v2beta2_int_test.go @@ -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)) +}