-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use service accounts as clients for controllers
- Loading branch information
Showing
2 changed files
with
204 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
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 app implements a server that runs a set of active | ||
// components. This includes replication controllers, service endpoints and | ||
// nodes. | ||
// | ||
// CAUTION: If you update code in this file, you may need to also update code | ||
// in contrib/mesos/pkg/controllermanager/controllermanager.go | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"k8s.io/kubernetes/pkg/api" | ||
apierrors "k8s.io/kubernetes/pkg/api/errors" | ||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" | ||
unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned" | ||
"k8s.io/kubernetes/pkg/client/restclient" | ||
"k8s.io/kubernetes/pkg/fields" | ||
"k8s.io/kubernetes/pkg/watch" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
// ControllerClientBuilder allow syou to get clients and configs for controllers | ||
type ControllerClientBuilder interface { | ||
Config(name string) (*restclient.Config, error) | ||
Client(name string) (clientset.Interface, error) | ||
ClientOrDie(name string) clientset.Interface | ||
} | ||
|
||
// SimpleControllerClientBuilder returns a fixed client with different user agents | ||
type SimpleControllerClientBuilder struct { | ||
// ClientConfig is a skeleton config to clone and use as the basis for each controller client | ||
ClientConfig *restclient.Config | ||
} | ||
|
||
func (b SimpleControllerClientBuilder) Config(name string) (*restclient.Config, error) { | ||
clientConfig := *b.ClientConfig | ||
return &clientConfig, nil | ||
} | ||
|
||
func (b SimpleControllerClientBuilder) Client(name string) (clientset.Interface, error) { | ||
clientConfig, err := b.Config(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return clientset.NewForConfig(restclient.AddUserAgent(clientConfig, name)) | ||
} | ||
|
||
func (b SimpleControllerClientBuilder) ClientOrDie(name string) clientset.Interface { | ||
client, err := b.Client(name) | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
return client | ||
} | ||
|
||
// SAControllerClientBuilder is a ControllerClientBuilder that returns clients identifying as | ||
// service accounts | ||
type SAControllerClientBuilder struct { | ||
// ClientConfig is a skeleton config to clone and use as the basis for each controller client | ||
ClientConfig *restclient.Config | ||
|
||
// CoreClient is used to provision service accounts if needed and watch for their associated tokens | ||
// to construct a controller client | ||
CoreClient unversionedcore.CoreInterface | ||
|
||
// Namespace is the namespace used to host the service accounts that will back the | ||
// controllers. It must be highly privileged namespace which normal users cannot inspect. | ||
Namespace string | ||
} | ||
|
||
// config returns a complete clientConfig for constructing clients. This is separate in anticipation of composition | ||
// which means that not all clientsets are known here | ||
func (b SAControllerClientBuilder) Config(name string) (*restclient.Config, error) { | ||
clientConfig := *b.ClientConfig | ||
|
||
// we need the SA UID to find a matching SA token | ||
sa, err := b.CoreClient.ServiceAccounts(b.Namespace).Get(name) | ||
if err != nil && !apierrors.IsNotFound(err) { | ||
return nil, err | ||
} else if apierrors.IsNotFound(err) { | ||
sa, err = b.CoreClient.ServiceAccounts(b.Namespace).Create( | ||
&api.ServiceAccount{ObjectMeta: api.ObjectMeta{Namespace: b.Namespace, Name: name}}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
watcher, err := b.CoreClient.Secrets(b.Namespace).Watch( | ||
api.ListOptions{ | ||
ResourceVersion: "0", | ||
FieldSelector: fields.SelectorFromSet(map[string]string{api.SecretTypeField: string(api.SecretTypeServiceAccountToken)}), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = watch.Until(30*time.Second, watcher, | ||
func(event watch.Event) (bool, error) { | ||
switch event.Type { | ||
case watch.Deleted: | ||
return false, nil | ||
case watch.Error: | ||
return false, fmt.Errorf("error watching") | ||
case watch.Added, watch.Modified: | ||
secret := event.Object.(*api.Secret) | ||
if secret.Annotations[api.ServiceAccountNameKey] != sa.Name || | ||
secret.Annotations[api.ServiceAccountUIDKey] != string(sa.UID) || | ||
len(secret.Data[api.ServiceAccountTokenKey]) == 0 { | ||
return false, nil | ||
} | ||
clientConfig.BearerToken = string(secret.Data[api.ServiceAccountTokenKey]) | ||
return true, nil | ||
|
||
default: | ||
return false, fmt.Errorf("unexpected event type: %v", event.Type) | ||
} | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get token for service account: %v", err) | ||
} | ||
|
||
return &clientConfig, nil | ||
} | ||
|
||
func (b SAControllerClientBuilder) Client(name string) (clientset.Interface, error) { | ||
clientConfig, err := b.Config(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return clientset.NewForConfig(clientConfig) | ||
} | ||
|
||
func (b SAControllerClientBuilder) ClientOrDie(name string) clientset.Interface { | ||
client, err := b.Client(name) | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
return client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters