Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GCP selflink for tracking instances throughout #8

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 23 additions & 3 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package handlers

import (
"fmt"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -33,6 +34,7 @@ func HandleCreationEvents(additions chan *gcppubsub.Message, instanceToClusterMa
a, err := messageToInstanceCreationEvent(addition)
if err != nil {
l.Warnf("failed to convert pubsub message to creation event: %s", err.Error())
continue
}
l.With("message_id", a.MessageID, "resource_id", a.ResourceID, "kubernetes_cluster", a.ClusterName).Info("added")
instanceToClusterMappings.Insert(a.ResourceID, a.ClusterName)
Expand Down Expand Up @@ -91,8 +93,14 @@ func messageToInstanceCreationEvent(m *gcppubsub.Message) (instanceCreationEvent
if err != nil {
return instanceCreationEvent{}, err
}
labels := entry.ProtoPayload.Request.GetFields()["labels"]
clusterName := "undefined"
requestFields := entry.ProtoPayload.Request.GetFields()

labels, ok := requestFields["labels"]
if !ok {
return instanceCreationEvent{}, fmt.Errorf("expected labels not found on instance creation request, operation ID: %s", entry.Operation.Id)
}

clusterName := "unknown"
for _, v := range labels.GetListValue().Values {
label := v.GetStructValue().AsMap()
labelKey := label["key"].(string)
Expand All @@ -101,9 +109,21 @@ func messageToInstanceCreationEvent(m *gcppubsub.Message) (instanceCreationEvent
break
}
}

if clusterName == "unknown" {
return instanceCreationEvent{}, fmt.Errorf("expected cluster label %s not found on instance creation request, operation ID: %s", compute.ClusterNameLabelKey, entry.Operation.Id)
}

responseFields := entry.ProtoPayload.Response.GetFields()
targetLink, ok := responseFields["targetLink"]
if !ok {
return instanceCreationEvent{}, fmt.Errorf("expected targetLink not found in instance creation response, operation ID: %s", entry.Operation.Id)
}
resourceID := strings.TrimPrefix(targetLink.GetStringValue(), "https://www.googleapis.com/compute/v1/")

return instanceCreationEvent{
MessageID: m.ID,
ResourceID: entry.ProtoPayload.ResourceName,
ResourceID: resourceID,
ClusterName: clusterName,
}, nil
}
4 changes: 2 additions & 2 deletions internal/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (suite *HandlersTestSuite) TestHandleCreationEvents() {
additions <- mockCreationMessage
close(additions)
wg.Wait()
resourceName := "projects/123456789/zones/europe-west1-c/instances/fake-resource"
resourceName := "projects/mock-project/zones/europe-west1-c/instances/fake-resource"

cluster, err := instanceToClusterMappings.Get(resourceName)
suite.NoError(err)
Expand All @@ -92,7 +92,7 @@ func (suite *HandlersTestSuite) TestMessageToInstanceInterruptionEvent() {
func (suite *HandlersTestSuite) TestMessageToInstanceCreationEvent() {
event, err := messageToInstanceCreationEvent(mockCreationMessage)
suite.NoError(err)
suite.Equal("projects/123456789/zones/europe-west1-c/instances/fake-resource", event.ResourceID)
suite.Equal("projects/mock-project/zones/europe-west1-c/instances/fake-resource", event.ResourceID)
suite.Equal("fake-cluster", event.ClusterName)
suite.Equal("12345", event.MessageID)
}
10 changes: 9 additions & 1 deletion internal/handlers/test_data/creation-event.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
"@type": "type.googleapis.com/google.cloud.audit.AuditLog",
"serviceName": "compute.googleapis.com",
"methodName": "v1.compute.instances.insert",
"resourceName": "projects/123456789/zones/europe-west1-c/instances/fake-resource",
"request": {
"labels": [
{
"key": "goog-k8s-cluster-name",
"value": "fake-cluster"
}
]
},
"response": {
"targetLink": "https://www.googleapis.com/compute/v1/projects/mock-project/zones/europe-west1-c/instances/fake-resource",
"@type": "type.googleapis.com/operation"
}
},
"operation": {
"id": "operation-1704446377001-60e2f58d702e9-78fe21e1-13682ff1",
"producer": "compute.googleapis.com",
"first": true
}
}
Loading