package kubernetes import ( "errors" "fmt" "log" "sync" "time" "k8s.io/client-go/1.5/kubernetes" "k8s.io/client-go/1.5/pkg/api" "k8s.io/client-go/1.5/pkg/api/v1" "k8s.io/client-go/1.5/pkg/labels" "k8s.io/client-go/1.5/pkg/runtime" "k8s.io/client-go/1.5/pkg/watch" "k8s.io/client-go/1.5/tools/cache" ) var ( namespace = api.NamespaceAll ) // storeToNamespaceLister makes a Store that lists Namespaces. type storeToNamespaceLister struct { cache.Store } const podIPIndex = "PodIP" // List lists all Namespaces in the store. func (s *storeToNamespaceLister) List() (ns api.NamespaceList, err error) { for _, m := range s.Store.List() { ns.Items = append(ns.Items, *(m.(*api.Namespace))) } return ns, nil } type dnsController interface { ServiceList() []*api.Service PodIndex(string) []interface{} EndpointsList() api.EndpointsList Run() Stop() error } type dnsControl struct { client *kubernetes.Clientset selector *labels.Selector svcController *cache.Controller podController *cache.Controller nsController *cache.Controller epController *cache.Controller svcLister cache.StoreToServiceLister podLister cache.StoreToPodLister nsLister storeToNamespaceLister epLister cache.StoreToEndpointsLister // stopLock is used to enforce only a single call to Stop is active. // Needed because we allow stopping through an http endpoint and // allowing concurrent stoppers leads to stack traces. stopLock sync.Mutex shutdown bool stopCh chan struct{} } // newDNSController creates a controller for CoreDNS. func newdnsController(kubeClient *kubernetes.Clientset, resyncPeriod time.Duration, lselector *labels.Selector, initPodCache bool) *dnsControl { dns := dnsControl{ client: kubeClient, selector: lselector, stopCh: make(chan struct{}), } dns.svcLister.Indexer, dns.svcController = cache.NewIndexerInformer( &cache.ListWatch{ ListFunc: serviceListFunc(dns.client, namespace, dns.selector), WatchFunc: serviceWatchFunc(dns.client, namespace, dns.selector), }, &api.Service{}, resyncPeriod, cache.ResourceEventHandlerFuncs{}, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) if initPodCache { dns.podLister.Indexer, dns.podController = cache.NewIndexerInformer( &cache.ListWatch{ ListFunc: podListFunc(dns.client, namespace, dns.selector), WatchFunc: podWatchFunc(dns.client, namespace, dns.selector), }, &api.Pod{}, // TODO replace with a lighter-weight custom struct resyncPeriod, cache.ResourceEventHandlerFuncs{}, cache.Indexers{podIPIndex: podIPIndexFunc}) } dns.nsLister.Store, dns.nsController = cache.NewInformer( &cache.ListWatch{ ListFunc: namespaceListFunc(dns.client, dns.selector), WatchFunc: namespaceWatchFunc(dns.client, dns.selector), }, &api.Namespace{}, resyncPeriod, cache.ResourceEventHandlerFuncs{}) dns.epLister.Store, dns.epController = cache.NewInformer( &cache.ListWatch{ ListFunc: endpointsListFunc(dns.client, namespace, dns.selector), WatchFunc: endpointsWatchFunc(dns.client, namespace, dns.selector), }, &api.Endpoints{}, resyncPeriod, cache.ResourceEventHandlerFuncs{}) return &dns } func podIPIndexFunc(obj interface{}) ([]string, error) { p, ok := obj.(*api.Pod) if !ok { return nil, errors.New("obj was not an *api.Pod") } return []string{p.Status.PodIP}, nil } func serviceListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) { return func(opts api.ListOptions) (runtime.Object, error) { if s != nil { opts.LabelSelector = *s } listV1, err := c.Core().Services(ns).List(opts) if err != nil { return nil, err } var listAPI api.ServiceList err = v1.Convert_v1_ServiceList_To_api_ServiceList(listV1, &listAPI, nil) if err != nil { return nil, err } return &listAPI, err } } func podListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) { return func(opts api.ListOptions) (runtime.Object, error) { if s != nil { opts.LabelSelector = *s } listV1, err := c.Core().Pods(ns).List(opts) if err != nil { return nil, err } var listAPI api.PodList err = v1.Convert_v1_PodList_To_api_PodList(listV1, &listAPI, nil) if err != nil { return nil, err } return &listAPI, err } } func v1ToAPIFilter(in watch.Event) (out watch.Event, keep bool) { if in.Type == watch.Error { return in, true } switch v1Obj := in.Object.(type) { case *v1.Service: var apiObj api.Service err := v1.Convert_v1_Service_To_api_Service(v1Obj, &apiObj, nil) if err != nil { log.Printf("[ERROR] Could not convert v1.Service: %s", err) return in, true } return watch.Event{Type: in.Type, Object: &apiObj}, true case *v1.Pod: var apiObj api.Pod err := v1.Convert_v1_Pod_To_api_Pod(v1Obj, &apiObj, nil) if err != nil { log.Printf("[ERROR] Could not convert v1.Pod: %s", err) return in, true } return watch.Event{Type: in.Type, Object: &apiObj}, true case *v1.Namespace: var apiObj api.Namespace err := v1.Convert_v1_Namespace_To_api_Namespace(v1Obj, &apiObj, nil) if err != nil { log.Printf("[ERROR] Could not convert v1.Namespace: %s", err) return in, true } return watch.Event{Type: in.Type, Object: &apiObj}, true case *v1.Endpoints: var apiObj api.Endpoints err := v1.Convert_v1_Endpoints_To_api_Endpoints(v1Obj, &apiObj, nil) if err != nil { log.Printf("[ERROR] Could not convert v1.Endpoint: %s", err) return in, true } return watch.Event{Type: in.Type, Object: &apiObj}, true } log.Printf("[WARN] Unhandled v1 type in event: %v", in) return in, true } func serviceWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) { return func(options api.ListOptions) (watch.Interface, error) { if s != nil { options.LabelSelector = *s } w, err := c.Core().Services(ns).Watch(options) if err != nil { return nil, err } return watch.Filter(w, v1ToAPIFilter), nil } } func podWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) { return func(options api.ListOptions) (watch.Interface, error) { if s != nil { options.LabelSelector = *s } w, err := c.Core().Pods(ns).Watch(options) if err != nil { return nil, err } return watch.Filter(w, v1ToAPIFilter), nil } } func namespaceListFunc(c *kubernetes.Clientset, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) { return func(opts api.ListOptions) (runtime.Object, error) { if s != nil { opts.LabelSelector = *s } listV1, err := c.Core().Namespaces().List(opts) if err != nil { return nil, err } var listAPI api.NamespaceList err = v1.Convert_v1_NamespaceList_To_api_NamespaceList(listV1, &listAPI, nil) if err != nil { return nil, err } return &listAPI, err } } func namespaceWatchFunc(c *kubernetes.Clientset, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) { return func(options api.ListOptions) (watch.Interface, error) { if s != nil { options.LabelSelector = *s } w, err := c.Core().Namespaces().Watch(options) if err != nil { return nil, err } return watch.Filter(w, v1ToAPIFilter), nil } } func endpointsListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) { return func(opts api.ListOptions) (runtime.Object, error) { if s != nil { opts.LabelSelector = *s } listV1, err := c.Core().Endpoints(ns).List(opts) if err != nil { return nil, err } var listAPI api.EndpointsList err = v1.Convert_v1_EndpointsList_To_api_EndpointsList(listV1, &listAPI, nil) if err != nil { return nil, err } return &listAPI, err } } func endpointsWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) { return func(options api.ListOptions) (watch.Interface, error) { if s != nil { options.LabelSelector = *s } w, err := c.Core().Endpoints(ns).Watch(options) if err != nil { return nil, err } return watch.Filter(w, v1ToAPIFilter), nil } } func (dns *dnsControl) controllersInSync() bool { return dns.svcController.HasSynced() } // Stop stops the controller. func (dns *dnsControl) Stop() error { dns.stopLock.Lock() defer dns.stopLock.Unlock() // Only try draining the workqueue if we haven't already. if !dns.shutdown { close(dns.stopCh) dns.shutdown = true return nil } return fmt.Errorf("shutdown already in progress") } // Run starts the controller. func (dns *dnsControl) Run() { go dns.svcController.Run(dns.stopCh) go dns.nsController.Run(dns.stopCh) go dns.epController.Run(dns.stopCh) if dns.podController != nil { go dns.podController.Run(dns.stopCh) } <-dns.stopCh } func (dns *dnsControl) NamespaceList() *api.NamespaceList { nsList, err := dns.nsLister.List() if err != nil { return &api.NamespaceList{} } return &nsList } func (dns *dnsControl) ServiceList() []*api.Service { svcs, err := dns.svcLister.List(labels.Everything()) if err != nil { return []*api.Service{} } return svcs } func (dns *dnsControl) PodIndex(ip string) []interface{} { pods, err := dns.podLister.Indexer.ByIndex(podIPIndex, ip) if err != nil { return nil } return pods } func (dns *dnsControl) EndpointsList() api.EndpointsList { epl, err := dns.epLister.List() if err != nil { return api.EndpointsList{} } return epl } // ServicesByNamespace returns a map of: // // namespacename :: [ kubernetesService ] func (dns *dnsControl) ServicesByNamespace() map[string][]api.Service { k8sServiceList := dns.ServiceList() items := make(map[string][]api.Service, len(k8sServiceList)) for _, i := range k8sServiceList { namespace := i.Namespace items[namespace] = append(items[namespace], *i) } return items } // ServiceInNamespace returns the Service that matches servicename in the namespace func (dns *dnsControl) ServiceInNamespace(namespace, servicename string) *api.Service { svcObj, err := dns.svcLister.Services(namespace).Get(servicename) if err != nil { // TODO(...): should return err here return nil } return svcObj } at/streaming-rendering'>feat/streaming-rendering Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/examples/blog-multiple-authors (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2022-02-28[ci] update lockfile (#2676)Gravatar Fred K. Schott 1-6/+6
2022-02-28fix(runtime): do not render empty Fragment (#2667)Gravatar Mateus Esdras 1-0/+3
2022-02-28fix(hmr): HMR regression related to .astro updates (#2681)Gravatar Nate Moore 6-7/+24
2022-02-28Fix HTMLElement expression warning (#2675)Gravatar Jonathan Neal 1-1/+1
2022-02-28[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-27[ci] update lockfile (#2668)Gravatar Fred K. Schott 1-80/+80
2022-02-27[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-26[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-25[ci] yarn formatGravatar natemoo-re 1-20/+20
2022-02-25[ci] release (#2666)astro@0.23.2Gravatar github-actions[bot] 32-59/+57
2022-02-25[ci] yarn formatGravatar natemoo-re 2-12/+6
2022-02-25fix astro scoping of "@import" inside of style tags (#2656)Gravatar Fred K. Schott 3-6/+35
2022-02-25[ci] update lockfile (#2659)Gravatar Fred K. Schott 1-20/+20
2022-02-25feat: improve third-party Astro package compatability (#2665)Gravatar Nate Moore 3-6/+100
2022-02-25get new example working during buildGravatar Fred K. Schott 4-16/+21
2022-02-25[ci] yarn formatGravatar FredKSchott 1-7/+6
2022-02-25Add Non-HTML Pages example (#2637)Gravatar Joel Kuzmarski 11-0/+136
2022-02-25[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-24[ci] yarn formatGravatar natemoo-re 2-24/+24
2022-02-24[ci] release (#2641)astro@0.23.1@astrojs/markdown-remark@0.6.2Gravatar github-actions[bot] 38-90/+81
2022-02-24ensure utf8 encoding when serving html (#2654)Gravatar Fred K. Schott 3-4/+9
2022-02-24fix(core): Issue #2625. error with process.env.LANG larger than 5 (#2645)Gravatar Javier Cortés 2-1/+6
2022-02-24[ci] update lockfile (#2646)Gravatar Fred K. Schott 1-130/+124
2022-02-24chore: upgrade compiler (#2653)Gravatar Nate Moore 3-11/+11
2022-02-24[ci] yarn formatGravatar natemoo-re 2-5/+5
2022-02-24Add fine-grained HMR support (#2649)Gravatar Nate Moore 7-36/+37
2022-02-24[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-23Fixed incorrect types and imports (#2630)Gravatar Juan Martín Seery 27-35/+37
2022-02-23Add sass dev dep to blog-multiple-authors example (#2643)Gravatar Joel Kuzmarski 1-1/+2
2022-02-23Fix(component): align starting position in Markdown slot (#2631)Gravatar Shinobu Hayashi 4-6/+61
2022-02-23[ci] yarn formatGravatar matthewp 1-1/+1
2022-02-23Run all smoke tests with the static build (#2609)Gravatar Matthew Phillips 2-26/+32
2022-02-23[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-22[ci] update lockfile (#2624)Gravatar Fred K. Schott 1-171/+201
2022-02-22Fixed shiki import to work with "type": "module" (#2628)Gravatar Juan Martín Seery 3-5/+13