|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package options |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/jellydator/ttlcache/v3" |
| 24 | + kcpkubernetesinformers "github.com/kcp-dev/client-go/informers" |
| 25 | + "github.com/kcp-dev/client-go/kubernetes" |
| 26 | + "github.com/kcp-dev/logicalcluster/v3" |
| 27 | + |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + clientset "k8s.io/client-go/kubernetes" |
| 34 | + kubecorev1lister "k8s.io/client-go/listers/core/v1" |
| 35 | + "k8s.io/kubernetes/pkg/serviceaccount" |
| 36 | + |
| 37 | + corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1" |
| 38 | + kcpinformers "github.com/kcp-dev/kcp/sdk/client/informers/externalversions" |
| 39 | + corev1alpha1listers "github.com/kcp-dev/kcp/sdk/client/listers/core/v1alpha1" |
| 40 | +) |
| 41 | + |
| 42 | +const ( |
| 43 | + // SuccessCacheTTL is the TTL to cache a successful lookup for remote clusters. |
| 44 | + SuccessCacheTTL = 1 * time.Minute |
| 45 | + // FailureCacheTTL is the TTL to cache a failed lookup for remote clusters. |
| 46 | + FailureCacheTTL = 10 * time.Second |
| 47 | +) |
| 48 | + |
| 49 | +type cacheKey struct { |
| 50 | + clusterName logicalcluster.Name |
| 51 | + types.NamespacedName |
| 52 | +} |
| 53 | + |
| 54 | +// newServiceAccountTokenCache creates a new service account token cache backed |
| 55 | +// by ttl caches for all remote clusters, and local informers logic for local |
| 56 | +// clusters. |
| 57 | +func newServiceAccountTokenCache(delayedKubeClusterClient *kubernetes.ClusterInterface, delayedKcpInformers *kcpinformers.SharedInformerFactory) func(kubeInformers kcpkubernetesinformers.SharedInformerFactory) serviceaccount.ServiceAccountTokenClusterGetter { |
| 58 | + return func(kubeInformers kcpkubernetesinformers.SharedInformerFactory) serviceaccount.ServiceAccountTokenClusterGetter { |
| 59 | + return &serviceAccountTokenCache{ |
| 60 | + delayedKubeClusterClient: delayedKubeClusterClient, |
| 61 | + |
| 62 | + delayedKcpInformers: delayedKcpInformers, |
| 63 | + kubeInformers: kubeInformers, |
| 64 | + |
| 65 | + serviceAccountCache: ttlcache.New[cacheKey, *corev1.ServiceAccount](), |
| 66 | + secretCache: ttlcache.New[cacheKey, *corev1.Secret](), |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +type serviceAccountTokenCache struct { |
| 72 | + delayedKubeClusterClient *kubernetes.ClusterInterface |
| 73 | + |
| 74 | + delayedKcpInformers *kcpinformers.SharedInformerFactory |
| 75 | + kubeInformers kcpkubernetesinformers.SharedInformerFactory |
| 76 | + |
| 77 | + serviceAccountCache *ttlcache.Cache[cacheKey, *corev1.ServiceAccount] |
| 78 | + secretCache *ttlcache.Cache[cacheKey, *corev1.Secret] |
| 79 | +} |
| 80 | + |
| 81 | +func (c *serviceAccountTokenCache) Cluster(clusterName logicalcluster.Name) serviceaccount.ServiceAccountTokenGetter { |
| 82 | + return &serviceAccountTokenGetter{ |
| 83 | + kubeClient: (*c.delayedKubeClusterClient).Cluster(clusterName.Path()), |
| 84 | + |
| 85 | + logicalClusters: (*c.delayedKcpInformers).Core().V1alpha1().LogicalClusters().Cluster(clusterName).Lister(), |
| 86 | + serviceAccounts: c.kubeInformers.Core().V1().ServiceAccounts().Cluster(clusterName).Lister(), |
| 87 | + secrets: c.kubeInformers.Core().V1().Secrets().Cluster(clusterName).Lister(), |
| 88 | + |
| 89 | + serviceAccountCache: c.serviceAccountCache, |
| 90 | + secretCache: c.secretCache, |
| 91 | + |
| 92 | + clusterName: clusterName, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +type serviceAccountTokenGetter struct { |
| 97 | + kubeClient clientset.Interface |
| 98 | + |
| 99 | + logicalClusters corev1alpha1listers.LogicalClusterLister |
| 100 | + serviceAccounts kubecorev1lister.ServiceAccountLister |
| 101 | + secrets kubecorev1lister.SecretLister |
| 102 | + |
| 103 | + serviceAccountCache *ttlcache.Cache[cacheKey, *corev1.ServiceAccount] |
| 104 | + secretCache *ttlcache.Cache[cacheKey, *corev1.Secret] |
| 105 | + |
| 106 | + clusterName logicalcluster.Name |
| 107 | +} |
| 108 | + |
| 109 | +func (g *serviceAccountTokenGetter) GetServiceAccount(namespace, name string) (*corev1.ServiceAccount, error) { |
| 110 | + // local cluster? |
| 111 | + if _, err := g.logicalClusters.Get(corev1alpha1.LogicalClusterName); err != nil { |
| 112 | + return g.serviceAccounts.ServiceAccounts(namespace).Get(name) |
| 113 | + } |
| 114 | + |
| 115 | + // cached? |
| 116 | + if sa := g.serviceAccountCache.Get(cacheKey{g.clusterName, types.NamespacedName{Namespace: namespace, Name: name}}); sa != nil && sa.Value() != nil { |
| 117 | + return sa.Value(), nil |
| 118 | + } |
| 119 | + |
| 120 | + // fetch with external client |
| 121 | + // TODO(sttts): here it's little racy, as we might fetch the service account multiple times. |
| 122 | + sa, err := g.kubeClient.CoreV1().ServiceAccounts(namespace).Get(context.Background(), name, metav1.GetOptions{}) |
| 123 | + if err != nil && !kerrors.IsNotFound(err) { |
| 124 | + return nil, err |
| 125 | + } else if kerrors.IsNotFound(err) { |
| 126 | + ttl := ttlcache.WithTTL[cacheKey, *corev1.ServiceAccount](FailureCacheTTL) |
| 127 | + g.serviceAccountCache.GetOrSet(cacheKey{g.clusterName, types.NamespacedName{Namespace: namespace, Name: name}}, nil, ttl) |
| 128 | + } |
| 129 | + |
| 130 | + g.serviceAccountCache.Set(cacheKey{g.clusterName, types.NamespacedName{Namespace: namespace, Name: name}}, sa, SuccessCacheTTL) |
| 131 | + return sa, nil |
| 132 | +} |
| 133 | + |
| 134 | +func (g *serviceAccountTokenGetter) GetPod(_, name string) (*corev1.Pod, error) { |
| 135 | + return nil, kerrors.NewNotFound(schema.GroupResource{Group: "", Resource: "pods"}, name) |
| 136 | +} |
| 137 | + |
| 138 | +func (g *serviceAccountTokenGetter) GetSecret(namespace, name string) (*corev1.Secret, error) { |
| 139 | + // local cluster? |
| 140 | + if _, err := g.logicalClusters.Get(corev1alpha1.LogicalClusterName); err != nil { |
| 141 | + return g.secrets.Secrets(namespace).Get(name) |
| 142 | + } |
| 143 | + |
| 144 | + // cached? |
| 145 | + if secret := g.secretCache.Get(cacheKey{g.clusterName, types.NamespacedName{Namespace: namespace, Name: name}}); secret != nil && secret.Value() != nil { |
| 146 | + return secret.Value(), nil |
| 147 | + } |
| 148 | + |
| 149 | + // fetch with external client |
| 150 | + // TODO(sttts): here it's little racy, as we might fetch the secret multiple times. |
| 151 | + secret, err := g.kubeClient.CoreV1().Secrets(namespace).Get(context.Background(), name, metav1.GetOptions{}) |
| 152 | + if err != nil && !kerrors.IsNotFound(err) { |
| 153 | + return nil, err |
| 154 | + } else if kerrors.IsNotFound(err) { |
| 155 | + ttl := ttlcache.WithTTL[cacheKey, *corev1.Secret](FailureCacheTTL) |
| 156 | + g.secretCache.GetOrSet(cacheKey{g.clusterName, types.NamespacedName{Namespace: namespace, Name: name}}, nil, ttl) |
| 157 | + } |
| 158 | + |
| 159 | + g.secretCache.Set(cacheKey{g.clusterName, types.NamespacedName{Namespace: namespace, Name: name}}, secret, SuccessCacheTTL) |
| 160 | + return secret, nil |
| 161 | +} |
| 162 | + |
| 163 | +func (g *serviceAccountTokenGetter) GetNode(name string) (*corev1.Node, error) { |
| 164 | + return nil, kerrors.NewNotFound(schema.GroupResource{Group: "", Resource: "nodes"}, name) |
| 165 | +} |
0 commit comments