Skip to content

Commit

Permalink
remove non-generic options from genericapiserver.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Nov 3, 2016
1 parent 4c12c3b commit d82f98c
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 137 deletions.
15 changes: 14 additions & 1 deletion cmd/kube-apiserver/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ func Run(s *options.ServerRunOptions) error {
ApplyOptions(s.GenericServerRunOptions). // apply the options selected
Complete() // set default values based on the known values

if err := genericConfig.MaybeGenerateServingCerts(); err != nil {
serviceIPRange, apiServerServiceIP, err := genericapiserver.DefaultServiceIPRange(s.GenericServerRunOptions.ServiceClusterIPRange)
if err != nil {
glog.Fatalf("Error determining service IP ranges: %v", err)
}
if err := genericConfig.MaybeGenerateServingCerts(apiServerServiceIP); err != nil {
glog.Fatalf("Failed to generate service certificate: %v", err)
}

Expand Down Expand Up @@ -322,6 +326,15 @@ func Run(s *options.ServerRunOptions) error {
ProxyTransport: proxyTransport,

Tunneler: tunneler,

ServiceIPRange: serviceIPRange,
APIServerServiceIP: apiServerServiceIP,
APIServerServicePort: 443,

ServiceNodePortRange: s.GenericServerRunOptions.ServiceNodePortRange,
KubernetesServiceNodePort: s.GenericServerRunOptions.KubernetesServiceNodePort,

MasterCount: s.GenericServerRunOptions.MasterCount,
}

if s.GenericServerRunOptions.EnableWatchCache {
Expand Down
4 changes: 3 additions & 1 deletion pkg/genericapiserver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
srcs = [
"config.go",
"default_storage_factory_builder.go",
"discovery.go",
"doc.go",
"genericapiserver.go",
"healthz.go",
Expand All @@ -23,6 +24,7 @@ go_library(
"resource_encoding_config.go",
"reststorage_interfaces.go",
"serve.go",
"services.go",
"storage_factory.go",
"tunneler.go",
],
Expand Down Expand Up @@ -102,11 +104,11 @@ go_test(
"//pkg/auth/user:go_default_library",
"//pkg/generated/openapi:go_default_library",
"//pkg/genericapiserver/options:go_default_library",
"//pkg/registry/core/service/ipallocator:go_default_library",
"//pkg/storage/etcd/testing:go_default_library",
"//pkg/storage/storagebackend:go_default_library",
"//pkg/util/cert:go_default_library",
"//pkg/util/clock:go_default_library",
"//pkg/util/net:go_default_library",
"//pkg/util/sets:go_default_library",
"//pkg/version:go_default_library",
"//vendor:github.com/go-openapi/spec",
Expand Down
88 changes: 10 additions & 78 deletions pkg/genericapiserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import (
"k8s.io/kubernetes/pkg/genericapiserver/options"
"k8s.io/kubernetes/pkg/genericapiserver/routes"
genericvalidation "k8s.io/kubernetes/pkg/genericapiserver/validation"
ipallocator "k8s.io/kubernetes/pkg/registry/core/service/ipallocator"
"k8s.io/kubernetes/pkg/runtime"
certutil "k8s.io/kubernetes/pkg/util/cert"
utilnet "k8s.io/kubernetes/pkg/util/net"
Expand Down Expand Up @@ -107,13 +106,13 @@ type Config struct {
// Note that it is up to the request handlers to ignore or honor this timeout. In seconds.
MinRequestTimeout int

// Number of masters running; all masters must be started with the
// same value for this field. (Numbers > 1 currently untested.)
MasterCount int

SecureServingInfo *SecureServingInfo
InsecureServingInfo *ServingInfo

// DiscoveryAddresses is used to build the IPs pass to discovery. If nil, the ExternalAddress is
// always reported
DiscoveryAddresses DiscoveryAddresses

// The port on PublicAddress where a read-write server will be installed.
// Defaults to 6443 if not set.
ReadWritePort int
Expand All @@ -126,34 +125,6 @@ type Config struct {
// If nil or 0.0.0.0, the host's default interface will be used.
PublicAddress net.IP

// The range of IPs to be assigned to services with type=ClusterIP or greater
ServiceClusterIPRange *net.IPNet

// The IP address for the GenericAPIServer service (must be inside ServiceClusterIPRange)
ServiceReadWriteIP net.IP

// Port for the apiserver service.
ServiceReadWritePort int

// The range of ports to be assigned to services with type=NodePort or greater
ServiceNodePortRange utilnet.PortRange

// Additional ports to be exposed on the GenericAPIServer service
// extraServicePorts is injectable in the event that more ports
// (other than the default 443/tcp) are exposed on the GenericAPIServer
// and those ports need to be load balanced by the GenericAPIServer
// service because this pkg is linked by out-of-tree projects
// like openshift which want to use the GenericAPIServer but also do
// more stuff.
ExtraServicePorts []api.ServicePort
// Additional ports to be exposed on the GenericAPIServer endpoints
// Port names should align with ports defined in ExtraServicePorts
ExtraEndpointPorts []api.EndpointPort

// If non-zero, the "kubernetes" services uses this port as NodePort.
// TODO(sttts): move into master
KubernetesServiceNodePort int

// EnableOpenAPISupport enables OpenAPI support. Allow downstream customers to disable OpenAPI spec.
EnableOpenAPISupport bool

Expand Down Expand Up @@ -221,9 +192,7 @@ func NewConfig() *Config {

config := &Config{
Serializer: api.Codecs,
MasterCount: 1,
ReadWritePort: 6443,
ServiceReadWritePort: 443,
RequestContextMapper: api.NewRequestContextMapper(),
BuildHandlerChainsFunc: DefaultBuildHandlerChain,
LegacyAPIGroupPrefixes: sets.NewString(DefaultLegacyAPIPrefix),
Expand Down Expand Up @@ -318,13 +287,9 @@ func (c *Config) ApplyOptions(options *options.ServerRunOptions) *Config {
c.EnableProfiling = options.EnableProfiling
c.EnableSwaggerUI = options.EnableSwaggerUI
c.ExternalAddress = options.ExternalHost
c.KubernetesServiceNodePort = options.KubernetesServiceNodePort
c.MasterCount = options.MasterCount
c.MaxRequestsInFlight = options.MaxRequestsInFlight
c.MinRequestTimeout = options.MinRequestTimeout
c.PublicAddress = options.AdvertiseAddress
c.ServiceClusterIPRange = &options.ServiceClusterIPRange
c.ServiceNodePortRange = options.ServiceNodePortRange
c.SupportsBasicAuth = len(options.BasicAuthFile) > 0

return c
Expand All @@ -337,35 +302,6 @@ type completedConfig struct {
// Complete fills in any fields not set that are required to have valid data and can be derived
// from other fields. If you're going to `ApplyOptions`, do that first. It's mutating the receiver.
func (c *Config) Complete() completedConfig {
if c.ServiceClusterIPRange == nil || c.ServiceClusterIPRange.IP == nil {
defaultNet := "10.0.0.0/24"
glog.Warningf("Network range for service cluster IPs is unspecified. Defaulting to %v.", defaultNet)
_, serviceClusterIPRange, err := net.ParseCIDR(defaultNet)
if err != nil {
glog.Fatalf("Unable to parse CIDR: %v", err)
}
if size := ipallocator.RangeSize(serviceClusterIPRange); size < 8 {
glog.Fatalf("The service cluster IP range must be at least %d IP addresses", 8)
}
c.ServiceClusterIPRange = serviceClusterIPRange
}
if c.ServiceReadWriteIP == nil {
// Select the first valid IP from ServiceClusterIPRange to use as the GenericAPIServer service IP.
serviceReadWriteIP, err := ipallocator.GetIndexedIP(c.ServiceClusterIPRange, 1)
if err != nil {
glog.Fatalf("Failed to generate service read-write IP for GenericAPIServer service: %v", err)
}
glog.V(4).Infof("Setting GenericAPIServer service IP to %q (read-write).", serviceReadWriteIP)
c.ServiceReadWriteIP = serviceReadWriteIP
}
if c.ServiceNodePortRange.Size == 0 {
// TODO: Currently no way to specify an empty range (do we need to allow this?)
// We should probably allow this for clouds that don't require NodePort to do load-balancing (GCE)
// but then that breaks the strict nestedness of ServiceType.
// Review post-v1
c.ServiceNodePortRange = options.DefaultServiceNodePortRange
glog.Infof("Node port range unspecified. Defaulting to %v.", c.ServiceNodePortRange)
}
if len(c.ExternalAddress) == 0 && c.PublicAddress != nil {
hostAndPort := c.PublicAddress.String()
if c.ReadWritePort != 0 {
Expand Down Expand Up @@ -395,6 +331,10 @@ func (c *Config) Complete() completedConfig {
}
}
}
if c.DiscoveryAddresses == nil {
c.DiscoveryAddresses = DefaultDiscoveryAddresses{DefaultAddress: c.ExternalAddress}
}

return completedConfig{c}
}

Expand Down Expand Up @@ -430,14 +370,8 @@ func (c completedConfig) New() (*GenericAPIServer, error) {
return nil, fmt.Errorf("Genericapiserver.New() called with config.Serializer == nil")
}

discoveryAddresses := DefaultDiscoveryAddresses{DefaultAddress: c.ExternalAddress}
if c.ServiceClusterIPRange != nil {
discoveryAddresses.DiscoveryCIDRRules = append(discoveryAddresses.DiscoveryCIDRRules,
DiscoveryCIDRRule{IPRange: *c.ServiceClusterIPRange, Address: net.JoinHostPort(c.ServiceReadWriteIP.String(), strconv.Itoa(c.ServiceReadWritePort))})
}

s := &GenericAPIServer{
discoveryAddresses: discoveryAddresses,
discoveryAddresses: c.DiscoveryAddresses,
LoopbackClientConfig: c.LoopbackClientConfig,
legacyAPIGroupPrefixes: c.LegacyAPIGroupPrefixes,
admissionControl: c.AdmissionControl,
Expand All @@ -447,7 +381,6 @@ func (c completedConfig) New() (*GenericAPIServer, error) {
minRequestTimeout: time.Duration(c.MinRequestTimeout) * time.Second,
enableSwaggerSupport: c.EnableSwaggerSupport,

MasterCount: c.MasterCount,
SecureServingInfo: c.SecureServingInfo,
InsecureServingInfo: c.InsecureServingInfo,
ExternalAddress: c.ExternalAddress,
Expand All @@ -470,12 +403,11 @@ func (c completedConfig) New() (*GenericAPIServer, error) {
}

// MaybeGenerateServingCerts generates serving certificates if requested and needed.
func (c completedConfig) MaybeGenerateServingCerts() error {
func (c completedConfig) MaybeGenerateServingCerts(alternateIPs ...net.IP) error {
// It would be nice to set a fqdn subject alt name, but only the kubelets know, the apiserver is clueless
// alternateDNS = append(alternateDNS, "kubernetes.default.svc.CLUSTER.DNS.NAME")
if c.SecureServingInfo != nil && c.SecureServingInfo.ServerCert.Generate && !certutil.CanReadCertOrKey(c.SecureServingInfo.ServerCert.CertFile, c.SecureServingInfo.ServerCert.KeyFile) {
// TODO (cjcullen): Is ClusterIP the right address to sign a cert with?
alternateIPs := []net.IP{c.ServiceReadWriteIP}
alternateDNS := []string{"kubernetes.default.svc", "kubernetes.default", "kubernetes", "localhost"}

if cert, key, err := certutil.GenerateSelfSignedCertKey(c.PublicAddress.String(), alternateIPs, alternateDNS); err != nil {
Expand Down
5 changes: 0 additions & 5 deletions pkg/genericapiserver/genericapiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package genericapiserver
import (
"fmt"
"mime"
"net"
"net/http"
"sort"
"strings"
Expand Down Expand Up @@ -147,10 +146,6 @@ type GenericAPIServer struct {
healthzLock sync.Mutex
healthzChecks []healthz.HealthzChecker
healthzCreated bool

// See Config.$name for documentation of these flags:

MasterCount int
}

func init() {
Expand Down
17 changes: 9 additions & 8 deletions pkg/genericapiserver/genericapiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"testing"

"k8s.io/kubernetes/pkg/api"
Expand Down Expand Up @@ -376,20 +375,17 @@ func TestDiscoveryAtAPIS(t *testing.T) {
}

func TestGetServerAddressByClientCIDRs(t *testing.T) {
s, etcdserver, config, _ := newMaster(t)
defer etcdserver.Terminate(t)

publicAddressCIDRMap := []unversioned.ServerAddressByClientCIDR{
{
ClientCIDR: "0.0.0.0/0",
ServerAddress: config.ExternalAddress,
ServerAddress: "ExternalAddress",
},
}
internalAddressCIDRMap := []unversioned.ServerAddressByClientCIDR{
publicAddressCIDRMap[0],
{
ClientCIDR: config.ServiceClusterIPRange.String(),
ServerAddress: net.JoinHostPort(config.ServiceReadWriteIP.String(), strconv.Itoa(config.ServiceReadWritePort)),
ClientCIDR: "10.0.0.0/24",
ServerAddress: "serviceIP",
},
}
internalIP := "10.0.0.1"
Expand Down Expand Up @@ -455,8 +451,13 @@ func TestGetServerAddressByClientCIDRs(t *testing.T) {
},
}

_, ipRange, _ := net.ParseCIDR("10.0.0.0/24")
discoveryAddresses := DefaultDiscoveryAddresses{DefaultAddress: "ExternalAddress"}
discoveryAddresses.DiscoveryCIDRRules = append(discoveryAddresses.DiscoveryCIDRRules,
DiscoveryCIDRRule{IPRange: *ipRange, Address: "serviceIP"})

for i, test := range testCases {
if a, e := s.discoveryAddresses.ServerAddressByClientCIDRs(utilnet.GetClientIP(&test.Request)), test.ExpectedMap; reflect.DeepEqual(e, a) != true {
if a, e := discoveryAddresses.ServerAddressByClientCIDRs(utilnet.GetClientIP(&test.Request)), test.ExpectedMap; reflect.DeepEqual(e, a) != true {
t.Fatalf("test case %d failed. expected: %v, actual: %v", i+1, e, a)
}
}
Expand Down
54 changes: 54 additions & 0 deletions pkg/genericapiserver/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package genericapiserver

import (
"fmt"
"net"

"github.com/golang/glog"

"k8s.io/kubernetes/pkg/registry/core/service/ipallocator"
)

// DefaultServiceIPRange takes a the serviceIPRange flag and returns the defaulted service ip range (if needed),
// api server service IP, and an error
// TODO move this out of the genericapiserver package
func DefaultServiceIPRange(passedServiceClusterIPRange net.IPNet) (net.IPNet, net.IP, error) {
serviceClusterIPRange := passedServiceClusterIPRange
if passedServiceClusterIPRange.IP == nil {
defaultNet := "10.0.0.0/24"
glog.Infof("Network range for service cluster IPs is unspecified. Defaulting to %v.", defaultNet)
_, defaultServiceClusterIPRange, err := net.ParseCIDR(defaultNet)
if err != nil {
return net.IPNet{}, net.IP{}, err
}
serviceClusterIPRange = *defaultServiceClusterIPRange
}
if size := ipallocator.RangeSize(&serviceClusterIPRange); size < 8 {
return net.IPNet{}, net.IP{}, fmt.Errorf("The service cluster IP range must be at least %d IP addresses", 8)
}

// Select the first valid IP from ServiceClusterIPRange to use as the GenericAPIServer service IP.
apiServerServiceIP, err := ipallocator.GetIndexedIP(&serviceClusterIPRange, 1)
if err != nil {
return net.IPNet{}, net.IP{}, err
}
glog.V(4).Infof("Setting service IP to %q (read-write).", apiServerServiceIP)

return serviceClusterIPRange, apiServerServiceIP, nil
}
2 changes: 1 addition & 1 deletion pkg/master/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ go_library(
"//pkg/apis/storage/v1beta1:go_default_library",
"//pkg/client/clientset_generated/internalclientset/typed/core/internalversion:go_default_library",
"//pkg/genericapiserver:go_default_library",
"//pkg/genericapiserver/options:go_default_library",
"//pkg/healthz:go_default_library",
"//pkg/kubelet/client:go_default_library",
"//pkg/master/thirdparty:go_default_library",
Expand Down Expand Up @@ -116,7 +117,6 @@ go_test(
"//pkg/generated/openapi:go_default_library",
"//pkg/genericapiserver:go_default_library",
"//pkg/kubelet/client:go_default_library",
"//pkg/registry/core/service/ipallocator:go_default_library",
"//pkg/registry/registrytest:go_default_library",
"//pkg/runtime:go_default_library",
"//pkg/storage/etcd/testing:go_default_library",
Expand Down
Loading

0 comments on commit d82f98c

Please sign in to comment.