Skip to content

Commit

Permalink
Merge pull request #317 from johanneswuerbach/openapi
Browse files Browse the repository at this point in the history
Serve openapi spec
  • Loading branch information
s-urbaniak authored Oct 26, 2020
2 parents 4c67353 + 7a1bdec commit b9e5a71
Show file tree
Hide file tree
Showing 955 changed files with 80,569 additions and 107,222 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ALL_ARCH=amd64 arm arm64 ppc64le s390x
ML_PLATFORMS=linux/amd64,linux/arm,linux/arm64,linux/ppc64le,linux/s390x
OUT_DIR?=$(PWD)/_output

OPENAPI_PATH=$(GOPATH)/src/k8s.io/kube-openapi

VERSION?=latest
GOIMAGE=golang:1.13
GO111MODULE=on
Expand Down Expand Up @@ -77,3 +79,8 @@ go-mod:
go mod verify

verify: verify-gofmt go-mod test

pkg/api/generated/openapi/zz_generated.openapi.go:
GO111MODULE=off go get -d k8s.io/kube-openapi || true
rm -rf pkg/api/generated/openapi
cd $(OPENAPI_PATH) && go run ./cmd/openapi-gen/openapi-gen.go --logtostderr -i k8s.io/metrics/pkg/apis/custom_metrics,k8s.io/metrics/pkg/apis/custom_metrics/v1beta1,k8s.io/metrics/pkg/apis/custom_metrics/v1beta2,k8s.io/metrics/pkg/apis/external_metrics,k8s.io/metrics/pkg/apis/external_metrics/v1beta1,k8s.io/metrics/pkg/apis/metrics,k8s.io/metrics/pkg/apis/metrics/v1beta1,k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/api/resource,k8s.io/apimachinery/pkg/version,k8s.io/api/core/v1 -p github.com/directxman12/k8s-prometheus-adapter/pkg/api/generated/openapi -O zz_generated.openapi -r /dev/null
9 changes: 9 additions & 0 deletions cmd/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ import (
"time"

"k8s.io/apimachinery/pkg/util/wait"
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/transport"
"k8s.io/component-base/logs"
"k8s.io/klog"
"k8s.io/sample-apiserver/pkg/apiserver"

basecmd "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/cmd"
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
"sigs.k8s.io/metrics-server/pkg/api"

generatedopenapi "github.com/directxman12/k8s-prometheus-adapter/pkg/api/generated/openapi"
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
mprom "github.com/directxman12/k8s-prometheus-adapter/pkg/client/metrics"
adaptercfg "github.com/directxman12/k8s-prometheus-adapter/pkg/config"
Expand Down Expand Up @@ -241,6 +245,11 @@ func main() {
MetricsRelistInterval: 10 * time.Minute,
}
cmd.Name = "prometheus-metrics-adapter"

cmd.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(generatedopenapi.GetOpenAPIDefinitions, openapinamer.NewDefinitionNamer(apiserver.Scheme))
cmd.OpenAPIConfig.Info.Title = "prometheus-metrics-adapter"
cmd.OpenAPIConfig.Info.Version = "1.0.0"

cmd.addFlags()
cmd.Flags().AddGoFlagSet(flag.CommandLine) // make sure we get the klog flags
if err := cmd.Flags().Parse(os.Args); err != nil {
Expand Down
35 changes: 17 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@ go 1.13

require (
github.com/NYTimes/gziphandler v1.0.1 // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
github.com/kubernetes-incubator/custom-metrics-apiserver v0.0.0-20200323093244-5046ce1afe6b
github.com/onsi/ginkgo v1.10.1
github.com/go-openapi/spec v0.19.8
github.com/imdario/mergo v0.3.8 // indirect
github.com/kubernetes-incubator/custom-metrics-apiserver v0.0.0-20201023074945-51cc7b53320e
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.7.0
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/common v0.4.1
github.com/spf13/cobra v0.0.5
github.com/prometheus/common v0.10.0
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.4.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
gopkg.in/yaml.v2 v2.2.8
k8s.io/api v0.17.3
k8s.io/apimachinery v0.17.3
k8s.io/client-go v0.17.3
k8s.io/component-base v0.17.3
k8s.io/api v0.18.5
k8s.io/apimachinery v0.18.5
k8s.io/apiserver v0.18.5
k8s.io/client-go v0.18.5
k8s.io/component-base v0.18.5
k8s.io/klog v1.0.0
k8s.io/metrics v0.17.3
sigs.k8s.io/metrics-server v0.3.7
k8s.io/kube-openapi v0.0.0-20200410145947-61e04a5be9a6
k8s.io/metrics v0.18.5
k8s.io/sample-apiserver v0.18.5
sigs.k8s.io/metrics-server v0.3.7-0.20200925134111-c39853110962
)

replace (
// forced by the inclusion of sigs.k8s.io/metrics-server's use of this in their go.mod
k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1 => ./localvendor/k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1
sigs.k8s.io/metrics-server v0.3.7 => sigs.k8s.io/metrics-server v0.0.0-20200406215547-5fcf6956a533
)
// forced by the inclusion of sigs.k8s.io/metrics-server's use of this in their go.mod
replace k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1 => ./localvendor/k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1
195 changes: 123 additions & 72 deletions go.sum

Large diffs are not rendered by default.

14,083 changes: 14,083 additions & 0 deletions pkg/api/generated/openapi/zz_generated.openapi.go

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions pkg/resourceprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ type nsQueryResults struct {
}

// GetContainerMetrics implements the api.MetricsProvider interface. It may return nil, nil, nil.
func (p *resourceProvider) GetContainerMetrics(pods ...apitypes.NamespacedName) ([]api.TimeInfo, [][]metrics.ContainerMetrics, error) {
func (p *resourceProvider) GetContainerMetrics(pods ...apitypes.NamespacedName) ([]api.TimeInfo, [][]metrics.ContainerMetrics) {
if len(pods) == 0 {
return nil, nil, nil
return nil, nil
}

// TODO(directxman12): figure out how well this scales if we go to list 1000+ pods
Expand Down Expand Up @@ -168,7 +168,7 @@ func (p *resourceProvider) GetContainerMetrics(pods ...apitypes.NamespacedName)
p.assignForPod(pod, resultsByNs, &resMetrics[i], &resTimes[i])
}

return resTimes, resMetrics, nil
return resTimes, resMetrics
}

// assignForPod takes the resource metrics for all containers in the given pod
Expand Down Expand Up @@ -240,18 +240,19 @@ func (p *resourceProvider) assignForPod(pod apitypes.NamespacedName, resultsByNs
*resMetrics = containerMetricsList
}

// GetNodeMetrics implements the api.MetricsProvider interface. It may return nil, nil, nil.
func (p *resourceProvider) GetNodeMetrics(nodes ...string) ([]api.TimeInfo, []corev1.ResourceList, error) {
// GetNodeMetrics implements the api.MetricsProvider interface. It may return nil, nil.
func (p *resourceProvider) GetNodeMetrics(nodes ...string) ([]api.TimeInfo, []corev1.ResourceList) {
if len(nodes) == 0 {
return nil, nil, nil
return nil, nil
}

now := pmodel.Now()

// run the actual query
qRes := p.queryBoth(now, nodeResource, "", nodes...)
if qRes.err != nil {
return nil, nil, qRes.err
klog.Errorf("failed querying node metrics: %v", qRes.err)
return nil, nil
}

resTimes := make([]api.TimeInfo, len(nodes))
Expand Down Expand Up @@ -295,7 +296,7 @@ func (p *resourceProvider) GetNodeMetrics(nodes ...string) ([]api.TimeInfo, []co
}
}

return resTimes, resMetrics, nil
return resTimes, resMetrics
}

// queryBoth queries for both CPU and memory metrics on the given
Expand Down
12 changes: 4 additions & 8 deletions pkg/resourceprovider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ var _ = Describe("Resource Metrics Provider", func() {
}

By("querying for metrics for some pods")
times, metricVals, err := prov.GetContainerMetrics(pods...)
Expect(err).NotTo(HaveOccurred())
times, metricVals := prov.GetContainerMetrics(pods...)

By("verifying that the reported times for each are the earliest times for each pod")
Expect(times).To(Equal([]api.TimeInfo{
Expand Down Expand Up @@ -185,11 +184,10 @@ var _ = Describe("Resource Metrics Provider", func() {
}

By("querying for metrics for some pods, one of which is missing")
times, metricVals, err := prov.GetContainerMetrics(
times, metricVals := prov.GetContainerMetrics(
types.NamespacedName{Namespace: "some-ns", Name: "pod1"},
types.NamespacedName{Namespace: "some-ns", Name: "pod-nonexistant"},
)
Expect(err).NotTo(HaveOccurred())

By("verifying that the missing pod had nil metrics")
Expect(metricVals).To(HaveLen(2))
Expand All @@ -216,8 +214,7 @@ var _ = Describe("Resource Metrics Provider", func() {
),
}
By("querying for metrics for some nodes")
times, metricVals, err := prov.GetNodeMetrics("node1", "node2")
Expect(err).NotTo(HaveOccurred())
times, metricVals := prov.GetNodeMetrics("node1", "node2")

By("verifying that the reported times for each are the earliest times for each pod")
Expect(times).To(Equal([]api.TimeInfo{
Expand All @@ -244,8 +241,7 @@ var _ = Describe("Resource Metrics Provider", func() {
),
}
By("querying for metrics for some nodes, one of which is missing")
times, metricVals, err := prov.GetNodeMetrics("node1", "node2", "node3")
Expect(err).NotTo(HaveOccurred())
times, metricVals := prov.GetNodeMetrics("node1", "node2", "node3")

By("verifying that the missing pod had nil metrics")
Expect(metricVals).To(HaveLen(3))
Expand Down
Loading

0 comments on commit b9e5a71

Please sign in to comment.