github.com/felipejfc/helm@v2.1.2+incompatible/cmd/tiller/tiller.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main // import "k8s.io/helm/cmd/tiller" 18 19 import ( 20 "fmt" 21 "log" 22 "net" 23 "net/http" 24 "os" 25 26 "github.com/spf13/cobra" 27 28 "k8s.io/helm/pkg/kube" 29 "k8s.io/helm/pkg/proto/hapi/services" 30 "k8s.io/helm/pkg/storage" 31 "k8s.io/helm/pkg/storage/driver" 32 "k8s.io/helm/pkg/tiller" 33 "k8s.io/helm/pkg/tiller/environment" 34 ) 35 36 const ( 37 storageMemory = "memory" 38 storageConfigMap = "configmap" 39 ) 40 41 // rootServer is the root gRPC server. 42 // 43 // Each gRPC service registers itself to this server during init(). 44 var rootServer = tiller.NewServer() 45 46 // env is the default environment. 47 // 48 // Any changes to env should be done before rootServer.Serve() is called. 49 var env = environment.New() 50 51 var ( 52 grpcAddr = ":44134" 53 probeAddr = ":44135" 54 traceAddr = ":44136" 55 enableTracing = false 56 store = storageConfigMap 57 ) 58 59 const globalUsage = `The Kubernetes Helm server. 60 61 Tiller is the server for Helm. It provides in-cluster resource management. 62 63 By default, Tiller listens for gRPC connections on port 44134. 64 ` 65 66 var rootCommand = &cobra.Command{ 67 Use: "tiller", 68 Short: "The Kubernetes Helm server.", 69 Long: globalUsage, 70 Run: start, 71 } 72 73 func main() { 74 log.SetFlags(log.Flags() | log.Lshortfile) 75 76 p := rootCommand.PersistentFlags() 77 p.StringVarP(&grpcAddr, "listen", "l", ":44134", "address:port to listen on") 78 p.StringVar(&store, "storage", storageConfigMap, "storage driver to use. One of 'configmap' or 'memory'") 79 p.BoolVar(&enableTracing, "trace", false, "enable rpc tracing") 80 rootCommand.Execute() 81 } 82 83 func start(c *cobra.Command, args []string) { 84 clientset, err := kube.New(nil).ClientSet() 85 if err != nil { 86 fmt.Fprintf(os.Stderr, "Cannot initialize Kubernetes connection: %s", err) 87 } 88 89 switch store { 90 case storageMemory: 91 env.Releases = storage.Init(driver.NewMemory()) 92 case storageConfigMap: 93 env.Releases = storage.Init(driver.NewConfigMaps(clientset.Core().ConfigMaps(environment.TillerNamespace))) 94 } 95 96 lstn, err := net.Listen("tcp", grpcAddr) 97 if err != nil { 98 fmt.Fprintf(os.Stderr, "Server died: %s\n", err) 99 os.Exit(1) 100 } 101 102 fmt.Printf("Tiller is listening on %s\n", grpcAddr) 103 fmt.Printf("Probes server is listening on %s\n", probeAddr) 104 fmt.Printf("Storage driver is %s\n", env.Releases.Name()) 105 106 if enableTracing { 107 startTracing(traceAddr) 108 } 109 110 srvErrCh := make(chan error) 111 probeErrCh := make(chan error) 112 go func() { 113 svc := tiller.NewReleaseServer(env, clientset) 114 services.RegisterReleaseServiceServer(rootServer, svc) 115 if err := rootServer.Serve(lstn); err != nil { 116 srvErrCh <- err 117 } 118 }() 119 120 go func() { 121 mux := newProbesMux() 122 if err := http.ListenAndServe(probeAddr, mux); err != nil { 123 probeErrCh <- err 124 } 125 }() 126 127 select { 128 case err := <-srvErrCh: 129 fmt.Fprintf(os.Stderr, "Server died: %s\n", err) 130 os.Exit(1) 131 case err := <-probeErrCh: 132 fmt.Fprintf(os.Stderr, "Probes server died: %s\n", err) 133 } 134 }