Skip to content

Commit

Permalink
feat(spacetemplates): added metrics for spacetempalte controller
Browse files Browse the repository at this point in the history
  • Loading branch information
abelhoula committed Oct 28, 2023
1 parent 2f1d24c commit f5b09e0
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 47 deletions.
13 changes: 11 additions & 2 deletions config/samples/space_with_tpl_merge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,23 @@ spec:
- name: ali
kind: User
networkPolicies:
enableDefaultStrictMode: false # false
enableDefaultStrictMode: true # false
items:
- policyTypes:
- Ingress
- Egress
egress:
- to:
- ipBlock:
cidr: 1.1.1.1/0
cidr: 0.0.0.0/0
except:
- 192.168.0.0/16
ingress:
- from:
- namespaceSelector:
matchLabels:
app.kubernetes.io/instance: space-all-in-one
- podSelector: { }
- ipBlock:
cidr: 192.168.0.0/16
podSelector: { }
9 changes: 4 additions & 5 deletions controllers/space/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,19 @@ func (r *Reconciler) reconcileSpaceFromTemplate(ctx context.Context, space *naut
return ctrl.Result{}, err
}

// Update the existing Space resource with the data from the SpaceTemplate
// Merge and override space.ResourceQuota and spacetemplate.ResourceQuota
rs, err := r.MergeResourceQuotas(space, spacetpl)
if err == nil {
space.Spec.ResourceQuota = *rs
}

// Merge and override space.AdditionalRoleBindings and spacetemplate.AdditionalRoleBindings
rb, err := r.MergeRoleBindings(space, spacetpl)
if err == nil {
space.Spec.AdditionalRoleBindings = rb
}

netPolicies, err := r.MergeNetworkPolicies(space, spacetpl)
if err == nil {
space.Spec.NetworkPolicies.Items = netPolicies.Items
if reflect.ValueOf(space.Spec.NetworkPolicies).IsZero() {
space.Spec.NetworkPolicies = spacetpl.Spec.NetworkPolicies
}

if reflect.ValueOf(space.Spec.LimitRanges).IsZero() {
Expand Down
31 changes: 3 additions & 28 deletions controllers/space/utlis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -64,25 +63,11 @@ func (r *Reconciler) MergeRoleBindings(space *nauticusiov1alpha1.Space, spaceTem
}
}

return mergedRoleBindings, nil
}

func (r *Reconciler) MergeNetworkPolicies(space *nauticusiov1alpha1.Space, spaceTemplate *nauticusiov1alpha1.SpaceTemplate) (nauticusiov1alpha1.NetworkPolicies, error) {
mergedPolicies := space.Spec.NetworkPolicies

// Check if EnableDefaultStrictMode is not provided in the space
if reflect.ValueOf(mergedPolicies.EnableDefaultStrictMode).IsZero() {
mergedPolicies.EnableDefaultStrictMode = spaceTemplate.Spec.NetworkPolicies.EnableDefaultStrictMode
}

for _, templatePolicy := range spaceTemplate.Spec.NetworkPolicies.Items {
// Check if the policy already exists in mergedPolicies
if !cmpNetworkPolicy(mergedPolicies, templatePolicy) {
mergedPolicies.Items = append(mergedPolicies.Items, templatePolicy)
}
if len(mergedRoleBindings) > 0 {
return mergedRoleBindings, nil
}

return mergedPolicies, nil
return nil, errors.New("no additional roles bindings merged from the template")
}

func overrideResourceQuotas(resourceQuotas *corev1.ResourceQuotaSpec, spaceHard, templateHard corev1.ResourceList, resource corev1.ResourceName) {
Expand All @@ -105,13 +90,3 @@ func cmpRoleBinding(roleBindings []nauticusiov1alpha1.AdditionalRoleBinding, rol

return false
}

func cmpNetworkPolicy(mergedPolicies nauticusiov1alpha1.NetworkPolicies, policy networkingv1.NetworkPolicySpec) bool {
for _, mergedPolicy := range mergedPolicies.Items {
if reflect.DeepEqual(mergedPolicy, policy) {
return true
}
}

return false
}
26 changes: 26 additions & 0 deletions controllers/spacetemplate/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022-2023 Edixos
// SPDX-License-Identifier: Apache-2.0
package spacetemplate

import (
"github.com/edixos/nauticus/api/v1alpha1"
"github.com/edixos/nauticus/pkg/controller/constants"
"github.com/edixos/nauticus/pkg/metrics"
)

func (r *Reconciler) setMetrics(spacetpl *v1alpha1.SpaceTemplate, conditionType v1alpha1.ConditionType) {
switch conditionType {
case v1alpha1.ConditionType(constants.SpaceTplConditionReady):
metrics.ReadySpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
metrics.InProgresSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
metrics.FailedSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
case v1alpha1.ConditionType(constants.SpaceTplConditionCreating):
metrics.ReadySpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
metrics.InProgresSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
metrics.FailedSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
case v1alpha1.ConditionType(constants.SpaceTplConditionFailed):
metrics.ReadySpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
metrics.InProgresSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
metrics.FailedSpaceTemplates.WithLabelValues(spacetpl.Name).Set(0)
}
}
26 changes: 16 additions & 10 deletions controllers/spacetemplate/reconsiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package spacetemplate
import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1"
"github.com/edixos/nauticus/pkg/controller/constants"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -19,19 +20,24 @@ func (r *Reconciler) reconcileSpaceTemplate(ctx context.Context, spaceTpl *nauti
controllerutil.AddFinalizer(spaceTpl, constants.NauticusSpaceFinalizer)

if err = r.Update(ctx, spaceTpl); err != nil {
r.Log.Info("Reconciling SpaceTemplate")
r.ProcessInProgressCondition(ctx, spaceTpl, constants.SpaceTplConditionCreating, metav1.ConditionUnknown, constants.SpaceTplCreatingReason, constants.SpaceTplCreatingMessage)
r.setMetrics(spaceTpl, nauticusiov1alpha1.ConditionType(constants.SpaceTplConditionCreating))
r.EmitEvent(spaceTpl, spaceTpl.GetName(), controllerutil.OperationResultCreated, constants.SpaceTplCreatingMessage, nil)

return ctrl.Result{}, err
}

r.ProcessFailedCondition(ctx, spaceTpl, constants.SpaceTplConditionFailed, metav1.ConditionFalse, constants.SpaceTplFailedReason, constants.SpaceTplFailedMessage)
r.setMetrics(spaceTpl, nauticusiov1alpha1.ConditionType(constants.SpaceTplConditionFailed))

r.EmitEvent(spaceTpl, spaceTpl.GetName(), controllerutil.OperationResultUpdatedStatus, constants.SpaceTplFailedMessage, nil)
}

r.ProcessCondition(
ctx,
spaceTpl,
constants.SpaceTplConditionReady,
metav1.ConditionTrue,
constants.SpaceTplSyncSuccessReason,
constants.SpaceTplSyncSuccessMessage,
)
r.EmitEvent(spaceTpl, spaceTpl.GetName(), controllerutil.OperationResultCreated, constants.SpaceTplCreatingMessage, nil)
r.ProcessFailedCondition(ctx, spaceTpl, constants.SpaceTplConditionReady, metav1.ConditionTrue, constants.SpaceTplSyncSuccessReason, constants.SpaceTplSyncSuccessMessage)
r.setMetrics(spaceTpl, nauticusiov1alpha1.ConditionType(constants.SpaceTplConditionReady))

r.EmitEvent(spaceTpl, spaceTpl.GetName(), controllerutil.OperationResultUpdatedStatus, constants.SpaceTplSyncSuccessMessage, nil)

return ctrl.Result{
RequeueAfter: constants.RequeueAfter,
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ const (
SpaceSyncFailMessage string = "Space failed to sync"
SpaceCreatingMessage string = "Creating Space in progress"

SpaceTplConditionReady string = "Ready"
SpaceTplConditionReady string = "Ready"
SpaceTplConditionCreating string = "Creating"
SpaceTplConditionFailed string = "Failed"

SpaceTplCreatingReason string = "SpaceTemplateCreating"
SpaceTplSyncSuccessReason string = "SpaceTemplateSyncedSuccessfully"
SpaceTplFailedReason string = "SpaceTemplateSyncFailed"

SpaceTplSyncSuccessMessage string = "SpaceTemplate synced successfully"
SpaceTplCreatingMessage string = "Creating SpaceTemplate in progress"
SpaceTplFailedMessage string = "SpaceTemplate Failed to sync"
)
22 changes: 21 additions & 1 deletion pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,28 @@ var (
Help: "In Progress Spaces",
}, []string{"name"},
)

ReadySpaceTemplates = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ready_spacetemplates",
Help: "ready SpaceTemplates",
}, []string{"name"},
)

FailedSpaceTemplates = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "not_ready_spacetemplates",
Help: "not ready SpaceTemplates",
}, []string{"name"},
)
InProgresSpaceTemplates = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "in_progress_spacetemplates",
Help: "In Progress SpaceTemplates",
}, []string{"name"},
)
)

func init() {
metrics.Registry.MustRegister(FailedSpaces, ReadySpaces, InProgressSpaces)
metrics.Registry.MustRegister(FailedSpaces, ReadySpaces, InProgressSpaces, ReadySpaceTemplates, FailedSpaceTemplates, InProgresSpaceTemplates)
}

0 comments on commit f5b09e0

Please sign in to comment.