Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Give modeler ability to clear the cache #626

Merged
merged 3 commits into from
Sep 22, 2020
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
2 changes: 1 addition & 1 deletion cmd/adminapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func realMain(ctx context.Context) error {
// Setup cacher
cacher, err := cache.CacherFor(ctx, &config.Cache, cache.MultiKeyFunc(
cache.HMACKeyFunc(sha1.New, config.Cache.HMACKey),
cache.PrefixKeyFunc("adminapi:cache:"),
cache.PrefixKeyFunc("cache:"),
))
if err != nil {
return fmt.Errorf("failed to create cacher: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func realMain(ctx context.Context) error {
// Setup cacher
cacher, err := cache.CacherFor(ctx, &config.Cache, cache.MultiKeyFunc(
cache.HMACKeyFunc(sha1.New, config.Cache.HMACKey),
cache.PrefixKeyFunc("apiserver:cache:"),
cache.PrefixKeyFunc("cache:"),
))
if err != nil {
return fmt.Errorf("failed to create cacher: %w", err)
Expand Down
14 changes: 13 additions & 1 deletion cmd/modeler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package main

import (
"context"
"crypto/sha1"
"fmt"
"os"
"strconv"

"github.com/google/exposure-notifications-verification-server/pkg/buildinfo"
"github.com/google/exposure-notifications-verification-server/pkg/cache"
"github.com/google/exposure-notifications-verification-server/pkg/config"
"github.com/google/exposure-notifications-verification-server/pkg/controller/modeler"
"github.com/google/exposure-notifications-verification-server/pkg/ratelimit"
Expand Down Expand Up @@ -76,12 +78,22 @@ func realMain(ctx context.Context) error {
defer oe.Close()
logger.Infow("observability exporter", "config", oeConfig)

// Setup cacher
cacher, err := cache.CacherFor(ctx, &config.Cache, cache.MultiKeyFunc(
cache.HMACKeyFunc(sha1.New, config.Cache.HMACKey),
cache.PrefixKeyFunc("cache:"),
))
if err != nil {
return fmt.Errorf("failed to create cacher: %w", err)
}
defer cacher.Close()

// Setup database
db, err := config.Database.Load(ctx)
if err != nil {
return fmt.Errorf("failed to load database config: %w", err)
}
if err := db.Open(ctx); err != nil {
if err := db.OpenWithCacher(ctx, cacher); err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
defer db.Close()
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func realMain(ctx context.Context) error {
// Setup cacher
cacher, err := cache.CacherFor(ctx, &config.Cache, cache.MultiKeyFunc(
cache.HMACKeyFunc(sha1.New, config.Cache.HMACKey),
cache.PrefixKeyFunc("server:cache:"),
cache.PrefixKeyFunc("cache:"),
))
if err != nil {
return fmt.Errorf("failed to create cacher: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/modeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package config
import (
"context"

"github.com/google/exposure-notifications-verification-server/pkg/cache"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/ratelimit"

Expand All @@ -27,6 +28,7 @@ import (

// Modeler is the configuration for the modeler service.
type Modeler struct {
Cache cache.Config
Database database.Config
Observability observability.Config
RateLimit ratelimit.Config
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/modeler/modeler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/google/exposure-notifications-verification-server/pkg/cache"
"github.com/google/exposure-notifications-verification-server/pkg/config"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/ratelimit"
Expand All @@ -38,6 +39,10 @@ func testModeler(tb testing.TB) *Controller {
Type: "IN_MEMORY",
HMACKey: []byte(""),
},
Cache: cache.Config{
Type: "IN_MEMORY",
HMACKey: []byte(""),
},
}
if err := envconfig.ProcessWith(ctx, &config, envconfig.MapLookuper(nil)); err != nil {
tb.Fatal(err)
Expand Down
7 changes: 7 additions & 0 deletions terraform/service_modeler.tf
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ resource "google_secret_manager_secret_iam_member" "modeler-db-verification-code
member = "serviceAccount:${google_service_account.modeler.email}"
}

resource "google_secret_manager_secret_iam_member" "modeler-cache-hmac-key" {
secret_id = google_secret_manager_secret.cache-hmac-key.id
role = "roles/secretmanager.secretAccessor"
member = "serviceAccount:${google_service_account.modeler.email}"
}

resource "google_secret_manager_secret_iam_member" "modeler-ratelimit-hmac-key" {
secret_id = google_secret_manager_secret.ratelimit-hmac-key.id
role = "roles/secretmanager.secretAccessor"
Expand Down Expand Up @@ -108,6 +114,7 @@ resource "google_cloud_run_service" "modeler" {

dynamic "env" {
for_each = merge(
local.cache_config,
local.database_config,
local.gcp_config,
local.rate_limit_config,
Expand Down