Skip to content

Commit

Permalink
Merge pull request #49 from ContainerSolutions/feature/faster-reconci…
Browse files Browse the repository at this point in the history
…liation

Feature/faster reconciliation
  • Loading branch information
gusfcarvalho authored Jul 5, 2023
2 parents e40396b + 8e10461 commit 9e11c1c
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions operator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/ContainerSolutions/argus/operator/internal/controller/resourceattestation"
"github.com/ContainerSolutions/argus/operator/internal/controller/resourceimplementation"
"github.com/ContainerSolutions/argus/operator/internal/controller/resourcerequirement"
"github.com/ContainerSolutions/argus/operator/internal/metrics"
//+kubebuilder:scaffold:imports
)

Expand All @@ -61,6 +62,7 @@ func main() {
var probeAddr string
var lvl zapcore.Level
var enc zapcore.TimeEncoder
metrics.SetUpMetrics()
lvlErr := lvl.UnmarshalText([]byte("info"))
if lvlErr != nil {
setupLog.Error(lvlErr, "error unmarshalling loglevel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

"github.com/ContainerSolutions/argus/operator/internal/metrics"
lib "github.com/ContainerSolutions/argus/operator/internal/resourceimplementation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -66,6 +67,13 @@ func (r *ResourceImplementationReconciler) Reconcile(ctx context.Context, req ct
res.Status.TotalAttestations = len(children)
res.Status.PassedAttestations = valid
res.Status.RunAt = metav1.Now()
labels := map[string]string{
"resource": res.Labels["argus.io/resource"],
"implementation": res.Labels["argus.io/implementation"],
"requirement": res.Labels["argus.io/requirement"],
}
metrics.GetGaugeVec(metrics.AttestationTotalKey).With(labels).Set(float64(res.Status.TotalAttestations))
metrics.GetGaugeVec(metrics.AttestationValidKey).With(labels).Set(float64(res.Status.PassedAttestations))
err = r.Client.Status().Patch(ctx, &res, client.MergeFrom(original))
if err != nil {
return ctrl.Result{}, fmt.Errorf("could not update requirement status: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

argusiov1alpha1 "github.com/ContainerSolutions/argus/operator/api/v1alpha1"
"github.com/ContainerSolutions/argus/operator/internal/metrics"
lib "github.com/ContainerSolutions/argus/operator/internal/resourcerequirement"
"github.com/go-logr/logr"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -65,6 +66,13 @@ func (r *ResourceRequirementReconciler) Reconcile(ctx context.Context, req ctrl.
res.Status.ApplicableResourceImplementations = implementations
res.Status.Status = "Not Implemented"
res.Status.RunAt = metav1.Now()
labels := map[string]string{
"resource": res.Labels["argus.io/resource"],
"requirement": res.Labels["argus.io/requirement"],
}
metrics.GetGaugeVec(metrics.ImplementationTotalKey).With(labels).Set(float64(res.Status.TotalImplementations))
metrics.GetGaugeVec(metrics.ImplementationValidKey).With(labels).Set(float64(res.Status.ValidImplementations))

if res.Status.TotalImplementations == res.Status.ValidImplementations && res.Status.TotalImplementations > 0 {
res.Status.Status = "Implemented"
}
Expand Down
77 changes: 77 additions & 0 deletions operator/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var implementationLabels = []string{"resource", "implementation", "requirement"}
var requirementLabels = []string{"resource", "requirement"}
var resourceLabels = []string{"resource"}

const (
AttestationTotalKey = "attestations_total"
AttestationValidKey = "attestations_valid"
ImplementationTotalKey = "implementations_total"
ImplementationValidKey = "implementations_valid"
RequirementTotalKey = "requirements_total"
RequirementValidKey = "requirements_valid"
)

var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}

func SetUpMetrics() {
// Only register once
if len(gaugeVecMetrics) == 6 {
return
}
// Obtain the prometheus metrics and register
attestationsTotal := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: "argus",
Name: "attestations_total",
Help: "Total number of Attestations",
}, implementationLabels)
attestationsValid := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: "argus",
Name: "attestations_valid",
Help: "Total number of Attestations",
}, implementationLabels)
implementationsTotal := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: "argus",
Name: "implementations_total",
Help: "Total number of Implementations",
}, requirementLabels)
// Obtain the prometheus metrics and register
implementationsValid := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: "argus",
Name: "implementations_valid",
Help: "Number of valid Implementations",
}, requirementLabels)
requirementsTotal := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: "argus",
Name: "requirements_total",
Help: "Total number of Requirements",
}, resourceLabels)
requirementsValid := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: "argus",
Name: "requirements_valid",
Help: "Number of valid Requirements",
}, resourceLabels)
metrics.Registry.MustRegister(
attestationsTotal, attestationsValid,
implementationsTotal, implementationsValid,
requirementsTotal, requirementsValid)

gaugeVecMetrics = map[string]*prometheus.GaugeVec{
AttestationTotalKey: attestationsTotal,
AttestationValidKey: attestationsValid,
ImplementationTotalKey: implementationsTotal,
ImplementationValidKey: implementationsValid,
RequirementTotalKey: requirementsTotal,
RequirementValidKey: requirementsValid,
}
}

func GetGaugeVec(key string) *prometheus.GaugeVec {
return gaugeVecMetrics[key]
}
6 changes: 6 additions & 0 deletions operator/internal/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

argusiov1alpha1 "github.com/ContainerSolutions/argus/operator/api/v1alpha1"
"github.com/ContainerSolutions/argus/operator/internal/metrics"
"github.com/hashicorp/go-multierror"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -36,6 +37,11 @@ func UpdateRequirements(resourceRequirementList argusiov1alpha1.ResourceRequirem
resource.Status.CompliantChildren = compliantChildren
resource.Status.TotalRequirements = len(resourceRequirementList.Items)
resource.Status.ImplementedRequirements = validRequirements
labels := map[string]string{
"resource": resource.Name,
}
metrics.GetGaugeVec(metrics.RequirementTotalKey).With(labels).Set(float64(resource.Status.TotalRequirements))
metrics.GetGaugeVec(metrics.RequirementValidKey).With(labels).Set(float64(resource.Status.ImplementedRequirements))
return resource
}

Expand Down
3 changes: 3 additions & 0 deletions operator/internal/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

argusiov1alpha1 "github.com/ContainerSolutions/argus/operator/api/v1alpha1"
"github.com/ContainerSolutions/argus/operator/internal/metrics"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -14,6 +15,7 @@ import (
)

func TestUpdateRequirements(t *testing.T) {
metrics.SetUpMetrics()
testCases := []struct {
name string
expectedOutput *argusiov1alpha1.Resource
Expand Down Expand Up @@ -94,6 +96,7 @@ func TestUpdateRequirements(t *testing.T) {
}

func TestUpdateChild(t *testing.T) {
metrics.SetUpMetrics()
err := argusiov1alpha1.AddToScheme(scheme.Scheme)
require.Nil(t, err)
testCases := []struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

argusiov1alpha1 "github.com/ContainerSolutions/argus/operator/api/v1alpha1"
"github.com/ContainerSolutions/argus/operator/internal/metrics"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -15,6 +16,7 @@ import (

func TestListResourceAttestations(t *testing.T) {
commonScheme := runtime.NewScheme()
metrics.SetUpMetrics()
err := argusiov1alpha1.AddToScheme(commonScheme)
require.Nil(t, err)
testCases := []struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

argusiov1alpha1 "github.com/ContainerSolutions/argus/operator/api/v1alpha1"
"github.com/ContainerSolutions/argus/operator/internal/metrics"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -14,6 +15,7 @@ import (
)

func TestGetValidResourceImplementations(t *testing.T) {
metrics.SetUpMetrics()
commonScheme := runtime.NewScheme()
err := argusiov1alpha1.AddToScheme(commonScheme)
require.Nil(t, err)
Expand Down

0 comments on commit 9e11c1c

Please sign in to comment.