Skip to content

Commit

Permalink
update container state to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
wonderflow committed Nov 7, 2021
1 parent 5a32374 commit 4cf6ca4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion stern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"

"github.com/pkg/errors"

"github.com/wercker/stern/kubernetes"
)

Expand All @@ -45,7 +46,7 @@ func Run(ctx context.Context, config *Config) error {
}
}

added, removed, err := Watch(ctx, clientset.CoreV1().Pods(namespace), config.PodQuery, config.ContainerQuery, config.ExcludeContainerQuery, config.ContainerState, config.LabelSelector)
added, removed, err := Watch(ctx, clientset.CoreV1().Pods(namespace), config.PodQuery, config.ContainerQuery, config.ExcludeContainerQuery,[]ContainerState{ config.ContainerState}, config.LabelSelector)
if err != nil {
return errors.Wrap(err, "failed to set up watch")
}
Expand Down
13 changes: 11 additions & 2 deletions stern/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,19 @@ func (t *Target) GetID() string {
return fmt.Sprintf("%s-%s-%s", t.Namespace, t.Pod, t.Container)
}

func matchState(containerStates []ContainerState,state corev1.ContainerState)bool{
for _,s:=range containerStates{
if s.Match(state){
return true
}
}
return false
}

// Watch starts listening to Kubernetes events and emits modified
// containers/pods. The first result is targets added, the second is targets
// removed
func Watch(ctx context.Context, i v1.PodInterface, podFilter *regexp.Regexp, containerFilter *regexp.Regexp, containerExcludeFilter *regexp.Regexp, containerState ContainerState, labelSelector labels.Selector) (chan *Target, chan *Target, error) {
func Watch(ctx context.Context, i v1.PodInterface, podFilter *regexp.Regexp, containerFilter *regexp.Regexp, containerExcludeFilter *regexp.Regexp, containerStates []ContainerState, labelSelector labels.Selector) (chan *Target, chan *Target, error) {
watcher, err := i.Watch(ctx, metav1.ListOptions{Watch: true, LabelSelector: labelSelector.String()})
if err != nil {
return nil, nil, errors.Wrap(err, "failed to set up watch")
Expand Down Expand Up @@ -84,7 +93,7 @@ func Watch(ctx context.Context, i v1.PodInterface, podFilter *regexp.Regexp, con
continue
}

if containerState.Match(c.State) {
if matchState(containerStates,c.State) {
added <- &Target{
Namespace: pod.Namespace,
Pod: pod.Name,
Expand Down

0 comments on commit 4cf6ca4

Please sign in to comment.