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

[KAPP-71] Watch pods in specific namespace when needed #97

Merged
merged 2 commits into from
Apr 7, 2020
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/kapp-linux-amd64
/kapp-windows-amd64.exe
/tmp

.idea
4 changes: 3 additions & 1 deletion pkg/kapp/cmd/core/strings_single_line_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ type ValueStringsSingleLine struct {
S []string
}

func NewValueStringsSingleLine(s []string) ValueStringsSingleLine { return ValueStringsSingleLine{S: s} }
func NewValueStringsSingleLine(s []string) ValueStringsSingleLine {
return ValueStringsSingleLine{S: s}
}

func (t ValueStringsSingleLine) String() string { return strings.Join(t.S, ", ") }
func (t ValueStringsSingleLine) Value() uitable.Value { return t }
Expand Down
11 changes: 6 additions & 5 deletions pkg/kapp/resources/identified_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
)

type IdentifiedResources struct {
coreClient kubernetes.Interface
resourceTypes ResourceTypes
resources *Resources
logger logger.Logger
coreClient kubernetes.Interface
fallbackAllowedNamespaces []string
resourceTypes ResourceTypes
resources *Resources
logger logger.Logger
}

func NewIdentifiedResources(coreClient kubernetes.Interface,
Expand All @@ -22,7 +23,7 @@ func NewIdentifiedResources(coreClient kubernetes.Interface,

resources := NewResources(resourceTypes, coreClient, dynamicClient, fallbackAllowedNamespaces, logger)

return IdentifiedResources{coreClient, resourceTypes, resources, logger.NewPrefixed("IdentifiedResources")}
return IdentifiedResources{coreClient, fallbackAllowedNamespaces, resourceTypes, resources, logger.NewPrefixed("IdentifiedResources")}
}

func (r IdentifiedResources) Create(resource Resource) (Resource, error) {
Expand Down
43 changes: 31 additions & 12 deletions pkg/kapp/resources/identified_resources_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ package resources

import (
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)

func (r IdentifiedResources) PodResources(labelSelector labels.Selector) UniquePodWatcher {
return UniquePodWatcher{labelSelector, r.coreClient}
return UniquePodWatcher{labelSelector, r.fallbackAllowedNamespaces, r.coreClient}
}

type PodWatcherI interface {
Watch(podsToWatchCh chan corev1.Pod, cancelCh chan struct{}) error
}

type UniquePodWatcher struct {
labelSelector labels.Selector
coreClient kubernetes.Interface
labelSelector labels.Selector
fallbackAllowedNamespaces []string
coreClient kubernetes.Interface
}

var _ PodWatcherI = UniquePodWatcher{}
Expand All @@ -28,14 +29,32 @@ func (w UniquePodWatcher) Watch(podsToWatchCh chan corev1.Pod, cancelCh chan str
nonUniquePodsToWatchCh := make(chan corev1.Pod)

go func() {
podWatcher := NewPodWatcher(
w.coreClient.CoreV1().Pods(""),
metav1.ListOptions{LabelSelector: w.labelSelector.String()},
)

err := podWatcher.Watch(nonUniquePodsToWatchCh, cancelCh)
if err != nil {
fmt.Printf("Pod watching error: %s\n", err) // TODO
// Watch Pods in all namespaces first and fallback to the
// fallbackAllowedNamespaces if lack of permission
namespace := ""
for {
podWatcher := NewPodWatcher(
w.coreClient.CoreV1().Pods(namespace),
metav1.ListOptions{LabelSelector: w.labelSelector.String()},
)

err := podWatcher.Watch(nonUniquePodsToWatchCh, cancelCh)
if err == nil {
break
}
if errors.IsForbidden(err) && namespace == "" {
// The '-n' flag or default state namespace can specify only 1 namespace, so there
// should be at most 1 item in fallbackAllowedNamespaces
if len(w.fallbackAllowedNamespaces) > 0 {
namespace = w.fallbackAllowedNamespaces[0]
if namespace == "" {
break
}
}
} else {
fmt.Printf("Pod watching error: %s\n", err) // TODO
break
}
}

close(nonUniquePodsToWatchCh)
Expand Down