Skip to content

Commit

Permalink
Merge pull request #1235 from wzshiming/feat/default-config-for-metri…
Browse files Browse the repository at this point in the history
…cs-server

[kwokctl] Set up metrics-usage.yaml as default
  • Loading branch information
wzshiming authored Sep 30, 2024
2 parents 43435cd + c316c47 commit 5b01073
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
28 changes: 28 additions & 0 deletions kustomize/metrics/resource/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2024 The Kubernetes 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 resource contains the metrics resource for kwok.
package resource

import (
_ "embed"
)

var (
// DefaultMetricsResource is the default metrics resource yaml.
//go:embed metrics-resource.yaml
DefaultMetricsResource string
)
28 changes: 28 additions & 0 deletions kustomize/metrics/usage/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2024 The Kubernetes 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 usage contains the usage for kwok.
package usage

import (
_ "embed"
)

var (
// DefaultUsageFromAnnotation is the default usage yaml.
//go:embed usage-from-annotation.yaml
DefaultUsageFromAnnotation string
)
26 changes: 22 additions & 4 deletions pkg/kwokctl/runtime/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/nxadm/tail"

"sigs.k8s.io/kwok/kustomize/crd"
"sigs.k8s.io/kwok/kustomize/metrics/resource"
"sigs.k8s.io/kwok/kustomize/metrics/usage"
nodefast "sigs.k8s.io/kwok/kustomize/stage/node/fast"
nodeheartbeat "sigs.k8s.io/kwok/kustomize/stage/node/heartbeat"
nodeheartbeatwithlease "sigs.k8s.io/kwok/kustomize/stage/node/heartbeat-with-lease"
Expand Down Expand Up @@ -210,8 +212,16 @@ func (c *Cluster) Save(ctx context.Context) error {
}

if !slices.Contains(conf.Options.EnableCRDs, v1alpha1.MetricKind) {
stages := config.FilterWithTypeFromContext[*internalversion.Metric](ctx)
objs = appendIntoInternalObjects(objs, stages...)
metrics := config.FilterWithTypeFromContext[*internalversion.Metric](ctx)
if len(metrics) != 0 {
objs = appendIntoInternalObjects(objs, metrics...)
} else if c.conf.Options.EnableMetricsServer {
m, err := config.UnmarshalWithType[*internalversion.Metric, string](resource.DefaultMetricsResource)
if err != nil {
return err
}
objs = appendIntoInternalObjects(objs, m)
}
}

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

if !slices.Contains(conf.Options.EnableCRDs, v1alpha1.ClusterResourceUsageKind) {
stages := config.FilterWithTypeFromContext[*internalversion.ClusterResourceUsage](ctx)
objs = appendIntoInternalObjects(objs, stages...)
cru := config.FilterWithTypeFromContext[*internalversion.ClusterResourceUsage](ctx)
if len(cru) != 0 {
objs = appendIntoInternalObjects(objs, cru...)
} else if c.conf.Options.EnableMetricsServer {
m, err := config.UnmarshalWithType[*internalversion.ClusterResourceUsage, string](usage.DefaultUsageFromAnnotation)
if err != nil {
return err
}
objs = appendIntoInternalObjects(objs, m)
}
}

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

0 comments on commit 5b01073

Please sign in to comment.