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

*: Move collectors pkg to internal dir #632

Merged
merged 1 commit into from
Jan 23, 2019

Conversation

lilic
Copy link
Member

@lilic lilic commented Jan 15, 2019

Rename the existing functions that are used in collectors to collector
pkg.

What this PR does / why we need it:
Expose helper functions that can be used outside of kube-state-metrics and move collector package collectors to internal package. The internal directory is in the root of the project, this is so we can call this in the main.go file, import of a path containing the element “internal” is disallowed if the importing code is outside the tree rooted at the parent of the “internal” directory.

This is what we need from kube-state-metrics, but if @wanghaoran1988 needs something more, I can adjust things. Also open for naming discussions, etc. :)

cc @mxinden @brancz

Which issue(s) this PR fixes:
Fixes #579

@k8s-ci-robot k8s-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Jan 15, 2019
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jan 15, 2019
@brancz
Copy link
Member

brancz commented Jan 15, 2019

This looks good to me (but giving @mxinden a chance for a review).

I think @mxinden was thinking through some changes to avoid duplication of metric name strings. It's unrelated to this change, just a heads up, that the API might change.

@lilic
Copy link
Member Author

lilic commented Jan 15, 2019

I think @mxinden was thinking through some changes to avoid duplication of metric name strings. It's unrelated to this change, just a heads up, that the API might change.

Yes, I was looking at that as well, sounds good improvement!

Copy link
Contributor

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this. Happy to do the rebase in case #633 merges first.

@@ -0,0 +1,83 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure builder.go is the right name here. Maybe metric_family.go instead as it is mostly concerned of groups of metrics?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, should we move these to the metrics pkg instead to metric_family.go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a great idea! Some of these could even be methods on the Family struct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mxinden which ones of the below were you thinking of binding to the Family struct? As all of them accept []metrics.FamilyGenerator and binding them to a single instance of the FamilyGenerator we would still need the functions that loop through the slices.

Also was thinking of moving it to pkg/metrics/family.go, sgty?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of:

func (f *FamilyGenerator) GenerateHeader() string {
		header := strings.Builder{}

		header.WriteString("# HELP ")
		header.WriteString(f.Name)
		header.WriteByte(' ')
		header.WriteString(f.Help)
		header.WriteByte('\n')
		header.WriteString("# TYPE ")
		header.WriteString(f.Name)
		header.WriteByte(' ')
		header.WriteString(string(f.Type))

		return header.String()
}

Keeping the looping logic in ExtractMetricFamilyHeaders. But I can do that in a follow up.


Also was thinking of moving it to pkg/metrics/family.go, sgty?

👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping the looping logic in ExtractMetricFamilyHeaders.

Where would that live?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe alongside the FamilyGenerator struct inside pkg/metrics/generator.go?

internal/collectors/builder.go Outdated Show resolved Hide resolved
pkg/whiteblacklist/whiteblacklist.go Outdated Show resolved Hide resolved
pkg/metrics/metrics.go Outdated Show resolved Hide resolved
pkg/metrics/metrics.go Outdated Show resolved Hide resolved
@mxinden
Copy link
Contributor

mxinden commented Jan 21, 2019

@lilic instead of cf42c78, what do you think about:

diff --git a/pkg/metrics_store/metrics_store_test.go b/pkg/metrics_store/metrics_store_test.go
index 6199d89..c750df7 100644
--- a/pkg/metrics_store/metrics_store_test.go
+++ b/pkg/metrics_store/metrics_store_test.go
@@ -9,9 +9,19 @@ import (
        "k8s.io/apimachinery/pkg/api/meta"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/apimachinery/pkg/types"
-       "k8s.io/kube-state-metrics/pkg/metric"
 )
 
+// Mock metricFamily instead of importing /pkg/metric to prevent cyclic
+// dependency.
+type metricFamily struct {
+       value string
+}
+
+// Implement FamilyStringer interface.
+func (f *metricFamily) String() string {
+       return f.value
+}
+
 func TestObjectsSameNameDifferentNamespaces(t *testing.T) {
        serviceIDS := []string{"a", "b"}
 
@@ -21,15 +31,11 @@ func TestObjectsSameNameDifferentNamespaces(t *testing.T) {
                        t.Fatal(err)
                }
 
-               metric := metric.Metric{
-                       Name:        "kube_service_info",
-                       LabelKeys:   []string{"uid"},
-                       LabelValues: []string{string(o.GetUID())},
-                       Value:       1,
+               metricFamily := metricFamily{
+                       fmt.Sprintf("kube_service_info{uid=\"%v\"} 1", string(o.GetUID())),
                }
-               metricFamily := metric.Family{&metric}
 
-               return []FamilyStringer{metricFamily}
+               return []FamilyStringer{&metricFamily}
        }
 
        ms := NewMetricsStore([]string{"Information about service."}, genFunc)

This would only alter test code but keep interfaces close to the caller side.

@lilic
Copy link
Member Author

lilic commented Jan 22, 2019

@mxinden

+// Mock metricFamily instead of importing /pkg/metric to prevent cyclic
+// dependency.

I am curious where would this happen?

This would only alter test code but keep interfaces close to the caller side.

Makes sense!

@mxinden
Copy link
Contributor

mxinden commented Jan 22, 2019

I am curious where would this happen?

I am a bit confused. type metricFamily struct { would be the mock, instead of importing metrics.Family. Does that explain the question?


In regards to the merge conflicts, those are due to #636. In case rebasing is not that easy I am happy to help out. Sorry for the inconvenience.

@lilic
Copy link
Member Author

lilic commented Jan 22, 2019

I am a bit confused. type metricFamily struct { would be the mock, instead of importing metrics.Family. Does that explain the question?

Sorry, I thought the initial comment was referring that can happen in my patch. All good 👍

@lilic
Copy link
Member Author

lilic commented Jan 22, 2019

In regards to the merge conflicts, those are due to #636. In case rebasing is not that easy I am happy to help out. Sorry for the inconvenience.

No worries! Wasn't too bad after all :)

@lilic
Copy link
Member Author

lilic commented Jan 22, 2019

@mxinden I addressed the comment above and rebased to current master, can you have another look, thanks!

Copy link
Contributor

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me.

I don't know how to tell the Kubernetes bot to squash the commits before merging. Tide seems to have a squash option, but I don't know how to set that.

@lilic in case you are lost as I am, would you mind squashing the commits manually into one or two bigger commits?

@lilic lilic force-pushed the lili/lib branch 3 times, most recently from fe41f0d to 4832763 Compare January 23, 2019 09:48
This makes the rest of the packages useful to be used in a standalone
library without importing the kube-state-metrics specific collectors.

* Rename collectors -> collector package

* Rename metrics -> metric package

* Add metricFamily mocking in tests to prevent cyclic dependency.
@lilic
Copy link
Member Author

lilic commented Jan 23, 2019

@mxinden

@lilic in case you are lost as I am, would you mind squashing the commits manually into one or two bigger commits?

I squashed into one commit, but detailed the important commits in the commit msg. Hope that is okay?

@mxinden
Copy link
Contributor

mxinden commented Jan 23, 2019

/approve

@brancz
Copy link
Member

brancz commented Jan 23, 2019

/approve doesn't include lgtm

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 23, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brancz, LiliC, mxinden

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 23, 2019
@k8s-ci-robot k8s-ci-robot merged commit 58a8c11 into kubernetes:master Jan 23, 2019
@lilic lilic deleted the lili/lib branch January 23, 2019 16:35
@ikaldus
Copy link

ikaldus commented Mar 1, 2019

Could it be related to this PR if unable to go get github.com/kubernetes/kube-state-metrics because of
src/github.com/kubernetes/kube-state-metrics/main.go:41:2: use of internal package k8s.io/kube-state-metrics/internal/collector not allowed?

@lilic
Copy link
Member Author

lilic commented Mar 1, 2019

@ikaldus You need to do go get k8s.io/kube-state-metrics, that worked for me.

@ikaldus
Copy link

ikaldus commented Mar 5, 2019

I'm not sure, how I ended up with that github.com, but you are right... go get k8s.io/kube-state-metrics works flawlessly. Thanks for pointing out the obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Make kube-state-metrics a library
6 participants