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

Feature/323 gcr name override #324

Merged
merged 5 commits into from
Dec 11, 2018
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
7 changes: 0 additions & 7 deletions cache/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"sync"

"github.com/keel-hq/keel/cache"

log "github.com/sirupsen/logrus"
)

type Cache struct {
Expand Down Expand Up @@ -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")
}
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/keel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 0 additions & 3 deletions provider/kubernetes/approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 8 additions & 2 deletions trigger/pubsub/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 14 additions & 12 deletions trigger/pubsub/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions trigger/pubsub/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
}
}