2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-09-08 23:36:49 +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"
|
|
|
|
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
|
|
|
|
|
2020-09-17 17:56:54 +00:00
|
|
|
pinnipedcontroller "github.com/vmware-tanzu/pinniped/internal/controller"
|
|
|
|
"github.com/vmware-tanzu/pinniped/internal/controllerlib"
|
2020-09-08 23:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type apiServiceUpdaterController struct {
|
|
|
|
namespace string
|
|
|
|
aggregatorClient aggregatorclient.Interface
|
|
|
|
secretInformer corev1informers.SecretInformer
|
2020-09-16 19:59:23 +00:00
|
|
|
apiServiceName string
|
2020-09-08 23:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAPIServiceUpdaterController(
|
|
|
|
namespace string,
|
2020-09-16 19:59:23 +00:00
|
|
|
apiServiceName string,
|
2020-09-08 23:36:49 +00:00
|
|
|
aggregatorClient aggregatorclient.Interface,
|
|
|
|
secretInformer corev1informers.SecretInformer,
|
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
) controllerlib.Controller {
|
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
|
|
|
Name: "certs-manager-controller",
|
|
|
|
Syncer: &apiServiceUpdaterController{
|
|
|
|
namespace: namespace,
|
|
|
|
aggregatorClient: aggregatorClient,
|
|
|
|
secretInformer: secretInformer,
|
2020-09-16 19:59:23 +00:00
|
|
|
apiServiceName: apiServiceName,
|
2020-09-08 23:36:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
withInformer(
|
|
|
|
secretInformer,
|
|
|
|
pinnipedcontroller.NameAndNamespaceExactMatchFilterFactory(certsSecretName, namespace),
|
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *apiServiceUpdaterController) Sync(ctx controllerlib.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 {
|
|
|
|
// The secret does not exist yet, so nothing to do.
|
|
|
|
klog.Info("apiServiceUpdaterController Sync found that the secret does not exist yet or was deleted")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the APIService to give it the new CA bundle.
|
2020-09-16 19:59:23 +00:00
|
|
|
if err := UpdateAPIService(ctx.Context, c.aggregatorClient, c.apiServiceName, certSecret.Data[caCertificateSecretKey]); err != nil {
|
2020-09-08 23:36:49 +00:00
|
|
|
return fmt.Errorf("could not update the API service: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
klog.Info("apiServiceUpdaterController Sync successfully updated API service")
|
|
|
|
return nil
|
|
|
|
}
|