Skip to content

Commit

Permalink
Adding events to federation apiserver
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiljindal committed Aug 12, 2016
1 parent a4acf08 commit 1d76884
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 91 deletions.
7 changes: 3 additions & 4 deletions cmd/hyperkube/federation-apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package main

import (
"k8s.io/kubernetes/federation/cmd/federation-apiserver/app"
genericoptions "k8s.io/kubernetes/pkg/genericapiserver/options"
"k8s.io/kubernetes/federation/cmd/federation-apiserver/app/options"
)

// NewFederationAPIServer creates a new hyperkube Server object that includes the
// description and flags.
func NewFederationAPIServer() *Server {
s := genericoptions.NewServerRunOptions().WithEtcdOptions()
s := options.NewServerRunOptions()

hks := Server{
SimpleUsage: "federation-apiserver",
Expand All @@ -33,7 +33,6 @@ func NewFederationAPIServer() *Server {
return app.Run(s)
},
}
s.AddUniversalFlags(hks.Flags())
s.AddEtcdStorageFlags(hks.Flags())
s.AddFlags(hks.Flags())
return &hks
}
4 changes: 2 additions & 2 deletions federation/apis/core/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"k8s.io/kubernetes/pkg/util/sets"
)

const importPrefix = "k8s.io/kubernetes/federation/api"
const importPrefix = "k8s.io/kubernetes/pkg/api"

var accessor = meta.NewAccessor()

Expand Down Expand Up @@ -99,7 +99,7 @@ func newRESTMapper(externalVersions []unversioned.GroupVersion) meta.RESTMapper
"DeleteOptions",
"Status")

mapper := api.NewDefaultRESTMapper(externalVersions, interfacesFor, importPrefix, ignoredKinds, rootScoped)
mapper := api.NewDefaultRESTMapperFromScheme(externalVersions, interfacesFor, importPrefix, ignoredKinds, rootScoped, core.Scheme)
// setup aliases for groups of resources
mapper.AddResourceAlias("all", userResources...)

Expand Down
2 changes: 2 additions & 0 deletions federation/apis/core/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&api.DeleteOptions{},
&api.Secret{},
&api.SecretList{},
&api.Event{},
&api.EventList{},
)

// Register Unversioned types under their own special group
Expand Down
9 changes: 9 additions & 0 deletions federation/apis/core/v1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,14 @@ func addConversionFuncs(scheme *runtime.Scheme) error {
return err
}
}
if err := v1.AddFieldLabelConversionsForEvent(scheme); err != nil {
return nil
}
if err := v1.AddFieldLabelConversionsForNamespace(scheme); err != nil {
return nil
}
if err := v1.AddFieldLabelConversionsForSecret(scheme); err != nil {
return nil
}
return nil
}
2 changes: 2 additions & 0 deletions federation/apis/core/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&v1.DeleteOptions{},
&v1.Secret{},
&v1.SecretList{},
&v1.Event{},
&v1.EventList{},
)

// Add common types
Expand Down
7 changes: 3 additions & 4 deletions federation/cmd/federation-apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"time"

"k8s.io/kubernetes/federation/cmd/federation-apiserver/app"
genericoptions "k8s.io/kubernetes/pkg/genericapiserver/options"
"k8s.io/kubernetes/federation/cmd/federation-apiserver/app/options"
"k8s.io/kubernetes/pkg/util/flag"
"k8s.io/kubernetes/pkg/util/logs"
"k8s.io/kubernetes/pkg/version/verflag"
Expand All @@ -36,9 +36,8 @@ import (
func main() {
rand.Seed(time.Now().UTC().UnixNano())

s := genericoptions.NewServerRunOptions().WithEtcdOptions()
s.AddUniversalFlags(pflag.CommandLine)
s.AddEtcdStorageFlags(pflag.CommandLine)
s := options.NewServerRunOptions()
s.AddFlags(pflag.CommandLine)

flag.InitFlags()
logs.InitLogs()
Expand Down
14 changes: 8 additions & 6 deletions federation/cmd/federation-apiserver/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,32 @@ package app

import (
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/genericapiserver"
genericoptions "k8s.io/kubernetes/pkg/genericapiserver/options"

"k8s.io/kubernetes/federation/apis/core"
_ "k8s.io/kubernetes/federation/apis/core/install"
"k8s.io/kubernetes/federation/apis/core/v1"
"k8s.io/kubernetes/federation/cmd/federation-apiserver/app/options"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/genericapiserver"
eventetcd "k8s.io/kubernetes/pkg/registry/event/etcd"
namespaceetcd "k8s.io/kubernetes/pkg/registry/namespace/etcd"
secretetcd "k8s.io/kubernetes/pkg/registry/secret/etcd"
serviceetcd "k8s.io/kubernetes/pkg/registry/service/etcd"
)

func installCoreAPIs(s *genericoptions.ServerRunOptions, g *genericapiserver.GenericAPIServer, f genericapiserver.StorageFactory) {
func installCoreAPIs(s *options.ServerRunOptions, g *genericapiserver.GenericAPIServer, f genericapiserver.StorageFactory) {
serviceStore, serviceStatusStore := serviceetcd.NewREST(createRESTOptionsOrDie(s, g, f, api.Resource("service")))
namespaceStore, namespaceStatusStore, _ := namespaceetcd.NewREST(createRESTOptionsOrDie(s, g, f, api.Resource("namespaces")))
secretStore := secretetcd.NewREST(createRESTOptionsOrDie(s, g, f, api.Resource("secrets")))
eventStore := eventetcd.NewREST(createRESTOptionsOrDie(s, g, f, api.Resource("events")), uint64(s.EventTTL.Seconds()))
coreResources := map[string]rest.Storage{
"secrets": secretStore,
"services": serviceStore,
"services/status": serviceStatusStore,
"namespaces": namespaceStore,
"namespaces/status": namespaceStatusStore,
"events": eventStore,
}
coreGroupMeta := registered.GroupOrDie(core.GroupName)
apiGroupInfo := genericapiserver.APIGroupInfo{
Expand All @@ -56,6 +58,6 @@ func installCoreAPIs(s *genericoptions.ServerRunOptions, g *genericapiserver.Gen
NegotiatedSerializer: core.Codecs,
}
if err := g.InstallAPIGroup(&apiGroupInfo); err != nil {
glog.Fatalf("Error in registering group version: %v", err)
glog.Fatalf("Error in registering group version: %+v.\n Error: %v\n", apiGroupInfo, err)
}
}
9 changes: 4 additions & 5 deletions federation/cmd/federation-apiserver/app/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ package app

import (
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/genericapiserver"
genericoptions "k8s.io/kubernetes/pkg/genericapiserver/options"

"k8s.io/kubernetes/federation/cmd/federation-apiserver/app/options"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/apis/extensions"
_ "k8s.io/kubernetes/pkg/apis/extensions/install"
"k8s.io/kubernetes/pkg/genericapiserver"
ingressetcd "k8s.io/kubernetes/pkg/registry/ingress/etcd"
replicasetetcd "k8s.io/kubernetes/pkg/registry/replicaset/etcd"
)

func installExtensionsAPIs(s *genericoptions.ServerRunOptions, g *genericapiserver.GenericAPIServer, f genericapiserver.StorageFactory) {
func installExtensionsAPIs(s *options.ServerRunOptions, g *genericapiserver.GenericAPIServer, f genericapiserver.StorageFactory) {
replicaSetStorage := replicasetetcd.NewStorage(createRESTOptionsOrDie(s, g, f, extensions.Resource("replicasets")))
ingressStorage, ingressStatusStorage := ingressetcd.NewREST(createRESTOptionsOrDie(s, g, f, extensions.Resource("ingresses")))
extensionsResources := map[string]rest.Storage{
Expand Down
4 changes: 2 additions & 2 deletions federation/cmd/federation-apiserver/app/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import (
"github.com/golang/glog"

"k8s.io/kubernetes/federation/apis/federation"
"k8s.io/kubernetes/federation/cmd/federation-apiserver/app/options"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/genericapiserver"
genericoptions "k8s.io/kubernetes/pkg/genericapiserver/options"

_ "k8s.io/kubernetes/federation/apis/federation/install"
clusteretcd "k8s.io/kubernetes/federation/registry/cluster/etcd"
)

func installFederationAPIs(s *genericoptions.ServerRunOptions, g *genericapiserver.GenericAPIServer, f genericapiserver.StorageFactory) {
func installFederationAPIs(s *options.ServerRunOptions, g *genericapiserver.GenericAPIServer, f genericapiserver.StorageFactory) {
clusterStorage, clusterStatusStorage := clusteretcd.NewREST(createRESTOptionsOrDie(s, g, f, federation.Resource("clusters")))
federationResources := map[string]rest.Storage{
"clusters": clusterStorage,
Expand Down
52 changes: 52 additions & 0 deletions federation/cmd/federation-apiserver/app/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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 options contains flags and options for initializing federation-apiserver.
package options

import (
"time"

genericoptions "k8s.io/kubernetes/pkg/genericapiserver/options"

"github.com/spf13/pflag"
)

// Runtime options for the federation-apiserver.
type ServerRunOptions struct {
*genericoptions.ServerRunOptions
EventTTL time.Duration
}

// NewServerRunOptions creates a new ServerRunOptions object with default values.
func NewServerRunOptions() *ServerRunOptions {
s := ServerRunOptions{
ServerRunOptions: genericoptions.NewServerRunOptions().WithEtcdOptions(),
EventTTL: 1 * time.Hour,
}
return &s
}

// AddFlags adds flags for ServerRunOptions fields to be specified via FlagSet.
func (s *ServerRunOptions) AddFlags(fs *pflag.FlagSet) {
// Add the generic flags.
s.ServerRunOptions.AddUniversalFlags(fs)
//Add etcd specific flags.
s.ServerRunOptions.AddEtcdStorageFlags(fs)

fs.DurationVar(&s.EventTTL, "event-ttl", s.EventTTL,
"Amount of time to retain events. Default is 1h.")
}
20 changes: 9 additions & 11 deletions federation/cmd/federation-apiserver/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"k8s.io/kubernetes/federation/cmd/federation-apiserver/app/options"
"k8s.io/kubernetes/pkg/admission"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apiserver"
"k8s.io/kubernetes/pkg/apiserver/authenticator"
"k8s.io/kubernetes/pkg/controller/framework/informers"
"k8s.io/kubernetes/pkg/genericapiserver"
genericoptions "k8s.io/kubernetes/pkg/genericapiserver/options"
genericvalidation "k8s.io/kubernetes/pkg/genericapiserver/validation"
"k8s.io/kubernetes/pkg/registry/cachesize"
"k8s.io/kubernetes/pkg/registry/generic"
Expand All @@ -43,9 +43,8 @@ import (

// NewAPIServerCommand creates a *cobra.Command object with default parameters
func NewAPIServerCommand() *cobra.Command {
s := genericoptions.NewServerRunOptions().WithEtcdOptions()
s.AddUniversalFlags(pflag.CommandLine)
s.AddEtcdStorageFlags(pflag.CommandLine)
s := options.NewServerRunOptions()
s.AddFlags(pflag.CommandLine)
cmd := &cobra.Command{
Use: "federation-apiserver",
Long: `The Kubernetes federation API server validates and configures data
Expand All @@ -55,14 +54,13 @@ cluster's shared state through which all other components interact.`,
Run: func(cmd *cobra.Command, args []string) {
},
}

return cmd
}

// Run runs the specified APIServer. This should never exit.
func Run(s *genericoptions.ServerRunOptions) error {
genericvalidation.VerifyEtcdServersList(s)
genericapiserver.DefaultAndValidateRunOptions(s)
func Run(s *options.ServerRunOptions) error {
genericvalidation.VerifyEtcdServersList(s.ServerRunOptions)
genericapiserver.DefaultAndValidateRunOptions(s.ServerRunOptions)

// TODO: register cluster federation resources here.
resourceConfig := genericapiserver.NewResourceConfig()
Expand Down Expand Up @@ -132,7 +130,7 @@ func Run(s *genericoptions.ServerRunOptions) error {
if err != nil {
glog.Fatalf("Failed to initialize plugins: %v", err)
}
genericConfig := genericapiserver.NewConfig(s)
genericConfig := genericapiserver.NewConfig(s.ServerRunOptions)
// TODO: Move the following to generic api server as well.
genericConfig.StorageFactory = storageFactory
genericConfig.Authenticator = authenticator
Expand All @@ -159,11 +157,11 @@ func Run(s *genericoptions.ServerRunOptions) error {
installExtensionsAPIs(s, m, storageFactory)

sharedInformers.Start(wait.NeverStop)
m.Run(s)
m.Run(s.ServerRunOptions)
return nil
}

func createRESTOptionsOrDie(s *genericoptions.ServerRunOptions, g *genericapiserver.GenericAPIServer, f genericapiserver.StorageFactory, resource unversioned.GroupResource) generic.RESTOptions {
func createRESTOptionsOrDie(s *options.ServerRunOptions, g *genericapiserver.GenericAPIServer, f genericapiserver.StorageFactory, resource unversioned.GroupResource) generic.RESTOptions {
storage, err := f.New(resource)
if err != nil {
glog.Fatalf("Unable to find storage destination for %v, due to %v", resource, err.Error())
Expand Down
31 changes: 28 additions & 3 deletions federation/cmd/federation-apiserver/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import (
"github.com/stretchr/testify/assert"

fed_v1b1 "k8s.io/kubernetes/federation/apis/federation/v1beta1"
"k8s.io/kubernetes/federation/cmd/federation-apiserver/app/options"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
ext_v1b1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
"k8s.io/kubernetes/pkg/genericapiserver/options"
)

func TestLongRunningRequestRegexp(t *testing.T) {
regexp := regexp.MustCompile(options.NewServerRunOptions().WithEtcdOptions().LongRunningRequestRE)
regexp := regexp.MustCompile(options.NewServerRunOptions().LongRunningRequestRE)
dontMatch := []string{
"/api/v1/watch-namespace/",
"/api/v1/namespace-proxy/",
Expand Down Expand Up @@ -84,7 +84,7 @@ var groupVersions = []unversioned.GroupVersion{
}

func TestRun(t *testing.T) {
s := options.NewServerRunOptions().WithEtcdOptions()
s := options.NewServerRunOptions()
s.InsecurePort = insecurePort
_, ipNet, _ := net.ParseCIDR("10.10.10.0/24")
s.ServiceClusterIPRange = *ipNet
Expand Down Expand Up @@ -258,6 +258,8 @@ func testFederationResourceList(t *testing.T) {
}
assert.Equal(t, "v1", apiResourceList.APIVersion)
assert.Equal(t, fed_v1b1.SchemeGroupVersion.String(), apiResourceList.GroupVersion)
// Assert that there are exactly 2 resources.
assert.Equal(t, 2, len(apiResourceList.APIResources))

found := findResource(apiResourceList.APIResources, "clusters")
assert.NotNil(t, found)
Expand All @@ -280,13 +282,34 @@ func testCoreResourceList(t *testing.T) {
}
assert.Equal(t, "", apiResourceList.APIVersion)
assert.Equal(t, v1.SchemeGroupVersion.String(), apiResourceList.GroupVersion)
// Assert that there are exactly 6 resources.
assert.Equal(t, 6, len(apiResourceList.APIResources))

// Verify services.
found := findResource(apiResourceList.APIResources, "services")
assert.NotNil(t, found)
assert.True(t, found.Namespaced)
found = findResource(apiResourceList.APIResources, "services/status")
assert.NotNil(t, found)
assert.True(t, found.Namespaced)

// Verify namespaces.
found = findResource(apiResourceList.APIResources, "namespaces")
assert.NotNil(t, found)
assert.True(t, found.Namespaced)
found = findResource(apiResourceList.APIResources, "namespaces/status")
assert.NotNil(t, found)
assert.True(t, found.Namespaced)

// Verify events.
found = findResource(apiResourceList.APIResources, "events")
assert.NotNil(t, found)
assert.True(t, found.Namespaced)

// Verify secrets.
found = findResource(apiResourceList.APIResources, "secrets")
assert.NotNil(t, found)
assert.True(t, found.Namespaced)
}

func testExtensionsResourceList(t *testing.T) {
Expand All @@ -303,6 +326,8 @@ func testExtensionsResourceList(t *testing.T) {
// empty APIVersion for extensions group
assert.Equal(t, "", apiResourceList.APIVersion)
assert.Equal(t, ext_v1b1.SchemeGroupVersion.String(), apiResourceList.GroupVersion)
// Assert that there are exactly 5 resources.
assert.Equal(t, 5, len(apiResourceList.APIResources))

// Verify replicasets.
found := findResource(apiResourceList.APIResources, "replicasets")
Expand Down
Loading

0 comments on commit 1d76884

Please sign in to comment.