Skip to content

Commit 5b01073

Browse files
authored
Merge pull request kubernetes-sigs#1235 from wzshiming/feat/default-config-for-metrics-server
[kwokctl] Set up metrics-usage.yaml as default
2 parents 43435cd + c316c47 commit 5b01073

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

kustomize/metrics/resource/embed.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package resource contains the metrics resource for kwok.
18+
package resource
19+
20+
import (
21+
_ "embed"
22+
)
23+
24+
var (
25+
// DefaultMetricsResource is the default metrics resource yaml.
26+
//go:embed metrics-resource.yaml
27+
DefaultMetricsResource string
28+
)

kustomize/metrics/usage/embed.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package usage contains the usage for kwok.
18+
package usage
19+
20+
import (
21+
_ "embed"
22+
)
23+
24+
var (
25+
// DefaultUsageFromAnnotation is the default usage yaml.
26+
//go:embed usage-from-annotation.yaml
27+
DefaultUsageFromAnnotation string
28+
)

pkg/kwokctl/runtime/cluster.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"github.com/nxadm/tail"
2929

3030
"sigs.k8s.io/kwok/kustomize/crd"
31+
"sigs.k8s.io/kwok/kustomize/metrics/resource"
32+
"sigs.k8s.io/kwok/kustomize/metrics/usage"
3133
nodefast "sigs.k8s.io/kwok/kustomize/stage/node/fast"
3234
nodeheartbeat "sigs.k8s.io/kwok/kustomize/stage/node/heartbeat"
3335
nodeheartbeatwithlease "sigs.k8s.io/kwok/kustomize/stage/node/heartbeat-with-lease"
@@ -210,8 +212,16 @@ func (c *Cluster) Save(ctx context.Context) error {
210212
}
211213

212214
if !slices.Contains(conf.Options.EnableCRDs, v1alpha1.MetricKind) {
213-
stages := config.FilterWithTypeFromContext[*internalversion.Metric](ctx)
214-
objs = appendIntoInternalObjects(objs, stages...)
215+
metrics := config.FilterWithTypeFromContext[*internalversion.Metric](ctx)
216+
if len(metrics) != 0 {
217+
objs = appendIntoInternalObjects(objs, metrics...)
218+
} else if c.conf.Options.EnableMetricsServer {
219+
m, err := config.UnmarshalWithType[*internalversion.Metric, string](resource.DefaultMetricsResource)
220+
if err != nil {
221+
return err
222+
}
223+
objs = appendIntoInternalObjects(objs, m)
224+
}
215225
}
216226

217227
if !slices.Contains(conf.Options.EnableCRDs, v1alpha1.ResourceUsageKind) {
@@ -220,8 +230,16 @@ func (c *Cluster) Save(ctx context.Context) error {
220230
}
221231

222232
if !slices.Contains(conf.Options.EnableCRDs, v1alpha1.ClusterResourceUsageKind) {
223-
stages := config.FilterWithTypeFromContext[*internalversion.ClusterResourceUsage](ctx)
224-
objs = appendIntoInternalObjects(objs, stages...)
233+
cru := config.FilterWithTypeFromContext[*internalversion.ClusterResourceUsage](ctx)
234+
if len(cru) != 0 {
235+
objs = appendIntoInternalObjects(objs, cru...)
236+
} else if c.conf.Options.EnableMetricsServer {
237+
m, err := config.UnmarshalWithType[*internalversion.ClusterResourceUsage, string](usage.DefaultUsageFromAnnotation)
238+
if err != nil {
239+
return err
240+
}
241+
objs = appendIntoInternalObjects(objs, m)
242+
}
225243
}
226244

227245
if !slices.Contains(conf.Options.EnableCRDs, v1alpha1.AttachKind) {

0 commit comments

Comments
 (0)