2020-08-09 17:04:05 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
package apicerts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
|
|
|
"github.com/suzerain-io/controller-go"
|
2020-08-20 17:54:15 +00:00
|
|
|
pinnipedcontroller "github.com/suzerain-io/pinniped/internal/controller"
|
|
|
|
"github.com/suzerain-io/pinniped/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-09 17:04:05 +00:00
|
|
|
) controller.Controller {
|
|
|
|
return controller.New(
|
|
|
|
controller.Config{
|
|
|
|
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-09 17:04:05 +00:00
|
|
|
controller.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *certsObserverController) Sync(_ controller.Context) error {
|
|
|
|
// 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 {
|
|
|
|
klog.Info("certsObserverController Sync() found that the secret does not exist yet or was deleted")
|
|
|
|
// 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
|
|
|
|
}
|