Skip to content

Commit

Permalink
wip: Modify pkg/kube-metrics to allow multiple CRs
Browse files Browse the repository at this point in the history
  • Loading branch information
lilic committed May 2, 2019
1 parent 14649df commit 4046c9c
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 148 deletions.
21 changes: 20 additions & 1 deletion Gopkg.lock

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

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

[[constraint]]
name = "k8s.io/kube-state-metrics"
branch = "master"
version = "v1.6.0-rc.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
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func apiRun(cmd *cobra.Command, args []string) error {
&scaffold.Doc{Resource: r},
&scaffold.CR{Resource: r},
&scaffold.CRD{Resource: r, IsOperatorGo: projutil.IsOperatorGo()},
&scaffold.Metrics{Resource: r},
//&scaffold.Metrics{Resource: r},
)
if err != nil {
return fmt.Errorf("api scaffold failed: (%v)", err)
Expand Down
1 change: 1 addition & 0 deletions cmd/operator-sdk/new/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func doScaffold() error {
&scaffold.Version{},
&scaffold.Gitignore{},
&scaffold.GopkgToml{},
&scaffold.Metrics{},
)
if err != nil {
return fmt.Errorf("new Go scaffold failed: (%v)", err)
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/scaffold/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import (
"{{ .Repo }}/pkg/apis"
"{{ .Repo }}/pkg/controller"
operatormetrics "{{ .Repo }}/pkg/metrics"
crmetrics "{{ .Repo }}/pkg/metrics"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"github.com/operator-framework/operator-sdk/pkg/leader"
Expand Down Expand Up @@ -115,8 +115,8 @@ func main() {
ctx := context.TODO()
// Start serving operator specific metrics.
if err := operatormetrics.ServeOperatorSpecificMetrics(cfg, metricsHost, operatorMetricsPort); err != nil {
// Start serving operator CR specific metrics.
if err := crmetrics.ServeCRMetrics(cfg, []string{}, metricsHost, operatorMetricsPort); err != nil {
log.Error(err, "")
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/scaffold/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
"github.com/example-inc/app-operator/pkg/apis"
"github.com/example-inc/app-operator/pkg/controller"
operatormetrics "github.com/example-inc/app-operator/pkg/metrics"
crmetrics "github.com/example-inc/app-operator/pkg/metrics"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"github.com/operator-framework/operator-sdk/pkg/leader"
Expand Down Expand Up @@ -114,8 +114,8 @@ func main() {
ctx := context.TODO()
// Start serving operator specific metrics.
if err := operatormetrics.ServeOperatorSpecificMetrics(cfg, metricsHost, operatorMetricsPort); err != nil {
// Start serving operator CR specific metrics.
if err := crmetrics.ServeCRMetrics(cfg, []string{}, metricsHost, operatorMetricsPort); err != nil {
log.Error(err, "")
}
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/scaffold/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ const (
OLMCatalogDir = DeployDir + filePathSep + "olm-catalog"
CRDsDir = DeployDir + filePathSep + "crds"
VersionDir = "version"
MetricsDir = PkgDir + filePathSep + "metrics"
)
2 changes: 1 addition & 1 deletion internal/pkg/scaffold/gopkgtoml.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ required = [
[[constraint]]
name = "k8s.io/kube-state-metrics"
branch = "master"
version = "v1.6.0-rc.0"
[[override]]
name = "sigs.k8s.io/controller-runtime"
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/scaffold/gopkgtoml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ required = [
[[constraint]]
name = "k8s.io/kube-state-metrics"
branch = "master"
version = "v1.6.0-rc.0"
[[override]]
name = "sigs.k8s.io/controller-runtime"
Expand Down
62 changes: 13 additions & 49 deletions internal/pkg/scaffold/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ import (
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input"
)

const MetricsFile = "metrics.go"
const (
metricsFile = "metrics.go"
metricsDir = PkgDir + filePathSep + "metrics"
)

type Metrics struct {
input.Input

// Resource defines the inputs for the new custom resource definition
Resource *Resource
}

func (s *Metrics) GetInput() (input.Input, error) {
if s.Path == "" {
s.Path = filepath.Join(MetricsDir, MetricsFile)
s.Path = filepath.Join(metricsDir, metricsFile)
}
s.TemplateBody = metricsPkgTemplate
return s.Input, nil
Expand All @@ -40,57 +40,21 @@ func (s *Metrics) GetInput() (input.Input, error) {
const metricsPkgTemplate = `package metrics
import (
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
km "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
"k8s.io/client-go/rest"
ksmetric "k8s.io/kube-state-metrics/pkg/metric"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
var log = logf.Log.WithName("metrics")
var resource = "{{ .Resource.APIVersion }}"
var kind = "{{ .Resource.Kind }}"
var metricName = "{{ .Resource.LowerKind }}_info"
var (
MetricFamilies = []ksmetric.FamilyGenerator{
ksmetric.FamilyGenerator{
Name: metricName,
Type: ksmetric.Gauge,
Help: "Information about the {{ .Resource.Kind }} operator replica.",
GenerateFunc: func(obj interface{}) *ksmetric.Family {
crd := obj.(*unstructured.Unstructured)
func ServeOperatorSpecificMetrics(cfg *rest.Config, ns, host string, port int32) error {
// TODO:
// gather all the different resource/kind and metric families from the different CR
// loop through them and create new collectors for all of them.
var kms []km.KubeMetrics
kms = append(kms, km.NewKubeMetrics(resource, kind))
return &ksmetric.Family{
Metrics: []*ksmetric.Metric{
{
Value: 1,
LabelKeys: []string{"namespace", "{{ .Resource.LowerKind }}"},
LabelValues: []string{crd.GetNamespace(), crd.GetName()},
},
},
}
},
},
}
)
func ServeOperatorSpecificMetrics(cfg *rest.Config, host string, port int32) error {
uc := kubemetrics.NewForConfig(cfg)
// By default the current namespace will be detected and used to create metrics.
// Add to the namespaces to include any other namespaces.
namespaces := []string{}
c, err := kubemetrics.NewCollector(uc, namespaces, resource, kind, MetricFamilies)
err := km.ServeCRMetrics(cfg, ns, kms, host, port)
if err != nil {
if err == k8sutil.ErrNoNamespace {
log.Info("Skipping operator specific metrics; not running in a cluster.")
return nil
}
return err
}
go kubemetrics.ServeMetrics(c, host, port)
return nil
}
`
58 changes: 9 additions & 49 deletions internal/pkg/scaffold/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ import (
)

func TestMetrics(t *testing.T) {
r, err := NewResource(appApiVersion, appKind)
if err != nil {
t.Fatal(err)
}
s, buf := setupScaffoldAndWriter()
err = s.Execute(appConfig, &Metrics{Resource: r})
err := s.Execute(appConfig, &Metrics{})
if err != nil {
t.Fatalf("Failed to execute the scaffold: (%v)", err)
}
Expand All @@ -40,57 +36,21 @@ func TestMetrics(t *testing.T) {
const metricsExp = `package metrics
import (
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
km "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
"k8s.io/client-go/rest"
ksmetric "k8s.io/kube-state-metrics/pkg/metric"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
var log = logf.Log.WithName("metrics")
var resource = "app.example.com/v1alpha1"
var kind = "AppService"
var metricName = "appservice_info"
var (
MetricFamilies = []ksmetric.FamilyGenerator{
ksmetric.FamilyGenerator{
Name: metricName,
Type: ksmetric.Gauge,
Help: "Information about the AppService operator replica.",
GenerateFunc: func(obj interface{}) *ksmetric.Family {
crd := obj.(*unstructured.Unstructured)
func ServeOperatorSpecificMetrics(cfg *rest.Config, ns, host string, port int32) error {
// TODO:
// gather all the different resource/kind and metric families from the different CR
// loop through them and create new collectors for all of them.
var kms []km.KubeMetrics
kms = append(kms, km.NewKubeMetrics(resource, kind))
return &ksmetric.Family{
Metrics: []*ksmetric.Metric{
{
Value: 1,
LabelKeys: []string{"namespace", "appservice"},
LabelValues: []string{crd.GetNamespace(), crd.GetName()},
},
},
}
},
},
}
)
func ServeOperatorSpecificMetrics(cfg *rest.Config, host string, port int32) error {
uc := kubemetrics.NewForConfig(cfg)
// By default the current namespace will be detected and used to create metrics.
// Add to the namespaces to include any other namespaces.
namespaces := []string{}
c, err := kubemetrics.NewCollector(uc, namespaces, resource, kind, MetricFamilies)
err := km.ServeCRMetrics(cfg, ns, kms, host, port)
if err != nil {
if err == k8sutil.ErrNoNamespace {
log.Info("Skipping operator specific metrics; not running in a cluster.")
return nil
}
return err
}
go kubemetrics.ServeMetrics(c, host, port)
return nil
}
`
6 changes: 3 additions & 3 deletions pkg/kube-metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
metricsstore "k8s.io/kube-state-metrics/pkg/metrics_store"
)

// NewCollector returns a collection of metrics in the namespaces provided, per the api/kind resource.
// NewCollectors returns collections of metrics in the namespaces provided, per the api/kind resource.
// The metrics are registered in the custom generateStore function that needs to be defined.
// Current operators namespace will be added to the passed namespaces.
func NewCollector(uc *Client,
// Current operators namespace will be added to the passed namespaces if it is not present.
func NewCollectors(uc *Client,
namespaces []string,
api string,
kind string,
Expand Down
Loading

0 comments on commit 4046c9c

Please sign in to comment.