2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-09 17:04:05 +00:00
|
|
|
|
|
|
|
package apicerts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-09-18 19:56:24 +00:00
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
|
|
|
"go.pinniped.dev/internal/provider"
|
2020-08-09 17:04:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type certsObserverController struct {
|
|
|
|
namespace string
|
2020-08-11 01:53:53 +00:00
|
|
|
dynamicCertProvider provider.DynamicTLSServingCertProvider
|
2020-08-09 17:04:05 +00:00
|
|
|
secretInformer corev1informers.SecretInformer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCertsObserverController(
|
|
|
|
namespace string,
|
2020-08-11 01:53:53 +00:00
|
|
|
dynamicCertProvider provider.DynamicTLSServingCertProvider,
|
2020-08-09 17:04:05 +00:00
|
|
|
secretInformer corev1informers.SecretInformer,
|
2020-08-20 17:54:15 +00:00
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
2020-08-28 15:59:09 +00:00
|
|
|
) controllerlib.Controller {
|
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
2020-08-09 17:04:05 +00:00
|
|
|
Name: "certs-observer-controller",
|
|
|
|
Syncer: &certsObserverController{
|
|
|
|
namespace: namespace,
|
|
|
|
dynamicCertProvider: dynamicCertProvider,
|
|
|
|
secretInformer: secretInformer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
withInformer(
|
|
|
|
secretInformer,
|
2020-08-20 17:54:15 +00:00
|
|
|
pinnipedcontroller.NameAndNamespaceExactMatchFilterFactory(certsSecretName, namespace),
|
2020-08-28 15:59:09 +00:00
|
|
|
controllerlib.InformerOption{},
|
2020-08-09 17:04:05 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:59:09 +00:00
|
|
|
func (c *certsObserverController) Sync(_ controllerlib.Context) error {
|
2020-08-09 17:04:05 +00:00
|
|
|
// Try to get the secret from the informer cache.
|
|
|
|
certSecret, err := c.secretInformer.Lister().Secrets(c.namespace).Get(certsSecretName)
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if err != nil && !notFound {
|
|
|
|
return fmt.Errorf("failed to get %s/%s secret: %w", c.namespace, certsSecretName, err)
|
|
|
|
}
|
|
|
|
if notFound {
|
2020-09-08 23:36:49 +00:00
|
|
|
klog.Info("certsObserverController Sync found that the secret does not exist yet or was deleted")
|
2020-08-09 17:04:05 +00:00
|
|
|
// The secret does not exist yet or was deleted.
|
2020-08-11 01:53:53 +00:00
|
|
|
c.dynamicCertProvider.Set(nil, nil)
|
2020-08-09 17:04:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutate the in-memory cert provider to update with the latest cert values.
|
2020-08-11 01:53:53 +00:00
|
|
|
c.dynamicCertProvider.Set(certSecret.Data[tlsCertificateChainSecretKey], certSecret.Data[tlsPrivateKeySecretKey])
|
2020-08-09 17:04:05 +00:00
|
|
|
klog.Info("certsObserverController Sync updated certs in the dynamic cert provider")
|
|
|
|
return nil
|
|
|
|
}
|