-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Watch controller Pods and make then available in k8sStore #3455
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 store | ||
|
||
import ( | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
// PodLister makes a Store that lists Pods. | ||
type PodLister struct { | ||
cache.Store | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,11 @@ import ( | |
corev1 "k8s.io/api/core/v1" | ||
extensions "k8s.io/api/extensions/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
k8sruntime "k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/informers" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
|
@@ -76,6 +79,9 @@ type Storer interface { | |
// ListIngresses returns a list of all Ingresses in the store. | ||
ListIngresses() []*ingress.Ingress | ||
|
||
// ListControllerPods returns a list of ingress-nginx controller Pods. | ||
ListControllerPods() []*corev1.Pod | ||
|
||
// GetLocalSSLCert returns the local copy of a SSLCert | ||
GetLocalSSLCert(name string) (*ingress.SSLCert, error) | ||
|
||
|
@@ -121,6 +127,7 @@ type Informer struct { | |
Service cache.SharedIndexInformer | ||
Secret cache.SharedIndexInformer | ||
ConfigMap cache.SharedIndexInformer | ||
Pod cache.SharedIndexInformer | ||
} | ||
|
||
// Lister contains object listers (stores). | ||
|
@@ -131,6 +138,7 @@ type Lister struct { | |
Secret SecretLister | ||
ConfigMap ConfigMapLister | ||
IngressAnnotation IngressAnnotationsLister | ||
Pod PodLister | ||
} | ||
|
||
// NotExistsError is returned when an object does not exist in a local store. | ||
|
@@ -147,6 +155,7 @@ func (i *Informer) Run(stopCh chan struct{}) { | |
go i.Service.Run(stopCh) | ||
go i.Secret.Run(stopCh) | ||
go i.ConfigMap.Run(stopCh) | ||
go i.Pod.Run(stopCh) | ||
|
||
// wait for all involved caches to be synced before processing items | ||
// from the queue | ||
|
@@ -211,6 +220,8 @@ type k8sStore struct { | |
defaultSSLCertificate string | ||
|
||
isDynamicCertificatesEnabled bool | ||
|
||
pod *k8s.PodInfo | ||
} | ||
|
||
// New creates a new object store to be used in the ingress controller | ||
|
@@ -220,7 +231,8 @@ func New(checkOCSP bool, | |
client clientset.Interface, | ||
fs file.Filesystem, | ||
updateCh *channels.RingChannel, | ||
isDynamicCertificatesEnabled bool) Storer { | ||
isDynamicCertificatesEnabled bool, | ||
pod *k8s.PodInfo) Storer { | ||
|
||
store := &k8sStore{ | ||
isOCSPCheckEnabled: checkOCSP, | ||
|
@@ -234,6 +246,7 @@ func New(checkOCSP bool, | |
secretIngressMap: NewObjectRefMap(), | ||
defaultSSLCertificate: defaultSSLCertificate, | ||
isDynamicCertificatesEnabled: isDynamicCertificatesEnabled, | ||
pod: pod, | ||
} | ||
|
||
eventBroadcaster := record.NewBroadcaster() | ||
|
@@ -270,6 +283,26 @@ func New(checkOCSP bool, | |
store.informers.Service = infFactory.Core().V1().Services().Informer() | ||
store.listers.Service.Store = store.informers.Service.GetStore() | ||
|
||
labelSelector := labels.SelectorFromSet(store.pod.Labels) | ||
store.informers.Pod = cache.NewSharedIndexInformer( | ||
&cache.ListWatch{ | ||
ListFunc: func(options metav1.ListOptions) (k8sruntime.Object, error) { | ||
|
||
options.LabelSelector = labelSelector.String() | ||
return client.CoreV1().Pods(store.pod.Namespace).List(options) | ||
}, | ||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for this newline. same as above |
||
options.LabelSelector = labelSelector.String() | ||
return client.CoreV1().Pods(store.pod.Namespace).Watch(options) | ||
}, | ||
}, | ||
&corev1.Pod{}, | ||
resyncPeriod, | ||
cache.Indexers{}, | ||
) | ||
store.listers.Pod.Store = store.informers.Pod.GetStore() | ||
|
||
ingEventHandler := cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) { | ||
ing := obj.(*extensions.Ingress) | ||
|
@@ -512,11 +545,40 @@ func New(checkOCSP bool, | |
}, | ||
} | ||
|
||
podEventHandler := cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) { | ||
updateCh.In() <- Event{ | ||
Type: CreateEvent, | ||
Obj: obj, | ||
} | ||
}, | ||
UpdateFunc: func(old, cur interface{}) { | ||
oldPod := old.(*corev1.Pod) | ||
curPod := cur.(*corev1.Pod) | ||
|
||
if oldPod.Status.Phase == curPod.Status.Phase { | ||
return | ||
} | ||
|
||
updateCh.In() <- Event{ | ||
Type: UpdateEvent, | ||
Obj: cur, | ||
} | ||
}, | ||
DeleteFunc: func(obj interface{}) { | ||
updateCh.In() <- Event{ | ||
Type: DeleteEvent, | ||
Obj: obj, | ||
} | ||
}, | ||
} | ||
|
||
store.informers.Ingress.AddEventHandler(ingEventHandler) | ||
store.informers.Endpoint.AddEventHandler(epEventHandler) | ||
store.informers.Secret.AddEventHandler(secrEventHandler) | ||
store.informers.ConfigMap.AddEventHandler(cmEventHandler) | ||
store.informers.Service.AddEventHandler(cache.ResourceEventHandlerFuncs{}) | ||
store.informers.Pod.AddEventHandler(podEventHandler) | ||
|
||
// do not wait for informers to read the configmap configuration | ||
ns, name, _ := k8s.ParseNameNS(configmap) | ||
|
@@ -773,3 +835,20 @@ func (s k8sStore) Run(stopCh chan struct{}) { | |
go wait.Until(s.checkSSLChainIssues, 60*time.Second, stopCh) | ||
} | ||
} | ||
|
||
// ListControllerPods returns a list of ingress-nginx controller Pods | ||
func (s k8sStore) ListControllerPods() []*corev1.Pod { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make this clear that it returns only |
||
var pods []*corev1.Pod | ||
|
||
for _, i := range s.listers.Pod.List() { | ||
pod := i.(*corev1.Pod) | ||
|
||
if pod.Status.Phase != corev1.PodRunning { | ||
continue | ||
} | ||
|
||
pods = append(pods, pod) | ||
} | ||
|
||
return pods | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you adding a podlister?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For https://github.com/kubernetes/ingress-nginx/pull/3455/files#diff-ece58b3bebec02a6d3173c8d8099d298R141
Do you think I can only use a
cache.Store
? Since we don't need any custom function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's fine. I posted the comment before reading the complete PR, sorry :)