Merge pull request #324 from keel-hq/feature/323_gcr_name_override
Feature/323 gcr name overridefeature/helm_approvals_deadline
commit
66143de387
|
@ -5,8 +5,6 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/keel-hq/keel/cache"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
|
@ -59,11 +57,6 @@ func (c *Cache) List(prefix string) (map[string][]byte, error) {
|
|||
dst := make([]byte, len(v))
|
||||
copy(dst, v)
|
||||
values[k] = dst
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"KEY": k,
|
||||
"VALUE": string(dst),
|
||||
}).Info("CACHE LIST")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -53,6 +53,7 @@ const (
|
|||
EnvTriggerPubSub = "PUBSUB" // set to 1 or something to enable pub/sub trigger
|
||||
EnvTriggerPoll = "POLL" // set to 1 or something to enable poll trigger
|
||||
EnvProjectID = "PROJECT_ID"
|
||||
EnvClusterName = "CLUSTER_NAME"
|
||||
|
||||
EnvNamespace = "NAMESPACE" // Keel's namespace
|
||||
|
||||
|
@ -298,7 +299,7 @@ func setupTriggers(ctx context.Context, providers provider.Providers, approvalsM
|
|||
return
|
||||
}
|
||||
|
||||
subManager := pubsub.NewDefaultManager(projectID, providers, ps)
|
||||
subManager := pubsub.NewDefaultManager(os.Getenv(EnvClusterName), projectID, providers, ps)
|
||||
go subManager.Start(ctx)
|
||||
}
|
||||
|
||||
|
|
|
@ -95,9 +95,6 @@ func (p *Provider) isApproved(event *types.Event, plan *UpdatePlan) (bool, error
|
|||
approval.Delta(),
|
||||
)
|
||||
|
||||
// fmt.Println("requesting approval, identifier: ", plan.Resource.Namespace)
|
||||
fmt.Println("requesting approval, identifier: ", identifier)
|
||||
|
||||
return false, p.approvalManager.Create(approval)
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,11 @@ type DefaultManager struct {
|
|||
// projectID is required to correctly set GCR subscriptions
|
||||
projectID string
|
||||
|
||||
// clusterName is used to create unique names for the subscriptions. Each subscription
|
||||
// has to have a unique name in order to receive all events (otherwise, if it is the same,
|
||||
// only 1 keel instance will receive a GCR event after a push event)
|
||||
clusterName string
|
||||
|
||||
// scanTick - scan interval in seconds, defaults to 60 seconds
|
||||
scanTick int
|
||||
|
||||
|
@ -39,11 +44,12 @@ type Subscriber interface {
|
|||
}
|
||||
|
||||
// NewDefaultManager - creates new pubsub manager to create subscription for deployments
|
||||
func NewDefaultManager(projectID string, providers provider.Providers, subClient Subscriber) *DefaultManager {
|
||||
func NewDefaultManager(clusterName, projectID string, providers provider.Providers, subClient Subscriber) *DefaultManager {
|
||||
return &DefaultManager{
|
||||
providers: providers,
|
||||
client: subClient,
|
||||
projectID: projectID,
|
||||
clusterName: clusterName,
|
||||
subscribers: make(map[string]context.Context),
|
||||
mu: &sync.Mutex{},
|
||||
scanTick: 60,
|
||||
|
@ -116,7 +122,7 @@ func (s *DefaultManager) ensureSubscription(gcrURI string) {
|
|||
if !ok {
|
||||
ctx, cancel := context.WithCancel(s.ctx)
|
||||
s.subscribers[gcrURI] = ctx
|
||||
subName := containerRegistrySubName(s.projectID, gcrURI)
|
||||
subName := containerRegistrySubName(s.clusterName, s.projectID, gcrURI)
|
||||
go func() {
|
||||
defer cancel()
|
||||
err := s.client.Subscribe(s.ctx, gcrURI, subName)
|
||||
|
|
|
@ -11,23 +11,25 @@ import (
|
|||
// MetadataEndpoint - default metadata server for gcloud pubsub
|
||||
const MetadataEndpoint = "http://metadata/computeMetadata/v1/instance/attributes/cluster-name"
|
||||
|
||||
func containerRegistrySubName(projectID, topic string) string {
|
||||
cluster := "unknown"
|
||||
clusterName, err := clusterName(MetadataEndpoint)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"error": err,
|
||||
"metadata_endpoint": MetadataEndpoint,
|
||||
}).Warn("trigger.pubsub.containerRegistrySubName: got error while retrieving cluster metadata, messages might be lost if more than one Keel instance is created")
|
||||
} else {
|
||||
cluster = clusterName
|
||||
func containerRegistrySubName(clusterName, projectID, topic string) string {
|
||||
|
||||
if clusterName == "" {
|
||||
var err error
|
||||
clusterName, err = getClusterName(MetadataEndpoint)
|
||||
if err != nil {
|
||||
clusterName = "unknown"
|
||||
log.WithFields(log.Fields{
|
||||
"error": err,
|
||||
"metadata_endpoint": MetadataEndpoint,
|
||||
}).Warn("trigger.pubsub.containerRegistrySubName: got error while retrieving cluster metadata, messages might be lost if more than one Keel instance is created")
|
||||
}
|
||||
}
|
||||
|
||||
return "keel-" + cluster + "-" + projectID + "-" + topic
|
||||
return "keel-" + clusterName + "-" + projectID + "-" + topic
|
||||
}
|
||||
|
||||
// https://cloud.google.com/compute/docs/storing-retrieving-metadata
|
||||
func clusterName(metadataEndpoint string) (string, error) {
|
||||
func getClusterName(metadataEndpoint string) (string, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, metadataEndpoint, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
|
@ -63,7 +63,7 @@ func TestClusterName(t *testing.T) {
|
|||
ts := httptest.NewServer(http.HandlerFunc(handler))
|
||||
defer ts.Close()
|
||||
|
||||
name, err := clusterName(ts.URL)
|
||||
name, err := getClusterName(ts.URL)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error while getting cluster name")
|
||||
}
|
||||
|
@ -75,9 +75,18 @@ func TestClusterName(t *testing.T) {
|
|||
|
||||
func TestGetContainerRegistryURI(t *testing.T) {
|
||||
|
||||
name := containerRegistrySubName("project-1", "topic-1")
|
||||
name := containerRegistrySubName("", "project-1", "topic-1")
|
||||
|
||||
if name != "keel-unknown-project-1-topic-1" {
|
||||
t.Errorf("unexpected topic name: %s", name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetContainerRegistryURIWithClusterNameSet(t *testing.T) {
|
||||
|
||||
name := containerRegistrySubName("testxxx", "project-1", "topic-1")
|
||||
|
||||
if name != "keel-testxxx-project-1-topic-1" {
|
||||
t.Errorf("unexpected topic name: %s", name)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue