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

Generate prom metrics periodically; Handle scanner-opa removal #259

Merged
merged 5 commits into from
Oct 5, 2023
Merged
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
4 changes: 4 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,5 +377,9 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {
}
m.Install(genericServer.Handler.NonGoRestfulMux)
}
if err := mgr.Add(manager.RunnableFunc(metricshandler.StartMetricsCollector(mgr))); err != nil {
setupLog.Error(err, "unable to start metrics collector")
os.Exit(1)
}
return s, nil
}
28 changes: 18 additions & 10 deletions pkg/graph/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"strings"
"sync/atomic"
"time"

scannerapi "kubeops.dev/scanner/apis/scanner/v1alpha1"
Expand Down Expand Up @@ -56,6 +57,8 @@ func PollNewResourceTypes(cfg *restclient.Config, pqr *projectquotacontroller.Pr
klog.ErrorS(err, "failed to list server preferred resources")
return false, nil
}
opaInstalled := false
scannerInstalled := false
for _, rsList := range rsLists {
for _, rs := range rsList.APIResources {
// skip sub resource
Expand Down Expand Up @@ -85,6 +88,15 @@ func PollNewResourceTypes(cfg *restclient.Config, pqr *projectquotacontroller.Pr
Kind: rs.Kind,
Scope: scope,
}

// As we are generating metrics based on these variables, we need to update them timely.
if rid.Group == "templates.gatekeeper.sh" && rid.Kind == "ConstraintTemplate" {
opaInstalled = true
}
if rid.Group == scannerapi.SchemeGroupVersion.Group && rid.Kind == scannerapi.ResourceKindImageScanRequest {
scannerInstalled = true
}

if _, found := resourceTracker[gvk]; !found {
resourceTracker[gvk] = rid
resourceChannel <- rid
Expand All @@ -94,6 +106,10 @@ func PollNewResourceTypes(cfg *restclient.Config, pqr *projectquotacontroller.Pr
}
}
}

OPAInstalled.Store(opaInstalled)
ScannerInstalled.Store(scannerInstalled)

return false, nil
}, ctx.Done())
if err != nil {
Expand All @@ -106,13 +122,10 @@ func PollNewResourceTypes(cfg *restclient.Config, pqr *projectquotacontroller.Pr
}

var (
opaInstalled bool
scannerInstalled bool
OPAInstalled atomic.Bool
ScannerInstalled atomic.Bool
)

func OPAInstalled() bool { return opaInstalled }
func ScannerInstalled() bool { return scannerInstalled }

func SetupGraphReconciler(mgr manager.Manager) func(ctx context.Context) error {
return func(ctx context.Context) error {
for rid := range resourceChannel {
Expand All @@ -124,13 +137,8 @@ func SetupGraphReconciler(mgr manager.Manager) func(ctx context.Context) error {
return err
}

if rid.Group == "templates.gatekeeper.sh" && rid.Kind == "ConstraintTemplate" {
opaInstalled = true
}

if rid.Group == scannerapi.SchemeGroupVersion.Group &&
rid.Kind == scannerapi.ResourceKindImageScanRequest {
scannerInstalled = true
if err := (&scannercontrollers.WorkloadReconciler{
Client: mgr.GetClient(),
}).SetupWithManager(mgr); err != nil {
Expand Down
17 changes: 7 additions & 10 deletions pkg/metricshandler/ancestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,43 @@ import (
"context"

"kubeops.dev/ui-server/pkg/graph"
"kubeops.dev/ui-server/pkg/metricsstore"

core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kube-state-metrics/v2/pkg/metric"
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
kmapi "kmodules.xyz/client-go/api/v1"
"kmodules.xyz/resource-metadata/apis/meta/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func collectPodAncestorMetrics(kc client.Client, generators []generator.FamilyGenerator, store *metricsstore.MetricsStore) error {
func (mc *Collector) collectPodAncestorMetrics() error {
var pods core.PodList
err := kc.List(context.TODO(), &pods)
err := mc.kc.List(context.TODO(), &pods)
if err != nil {
return err
}

family := generators[0].Generate(nil)
family := mc.generators[0].Generate(nil)
for _, pod := range pods.Items {
g, err := getResourceGraph(kc, pod.ObjectMeta)
g, err := mc.getResourceGraph(pod.ObjectMeta)
if err != nil {
return err
}
family.Metrics = append(family.Metrics, getMetricsForSinglePod(g, pod.Name)...)
}

store.Add(family)
mc.store.Add(family)
return nil
}

func getResourceGraph(kc client.Client, podMeta metav1.ObjectMeta) (*v1alpha1.ResourceGraphResponse, error) {
func (mc *Collector) getResourceGraph(podMeta metav1.ObjectMeta) (*v1alpha1.ResourceGraphResponse, error) {
src := kmapi.ObjectID{
Group: "",
Kind: "Pod",
Namespace: podMeta.Namespace,
Name: podMeta.Name,
}

return graph.ResourceGraph(kc.RESTMapper(), src, []kmapi.EdgeLabel{
return graph.ResourceGraph(mc.kc.RESTMapper(), src, []kmapi.EdgeLabel{
kmapi.EdgeLabelOffshoot,
})
}
Expand Down
Loading
Loading