Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add NodeMetric API #1

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ layout:
multigroup: true
projectName: koordinator
repo: github.com/koordinator-sh/koordinator
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: koordinator.sh
group: slo
kind: NodeMetric
path: github.com/koordinator-sh/koordinator/apis/slo/v1alpha1
version: v1alpha1
version: "3"
36 changes: 36 additions & 0 deletions apis/slo/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2022 The Koordinator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1alpha1 contains API Schema definitions for the slo v1alpha1 API group
//+kubebuilder:object:generate=true
//+groupName=slo.koordinator.sh
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "slo.koordinator.sh", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
80 changes: 80 additions & 0 deletions apis/slo/v1alpha1/nodemetric_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2022 The Koordinator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

type NodeMetricInfo struct {
NodeUsage ResourceMap `json:"nodeUsage,omitempty"`
}

type PodMetricInfo struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
PodUsage ResourceMap `json:"podUsage,omitempty"`
}

type ResourceMap struct {
corev1.ResourceList `json:"resources,omitempty"`
}

// NodeMetricSpec defines the desired state of NodeMetric
type NodeMetricSpec struct{}

// NodeMetricStatus defines the observed state of NodeMetric
type NodeMetricStatus struct {
// UpdateTime is the last time this NodeMetric was updated.
UpdateTime *metav1.Time `json:"updateTime,omitempty"`

// NodeMetric contains the metrics for this node.
NodeMetric *NodeMetricInfo `json:"nodeMetric,omitempty"`

// PodsMetric contains the metrics for pods belong to this node.
PodsMetric []*PodMetricInfo `json:"podsMetric,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster

// NodeMetric is the Schema for the nodemetrics API
type NodeMetric struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec NodeMetricSpec `json:"spec,omitempty"`
Status NodeMetricStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// NodeMetricList contains a list of NodeMetric
type NodeMetricList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []NodeMetric `json:"items"`
}

func init() {
SchemeBuilder.Register(&NodeMetric{}, &NodeMetricList{})
}
190 changes: 190 additions & 0 deletions apis/slo/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions cmd/slo-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package slo_agent
package main

// TODO
import (
"flag"
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"

"github.com/koordinator-sh/koordinator/cmd/slo-agent/options"
)

func main() {
klog.InitFlags(flag.CommandLine)
flag.Parse()

// Default klog flush interval is 5 seconds, set to LogFlushFreq.
go wait.Until(klog.Flush, *options.LogFlushFreq, wait.NeverStop)
defer klog.Flush()

go func() {
klog.Info("starting prometheus server on %v", *options.PromAddr)
http.Handle("/metrics", promhttp.Handler())
klog.Fatalf("failed to start prometheus, error: %v", http.ListenAndServe(*options.PromAddr, nil))
}()
}
27 changes: 27 additions & 0 deletions cmd/slo-agent/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2022 The Koordinator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package options

import (
"flag"
"time"
)

var (
PromAddr = flag.String("prom-addr", ":9316", "Port of prometheus server")
LogFlushFreq = flag.Duration("log-flush-freq", 5*time.Second, "Maximum duration between log flushes")
)
Loading