Skip to content

Commit

Permalink
chore(kamel log): Renamed to "defaultContainerName" + fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuss committed Jan 21, 2019
1 parent f366a15 commit 5426ac3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
24 changes: 12 additions & 12 deletions pkg/util/log/annotation_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ import (

// SelectorScraper scrapes all pods with a given selector
type SelectorScraper struct {
client kubernetes.Interface
namespace string
integrationName string
labelSelector string
podScrapers sync.Map
counter uint64
client kubernetes.Interface
namespace string
defaultContainerName string
labelSelector string
podScrapers sync.Map
counter uint64
}

// NewSelectorScraper creates a new SelectorScraper
func NewSelectorScraper(client kubernetes.Interface, namespace string, integrationName string, labelSelector string) *SelectorScraper {
func NewSelectorScraper(client kubernetes.Interface, namespace string, defaultContainerName string, labelSelector string) *SelectorScraper {
return &SelectorScraper{
client: client,
namespace: namespace,
integrationName: integrationName,
labelSelector: labelSelector,
client: client,
namespace: namespace,
defaultContainerName: defaultContainerName,
labelSelector: labelSelector,
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func (s *SelectorScraper) synchronize(ctx context.Context, out *bufio.Writer) er
}

func (s *SelectorScraper) addPodScraper(ctx context.Context, podName string, out *bufio.Writer) {
podScraper := NewPodScraper(s.client, s.namespace, podName, s.integrationName)
podScraper := NewPodScraper(s.client, s.namespace, podName, s.defaultContainerName)
podCtx, podCancel := context.WithCancel(ctx)
id := atomic.AddUint64(&s.counter, 1)
prefix := "[" + strconv.FormatUint(id, 10) + "] "
Expand Down
30 changes: 15 additions & 15 deletions pkg/util/log/pod_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ var commonUserContainerNames = map[string]bool{

// PodScraper scrapes logs of a specific pod
type PodScraper struct {
namespace string
podName string
integrationName string
client kubernetes.Interface
namespace string
podName string
defaultContainerName string
client kubernetes.Interface
}

// NewPodScraper creates a new pod scraper
func NewPodScraper(c kubernetes.Interface, namespace string, podName string, integrationName string) *PodScraper {
func NewPodScraper(c kubernetes.Interface, namespace string, podName string, defaultContainerName string) *PodScraper {
return &PodScraper{
namespace: namespace,
podName: podName,
integrationName: integrationName,
client: c,
namespace: namespace,
podName: podName,
defaultContainerName: defaultContainerName,
client: c,
}
}

Expand All @@ -71,7 +71,7 @@ func (s *PodScraper) Start(ctx context.Context) *bufio.Reader {
}

func (s *PodScraper) doScrape(ctx context.Context, out *bufio.Writer, clientCloser func() error) {
containerName, err := s.waitForPodRunning(ctx, s.namespace, s.podName, s.integrationName)
containerName, err := s.waitForPodRunning(ctx, s.namespace, s.podName, s.defaultContainerName)
if err != nil {
s.handleAndRestart(ctx, err, 5*time.Second, out, clientCloser)
return
Expand Down Expand Up @@ -135,7 +135,7 @@ func (s *PodScraper) handleAndRestart(ctx context.Context, err error, wait time.

// waitForPodRunning waits for a given pod to reach the running state.
// It may return the internal container to watch if present
func (s *PodScraper) waitForPodRunning(ctx context.Context, namespace string, podName string, integrationName string) (string, error) {
func (s *PodScraper) waitForPodRunning(ctx context.Context, namespace string, podName string, defaultContainerName string) (string, error) {
pod := v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
Expand Down Expand Up @@ -183,7 +183,7 @@ func (s *PodScraper) waitForPodRunning(ctx context.Context, namespace string, po
}

if recvPod != nil && recvPod.Status.Phase == v1.PodRunning {
return s.chooseContainer(recvPod, integrationName), nil
return s.chooseContainer(recvPod, defaultContainerName), nil
}
} else if e.Type == watch.Deleted || e.Type == watch.Error {
return "", errors.New("unable to watch pod " + s.podName)
Expand All @@ -195,7 +195,7 @@ func (s *PodScraper) waitForPodRunning(ctx context.Context, namespace string, po

}

func (s *PodScraper) chooseContainer(p *v1.Pod, integrationName string) string {
func (s *PodScraper) chooseContainer(p *v1.Pod, defaultContainerName string) string {
if p != nil {
if len(p.Spec.Containers) == 1 {
// Let Kubernetes auto-detect
Expand All @@ -206,8 +206,8 @@ func (s *PodScraper) chooseContainer(p *v1.Pod, integrationName string) string {
for _, c := range p.Spec.Containers {
if _, ok := commonUserContainerNames[c.Name]; ok {
return c.Name
} else if c.Name == integrationName {
containerNameFound = integrationName
} else if c.Name == defaultContainerName {
containerNameFound = defaultContainerName
}
}
return containerNameFound
Expand Down
4 changes: 2 additions & 2 deletions test/log_scrape_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestPodLogScrape(t *testing.T) {

ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()
scraper := log.NewPodScraper(testClient, pod.Namespace, pod.Name)
scraper := log.NewPodScraper(testClient, pod.Namespace, pod.Name, "scraped")
in := scraper.Start(ctx)

res := make(chan bool)
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestSelectorLogScrape(t *testing.T) {

ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()
scraper := log.NewSelectorScraper(testClient, deployment.Namespace, "scrape=me")
scraper := log.NewSelectorScraper(testClient, deployment.Namespace, "main", "scrape=me")
in := scraper.Start(ctx)

res := make(chan string)
Expand Down

0 comments on commit 5426ac3

Please sign in to comment.