Skip to content

Commit

Permalink
Add metrics for components
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Nov 24, 2023
1 parent 156cda1 commit 63d0e9f
Show file tree
Hide file tree
Showing 34 changed files with 1,215 additions and 1,076 deletions.
25 changes: 25 additions & 0 deletions pkg/apis/config/v1alpha1/kwokctl_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ type Component struct {
// +optional
Volumes []Volume `json:"volumes,omitempty"`

// Metric is the metric of the component.
Metric *ComponentMetric `json:"metric,omitempty"`

// MetricsDiscoveries is the metrics discovery of the component.
MetricsDiscoveries []ComponentMetric `json:"metricsDiscoveries,omitempty"`

// Version is the version of the component.
// +optional
Version string `json:"version,omitempty"`
Expand Down Expand Up @@ -494,6 +500,25 @@ type Port struct {
Protocol Protocol `json:"protocol,omitempty"`
}

// ComponentMetric represents a metric of a component.
type ComponentMetric struct {
// Name is the name of the metrics discovery.
Name string `json:"name,omitempty"`
// Scheme is the scheme of the metric.
Scheme string `json:"scheme"`
// Host is the host of the metric.
Host string `json:"host"`
// Path is the path of the metric.
Path string `json:"path"`

// CertPath is the cert path of the metric.
CertPath string `json:"certPath,omitempty"`
// KeyPath is the key path of the metric.
KeyPath string `json:"keyPath,omitempty"`
// InsecureSkipVerify is the flag to skip verify the metric.
InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"`
}

// Protocol defines network protocols supported for things like component ports.
// +enum
type Protocol string
Expand Down
26 changes: 26 additions & 0 deletions pkg/apis/config/v1alpha1/zz_generated.deepcopy.go

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

25 changes: 25 additions & 0 deletions pkg/apis/internalversion/kwokctl_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ type Component struct {
// Volumes is a list of named volumes that can be mounted by containers belonging to the component.
Volumes []Volume

// Metric is the metric of the component.
Metric *ComponentMetric

// MetricsDiscoveries is the metrics discovery of the component.
MetricsDiscoveries []ComponentMetric

// Version is the version of the component.
Version string
}
Expand Down Expand Up @@ -331,6 +337,25 @@ type Port struct {
Protocol Protocol
}

// ComponentMetric represents a metric of a component.
type ComponentMetric struct {
// Name is the name of the metrics discovery.
Name string
// Scheme is the scheme of the metric.
Scheme string
// Host is the host of the metric.
Host string
// Path is the path of the metric.
Path string

// CertPath is the cert path of the metric.
CertPath string
// KeyPath is the key path of the metric.
KeyPath string
// InsecureSkipVerify is the flag to skip verify the metric.
InsecureSkipVerify bool
}

// Protocol defines network protocols supported for things like component ports.
type Protocol string

Expand Down
46 changes: 46 additions & 0 deletions pkg/apis/internalversion/zz_generated.conversion.go

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

26 changes: 26 additions & 0 deletions pkg/apis/internalversion/zz_generated.deepcopy.go

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

17 changes: 17 additions & 0 deletions pkg/kwokctl/components/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sigs.k8s.io/kwok/pkg/consts"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/utils/format"
"sigs.k8s.io/kwok/pkg/utils/net"
"sigs.k8s.io/kwok/pkg/utils/version"
)

Expand All @@ -31,6 +32,7 @@ type BuildEtcdComponentConfig struct {
Runtime string
Binary string
Image string
ProjectName string
Version version.Version
DataPath string
Workdir string
Expand Down Expand Up @@ -67,6 +69,8 @@ func BuildEtcdComponent(conf BuildEtcdComponentConfig) (component internalversio
}
etcdArgs = append(etcdArgs, extraArgsToStrings(conf.ExtraArgs)...)

var metric *internalversion.ComponentMetric

if getRuntimeKind(conf.Runtime) != runtimeKindRaw {
// TODO: use a volume for the data path
// volumes = append(volumes,
Expand Down Expand Up @@ -104,6 +108,12 @@ func BuildEtcdComponent(conf BuildEtcdComponentConfig) (component internalversio
"--listen-client-urls=http://"+conf.BindAddress+":2379",
"--initial-cluster=node0=http://"+conf.BindAddress+":2380",
)

metric = &internalversion.ComponentMetric{
Scheme: "http",
Host: conf.ProjectName + "-" + consts.ComponentEtcd + ":2379",
Path: "/metrics",
}
} else {
etcdPeerPortStr := format.String(conf.PeerPort)
etcdClientPortStr := format.String(conf.Port)
Expand All @@ -115,6 +125,12 @@ func BuildEtcdComponent(conf BuildEtcdComponentConfig) (component internalversio
"--listen-client-urls=http://"+conf.BindAddress+":"+etcdClientPortStr,
"--initial-cluster=node0=http://"+conf.BindAddress+":"+etcdPeerPortStr,
)

metric = &internalversion.ComponentMetric{
Scheme: "http",
Host: net.LocalAddress + ":" + etcdClientPortStr,
Path: "/metrics",
}
}

if conf.Version.GTE(version.NewVersion(3, 4, 0)) {
Expand Down Expand Up @@ -144,6 +160,7 @@ func BuildEtcdComponent(conf BuildEtcdComponentConfig) (component internalversio
Args: etcdArgs,
Binary: conf.Binary,
Ports: ports,
Metric: metric,
Image: conf.Image,
WorkDir: conf.Workdir,
Envs: envs,
Expand Down
30 changes: 30 additions & 0 deletions pkg/kwokctl/components/kube_apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
"sigs.k8s.io/kwok/pkg/consts"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/utils/format"
"sigs.k8s.io/kwok/pkg/utils/net"
"sigs.k8s.io/kwok/pkg/utils/version"
)

// BuildKubeApiserverComponentConfig is the configuration for building a kube-apiserver component.
type BuildKubeApiserverComponentConfig struct {
Runtime string
ProjectName string
Binary string
Image string
Version version.Version
Expand Down Expand Up @@ -121,6 +123,7 @@ func BuildKubeApiserverComponent(conf BuildKubeApiserverComponentConfig) (compon
var ports []internalversion.Port
var volumes []internalversion.Volume
volumes = append(volumes, conf.ExtraVolumes...)
var metric *internalversion.ComponentMetric

if getRuntimeKind(conf.Runtime) != runtimeKindRaw {
kubeApiserverArgs = append(kubeApiserverArgs,
Expand Down Expand Up @@ -173,6 +176,14 @@ func BuildKubeApiserverComponent(conf BuildKubeApiserverComponentConfig) (compon
"--service-account-signing-key-file=/etc/kubernetes/pki/admin.key",
"--service-account-issuer=https://kubernetes.default.svc.cluster.local",
)
metric = &internalversion.ComponentMetric{
Scheme: "https",
Host: conf.ProjectName + "-" + consts.ComponentKubeApiserver + ":6443",
Path: "/metrics",
CertPath: "/etc/kubernetes/pki/admin.crt",
KeyPath: "/etc/kubernetes/pki/admin.key",
InsecureSkipVerify: true,
}
} else {
kubeApiserverArgs = append(kubeApiserverArgs,
"--bind-address="+conf.BindAddress,
Expand All @@ -184,6 +195,14 @@ func BuildKubeApiserverComponent(conf BuildKubeApiserverComponentConfig) (compon
"--service-account-signing-key-file="+conf.AdminKeyPath,
"--service-account-issuer=https://kubernetes.default.svc.cluster.local",
)
metric = &internalversion.ComponentMetric{
Scheme: "https",
Host: net.LocalAddress + ":" + format.String(conf.Port),
Path: "/metrics",
CertPath: conf.AdminCertPath,
KeyPath: conf.AdminKeyPath,
InsecureSkipVerify: true,
}
}
} else {
if getRuntimeKind(conf.Runtime) != runtimeKindRaw {
Expand All @@ -198,11 +217,21 @@ func BuildKubeApiserverComponent(conf BuildKubeApiserverComponentConfig) (compon
"--insecure-bind-address="+conf.BindAddress,
"--insecure-port=8080",
)
metric = &internalversion.ComponentMetric{
Scheme: "http",
Host: conf.ProjectName + "-" + consts.ComponentKubeApiserver + ":8080",
Path: "/metrics",
}
} else {
kubeApiserverArgs = append(kubeApiserverArgs,
"--insecure-bind-address="+conf.BindAddress,
"--insecure-port="+format.String(conf.Port),
)
metric = &internalversion.ComponentMetric{
Scheme: "http",
Host: net.LocalAddress + ":" + format.String(conf.Port),
Path: "/metrics",
}
}
}

Expand Down Expand Up @@ -273,6 +302,7 @@ func BuildKubeApiserverComponent(conf BuildKubeApiserverComponentConfig) (compon
Args: kubeApiserverArgs,
Binary: conf.Binary,
Image: conf.Image,
Metric: metric,
WorkDir: conf.Workdir,
Envs: envs,
}, nil
Expand Down
Loading

0 comments on commit 63d0e9f

Please sign in to comment.