// Run runs the specified ProxyServer. This should never exit. func (s *ProxyServer) Run(_ []string) error { if err := util.ApplyOomScoreAdj(0, s.OOMScoreAdj); err != nil { glog.Info(err) } serviceConfig := config.NewServiceConfig() endpointsConfig := config.NewEndpointsConfig() protocol := iptables.ProtocolIpv4 if net.IP(s.BindAddress).To4() == nil { protocol = iptables.ProtocolIpv6 } loadBalancer := proxy.NewLoadBalancerRR() proxier := proxy.NewProxier(loadBalancer, net.IP(s.BindAddress), iptables.New(exec.New(), protocol)) if proxier == nil { glog.Fatalf("failed to create proxier, aborting") } // Wire proxier to handle changes to services serviceConfig.RegisterHandler(proxier) // And wire loadBalancer to handle changes to endpoints to services endpointsConfig.RegisterHandler(loadBalancer) // Note: RegisterHandler() calls need to happen before creation of Sources because sources // only notify on changes, and the initial update (on process start) may be lost if no handlers // are registered yet. // define api config source if s.ClientConfig.Host != "" { glog.Infof("Using API calls to get config %v", s.ClientConfig.Host) client, err := client.New(&s.ClientConfig) if err != nil { glog.Fatalf("Invalid API configuration: %v", err) } config.NewSourceAPI( client.Services(api.NamespaceAll), client.Endpoints(api.NamespaceAll), 30*time.Second, serviceConfig.Channel("api"), endpointsConfig.Channel("api"), ) } if s.HealthzPort > 0 { go util.Forever(func() { err := http.ListenAndServe(s.BindAddress.String()+":"+strconv.Itoa(s.HealthzPort), nil) if err != nil { glog.Errorf("Starting health server failed: %v", err) } }, 5*time.Second) } // Just loop forever for now... proxier.SyncLoop() return nil }
func main() { config := &client.Config{ Host: "http://localhost:8080", Username: "******", Password: "******", } client, err := client.New(config) if err != nil { // handle error } //TODO: have namespace a flag for the script ns := api.NamespaceDefault list, err := client.Services(ns).List(labels.Everything()) if err != nil { fmt.Println(err) } //TODO: allow selction of nodes by labels or fields nodes, err := client.Nodes().List(labels.Everything(), fields.Everything()) if err != nil { fmt.Println(err) } //TODO: this data structure is a mess. Maybe put in Kubernetes // client helper methods for some of these data points var templateServiceServers []ServiceServers for _, service := range list.Items { if service.Name == "kubernetes" { // TODO: better way of filtering out the kubernetes service? continue } s := &ServiceServers{ServiceName: service.Name} sp := &ServerPort{} for _, port := range service.Spec.Ports { sp.ServicePort = port.NodePort } for _, node := range nodes.Items { for _, address := range node.Status.Addresses { sp.ServerAddress = address.Address } } s.ServerPorts = append(s.ServerPorts, *sp) templateServiceServers = append(templateServiceServers, *s) } // TODO: allow your own template file t := template.New("Nginx config template") t, err = t.ParseFiles("nginx.tmpl") if err != nil { fmt.Println(err) } t.ExecuteTemplate(os.Stdout, "nginx.tmpl", templateServiceServers) }
func WatchKubernetes(eclient *etcd.Client) { serviceConfig := pconfig.NewServiceConfig() endpointsConfig := pconfig.NewEndpointsConfig() if clientConfig.Host != "" { log.Printf("using api calls to get Kubernetes config %v\n", clientConfig.Host) client, err := client.New(clientConfig) if err != nil { log.Fatalf("Kubernetes requested, but received invalid API configuration: %v", err) } pconfig.NewSourceAPI( client.Services(api.NamespaceAll), client.Endpoints(api.NamespaceAll), syncInterval, serviceConfig.Channel("api"), endpointsConfig.Channel("api"), ) } ks := NewKubernetesSync(eclient) // Wire skydns to handle changes to services serviceConfig.RegisterHandler(ks) }
func main() { flag.Parse() util.InitLogs() defer util.FlushLogs() verflag.PrintAndExitIfRequested() serviceConfig := config.NewServiceConfig() endpointsConfig := config.NewEndpointsConfig() // define api config source if clientConfig.Host != "" { glog.Infof("Using api calls to get config %v", clientConfig.Host) client, err := client.New(clientConfig) if err != nil { glog.Fatalf("Invalid API configuration: %v", err) } config.NewSourceAPI( client.Services(api.NamespaceAll), client.Endpoints(api.NamespaceAll), 30*time.Second, serviceConfig.Channel("api"), endpointsConfig.Channel("api"), ) } else { var etcdClient *etcd.Client // Set up etcd client if len(etcdServerList) > 0 { // Set up logger for etcd client etcd.SetLogger(util.NewLogger("etcd ")) etcdClient = etcd.NewClient(etcdServerList) } else if *etcdConfigFile != "" { // Set up logger for etcd client etcd.SetLogger(util.NewLogger("etcd ")) var err error etcdClient, err = etcd.NewClientFromFile(*etcdConfigFile) if err != nil { glog.Fatalf("Error with etcd config file: %v", err) } } // Create a configuration source that handles configuration from etcd. if etcdClient != nil { glog.Infof("Using etcd servers %v", etcdClient.GetCluster()) config.NewConfigSourceEtcd(etcdClient, serviceConfig.Channel("etcd"), endpointsConfig.Channel("etcd")) } } loadBalancer := proxy.NewLoadBalancerRR() proxier := proxy.NewProxier(loadBalancer, net.IP(bindAddress), iptables.New(exec.New())) // Wire proxier to handle changes to services serviceConfig.RegisterHandler(proxier) // And wire loadBalancer to handle changes to endpoints to services endpointsConfig.RegisterHandler(loadBalancer) // Just loop forever for now... proxier.SyncLoop() }
func main() { util.InitFlags() util.InitLogs() defer util.FlushLogs() if err := util.ApplyOomScoreAdj(*oomScoreAdj); err != nil { glog.Info(err) } verflag.PrintAndExitIfRequested() serviceConfig := config.NewServiceConfig() endpointsConfig := config.NewEndpointsConfig() protocol := iptables.ProtocolIpv4 if net.IP(bindAddress).To4() == nil { protocol = iptables.ProtocolIpv6 } loadBalancer := proxy.NewLoadBalancerRR() proxier := proxy.NewProxier(loadBalancer, net.IP(bindAddress), iptables.New(exec.New(), protocol)) if proxier == nil { glog.Fatalf("failed to create proxier, aborting") } // Wire proxier to handle changes to services serviceConfig.RegisterHandler(proxier) // And wire loadBalancer to handle changes to endpoints to services endpointsConfig.RegisterHandler(loadBalancer) // Note: RegisterHandler() calls need to happen before creation of Sources because sources // only notify on changes, and the initial update (on process start) may be lost if no handlers // are registered yet. // define api config source if clientConfig.Host != "" { glog.Infof("Using api calls to get config %v", clientConfig.Host) client, err := client.New(clientConfig) if err != nil { glog.Fatalf("Invalid API configuration: %v", err) } config.NewSourceAPI( client.Services(api.NamespaceAll), client.Endpoints(api.NamespaceAll), 30*time.Second, serviceConfig.Channel("api"), endpointsConfig.Channel("api"), ) } else { var etcdClient *etcd.Client // Set up etcd client if len(etcdServerList) > 0 { // Set up logger for etcd client etcd.SetLogger(util.NewLogger("etcd ")) etcdClient = etcd.NewClient(etcdServerList) } else if *etcdConfigFile != "" { // Set up logger for etcd client etcd.SetLogger(util.NewLogger("etcd ")) var err error etcdClient, err = etcd.NewClientFromFile(*etcdConfigFile) if err != nil { glog.Fatalf("Error with etcd config file: %v", err) } } // Create a configuration source that handles configuration from etcd. if etcdClient != nil { glog.Infof("Using etcd servers %v", etcdClient.GetCluster()) config.NewConfigSourceEtcd(etcdClient, serviceConfig.Channel("etcd"), endpointsConfig.Channel("etcd")) } } if *healthz_port > 0 { go util.Forever(func() { err := http.ListenAndServe(bindAddress.String()+":"+strconv.Itoa(*healthz_port), nil) if err != nil { glog.Errorf("Starting health server failed: %v", err) } }, 5*time.Second) } // Just loop forever for now... proxier.SyncLoop() }
// Run runs the specified ProxyServer. This should never exit. func (s *ProxyServer) Run(_ []string) error { // TODO(vmarmol): Use container config for this. if err := util.ApplyOomScoreAdj(0, s.OOMScoreAdj); err != nil { glog.Info(err) } // Run in its own container. if err := util.RunInResourceContainer(s.ResourceContainer); err != nil { glog.Warningf("Failed to start in resource-only container %q: %v", s.ResourceContainer, err) } else { glog.Infof("Running in resource-only container %q", s.ResourceContainer) } serviceConfig := config.NewServiceConfig() endpointsConfig := config.NewEndpointsConfig() protocol := iptables.ProtocolIpv4 if net.IP(s.BindAddress).To4() == nil { protocol = iptables.ProtocolIpv6 } loadBalancer := proxy.NewLoadBalancerRR() proxier := proxy.NewProxier(loadBalancer, net.IP(s.BindAddress), iptables.New(exec.New(), protocol)) if proxier == nil { glog.Fatalf("failed to create proxier, aborting") } // Wire proxier to handle changes to services serviceConfig.RegisterHandler(proxier) // And wire loadBalancer to handle changes to endpoints to services endpointsConfig.RegisterHandler(loadBalancer) // Note: RegisterHandler() calls need to happen before creation of Sources because sources // only notify on changes, and the initial update (on process start) may be lost if no handlers // are registered yet. // define api config source if s.Kubeconfig == "" && s.Master == "" { glog.Warningf("Neither --kubeconfig nor --master was specified. Using default API client. This might not work.") } // This creates a client, first loading any specified kubeconfig // file, and then overriding the Master flag, if non-empty. kubeconfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( &clientcmd.ClientConfigLoadingRules{ExplicitPath: s.Kubeconfig}, &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: s.Master}}).ClientConfig() if err != nil { return err } client, err := client.New(kubeconfig) if err != nil { glog.Fatalf("Invalid API configuration: %v", err) } config.NewSourceAPI( client.Services(api.NamespaceAll), client.Endpoints(api.NamespaceAll), 30*time.Second, serviceConfig.Channel("api"), endpointsConfig.Channel("api"), ) if s.HealthzPort > 0 { go util.Forever(func() { err := http.ListenAndServe(s.HealthzBindAddress.String()+":"+strconv.Itoa(s.HealthzPort), nil) if err != nil { glog.Errorf("Starting health server failed: %v", err) } }, 5*time.Second) } // Just loop forever for now... proxier.SyncLoop() return nil }