-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the functionality to backfill for extracting node-pack info…
…rmation (#108) Co-authored-by: James Kwon <[email protected]>
- Loading branch information
1 parent
4d8f856
commit d6aa70b
Showing
23 changed files
with
523 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package config | ||
|
||
type Config struct { | ||
ProjectID string | ||
DripEnv string | ||
SlackRegistryChannelWebhook string | ||
JWTSecret string | ||
DiscordSecurityChannelWebhook string | ||
ProjectID string | ||
DripEnv string | ||
SlackRegistryChannelWebhook string | ||
JWTSecret string | ||
DiscordSecurityChannelWebhook string | ||
DiscordSecurityPrivateChannelWebhook string | ||
SecretScannerURL string | ||
IDTokenAudience string | ||
AlgoliaAppID string | ||
AlgoliaAPIKey string | ||
CloudStorageBucketName string | ||
SecretScannerURL string | ||
IDTokenAudience string | ||
AlgoliaAppID string | ||
AlgoliaAPIKey string | ||
CloudStorageBucketName string | ||
PubSubTopic string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package pubsub | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"registry-backend/config" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"cloud.google.com/go/pubsub" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type PubSubService interface { | ||
PublishNodePack(ctx context.Context, storageURL string) error | ||
} | ||
|
||
var _ PubSubService = (*pubsubimpl)(nil) | ||
|
||
type pubsubimpl struct { | ||
client *pubsub.Client | ||
config *config.Config | ||
topic *pubsub.Topic | ||
} | ||
|
||
func NewPubSubService(c *config.Config) (PubSubService, error) { | ||
if c == nil || c.PubSubTopic == "" { | ||
// Return a noop implementation if config is nil or storage is not enabled | ||
log.Info().Msg("No pub sub configuration found, using noop implementation") | ||
return &pubsubNoop{}, nil | ||
} | ||
|
||
// Initialize GCP storage client | ||
client, err := pubsub.NewClient(context.Background(), c.ProjectID) | ||
if err != nil { | ||
return nil, fmt.Errorf("NewPubSubService: %v", err) | ||
} | ||
return &pubsubimpl{ | ||
client: client, | ||
config: c, | ||
topic: client.Topic(c.PubSubTopic), | ||
}, nil | ||
} | ||
|
||
// PublishNodePack implements PubSubService. | ||
func (p *pubsubimpl) PublishNodePack(ctx context.Context, storageURL string) (err error) { | ||
u, err := url.Parse(storageURL) | ||
if err != nil { | ||
return fmt.Errorf("invalid storage URL: %w", err) | ||
} | ||
|
||
segments := strings.Split(u.Path, "/") | ||
if len(segments) < 2 { | ||
return fmt.Errorf("invalid storage URL: %w", err) | ||
} | ||
bucket := segments[1] | ||
object := strings.Join(segments[2:], "/") | ||
now := time.Now() | ||
messagePayload := map[string]interface{}{ | ||
"kind": "storage#object", | ||
"id": fmt.Sprintf("%s/%s/%d", bucket, object, now.Unix()), | ||
"selfLink": fmt.Sprintf("https://www.googleapis.com/storage/v1/b/%s/o/%s", object, bucket), | ||
"name": object, | ||
"bucket": bucket, | ||
"generation": strconv.FormatInt(time.Now().Unix(), 10), | ||
"metageneration": "1", | ||
"mediaLink": fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucket, object), | ||
} | ||
// Marshal the payload to JSON | ||
jsonData, err := json.Marshal(messagePayload) | ||
if err != nil { | ||
return fmt.Errorf("Failed to marshal JSON: %v", err) | ||
} | ||
|
||
result := p.topic.Publish(ctx, &pubsub.Message{ | ||
Data: jsonData, | ||
Attributes: map[string]string{ | ||
"eventType": "OBJECT_FINALIZE", // Optional attribute for event type | ||
}, | ||
}) | ||
|
||
_, err = result.Get(ctx) | ||
if err != nil { | ||
return fmt.Errorf("Failed to publish message: %v", err) | ||
} | ||
|
||
return | ||
} | ||
|
||
var _ PubSubService = (*pubsubNoop)(nil) | ||
|
||
type pubsubNoop struct{} | ||
|
||
// PublishNodePack implements PubSubService. | ||
func (p *pubsubNoop) PublishNodePack(ctx context.Context, storageURL string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pubsub | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"registry-backend/config" | ||
"testing" | ||
"time" | ||
|
||
"cloud.google.com/go/pubsub" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPublish(t *testing.T) { | ||
projectID, ok := os.LookupEnv("PROJECT_ID") | ||
if !ok { | ||
t.Skip("PROJECT_ID is not set") | ||
} | ||
|
||
client, err := pubsub.NewClient(context.Background(), projectID) | ||
require.NoError(t, err) | ||
|
||
topic := client.Topic(fmt.Sprintf("pubsub-topic-test-%d", time.Now().Unix())) | ||
t.Cleanup(func() { | ||
t.Logf("Deleting topic %s", topic.ID()) | ||
topic.Delete(context.Background()) | ||
}) | ||
client.CreateTopic(context.Background(), topic.ID()) | ||
|
||
pubsubsvc, err := NewPubSubService(&config.Config{ProjectID: projectID, PubSubTopic: topic.ID()}) | ||
require.NoError(t, err) | ||
|
||
subscriptionID := fmt.Sprintf("sub-%d", time.Now().Unix()) | ||
sub, err := client.CreateSubscription(context.Background(), subscriptionID, pubsub.SubscriptionConfig{ | ||
Topic: topic, | ||
AckDeadline: 10 * time.Second, | ||
RetainAckedMessages: false, | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = pubsubsvc.PublishNodePack(context.Background(), "https://storage.cloud.google.com/testbucket/path1/path2/file.tar.gz") | ||
require.NoError(t, err) | ||
pubsubsvc.(*pubsubimpl).topic.Flush() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
data := map[string]string{} | ||
err = sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) { | ||
t.Log(msg) | ||
assert.NoError(t, json.Unmarshal(msg.Data, &data)) | ||
t.Log(data) | ||
msg.Ack() | ||
cancel() | ||
}) | ||
<-ctx.Done() | ||
require.NoError(t, err) | ||
assert.Equal(t, "testbucket", data["bucket"]) | ||
assert.Equal(t, "path1/path2/file.tar.gz", data["name"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.