2021-01-07 22:58:09 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-09-16 14:19:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-20 17:54:15 +00:00
|
|
|
|
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"
|
|
|
|
|
2021-01-07 22:58:09 +00:00
|
|
|
configv1alpha1 "go.pinniped.dev/generated/1.20/apis/concierge/config/v1alpha1"
|
|
|
|
pinnipedclientset "go.pinniped.dev/generated/1.20/client/concierge/clientset/versioned"
|
2020-09-18 19:56:24 +00:00
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2020-11-10 15:22:16 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
2020-08-20 17:54:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
ClusterInfoNamespace = "kube-public"
|
2020-08-20 17:54:15 +00:00
|
|
|
clusterInfoName = "cluster-info"
|
|
|
|
clusterInfoConfigMapKey = "kubeconfig"
|
|
|
|
)
|
|
|
|
|
2020-09-23 11:58:01 +00:00
|
|
|
type kubeConigInfoPublisherController struct {
|
2020-11-02 21:39:43 +00:00
|
|
|
credentialIssuerNamespaceName string
|
|
|
|
credentialIssuerResourceName string
|
|
|
|
credentialIssuerLabels map[string]string
|
|
|
|
serverOverride *string
|
|
|
|
pinnipedClient pinnipedclientset.Interface
|
|
|
|
configMapInformer corev1informers.ConfigMapInformer
|
2020-08-20 17:54:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 11:58:01 +00:00
|
|
|
// NewKubeConfigInfoPublisherController returns a controller that syncs the
|
2020-11-02 21:39:43 +00:00
|
|
|
// configv1alpha1.CredentialIssuer.Status.KubeConfigInfo field with the cluster-info ConfigMap
|
2020-09-23 11:58:01 +00:00
|
|
|
// in the kube-public namespace.
|
|
|
|
func NewKubeConfigInfoPublisherController(
|
2020-11-02 21:39:43 +00:00
|
|
|
credentialIssuerNamespaceName string,
|
|
|
|
credentialIssuerResourceName string,
|
|
|
|
credentialIssuerLabels map[string]string,
|
2020-08-20 17:54:15 +00:00
|
|
|
serverOverride *string,
|
|
|
|
pinnipedClient pinnipedclientset.Interface,
|
|
|
|
configMapInformer corev1informers.ConfigMapInformer,
|
|
|
|
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",
|
2020-09-23 11:58:01 +00:00
|
|
|
Syncer: &kubeConigInfoPublisherController{
|
2020-11-02 21:39:43 +00:00
|
|
|
credentialIssuerResourceName: credentialIssuerResourceName,
|
|
|
|
credentialIssuerNamespaceName: credentialIssuerNamespaceName,
|
|
|
|
credentialIssuerLabels: credentialIssuerLabels,
|
|
|
|
serverOverride: serverOverride,
|
|
|
|
pinnipedClient: pinnipedClient,
|
|
|
|
configMapInformer: configMapInformer,
|
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
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-23 11:58:01 +00:00
|
|
|
func (c *kubeConigInfoPublisherController) 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 {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug(
|
2020-08-20 17:54:15 +00:00
|
|
|
"could not find config map",
|
|
|
|
"configmap",
|
|
|
|
klog.KRef(ClusterInfoNamespace, clusterInfoName),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeConfig, kubeConfigPresent := configMap.Data[clusterInfoConfigMapKey]
|
|
|
|
if !kubeConfigPresent {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("could not find kubeconfig configmap key")
|
2020-08-20 17:54:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-27 14:43:13 +00:00
|
|
|
config, err := clientcmd.Load([]byte(kubeConfig))
|
|
|
|
if err != nil {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("could not load kubeconfig configmap key")
|
2020-08-27 14:43:13 +00:00
|
|
|
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-11-02 21:39:43 +00:00
|
|
|
updateServerAndCAFunc := func(c *configv1alpha1.CredentialIssuer) {
|
|
|
|
c.Status.KubeConfigInfo = &configv1alpha1.CredentialIssuerKubeConfigInfo{
|
2020-08-25 01:07:34 +00:00
|
|
|
Server: server,
|
|
|
|
CertificateAuthorityData: certificateAuthorityData,
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 00:11:10 +00:00
|
|
|
|
2020-11-02 21:39:43 +00:00
|
|
|
return CreateOrUpdateCredentialIssuer(
|
2020-08-25 01:07:34 +00:00
|
|
|
ctx.Context,
|
2020-11-02 21:39:43 +00:00
|
|
|
c.credentialIssuerNamespaceName,
|
|
|
|
c.credentialIssuerResourceName,
|
|
|
|
c.credentialIssuerLabels,
|
2020-08-25 01:07:34 +00:00
|
|
|
c.pinnipedClient,
|
2020-09-24 16:19:57 +00:00
|
|
|
updateServerAndCAFunc,
|
|
|
|
)
|
2020-08-20 17:54:15 +00:00
|
|
|
}
|