elasticsearch_logging_discovery.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "flag"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "time"
  21. corev1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. clientset "k8s.io/client-go/kubernetes"
  24. restclient "k8s.io/client-go/rest"
  25. "k8s.io/client-go/tools/clientcmd"
  26. clientapi "k8s.io/client-go/tools/clientcmd/api"
  27. "k8s.io/klog"
  28. )
  29. func buildConfigFromEnvs(masterURL, kubeconfigPath string) (*restclient.Config, error) {
  30. if kubeconfigPath == "" && masterURL == "" {
  31. kubeconfig, err := restclient.InClusterConfig()
  32. if err != nil {
  33. return nil, err
  34. }
  35. return kubeconfig, nil
  36. }
  37. return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
  38. &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
  39. &clientcmd.ConfigOverrides{ClusterInfo: clientapi.Cluster{Server: masterURL}}).ClientConfig()
  40. }
  41. func flattenSubsets(subsets []corev1.EndpointSubset) []string {
  42. ips := []string{}
  43. for _, ss := range subsets {
  44. for _, addr := range ss.Addresses {
  45. ips = append(ips, fmt.Sprintf(`"%s"`, addr.IP))
  46. }
  47. }
  48. return ips
  49. }
  50. func main() {
  51. flag.Parse()
  52. klog.Info("Kubernetes Elasticsearch logging discovery")
  53. cc, err := buildConfigFromEnvs(os.Getenv("APISERVER_HOST"), os.Getenv("KUBE_CONFIG_FILE"))
  54. if err != nil {
  55. klog.Fatalf("Failed to make client: %v", err)
  56. }
  57. client, err := clientset.NewForConfig(cc)
  58. if err != nil {
  59. klog.Fatalf("Failed to make client: %v", err)
  60. }
  61. namespace := metav1.NamespaceSystem
  62. envNamespace := os.Getenv("NAMESPACE")
  63. if envNamespace != "" {
  64. if _, err := client.CoreV1().Namespaces().Get(envNamespace, metav1.GetOptions{}); err != nil {
  65. klog.Fatalf("%s namespace doesn't exist: %v", envNamespace, err)
  66. }
  67. namespace = envNamespace
  68. }
  69. var elasticsearch *corev1.Service
  70. serviceName := os.Getenv("ELASTICSEARCH_SERVICE_NAME")
  71. if serviceName == "" {
  72. serviceName = "elasticsearch-logging"
  73. }
  74. // Look for endpoints associated with the Elasticsearch logging service.
  75. // First wait for the service to become available.
  76. for t := time.Now(); time.Since(t) < 5*time.Minute; time.Sleep(10 * time.Second) {
  77. elasticsearch, err = client.CoreV1().Services(namespace).Get(serviceName, metav1.GetOptions{})
  78. if err == nil {
  79. break
  80. }
  81. }
  82. // If we did not find an elasticsearch logging service then log a warning
  83. // and return without adding any unicast hosts.
  84. if elasticsearch == nil {
  85. klog.Warningf("Failed to find the elasticsearch-logging service: %v", err)
  86. return
  87. }
  88. var endpoints *corev1.Endpoints
  89. addrs := []string{}
  90. // Wait for some endpoints.
  91. count, _ := strconv.Atoi(os.Getenv("MINIMUM_MASTER_NODES"))
  92. for t := time.Now(); time.Since(t) < 5*time.Minute; time.Sleep(10 * time.Second) {
  93. endpoints, err = client.CoreV1().Endpoints(namespace).Get(serviceName, metav1.GetOptions{})
  94. if err != nil {
  95. continue
  96. }
  97. addrs = flattenSubsets(endpoints.Subsets)
  98. klog.Infof("Found %s", addrs)
  99. if len(addrs) > 0 && len(addrs) >= count {
  100. break
  101. }
  102. }
  103. // If there was an error finding endpoints then log a warning and quit.
  104. if err != nil {
  105. klog.Warningf("Error finding endpoints: %v", err)
  106. return
  107. }
  108. klog.Infof("Endpoints = %s", addrs)
  109. fmt.Printf("discovery.zen.ping.unicast.hosts: [%s]\n", strings.Join(addrs, ", "))
  110. }