Skip to content

Commit

Permalink
✨ Add NodeMetric API (#1)
Browse files Browse the repository at this point in the history
* Setup NodeMetric API

Signed-off-by: Jason Liu <jasonliu747@gmail.com>

* implement Watches for SetupWithManager

Signed-off-by: Jason Liu <jasonliu747@gmail.com>

* add test for nodemetric controller

Signed-off-by: Jason Liu <jasonliu747@gmail.com>
  • Loading branch information
jasonliu747 committed Mar 22, 2022
1 parent ac0d789 commit a3f4a11
Show file tree
Hide file tree
Showing 29 changed files with 1,402 additions and 306 deletions.
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

0 comments on commit a3f4a11

Please sign in to comment.