-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/kube-metrics: Add initial operator specific metrics
Operator specific metrics are generated based on the api/kind given deployed in the given namespace(s).
- Loading branch information
Showing
20 changed files
with
1,331 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2019 The Operator-SDK 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 scaffold | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input" | ||
) | ||
|
||
const MetricsFile = "metrics.go" | ||
|
||
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.TemplateBody = metricsPkgTemplate | ||
return s.Input, nil | ||
} | ||
|
||
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" | ||
"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) | ||
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) | ||
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 | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2019 The Operator-SDK 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 scaffold | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/operator-framework/operator-sdk/internal/util/diffutil" | ||
) | ||
|
||
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}) | ||
if err != nil { | ||
t.Fatalf("Failed to execute the scaffold: (%v)", err) | ||
} | ||
|
||
if metricsExp != buf.String() { | ||
diffs := diffutil.Diff(metricsExp, buf.String()) | ||
t.Fatalf("Expected vs actual differs.\n%v", diffs) | ||
} | ||
} | ||
|
||
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" | ||
"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) | ||
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) | ||
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 | ||
} | ||
` |
Oops, something went wrong.