github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/cmd/apiserver/apiserver.go (about)

     1  /*
     2  Copyright 2014 Google Inc. 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  // apiserver is the main api server and master for the cluster.
    17  // it is responsible for serving the cluster management API.
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"log"
    24  	"net/http"
    25  	"time"
    26  
    27  	"github.com/coreos/go-etcd/etcd"
    28  
    29  	"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
    30  	kube_client "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
    31  	"github.com/GoogleCloudPlatform/kubernetes/pkg/registry"
    32  	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
    33  )
    34  
    35  var (
    36  	port                        = flag.Uint("port", 8080, "The port to listen on.  Default 8080.")
    37  	address                     = flag.String("address", "127.0.0.1", "The address on the local server to listen to. Default 127.0.0.1")
    38  	apiPrefix                   = flag.String("api_prefix", "/api/v1beta1", "The prefix for API requests on the server. Default '/api/v1beta1'")
    39  	etcdServerList, machineList util.StringList
    40  )
    41  
    42  func init() {
    43  	flag.Var(&etcdServerList, "etcd_servers", "Servers for the etcd (http://ip:port), comma separated")
    44  	flag.Var(&machineList, "machines", "List of machines to schedule onto, comma separated.")
    45  }
    46  
    47  func main() {
    48  	flag.Parse()
    49  
    50  	if len(machineList) == 0 {
    51  		log.Fatal("No machines specified!")
    52  	}
    53  
    54  	var (
    55  		taskRegistry       registry.TaskRegistry
    56  		controllerRegistry registry.ControllerRegistry
    57  		serviceRegistry    registry.ServiceRegistry
    58  	)
    59  
    60  	if len(etcdServerList) > 0 {
    61  		log.Printf("Creating etcd client pointing to %v", etcdServerList)
    62  		etcdClient := etcd.NewClient(etcdServerList)
    63  		taskRegistry = registry.MakeEtcdRegistry(etcdClient, machineList)
    64  		controllerRegistry = registry.MakeEtcdRegistry(etcdClient, machineList)
    65  		serviceRegistry = registry.MakeEtcdRegistry(etcdClient, machineList)
    66  	} else {
    67  		taskRegistry = registry.MakeMemoryRegistry()
    68  		controllerRegistry = registry.MakeMemoryRegistry()
    69  		serviceRegistry = registry.MakeMemoryRegistry()
    70  	}
    71  
    72  	containerInfo := &kube_client.HTTPContainerInfo{
    73  		Client: http.DefaultClient,
    74  		Port:   10250,
    75  	}
    76  
    77  	storage := map[string]apiserver.RESTStorage{
    78  		"tasks":                  registry.MakeTaskRegistryStorage(taskRegistry, containerInfo, registry.MakeFirstFitScheduler(machineList, taskRegistry)),
    79  		"replicationControllers": registry.MakeControllerRegistryStorage(controllerRegistry),
    80  		"services":               registry.MakeServiceRegistryStorage(serviceRegistry),
    81  	}
    82  
    83  	endpoints := registry.MakeEndpointController(serviceRegistry, taskRegistry)
    84  	go util.Forever(func() { endpoints.SyncServiceEndpoints() }, time.Second*10)
    85  
    86  	s := &http.Server{
    87  		Addr:           fmt.Sprintf("%s:%d", *address, *port),
    88  		Handler:        apiserver.New(storage, *apiPrefix),
    89  		ReadTimeout:    10 * time.Second,
    90  		WriteTimeout:   10 * time.Second,
    91  		MaxHeaderBytes: 1 << 20,
    92  	}
    93  	log.Fatal(s.ListenAndServe())
    94  }