2016-05-16 02:18:18 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-05-08 21:44:45 +00:00
|
|
|
// Package certificates implements an abstract controller that is useful for
|
|
|
|
// building controllers that manage CSRs
|
2016-05-16 02:18:18 +00:00
|
|
|
package certificates
|
|
|
|
|
|
|
|
import (
|
2016-08-08 22:36:10 +00:00
|
|
|
"fmt"
|
2016-05-16 02:18:18 +00:00
|
|
|
"time"
|
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
certificates "k8s.io/api/certificates/v1beta1"
|
2017-02-07 18:21:08 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2017-01-11 14:09:48 +00:00
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2017-04-27 00:13:48 +00:00
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
2017-01-30 18:39:54 +00:00
|
|
|
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
2017-01-24 14:11:51 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2017-01-30 18:39:54 +00:00
|
|
|
"k8s.io/client-go/tools/record"
|
2017-01-27 15:20:40 +00:00
|
|
|
"k8s.io/client-go/util/workqueue"
|
2017-01-06 06:34:29 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
2017-02-08 21:18:21 +00:00
|
|
|
certificatesinformers "k8s.io/kubernetes/pkg/client/informers/informers_generated/externalversions/certificates/v1beta1"
|
2017-02-07 18:21:08 +00:00
|
|
|
certificateslisters "k8s.io/kubernetes/pkg/client/listers/certificates/v1beta1"
|
2016-05-16 02:18:18 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
2016-08-08 22:36:10 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2016-05-16 02:18:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CertificateController struct {
|
|
|
|
kubeClient clientset.Interface
|
|
|
|
|
2017-02-07 18:21:08 +00:00
|
|
|
csrLister certificateslisters.CertificateSigningRequestLister
|
|
|
|
csrsSynced cache.InformerSynced
|
2016-05-16 02:18:18 +00:00
|
|
|
|
2017-05-08 21:44:45 +00:00
|
|
|
handler func(*certificates.CertificateSigningRequest) error
|
2016-05-16 02:18:18 +00:00
|
|
|
|
2016-09-14 12:19:19 +00:00
|
|
|
queue workqueue.RateLimitingInterface
|
2016-05-16 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:44:45 +00:00
|
|
|
func NewCertificateController(
|
|
|
|
kubeClient clientset.Interface,
|
|
|
|
csrInformer certificatesinformers.CertificateSigningRequestInformer,
|
|
|
|
handler func(*certificates.CertificateSigningRequest) error,
|
|
|
|
) (*CertificateController, error) {
|
2016-05-16 02:18:18 +00:00
|
|
|
// Send events to the apiserver
|
|
|
|
eventBroadcaster := record.NewBroadcaster()
|
|
|
|
eventBroadcaster.StartLogging(glog.Infof)
|
2017-01-30 18:39:54 +00:00
|
|
|
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.Core().RESTClient()).Events("")})
|
2016-05-16 02:18:18 +00:00
|
|
|
|
|
|
|
cc := &CertificateController{
|
|
|
|
kubeClient: kubeClient,
|
2016-09-14 12:19:19 +00:00
|
|
|
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "certificate"),
|
2017-05-08 21:44:45 +00:00
|
|
|
handler: handler,
|
2016-05-16 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Manage the addition/update of certificate requests
|
2017-02-07 18:21:08 +00:00
|
|
|
csrInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: func(obj interface{}) {
|
|
|
|
csr := obj.(*certificates.CertificateSigningRequest)
|
|
|
|
glog.V(4).Infof("Adding certificate request %s", csr.Name)
|
|
|
|
cc.enqueueCertificateRequest(obj)
|
2016-05-16 02:18:18 +00:00
|
|
|
},
|
2017-02-07 18:21:08 +00:00
|
|
|
UpdateFunc: func(old, new interface{}) {
|
|
|
|
oldCSR := old.(*certificates.CertificateSigningRequest)
|
|
|
|
glog.V(4).Infof("Updating certificate request %s", oldCSR.Name)
|
|
|
|
cc.enqueueCertificateRequest(new)
|
|
|
|
},
|
|
|
|
DeleteFunc: func(obj interface{}) {
|
|
|
|
csr, ok := obj.(*certificates.CertificateSigningRequest)
|
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
|
|
|
glog.V(2).Infof("Couldn't get object from tombstone %#v", obj)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
csr, ok = tombstone.Obj.(*certificates.CertificateSigningRequest)
|
2016-09-18 20:48:46 +00:00
|
|
|
if !ok {
|
2017-02-07 18:21:08 +00:00
|
|
|
glog.V(2).Infof("Tombstone contained object that is not a CSR: %#v", obj)
|
|
|
|
return
|
2016-09-18 20:48:46 +00:00
|
|
|
}
|
2017-02-07 18:21:08 +00:00
|
|
|
}
|
|
|
|
glog.V(4).Infof("Deleting certificate request %s", csr.Name)
|
|
|
|
cc.enqueueCertificateRequest(obj)
|
2016-05-16 02:18:18 +00:00
|
|
|
},
|
2017-02-07 18:21:08 +00:00
|
|
|
})
|
|
|
|
cc.csrLister = csrInformer.Lister()
|
|
|
|
cc.csrsSynced = csrInformer.Informer().HasSynced
|
2017-05-08 21:44:45 +00:00
|
|
|
cc.handler = handler
|
2016-05-16 02:18:18 +00:00
|
|
|
return cc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the main goroutine responsible for watching and syncing jobs.
|
|
|
|
func (cc *CertificateController) Run(workers int, stopCh <-chan struct{}) {
|
|
|
|
defer utilruntime.HandleCrash()
|
2016-09-14 12:19:19 +00:00
|
|
|
defer cc.queue.ShutDown()
|
|
|
|
|
2017-04-12 19:49:17 +00:00
|
|
|
glog.Infof("Starting certificate controller")
|
|
|
|
defer glog.Infof("Shutting down certificate controller")
|
2017-02-07 18:21:08 +00:00
|
|
|
|
2017-04-12 19:49:17 +00:00
|
|
|
if !controller.WaitForCacheSync("certificate", stopCh, cc.csrsSynced) {
|
2017-02-07 18:21:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-16 02:18:18 +00:00
|
|
|
for i := 0; i < workers; i++ {
|
|
|
|
go wait.Until(cc.worker, time.Second, stopCh)
|
|
|
|
}
|
2017-04-12 19:49:17 +00:00
|
|
|
|
2016-05-16 02:18:18 +00:00
|
|
|
<-stopCh
|
|
|
|
}
|
|
|
|
|
|
|
|
// worker runs a thread that dequeues CSRs, handles them, and marks them done.
|
|
|
|
func (cc *CertificateController) worker() {
|
2016-09-14 12:19:19 +00:00
|
|
|
for cc.processNextWorkItem() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processNextWorkItem deals with one key off the queue. It returns false when it's time to quit.
|
|
|
|
func (cc *CertificateController) processNextWorkItem() bool {
|
|
|
|
cKey, quit := cc.queue.Get()
|
|
|
|
if quit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer cc.queue.Done(cKey)
|
|
|
|
|
2017-05-08 21:44:45 +00:00
|
|
|
if err := cc.syncFunc(cKey.(string)); err != nil {
|
|
|
|
cc.queue.AddRateLimited(cKey)
|
|
|
|
utilruntime.HandleError(fmt.Errorf("Sync %v failed with : %v", cKey, err))
|
2016-09-14 12:19:19 +00:00
|
|
|
return true
|
2016-05-16 02:18:18 +00:00
|
|
|
}
|
2016-09-14 12:19:19 +00:00
|
|
|
|
2017-05-08 21:44:45 +00:00
|
|
|
cc.queue.Forget(cKey)
|
2016-09-14 12:19:19 +00:00
|
|
|
return true
|
2017-05-08 21:44:45 +00:00
|
|
|
|
2016-05-16 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *CertificateController) enqueueCertificateRequest(obj interface{}) {
|
|
|
|
key, err := controller.KeyFunc(obj)
|
|
|
|
if err != nil {
|
2016-09-14 12:19:19 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %+v: %v", obj, err))
|
2016-05-16 02:18:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cc.queue.Add(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybeSignCertificate will inspect the certificate request and, if it has
|
|
|
|
// been approved and meets policy expectations, generate an X509 cert using the
|
|
|
|
// cluster CA assets. If successful it will update the CSR approve subresource
|
|
|
|
// with the signed certificate.
|
2017-05-08 21:44:45 +00:00
|
|
|
func (cc *CertificateController) syncFunc(key string) error {
|
2016-05-16 02:18:18 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
glog.V(4).Infof("Finished syncing certificate request %q (%v)", key, time.Now().Sub(startTime))
|
|
|
|
}()
|
2017-02-07 18:21:08 +00:00
|
|
|
csr, err := cc.csrLister.Get(key)
|
|
|
|
if errors.IsNotFound(err) {
|
|
|
|
glog.V(3).Infof("csr has been deleted: %v", key)
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-16 02:18:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-07 18:21:08 +00:00
|
|
|
|
|
|
|
if csr.Status.Certificate != nil {
|
|
|
|
// no need to do anything because it already has a cert
|
2016-05-16 02:18:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-07 18:21:08 +00:00
|
|
|
|
|
|
|
// need to operate on a copy so we don't mutate the csr in the shared cache
|
2017-04-27 00:13:48 +00:00
|
|
|
copy, err := scheme.Scheme.DeepCopy(csr)
|
2017-02-07 18:21:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
csr = copy.(*certificates.CertificateSigningRequest)
|
2016-05-16 02:18:18 +00:00
|
|
|
|
2017-05-08 21:44:45 +00:00
|
|
|
return cc.handler(csr)
|
2016-05-16 02:18:18 +00:00
|
|
|
}
|