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

[Refactor] Move kubernetes log code to pkg/skaffold/kubernetes/logger #5761

Merged
merged 1 commit into from
Apr 30, 2021
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
62 changes: 62 additions & 0 deletions pkg/skaffold/kubernetes/image_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2021 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kubernetes

import (
"sync"

v1 "k8s.io/api/core/v1"
)

// PodSelector is used to choose which pods to log.
type PodSelector interface {
Select(pod *v1.Pod) bool
}

// ImageList implements PodSelector based on a list of images names.
type ImageList struct {
sync.RWMutex
names map[string]bool
}

// NewImageList creates a new ImageList.
func NewImageList() *ImageList {
return &ImageList{
names: make(map[string]bool),
}
}

// Add adds an image to the list.
func (l *ImageList) Add(image string) {
l.Lock()
l.names[image] = true
l.Unlock()
}

// Select returns true if one of the pod's images is in the list.
func (l *ImageList) Select(pod *v1.Pod) bool {
l.RLock()
defer l.RUnlock()

for _, container := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
if l.names[container.Image] {
return true
}
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package kubernetes
package logger

import (
"bufio"
Expand All @@ -30,6 +30,7 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
latest_v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/tag"
)
Expand All @@ -39,12 +40,12 @@ type LogAggregator struct {
output io.Writer
kubectlcli *kubectl.CLI
config Config
podWatcher PodWatcher
colorPicker ColorPicker
podWatcher kubernetes.PodWatcher
colorPicker kubernetes.ColorPicker

muted int32
sinceTime time.Time
events chan PodEvent
events chan kubernetes.PodEvent
trackedContainers trackedContainers
outputLock sync.Mutex
}
Expand All @@ -55,14 +56,14 @@ type Config interface {
}

// NewLogAggregator creates a new LogAggregator for a given output.
func NewLogAggregator(out io.Writer, cli *kubectl.CLI, imageNames []string, podSelector PodSelector, config Config) *LogAggregator {
func NewLogAggregator(out io.Writer, cli *kubectl.CLI, imageNames []string, podSelector kubernetes.PodSelector, config Config) *LogAggregator {
return &LogAggregator{
output: out,
kubectlcli: cli,
config: config,
podWatcher: NewPodWatcher(podSelector),
colorPicker: NewColorPicker(imageNames),
events: make(chan PodEvent),
podWatcher: kubernetes.NewPodWatcher(podSelector),
colorPicker: kubernetes.NewColorPicker(imageNames),
events: make(chan kubernetes.PodEvent),
}
}

Expand Down Expand Up @@ -288,42 +289,3 @@ func (t *trackedContainers) add(id string) bool {

return alreadyTracked
}

// PodSelector is used to choose which pods to log.
type PodSelector interface {
Select(pod *v1.Pod) bool
}

// ImageList implements PodSelector based on a list of images names.
type ImageList struct {
sync.RWMutex
names map[string]bool
}

// NewImageList creates a new ImageList.
func NewImageList() *ImageList {
return &ImageList{
names: make(map[string]bool),
}
}

// Add adds an image to the list.
func (l *ImageList) Add(image string) {
l.Lock()
l.names[image] = true
l.Unlock()
}

// Select returns true if one of the pod's images is in the list.
func (l *ImageList) Select(pod *v1.Pod) bool {
l.RLock()
defer l.RUnlock()

for _, container := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
if l.names[container.Image] {
return true
}
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package kubernetes
package logger

import (
"bytes"
Expand All @@ -28,6 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
latest_v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/testutil"
)
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestSelect(t *testing.T) {
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
list := NewImageList()
list := kubernetes.NewImageList()
list.Add("image1")
list.Add("image2")

Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/runner/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/filemon"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/logger"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/portforward"
latest_v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/sync"
Expand All @@ -50,7 +50,7 @@ var (
fileSyncSucceeded = event.FileSyncSucceeded
)

func (r *SkaffoldRunner) doDev(ctx context.Context, out io.Writer, logger *kubernetes.LogAggregator, forwarderManager portforward.Forwarder) error {
func (r *SkaffoldRunner) doDev(ctx context.Context, out io.Writer, logger *logger.LogAggregator, forwarderManager portforward.Forwarder) error {
// never queue intents from user, even if they're not used
defer r.intents.reset()

Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/runner/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/logger"
)

func (r *SkaffoldRunner) createLogger(out io.Writer, artifacts []graph.Artifact) *kubernetes.LogAggregator {
func (r *SkaffoldRunner) createLogger(out io.Writer, artifacts []graph.Artifact) *logger.LogAggregator {
if !r.runCtx.Tail() {
return nil
}
Expand All @@ -33,5 +33,5 @@ func (r *SkaffoldRunner) createLogger(out io.Writer, artifacts []graph.Artifact)
imageNames = append(imageNames, artifact.Tag)
}

return kubernetes.NewLogAggregator(out, r.kubectlCLI, imageNames, r.podSelector, r.runCtx)
return logger.NewLogAggregator(out, r.kubectlCLI, imageNames, r.podSelector, r.runCtx)
}