Skip to content

Commit

Permalink
Resume publish when creating a new publisher (#226)
Browse files Browse the repository at this point in the history
* Resume publish when creating a new publisher

* Make labels consistent
  • Loading branch information
Starefossen authored Oct 25, 2023
1 parent f242c2b commit ff9c968
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
15 changes: 11 additions & 4 deletions controllers/unleash_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ import (
"github.com/nais/unleasherator/pkg/unleashclient"
)

const unleashFinalizer = "unleash.nais.io/finalizer"
const (
unleashFinalizer = "unleash.nais.io/finalizer"
unleashPublishMetricStatusSending = "sending"
unleashPublishMetricStatusSuccess = "success"
unleashPublishMetricStatusFailed = "failed"
)

var (
// unleashStatus is a Prometheus metric which will be used to expose the status of the Unleash instances
Expand Down Expand Up @@ -321,21 +326,23 @@ func (r *UnleashReconciler) publish(ctx context.Context, unleash *unleashv1.Unle
}

log.Info("Publishing Unleash instance to federation")
// Count the number of Unleash instances published
unleashPublished.WithLabelValues("provisioned", unleashPublishMetricStatusSending).Inc()

token, err := unleash.AdminToken(ctx, r.Client, r.OperatorNamespace)
if err != nil {
unleashPublished.WithLabelValues("provisioned", "failed").Inc()
unleashPublished.WithLabelValues("provisioned", unleashPublishMetricStatusFailed).Inc()
log.Error(err, "Failed to fetch API token")
return fmt.Errorf("publish could not fetch API token: %w", err)
}

err = r.Federation.Publisher.Publish(ctx, unleash, string(token))
if err != nil {
unleashPublished.WithLabelValues("provisioned", "failed").Inc()
unleashPublished.WithLabelValues("provisioned", unleashPublishMetricStatusFailed).Inc()
return fmt.Errorf("publish could not publish Unleash instance: %w", err)
}

unleashPublished.WithLabelValues("provisioned", "success").Inc()
unleashPublished.WithLabelValues("provisioned", unleashPublishMetricStatusSuccess).Inc()
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion controllers/unleash_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ var _ = Describe("Unleash controller", func() {
Expect(mockPublisher.AssertCalled(GinkgoT(), "Publish", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("*unleash_nais_io_v1.Unleash"), mock.AnythingOfType("string"))).To(BeTrue())
Expect(mockPublisher.AssertNumberOfCalls(GinkgoT(), "Publish", 1)).To(BeTrue())

val, err := promCounterVecVal(unleashPublished, "provisioned", "success")
val, err := promCounterVecVal(unleashPublished, "provisioned", unleashPublishMetricStatusSending)
Expect(err).To(BeNil())
Expect(val).To(Equal(float64(1)))

val, err = promCounterVecVal(unleashPublished, "provisioned", unleashPublishMetricStatusSuccess)
Expect(err).To(BeNil())
Expect(val).To(Equal(float64(1)))

Expand Down
3 changes: 3 additions & 0 deletions pkg/federation/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ func (p *publisher) Publish(ctx context.Context, unleash *unleashv1.Unleash, api
}

func NewPublisher(client *pubsub.Client, topic *pubsub.Topic) Publisher {
// Fix for the following pubsub error, this clears the ordering key for the topic when the publisher is created
// pubsub: Publishing for ordering key, order, paused due to previous error. Call topic.ResumePublish(orderingKey) before resuming publishing
topic.ResumePublish(pubsubOrderingKey)
return &publisher{client: client, topic: topic}
}

0 comments on commit ff9c968

Please sign in to comment.