2020-08-20 17:54:15 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-08-21 16:55:44 +00:00
|
|
|
package issuerconfig
|
2020-08-20 17:54:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-08-24 19:30:45 +00:00
|
|
|
crdpinnipedv1alpha1 "github.com/suzerain-io/pinniped/generated/1.19/apis/crdpinniped/v1alpha1"
|
|
|
|
pinnipedclientset "github.com/suzerain-io/pinniped/generated/1.19/client/clientset/versioned"
|
|
|
|
crdpinnipedv1alpha1informers "github.com/suzerain-io/pinniped/generated/1.19/client/informers/externalversions/crdpinniped/v1alpha1"
|
2020-08-20 17:54:15 +00:00
|
|
|
pinnipedcontroller "github.com/suzerain-io/pinniped/internal/controller"
|
2020-08-28 15:59:09 +00:00
|
|
|
"github.com/suzerain-io/pinniped/internal/controllerlib"
|
2020-08-20 17:54:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ClusterInfoNamespace = "kube-public"
|
|
|
|
|
2020-09-15 02:07:18 +00:00
|
|
|
ConfigName = "pinniped-config"
|
|
|
|
|
2020-08-20 17:54:15 +00:00
|
|
|
clusterInfoName = "cluster-info"
|
|
|
|
clusterInfoConfigMapKey = "kubeconfig"
|
|
|
|
)
|
|
|
|
|
|
|
|
type publisherController struct {
|
2020-08-21 16:55:44 +00:00
|
|
|
namespace string
|
|
|
|
serverOverride *string
|
|
|
|
pinnipedClient pinnipedclientset.Interface
|
|
|
|
configMapInformer corev1informers.ConfigMapInformer
|
|
|
|
credentialIssuerConfigInformer crdpinnipedv1alpha1informers.CredentialIssuerConfigInformer
|
2020-08-20 17:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPublisherController(
|
|
|
|
namespace string,
|
|
|
|
serverOverride *string,
|
|
|
|
pinnipedClient pinnipedclientset.Interface,
|
|
|
|
configMapInformer corev1informers.ConfigMapInformer,
|
2020-08-21 16:55:44 +00:00
|
|
|
credentialIssuerConfigInformer crdpinnipedv1alpha1informers.CredentialIssuerConfigInformer,
|
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-20 17:54:15 +00:00
|
|
|
Name: "publisher-controller",
|
|
|
|
Syncer: &publisherController{
|
2020-08-21 16:55:44 +00:00
|
|
|
namespace: namespace,
|
|
|
|
serverOverride: serverOverride,
|
|
|
|
pinnipedClient: pinnipedClient,
|
|
|
|
configMapInformer: configMapInformer,
|
|
|
|
credentialIssuerConfigInformer: credentialIssuerConfigInformer,
|
2020-08-20 17:54:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
withInformer(
|
|
|
|
configMapInformer,
|
|
|
|
pinnipedcontroller.NameAndNamespaceExactMatchFilterFactory(clusterInfoName, ClusterInfoNamespace),
|
2020-08-28 15:59:09 +00:00
|
|
|
controllerlib.InformerOption{},
|
2020-08-20 17:54:15 +00:00
|
|
|
),
|
|
|
|
withInformer(
|
2020-08-21 16:55:44 +00:00
|
|
|
credentialIssuerConfigInformer,
|
2020-09-15 02:07:18 +00:00
|
|
|
pinnipedcontroller.NameAndNamespaceExactMatchFilterFactory(ConfigName, namespace),
|
2020-08-28 15:59:09 +00:00
|
|
|
controllerlib.InformerOption{},
|
2020-08-20 17:54:15 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:59:09 +00:00
|
|
|
func (c *publisherController) Sync(ctx controllerlib.Context) error {
|
2020-08-20 17:54:15 +00:00
|
|
|
configMap, err := c.configMapInformer.
|
|
|
|
Lister().
|
|
|
|
ConfigMaps(ClusterInfoNamespace).
|
|
|
|
Get(clusterInfoName)
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if err != nil && !notFound {
|
|
|
|
return fmt.Errorf("failed to get %s configmap: %w", clusterInfoName, err)
|
|
|
|
}
|
|
|
|
if notFound {
|
|
|
|
klog.InfoS(
|
|
|
|
"could not find config map",
|
|
|
|
"configmap",
|
|
|
|
klog.KRef(ClusterInfoNamespace, clusterInfoName),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeConfig, kubeConfigPresent := configMap.Data[clusterInfoConfigMapKey]
|
|
|
|
if !kubeConfigPresent {
|
|
|
|
klog.InfoS("could not find kubeconfig configmap key")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-27 14:43:13 +00:00
|
|
|
config, err := clientcmd.Load([]byte(kubeConfig))
|
|
|
|
if err != nil {
|
|
|
|
klog.InfoS("could not load kubeconfig configmap key")
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-20 17:54:15 +00:00
|
|
|
|
|
|
|
var certificateAuthorityData, server string
|
|
|
|
for _, v := range config.Clusters {
|
|
|
|
certificateAuthorityData = base64.StdEncoding.EncodeToString(v.CertificateAuthorityData)
|
|
|
|
server = v.Server
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.serverOverride != nil {
|
|
|
|
server = *c.serverOverride
|
|
|
|
}
|
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
existingCredentialIssuerConfigFromInformerCache, err := c.credentialIssuerConfigInformer.
|
|
|
|
Lister().
|
|
|
|
CredentialIssuerConfigs(c.namespace).
|
2020-09-15 02:07:18 +00:00
|
|
|
Get(ConfigName)
|
2020-08-25 01:07:34 +00:00
|
|
|
notFound = k8serrors.IsNotFound(err)
|
|
|
|
if err != nil && !notFound {
|
|
|
|
return fmt.Errorf("could not get credentialissuerconfig: %w", err)
|
2020-08-20 17:54:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
updateServerAndCAFunc := func(c *crdpinnipedv1alpha1.CredentialIssuerConfig) {
|
|
|
|
c.Status.KubeConfigInfo = &crdpinnipedv1alpha1.CredentialIssuerConfigKubeConfigInfo{
|
|
|
|
Server: server,
|
|
|
|
CertificateAuthorityData: certificateAuthorityData,
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 00:11:10 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
err = createOrUpdateCredentialIssuerConfig(
|
|
|
|
ctx.Context,
|
|
|
|
existingCredentialIssuerConfigFromInformerCache,
|
|
|
|
notFound,
|
2020-09-15 02:07:18 +00:00
|
|
|
ConfigName,
|
2020-08-25 01:07:34 +00:00
|
|
|
c.namespace,
|
|
|
|
c.pinnipedClient,
|
|
|
|
updateServerAndCAFunc)
|
2020-08-28 00:11:10 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create or update credentialissuerconfig: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-08-20 17:54:15 +00:00
|
|
|
}
|