Skip to content

PoC: Configure features through CRD #203

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 7 additions & 2 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def synced_helm(path_to_chart, name, values=[]):
"""

# Build the chart dependencies from the Chart.lock.
local('helm dep build ' + path_to_chart)
# local('helm dep build ' + path_to_chart)

# Build the command to get the rendered kubernetes yaml.
cmd = 'helm template'
Expand All @@ -54,7 +54,8 @@ def synced_helm(path_to_chart, name, values=[]):
########### Cortex Core Services
tilt_values = os.getenv('TILT_VALUES_PATH')
docker_build('ghcr.io/cobaltcore-dev/cortex', '.', only=[
'internal/', 'commands/', 'main.go', 'go.mod', 'go.sum', 'Makefile', tilt_values,
'internal/', 'extractor/', 'commands/',
'main.go', 'go.mod', 'go.sum', 'Makefile', tilt_values,
])
k8s_yaml(synced_helm('./helm/cortex', name='cortex', values=[tilt_values]))
k8s_resource('cortex-syncer', port_forwards=[
Expand All @@ -79,6 +80,10 @@ k8s_resource('cortex-kpis', port_forwards=[
link('localhost:8004/metrics', '/metrics'),
], labels=['Core-Services'])

########### Cortex Features

k8s_yaml(synced_helm('./helm/features', name='cortex-features'))

########### Cortex Commands
k8s_resource('cortex-cli', labels=['Commands'])
local_resource(
Expand Down
53 changes: 53 additions & 0 deletions extractor/api/v1/feature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2025 SAP SE
// SPDX-License-Identifier: Apache-2.0

package v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// FeatureSpec defines the desired state of Feature.
type FeatureSpec struct {
Foo string `json:"foo,omitempty"`
}

// FeatureStatus defines the observed state of Feature.
type FeatureStatus struct {
}

// Feature is the Schema for the features API.
type Feature struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec FeatureSpec `json:"spec,omitempty"`
Status FeatureStatus `json:"status,omitempty"`
}

// Conform to the runtime.Object interface.
func (in *Feature) DeepCopyObject() runtime.Object {
return &Feature{
TypeMeta: in.TypeMeta,
ObjectMeta: in.ObjectMeta,
Spec: in.Spec,
Status: in.Status,
}
}

// FeatureList contains a list of Feature.
type FeatureList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Feature `json:"items"`
}

// Conform to the runtime.Object interface.
func (in *FeatureList) DeepCopyObject() runtime.Object {
return &FeatureList{
TypeMeta: in.TypeMeta,
ListMeta: in.ListMeta,
Items: in.Items,
}
}
85 changes: 85 additions & 0 deletions extractor/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2025 SAP SE
// SPDX-License-Identifier: Apache-2.0

package extractor

import (
"crypto/tls"
"log/slog"

"github.com/cobaltcore-dev/cortex/internal/conf"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/monitoring"
"github.com/cobaltcore-dev/cortex/internal/mqtt"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/webhook"

extractorv1 "github.com/cobaltcore-dev/cortex/extractor/api/v1"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

func Run(registry *monitoring.Registry, config conf.FeaturesConfig, db db.DB) {
// Set up the kubernetes operator.
schemeBuilder := &scheme.Builder{GroupVersion: schema.GroupVersion{
Group: "extractor.cortex.sap",
Version: "v1",
}}
schemeBuilder.Register(&extractorv1.Feature{}, &extractorv1.FeatureList{})
slog.Info("Registering scheme for feature CRD")
scheme, err := schemeBuilder.Build()
if err != nil {
panic("failed to build scheme: " + err.Error())
}
// If the enable-http2 flag is false (the default), http/2 should be disabled
// due to its vulnerabilities. More specifically, disabling http/2 will
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
// Rapid Reset CVEs. For more information see:
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
// - https://github.com/advisories/GHSA-4374-p667-p6c8
webhookServer := webhook.NewServer(webhook.Options{
TLSOpts: []func(*tls.Config){
func(c *tls.Config) {
slog.Info("Setting up TLS for webhook server")
c.NextProtos = []string{"http/1.1"}
},
},
})
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Metrics: metricsserver.Options{BindAddress: ":8081"}, // TODO: Conf + port + svcmonitor
Scheme: scheme,
WebhookServer: webhookServer,
})
if err != nil {
panic("failed to create controller manager: " + err.Error())
}
slog.Info("Created controller manager")

// Setup the feature extractor.
monitor := NewPipelineMonitor(registry)

mqttClient := mqtt.NewClient(mqtt.NewMQTTMonitor(registry))
if err := mqttClient.Connect(); err != nil {
panic("failed to connect to mqtt broker: " + err.Error())
}
defer mqttClient.Disconnect()

pipeline := NewPipeline(config, db, monitor, mqttClient)
// Selects the extractors to run based on the config.
pipeline.Init(SupportedExtractors)
go pipeline.ExtractOnTrigger() // blocking

// Bind the reconciliation loop.
ctrl.NewControllerManagedBy(mgr).
For(&extractorv1.Feature{}).
Named("feature").
Complete(&pipeline)

Check failure on line 77 in extractor/main.go

View workflow job for this annotation

GitHub Actions / Checks

Error return value of `(*sigs.k8s.io/controller-runtime/pkg/builder.TypedBuilder[sigs.k8s.io/controller-runtime/pkg/reconcile.Request]).Complete` is not checked (errcheck)

slog.Info("starting manager")
go func() {
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
panic("failed to start controller manager: " + err.Error())
}
}()
}
4 changes: 2 additions & 2 deletions internal/features/monitor.go → extractor/monitor.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright 2025 SAP SE
// SPDX-License-Identifier: Apache-2.0

package features
package extractor

import (
"log/slog"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/conf"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/monitoring"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2025 SAP SE
// SPDX-License-Identifier: Apache-2.0
package features
package extractor

import (
"strings"
"testing"

"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/monitoring"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
Expand Down
24 changes: 19 additions & 5 deletions internal/features/pipeline.go → extractor/pipeline.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
// Copyright 2025 SAP SE
// SPDX-License-Identifier: Apache-2.0

package features
package extractor

import (
"context"
"log/slog"
"slices"
"sync"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/extractor/plugins/kvm"
"github.com/cobaltcore-dev/cortex/extractor/plugins/shared"
"github.com/cobaltcore-dev/cortex/extractor/plugins/vmware"
"github.com/cobaltcore-dev/cortex/internal/conf"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/features/plugins/kvm"
"github.com/cobaltcore-dev/cortex/internal/features/plugins/shared"
"github.com/cobaltcore-dev/cortex/internal/features/plugins/vmware"
"github.com/cobaltcore-dev/cortex/internal/mqtt"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

pahomqtt "github.com/eclipse/paho.mqtt.golang"
)
Expand All @@ -36,6 +40,11 @@ var SupportedExtractors = []plugins.FeatureExtractor{

// Pipeline that contains multiple feature extractors and executes them.
type FeatureExtractorPipeline struct {
// Client for the kubernetes API.
client.Client
// Kubernetes scheme to use for the feature extractors.
Scheme *runtime.Scheme

// The dependency graph of the feature extractors, which is used to
// determine the execution order of the feature extractors.
//
Expand Down Expand Up @@ -203,3 +212,8 @@ func (p *FeatureExtractorPipeline) extract(order [][]plugins.FeatureExtractor) {
wg.Wait()
}
}

func (r *FeatureExtractorPipeline) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
slog.Info("reconciling feature extractor", "name", req.Name, "namespace", req.Namespace)
return ctrl.Result{}, nil
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright 2025 SAP SE
// SPDX-License-Identifier: Apache-2.0

package features
package extractor

import (
"errors"
"os"
"sync"
"testing"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/conf"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/mqtt"
"github.com/cobaltcore-dev/cortex/testlib/mqtt/containers"
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package kvm
import (
_ "embed"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/sync/prometheus"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package kvm
import (
_ "embed"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/sync/prometheus"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package shared
import (
_ "embed"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/sync/openstack"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package shared
import (
_ "embed"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/sync/openstack"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package shared
import (
_ "embed"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/sync/openstack"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package vmware
import (
_ "embed"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/sync/prometheus"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package vmware
import (
_ "embed"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/sync/openstack"
"github.com/cobaltcore-dev/cortex/internal/sync/prometheus"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package vmware
import (
_ "embed"

"github.com/cobaltcore-dev/cortex/extractor/plugins"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features/plugins"
"github.com/cobaltcore-dev/cortex/internal/sync/openstack"
"github.com/cobaltcore-dev/cortex/internal/sync/prometheus"
)
Expand Down
Loading
Loading