Skip to content

Commit

Permalink
cronjob to cleanup resolved alarms (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelsoccupied authored Jan 17, 2025
1 parent df51386 commit 6ad6c09
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 13 deletions.
12 changes: 12 additions & 0 deletions bundle/manifests/oran-o2ims.clusterserviceversion.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ rules:
- subjectaccessreviews
verbs:
- create
- apiGroups:
- batch
resources:
- cronjobs
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- cluster.open-cluster-management.io
resources:
Expand Down
58 changes: 58 additions & 0 deletions internal/controllers/inventory_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import (
//+kubebuilder:rbac:urls="/internal/v1/caas-alerts/alertmanager",verbs=create;post
//+kubebuilder:rbac:urls="/o2ims-infrastructureCluster/v1/nodeClusterTypes",verbs=get;list
//+kubebuilder:rbac:urls="/o2ims-infrastructureCluster/v1/nodeClusters",verbs=get;list
//+kubebuilder:rbac:groups="batch",resources=cronjobs,verbs=get;list;watch;create;update;patch;delete

// Reconciler reconciles a Inventory object
type Reconciler struct {
Expand Down Expand Up @@ -1140,12 +1141,15 @@ func (t *reconcilerTask) createAlarmServerClusterRole(ctx context.Context) error
},
Resources: []string{
"secrets",
"configmaps",
},
Verbs: []string{
"get",
"list",
"watch",
"update",
"create",
"delete",
},
},
{
Expand All @@ -1161,6 +1165,36 @@ func (t *reconcilerTask) createAlarmServerClusterRole(ctx context.Context) error
"watch",
},
},
{
APIGroups: []string{
"apps",
},
Resources: []string{
"deployments",
},
Verbs: []string{
"get",
"list",
"watch",
},
},
{
APIGroups: []string{
"batch",
},
Resources: []string{
"cronjobs",
},
Verbs: []string{
"get",
"list",
"watch",
"create",
"delete",
"update",
"patch",
},
},
{
NonResourceURLs: []string{
"/o2ims-infrastructureCluster/v1/nodeClusterTypes",
Expand Down Expand Up @@ -1405,6 +1439,30 @@ func (t *reconcilerTask) deployServer(ctx context.Context, serverName string) (u
}...)
}

// Common evn for server deployments
envVars = append(envVars,
corev1.EnvVar{
Name: "POD_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
)

// Server specific env var
if serverName == utils.InventoryAlarmServerName {
postgresImage := os.Getenv(utils.PostgresImageName)
if postgresImage == "" {
return "", fmt.Errorf("missing %s environment variable value", utils.PostgresImageName)
}
envVars = append(envVars, corev1.EnvVar{
Name: utils.PostgresImageName,
Value: postgresImage,
})
}

// Build the deployment's spec.
deploymentSpec := appsv1.DeploymentSpec{
Replicas: k8sptr.To(int32(1)),
Expand Down
20 changes: 16 additions & 4 deletions internal/service/alarms/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"time"

"github.com/openshift-kni/oran-o2ims/internal/service/alarms/internal/serviceconfig"

"github.com/google/uuid"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
Expand All @@ -27,8 +29,8 @@ import (
)

const (
DefaultRetentionPeriod = 10
minRetentionPeriod = 1
DefaultRetentionPeriod = 1 // Default retention of resolved alarms in days
minRetentionPeriod = 1 // Minimum retention of resolved alarms in days
)

// AlarmsServerConfig defines the configuration attributes for the alarms server
Expand All @@ -51,6 +53,8 @@ type AlarmsServer struct {
NotificationProvider notifier.NotificationProvider
// Notifier to notify subscribers with new events
Notifier *notifier.Notifier
// ServiceConfig config needed to manage ServiceConfig
ServiceConfig serviceconfig.Config
}

// AlarmsServer implements StrictServerInterface. This ensures that we've conformed to the `StrictServerInterface` with a compile-time check
Expand Down Expand Up @@ -446,8 +450,12 @@ func (a *AlarmsServer) PatchAlarmServiceConfiguration(ctx context.Context, reque
return nil, fmt.Errorf("failed to patch Alarm Service Configuration: %w", err)
}

slog.Debug("Alarm Service Configuration patched", "retentionPeriod", patched.RetentionPeriod, "extensions", patched.Extensions)
// Update Cronjob
if err := a.ServiceConfig.EnsureCleanupCronJob(ctx, patched); err != nil {
return nil, fmt.Errorf("failed to start cleanup cronjob during AlarmServiceConfiguration patch: %w", err)
}

slog.Debug("Alarm Service Configuration patched", "retentionPeriod", patched.RetentionPeriod, "extensions", patched.Extensions)
return api.PatchAlarmServiceConfiguration200JSONResponse(models.ConvertServiceConfigurationToAPI(*patched)), nil
}

Expand Down Expand Up @@ -486,8 +494,12 @@ func (a *AlarmsServer) UpdateAlarmServiceConfiguration(ctx context.Context, requ
return nil, fmt.Errorf("failed to update Alarm Service Configuration: %w", err)
}

slog.Debug("Alarm Service Configuration updated", "retentionPeriod", updated.RetentionPeriod, "extensions", updated.Extensions)
// Update Cronjob
if err := a.ServiceConfig.EnsureCleanupCronJob(ctx, updated); err != nil {
return nil, fmt.Errorf("failed to start cleanup cronjob during AlarmServiceConfiguration update: %w", err)
}

slog.Debug("Alarm Service Configuration updated", "retentionPeriod", updated.RetentionPeriod, "extensions", updated.Extensions)
return api.UpdateAlarmServiceConfiguration200JSONResponse(models.ConvertServiceConfigurationToAPI(*updated)), nil

}
Expand Down
Loading

0 comments on commit 6ad6c09

Please sign in to comment.