Skip to content

Commit

Permalink
fix(operator): refactored metric adapter for helm generation (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealAnna authored Jan 31, 2023
1 parent ebdebad commit e271162
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
3 changes: 2 additions & 1 deletion operator/cmd/metrics/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (

type MetricsAdapter struct {
basecmd.AdapterBase
KltNamespace string
}

// RunAdapter starts the Keptn Metrics adapter to provide KeptnMetrics via the Kubernetes Custom Metrics API.
Expand Down Expand Up @@ -69,7 +70,7 @@ func (a *MetricsAdapter) makeProviderOrDie(ctx context.Context) provider.CustomM
klog.Fatalf("unable to construct dynamic client: %v", err)
}

return kmprovider.NewProvider(ctx, client)
return kmprovider.NewProvider(ctx, client, a.KltNamespace)
}

func addFlags() {
Expand Down
16 changes: 8 additions & 8 deletions operator/cmd/metrics/adapter/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ import (
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
)

const kltNamespace = "keptn-lifecycle-toolkit-system"

var keptnMetricGroupVersionResource = schema.GroupVersionResource{Group: "metrics.keptn.sh", Version: "v1alpha1", Resource: "keptnmetrics"}

var providerInstance *keptnMetricsProvider

var providerOnce sync.Once

type keptnMetricsProvider struct {
client dynamic.Interface
scheme *runtime.Scheme
logger logr.Logger
client dynamic.Interface
scheme *runtime.Scheme
logger logr.Logger
KltNamespace string

// cache is being populated via the updates received by the provider's dynamic informer
// this way, we avoid sending a request to the Kubernetes API each time a custom metric value should be retrieved
Expand All @@ -45,7 +44,7 @@ type keptnMetricsProvider struct {

// NewProvider creates and starts a new keptnMetricsProvider. The provider will run until the given context is cancelled.
// the client passed to this function will be used to set up a dynamic informer that listens for KeptnMetric CRDs and provides metric values that reflect their states.
func NewProvider(ctx context.Context, client dynamic.Interface) provider.CustomMetricsProvider {
func NewProvider(ctx context.Context, client dynamic.Interface, namespace string) provider.CustomMetricsProvider {
providerOnce.Do(func() {
scheme := runtime.NewScheme()

Expand All @@ -55,7 +54,8 @@ func NewProvider(ctx context.Context, client dynamic.Interface) provider.CustomM
cache: CustomMetricsCache{
metrics: map[string]CustomMetricValue{},
},
logger: ctrl.Log.WithName("provider"),
logger: ctrl.Log.WithName("provider"),
KltNamespace: namespace,
}

if err := providerInstance.watchMetrics(ctx); err != nil {
Expand Down Expand Up @@ -110,7 +110,7 @@ func (p *keptnMetricsProvider) GetMetricBySelector(ctx context.Context, _ string
}

func (p *keptnMetricsProvider) watchMetrics(ctx context.Context) error {
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(p.client, 0, kltNamespace, nil)
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(p.client, 0, p.KltNamespace, nil)

informer := factory.ForResource(keptnMetricGroupVersionResource).Informer()

Expand Down
4 changes: 3 additions & 1 deletion operator/cmd/metrics/adapter/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
provider2 "sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
)

const kltNamespace = "my-namespace"

func TestProvider(t *testing.T) {
metricObj1 := getSampleKeptnMetric("my-metric", map[string]interface{}{})

Expand All @@ -24,7 +26,7 @@ func TestProvider(t *testing.T) {
scheme := runtime.NewScheme()
fakeClient := fake.NewSimpleDynamicClient(scheme, km)

provider := NewProvider(context.TODO(), fakeClient)
provider := NewProvider(context.TODO(), fakeClient, kltNamespace)

require.NotNil(t, provider)

Expand Down
10 changes: 10 additions & 0 deletions operator/config/rbac/extra_roles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: keptn-metrics-server-resources
rules:
- apiGroups:
- custom.metrics.k8s.io
resources: ["*"]
verbs: ["*"]
1 change: 1 addition & 0 deletions operator/config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ resources:
- role.yaml
- role_binding.yaml
- extra_role_binding.yaml
- extra_roles.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
# Comment the following 4 lines if you want to disable
Expand Down
10 changes: 0 additions & 10 deletions operator/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,3 @@ rules:
- secrets
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: keptn-metrics-server-resources
rules:
- apiGroups:
- custom.metrics.k8s.io
resources: ["*"]
verbs: ["*"]
6 changes: 3 additions & 3 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func main() {
go serveMetrics()

// Start the custom metrics adapter
go startCustomMetricsAdapter()
go startCustomMetricsAdapter(env.PodNamespace)

// As recommended by the kubebuilder docs, webhook registration should be disabled if running locally. See https://book.kubebuilder.io/cronjob-tutorial/running.html#running-webhooks-locally for reference
flag.BoolVar(&disableWebhook, "disable-webhook", false, "Disable the registration of webhooks.")
Expand Down Expand Up @@ -579,10 +579,10 @@ func serveMetrics() {
}
}

func startCustomMetricsAdapter() {
func startCustomMetricsAdapter(namespace string) {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM)
defer cancel()

adapter := adapter.MetricsAdapter{}
adapter := adapter.MetricsAdapter{KltNamespace: namespace}
adapter.RunAdapter(ctx)
}

0 comments on commit e271162

Please sign in to comment.