2015-04-21 14:34:21 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
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 serviceaccount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-11-03 05:35:19 +00:00
|
|
|
apierrs "k8s.io/kubernetes/pkg/api/errors"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
2015-11-17 09:59:13 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-09-03 21:40:58 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/cache"
|
2015-09-03 21:43:19 +00:00
|
|
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller/framework"
|
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-09-09 17:45:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/watch"
|
2015-04-21 14:34:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// nameIndexFunc is an index function that indexes based on an object's name
|
2015-07-28 14:01:05 +00:00
|
|
|
func nameIndexFunc(obj interface{}) ([]string, error) {
|
2015-04-21 14:34:21 +00:00
|
|
|
meta, err := meta.Accessor(obj)
|
|
|
|
if err != nil {
|
2015-07-28 14:01:05 +00:00
|
|
|
return []string{""}, fmt.Errorf("object has no meta: %v", err)
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
2015-07-28 14:01:05 +00:00
|
|
|
return []string{meta.Name()}, nil
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
// ServiceAccountsControllerOptions contains options for running a ServiceAccountsController
|
|
|
|
type ServiceAccountsControllerOptions struct {
|
|
|
|
// Names is the set of service account names to ensure exist in every namespace
|
2015-09-09 17:45:01 +00:00
|
|
|
Names sets.String
|
2015-05-14 17:07:31 +00:00
|
|
|
|
|
|
|
// ServiceAccountResync is the interval between full resyncs of ServiceAccounts.
|
|
|
|
// If non-zero, all service accounts will be re-listed this often.
|
|
|
|
// Otherwise, re-list will be delayed as long as possible (until the watch is closed or times out).
|
2015-04-21 14:34:21 +00:00
|
|
|
ServiceAccountResync time.Duration
|
2015-05-14 17:07:31 +00:00
|
|
|
|
|
|
|
// NamespaceResync is the interval between full resyncs of Namespaces.
|
|
|
|
// If non-zero, all namespaces will be re-listed this often.
|
|
|
|
// Otherwise, re-list will be delayed as long as possible (until the watch is closed or times out).
|
|
|
|
NamespaceResync time.Duration
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
func DefaultServiceAccountsControllerOptions() ServiceAccountsControllerOptions {
|
2015-09-09 17:45:01 +00:00
|
|
|
return ServiceAccountsControllerOptions{Names: sets.NewString("default")}
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServiceAccountsController returns a new *ServiceAccountsController.
|
2015-05-14 17:07:31 +00:00
|
|
|
func NewServiceAccountsController(cl client.Interface, options ServiceAccountsControllerOptions) *ServiceAccountsController {
|
2015-04-21 14:34:21 +00:00
|
|
|
e := &ServiceAccountsController{
|
|
|
|
client: cl,
|
2015-05-14 17:07:31 +00:00
|
|
|
names: options.Names,
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
accountSelector := fields.Everything()
|
|
|
|
if len(options.Names) == 1 {
|
|
|
|
// If we're maintaining a single account, we can scope the accounts we watch to just that name
|
|
|
|
accountSelector = fields.SelectorFromSet(map[string]string{client.ObjectNameField: options.Names.List()[0]})
|
|
|
|
}
|
2015-04-21 14:34:21 +00:00
|
|
|
e.serviceAccounts, e.serviceAccountController = framework.NewIndexerInformer(
|
|
|
|
&cache.ListWatch{
|
|
|
|
ListFunc: func() (runtime.Object, error) {
|
2015-12-02 11:12:57 +00:00
|
|
|
options := unversioned.ListOptions{FieldSelector: unversioned.FieldSelector{accountSelector}}
|
|
|
|
return e.client.ServiceAccounts(api.NamespaceAll).List(options)
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
2015-11-17 09:59:13 +00:00
|
|
|
WatchFunc: func(options unversioned.ListOptions) (watch.Interface, error) {
|
2015-11-26 15:27:45 +00:00
|
|
|
options.FieldSelector.Selector = accountSelector
|
|
|
|
return e.client.ServiceAccounts(api.NamespaceAll).Watch(options)
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&api.ServiceAccount{},
|
|
|
|
options.ServiceAccountResync,
|
|
|
|
framework.ResourceEventHandlerFuncs{
|
|
|
|
DeleteFunc: e.serviceAccountDeleted,
|
|
|
|
},
|
|
|
|
cache.Indexers{"namespace": cache.MetaNamespaceIndexFunc},
|
|
|
|
)
|
|
|
|
|
|
|
|
e.namespaces, e.namespaceController = framework.NewIndexerInformer(
|
|
|
|
&cache.ListWatch{
|
|
|
|
ListFunc: func() (runtime.Object, error) {
|
2015-12-02 11:12:57 +00:00
|
|
|
return e.client.Namespaces().List(unversioned.ListOptions{})
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
2015-11-17 09:59:13 +00:00
|
|
|
WatchFunc: func(options unversioned.ListOptions) (watch.Interface, error) {
|
2015-11-26 15:27:45 +00:00
|
|
|
return e.client.Namespaces().Watch(options)
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&api.Namespace{},
|
|
|
|
options.NamespaceResync,
|
|
|
|
framework.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: e.namespaceAdded,
|
|
|
|
UpdateFunc: e.namespaceUpdated,
|
|
|
|
},
|
|
|
|
cache.Indexers{"name": nameIndexFunc},
|
|
|
|
)
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceAccountsController manages ServiceAccount objects inside Namespaces
|
|
|
|
type ServiceAccountsController struct {
|
|
|
|
stopChan chan struct{}
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
client client.Interface
|
2015-09-09 17:45:01 +00:00
|
|
|
names sets.String
|
2015-04-21 14:34:21 +00:00
|
|
|
|
|
|
|
serviceAccounts cache.Indexer
|
|
|
|
namespaces cache.Indexer
|
|
|
|
|
|
|
|
// Since we join two objects, we'll watch both of them with controllers.
|
|
|
|
serviceAccountController *framework.Controller
|
|
|
|
namespaceController *framework.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs controller loops and returns immediately
|
|
|
|
func (e *ServiceAccountsController) Run() {
|
|
|
|
if e.stopChan == nil {
|
|
|
|
e.stopChan = make(chan struct{})
|
|
|
|
go e.serviceAccountController.Run(e.stopChan)
|
|
|
|
go e.namespaceController.Run(e.stopChan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop gracefully shuts down this controller
|
|
|
|
func (e *ServiceAccountsController) Stop() {
|
|
|
|
if e.stopChan != nil {
|
|
|
|
close(e.stopChan)
|
|
|
|
e.stopChan = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serviceAccountDeleted reacts to a ServiceAccount deletion by recreating a default ServiceAccount in the namespace if needed
|
|
|
|
func (e *ServiceAccountsController) serviceAccountDeleted(obj interface{}) {
|
|
|
|
serviceAccount, ok := obj.(*api.ServiceAccount)
|
|
|
|
if !ok {
|
|
|
|
// Unknown type. If we missed a ServiceAccount deletion, the
|
|
|
|
// corresponding secrets will be cleaned up during the Secret re-list
|
|
|
|
return
|
|
|
|
}
|
2015-05-14 17:07:31 +00:00
|
|
|
// If the deleted service account is one we're maintaining, recreate it
|
|
|
|
if e.names.Has(serviceAccount.Name) {
|
|
|
|
e.createServiceAccountIfNeeded(serviceAccount.Name, serviceAccount.Namespace)
|
|
|
|
}
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// namespaceAdded reacts to a Namespace creation by creating a default ServiceAccount object
|
|
|
|
func (e *ServiceAccountsController) namespaceAdded(obj interface{}) {
|
|
|
|
namespace := obj.(*api.Namespace)
|
2015-05-14 17:07:31 +00:00
|
|
|
for _, name := range e.names.List() {
|
|
|
|
e.createServiceAccountIfNeeded(name, namespace.Name)
|
|
|
|
}
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// namespaceUpdated reacts to a Namespace update (or re-list) by creating a default ServiceAccount in the namespace if needed
|
|
|
|
func (e *ServiceAccountsController) namespaceUpdated(oldObj interface{}, newObj interface{}) {
|
|
|
|
newNamespace := newObj.(*api.Namespace)
|
2015-05-14 17:07:31 +00:00
|
|
|
for _, name := range e.names.List() {
|
|
|
|
e.createServiceAccountIfNeeded(name, newNamespace.Name)
|
|
|
|
}
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
// createServiceAccountIfNeeded creates a ServiceAccount with the given name in the given namespace if:
|
|
|
|
// * the named ServiceAccount does not already exist
|
2015-04-21 14:34:21 +00:00
|
|
|
// * the specified namespace exists
|
|
|
|
// * the specified namespace is in the ACTIVE phase
|
2015-05-14 17:07:31 +00:00
|
|
|
func (e *ServiceAccountsController) createServiceAccountIfNeeded(name, namespace string) {
|
|
|
|
serviceAccount, err := e.getServiceAccount(name, namespace)
|
2015-04-21 14:34:21 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if serviceAccount != nil {
|
|
|
|
// If service account already exists, it doesn't need to be created
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaceObj, err := e.getNamespace(namespace)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if namespaceObj == nil {
|
|
|
|
// If namespace does not exist, no service account is needed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if namespaceObj.Status.Phase != api.NamespaceActive {
|
|
|
|
// If namespace is not active, we shouldn't try to create anything
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
e.createServiceAccount(name, namespace)
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 05:35:19 +00:00
|
|
|
// createServiceAccount creates a ServiceAccount with the specified name and namespace
|
2015-05-14 17:07:31 +00:00
|
|
|
func (e *ServiceAccountsController) createServiceAccount(name, namespace string) {
|
2015-04-21 14:34:21 +00:00
|
|
|
serviceAccount := &api.ServiceAccount{}
|
2015-05-14 17:07:31 +00:00
|
|
|
serviceAccount.Name = name
|
2015-04-21 14:34:21 +00:00
|
|
|
serviceAccount.Namespace = namespace
|
2015-11-03 05:35:19 +00:00
|
|
|
_, err := e.client.ServiceAccounts(namespace).Create(serviceAccount)
|
|
|
|
if err != nil && !apierrs.IsAlreadyExists(err) {
|
2015-04-21 14:34:21 +00:00
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 05:35:19 +00:00
|
|
|
// getServiceAccount returns the ServiceAccount with the given name for the given namespace
|
2015-05-14 17:07:31 +00:00
|
|
|
func (e *ServiceAccountsController) getServiceAccount(name, namespace string) (*api.ServiceAccount, error) {
|
2015-04-21 14:34:21 +00:00
|
|
|
key := &api.ServiceAccount{ObjectMeta: api.ObjectMeta{Namespace: namespace}}
|
|
|
|
accounts, err := e.serviceAccounts.Index("namespace", key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, obj := range accounts {
|
|
|
|
serviceAccount := obj.(*api.ServiceAccount)
|
2015-05-14 17:07:31 +00:00
|
|
|
if name == serviceAccount.Name {
|
2015-04-21 14:34:21 +00:00
|
|
|
return serviceAccount, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNamespace returns the Namespace with the given name
|
|
|
|
func (e *ServiceAccountsController) getNamespace(name string) (*api.Namespace, error) {
|
|
|
|
key := &api.Namespace{ObjectMeta: api.ObjectMeta{Name: name}}
|
|
|
|
namespaces, err := e.namespaces.Index("name", key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if len(namespaces) == 1 {
|
|
|
|
return namespaces[0].(*api.Namespace), nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("%d namespaces with the name %s indexed", len(namespaces), name)
|
|
|
|
}
|