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

pkg/kube-metrics: Add initial operator specific metrics #1277

Merged
merged 27 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9baa99d
pkg/kube-metrics: Add initial operator specific metrics
lilic Mar 22, 2019
699af61
*: Bump kube-state-metrics to v1.6.0
lilic May 6, 2019
bf1ce5c
pkg/kube-metrics/metrics.go: Use lower case kind
lilic May 6, 2019
45748d3
pkg/kube-metrics/collector.go: Address PR comment
lilic May 6, 2019
60ff42c
Address PR comments
lilic May 13, 2019
835efb2
*: Add dep k8s.io/kube-state-metrics to gomod
lilic May 14, 2019
04206ef
Update test/e2e/memcached_test.go
lilic May 28, 2019
7bc7426
Update pkg/kube-metrics/metrics.go
lilic May 28, 2019
61642b9
Update internal/pkg/scaffold/cmd.go
lilic May 28, 2019
59d32ee
Address PR comments
lilic May 28, 2019
47cd6af
cmd: Line up comment
lilic May 28, 2019
8f11918
*: Let user have control over which namespaces are used
lilic May 29, 2019
96d9626
pkg/kube-metrics/*: Refactor uclient to take in cfg
lilic Jun 4, 2019
9e85674
pkg/kube-metrics/*: Pass in dynamic client
lilic Jun 4, 2019
d22fc52
Gopkg.lock: Rerun dep ensure
lilic Jun 5, 2019
ac2448e
Update pkg/kube-metrics/metrics.go
lilic Jun 6, 2019
de4ecd1
Update internal/pkg/scaffold/cmd.go
lilic Jun 6, 2019
40b930d
Address PR comments
lilic Jun 6, 2019
940e705
CHANGELOG.md: Mention custom resource metrics
lilic Jun 7, 2019
af21f02
pkg/kube-metrics/server.go: Change log
lilic Jun 7, 2019
03aa741
Update pkg/kube-metrics/metrics.go
lilic Jun 11, 2019
100ea71
Update pkg/kube-metrics/metrics.go
lilic Jun 11, 2019
277b0c8
Update pkg/kube-metrics/metrics.go
lilic Jun 11, 2019
db160aa
*: Address PR comments
lilic Jun 11, 2019
329303f
Update pkg/kube-metrics/metrics.go
lilic Jun 11, 2019
08c8cb4
Update pkg/kube-metrics/metrics.go
lilic Jun 11, 2019
2834a72
Add TODO
lilic Jun 11, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- New option for [`operator-sdk up local --enable-delve`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#up), which can be used to start the operator in remote debug mode with the [delve](https://github.com/go-delve/delve) debugger listening on port 2345. ([#1422](https://github.com/operator-framework/operator-sdk/pull/1422))
- Enables controller-runtime metrics in Helm operator projects. ([#1482](https://github.com/operator-framework/operator-sdk/pull/1482))
- New flags `--vendor` and `--skip-validation` for [`operator-sdk new`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#new) that direct the SDK to initialize a new project with a `vendor/` directory, and without validating project dependencies. `vendor/` is not written by default. ([#1519](https://github.com/operator-framework/operator-sdk/pull/1519))
- Generating and serving info metrics about each custom resource. By default these metrics are exposed on port 8686. ([#1277](https://github.com/operator-framework/operator-sdk/pull/1277))
lilic marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
20 changes: 20 additions & 0 deletions Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
name = "github.com/spf13/cobra"
version = "0.0.3"

[[override]]
name = "k8s.io/kube-state-metrics"
version = "v1.6.0"

# We need overrides for the following imports because dep can't resolve them
# correctly. The easiest way to get this right is to use the versions that
# k8s.io/helm uses. See https://github.com/helm/helm/blob/v2.13.1/glide.lock
Expand Down
36 changes: 33 additions & 3 deletions internal/pkg/scaffold/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ import (

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"

"{{ .Repo }}/pkg/apis"
"{{ .Repo }}/pkg/controller"

"github.com/operator-framework/operator-sdk/pkg/k8sutil"
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/log/zap"
"github.com/operator-framework/operator-sdk/pkg/metrics"
Expand All @@ -64,8 +66,9 @@ import (

// Change below variables to serve metrics on different host or port.
var (
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
operatorMetricsPort int32 = 8686
)
var log = logf.Log.WithName("cmd")

Expand Down Expand Up @@ -112,7 +115,6 @@ func main() {
}

ctx := context.TODO()

// Become the leader before proceeding
err = leader.Become(ctx, "{{ .ProjectName }}-lock")
if err != nil {
Expand Down Expand Up @@ -145,6 +147,10 @@ func main() {
os.Exit(1)
}

if err = serveCRMetrics(cfg); err != nil {
log.Info("Could not generate and serve custom resource metrics: ", err.Error())
}

// Create Service object to expose the metrics port.
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
if err != nil {
Expand All @@ -159,4 +165,28 @@ func main() {
os.Exit(1)
}
}

// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
func serveCRMetrics(cfg *rest.Config) error {
// Below function returns filtered operator/CustomResource specific GVKs.
// For more control override the below GVK list with your own custom logic.
filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme)
if err != nil {
return err
}
// Get the namespace the operator is currently deployed in.
operatorNs, err := k8sutil.GetOperatorNamespace()
if err != nil {
return err
}
// To generate metrics in other namespaces, add the values below.
ns := []string{operatorNs}
// Generate and serve custom resource specific metrics.
err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort)
if err != nil {
return err
}
return nil
}
`
36 changes: 33 additions & 3 deletions internal/pkg/scaffold/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ import (

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"

"github.com/example-inc/app-operator/pkg/apis"
"github.com/example-inc/app-operator/pkg/controller"

"github.com/operator-framework/operator-sdk/pkg/k8sutil"
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/log/zap"
"github.com/operator-framework/operator-sdk/pkg/metrics"
Expand All @@ -63,8 +65,9 @@ import (

// Change below variables to serve metrics on different host or port.
var (
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
operatorMetricsPort int32 = 8686
)
var log = logf.Log.WithName("cmd")

Expand Down Expand Up @@ -111,7 +114,6 @@ func main() {
}

ctx := context.TODO()

// Become the leader before proceeding
err = leader.Become(ctx, "app-operator-lock")
if err != nil {
Expand Down Expand Up @@ -144,6 +146,10 @@ func main() {
os.Exit(1)
}

if err = serveCRMetrics(cfg); err != nil {
log.Info("Could not generate and serve custom resource metrics: ", err.Error())
}

// Create Service object to expose the metrics port.
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
if err != nil {
Expand All @@ -158,4 +164,28 @@ func main() {
os.Exit(1)
}
}

// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
func serveCRMetrics(cfg *rest.Config) error {
// Below function returns filtered operator/CustomResource specific GVKs.
// For more control override the below GVK list with your own custom logic.
filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme)
if err != nil {
return err
}
// Get the namespace the operator is currently deployed in.
operatorNs, err := k8sutil.GetOperatorNamespace()
if err != nil {
return err
}
// To generate metrics in other namespaces, add the values below.
ns := []string{operatorNs}
// Generate and serve custom resource specific metrics.
err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort)
if err != nil {
return err
}
return nil
}
`
2 changes: 2 additions & 0 deletions internal/pkg/scaffold/go_mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ require (
k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628
k8s.io/client-go v2.0.0-alpha.0.0.20181126152608-d082d5923d3c+incompatible
k8s.io/kube-openapi v0.0.0-20190603182131-db7b694dc208 // indirect
k8s.io/kube-state-metrics v1.6.0 // indirect
lilic marked this conversation as resolved.
Show resolved Hide resolved
sigs.k8s.io/controller-runtime v0.1.10
sigs.k8s.io/controller-tools v0.1.10
sigs.k8s.io/testing_frameworks v0.1.0 // indirect
Expand All @@ -85,6 +86,7 @@ replace (
github.com/coreos/prometheus-operator => github.com/coreos/prometheus-operator v0.29.0
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.1.10
sigs.k8s.io/controller-tools => sigs.k8s.io/controller-tools v0.1.11-0.20190411181648-9d55346c2bde
k8s.io/kube-state-metrics => k8s.io/kube-state-metrics v1.6.0
)
`

Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/scaffold/go_mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ require (
k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628
k8s.io/client-go v2.0.0-alpha.0.0.20181126152608-d082d5923d3c+incompatible
k8s.io/kube-openapi v0.0.0-20190603182131-db7b694dc208 // indirect
k8s.io/kube-state-metrics v1.6.0 // indirect
sigs.k8s.io/controller-runtime v0.1.10
sigs.k8s.io/controller-tools v0.1.10
sigs.k8s.io/testing_frameworks v0.1.0 // indirect
Expand All @@ -86,5 +87,6 @@ replace (
github.com/coreos/prometheus-operator => github.com/coreos/prometheus-operator v0.29.0
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.1.10
sigs.k8s.io/controller-tools => sigs.k8s.io/controller-tools v0.1.11-0.20190411181648-9d55346c2bde
k8s.io/kube-state-metrics => k8s.io/kube-state-metrics v1.6.0
)
`
9 changes: 9 additions & 0 deletions internal/pkg/scaffold/gopkgtoml.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ required = [
name = "github.com/coreos/prometheus-operator"
version = "=v0.29.0"

[[override]]
name = "k8s.io/kube-state-metrics"
version = "v1.6.0"

[[override]]
name = "sigs.k8s.io/controller-runtime"
version = "=v0.1.10"
Expand All @@ -85,6 +89,11 @@ required = [
[prune]
go-tests = true
non-go = true

[[prune.project]]
name = "k8s.io/kube-state-metrics"
unused-packages = true

`

func PrintDepGopkgTOML(asFile bool) error {
Expand Down
9 changes: 9 additions & 0 deletions internal/pkg/scaffold/gopkgtoml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ required = [
name = "github.com/coreos/prometheus-operator"
version = "=v0.29.0"

[[override]]
name = "k8s.io/kube-state-metrics"
version = "v1.6.0"

[[override]]
name = "sigs.k8s.io/controller-runtime"
version = "=v0.1.10"
Expand All @@ -83,4 +87,9 @@ required = [
[prune]
go-tests = true
non-go = true

[[prune.project]]
name = "k8s.io/kube-state-metrics"
unused-packages = true

`
41 changes: 41 additions & 0 deletions pkg/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
discovery "k8s.io/client-go/discovery"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
Expand Down Expand Up @@ -115,3 +117,42 @@ func GetPod(ctx context.Context, client crclient.Client, ns string) (*corev1.Pod

return pod, nil
}

// GetGVKsFromAddToScheme takes in the runtime scheme and filters out all generic apimachinery meta types.
// It returns just the GVK specific to this scheme.
func GetGVKsFromAddToScheme(addToSchemeFunc func(*runtime.Scheme) error) ([]schema.GroupVersionKind, error) {
s := runtime.NewScheme()
err := addToSchemeFunc(s)
if err != nil {
return nil, err
}
schemeAllKnownTypes := s.AllKnownTypes()
ownGVKs := []schema.GroupVersionKind{}
for gvk, _ := range schemeAllKnownTypes {
if !isKubeMetaKind(gvk.Kind) {
ownGVKs = append(ownGVKs, gvk)
}
}

return ownGVKs, nil
}

func isKubeMetaKind(kind string) bool {
if strings.HasSuffix(kind, "List") ||
kind == "GetOptions" ||
lilic marked this conversation as resolved.
Show resolved Hide resolved
kind == "DeleteOptions" ||
kind == "ExportOptions" ||
kind == "APIVersions" ||
kind == "APIGroupList" ||
kind == "APIResourceList" ||
kind == "UpdateOptions" ||
kind == "CreateOptions" ||
kind == "Status" ||
kind == "WatchEvent" ||
kind == "ListOptions" ||
kind == "APIGroup" {
return true
}

return false
}
Loading