Skip to content

Commit 233d5c1

Browse files
committed
use the cache ref to mount the cache kubeconfig into shard deployments
On-behalf-of: @SAP christoph.mewes@sap.com
1 parent 0974de9 commit 233d5c1

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

internal/resources/cacheserver/certificates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
// RootCACertificateReconciler creates a standalone CA just for a single cache-server.
3030
func RootCACertificateReconciler(server *operatorv1alpha1.CacheServer) reconciling.NamedCertificateReconcilerFactory {
31-
name := resources.GetCacheServerCAName(server, operatorv1alpha1.RootCA)
31+
name := resources.GetCacheServerCAName(server.Name, operatorv1alpha1.RootCA)
3232
template := server.Spec.CertificateTemplates.CATemplate(operatorv1alpha1.RootCA)
3333

3434
if server.Spec.Certificates.IssuerRef == nil {
@@ -97,7 +97,7 @@ func ServerCertificateReconciler(server *operatorv1alpha1.CacheServer) reconcili
9797
},
9898

9999
IssuerRef: certmanagermetav1.ObjectReference{
100-
Name: resources.GetCacheServerCAName(server, operatorv1alpha1.RootCA),
100+
Name: resources.GetCacheServerCAName(server.Name, operatorv1alpha1.RootCA),
101101
Kind: "Issuer",
102102
Group: "cert-manager.io",
103103
},

internal/resources/cacheserver/issuers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func RootCAIssuerReconciler(server *operatorv1alpha1.CacheServer) reconciling.NamedIssuerReconcilerFactory {
28-
name := resources.GetCacheServerCAName(server, operatorv1alpha1.RootCA)
28+
name := resources.GetCacheServerCAName(server.Name, operatorv1alpha1.RootCA)
2929

3030
secretName := name
3131
if server.Spec.Certificates.CASecretRef != nil {

internal/resources/cacheserver/kubeconfigs.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package cacheserver
1818

1919
import (
20-
"fmt"
21-
2220
k8creconciling "k8c.io/reconciler/pkg/reconciling"
2321

2422
corev1 "k8s.io/api/core/v1"
@@ -29,18 +27,14 @@ import (
2927
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
3028
)
3129

32-
func kubeconfigSecret(server *operatorv1alpha1.CacheServer) string {
33-
return fmt.Sprintf("%s-kubeconfig", server.Name)
34-
}
35-
3630
func KubeconfigReconciler(server *operatorv1alpha1.CacheServer) k8creconciling.NamedSecretReconcilerFactory {
3731
const (
3832
serverName = "cache"
3933
contextName = "cache"
4034
)
4135

4236
return func() (string, k8creconciling.SecretReconciler) {
43-
return kubeconfigSecret(server), func(secret *corev1.Secret) (*corev1.Secret, error) {
37+
return resources.GetCacheServerKubeconfigName(server.Name), func(secret *corev1.Secret) (*corev1.Secret, error) {
4438
var config *clientcmdapi.Config
4539
if secret.Data == nil {
4640
secret.Data = make(map[string][]byte)

internal/resources/resources.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ func GetRootShardCAName(r *operatorv1alpha1.RootShard, caName operatorv1alpha1.C
194194
return fmt.Sprintf("%s-%s-ca", r.Name, caName)
195195
}
196196

197-
func GetCacheServerCAName(s *operatorv1alpha1.CacheServer, caName operatorv1alpha1.CA) string {
197+
func GetCacheServerCAName(cacheServerName string, caName operatorv1alpha1.CA) string {
198198
if caName == operatorv1alpha1.RootCA {
199-
return fmt.Sprintf("%s-ca", s.Name)
199+
return fmt.Sprintf("%s-ca", cacheServerName)
200200
}
201-
return fmt.Sprintf("%s-%s-ca", s.Name, caName)
201+
return fmt.Sprintf("%s-%s-ca", cacheServerName, caName)
202202
}
203203

204204
func GetFrontProxyResourceLabels(f *operatorv1alpha1.FrontProxy) map[string]string {
@@ -221,6 +221,10 @@ func GetFrontProxyDynamicKubeconfigName(r *operatorv1alpha1.RootShard, f *operat
221221
return fmt.Sprintf("%s-%s-dynamic-kubeconfig", r.Name, f.Name)
222222
}
223223

224+
func GetCacheServerKubeconfigName(cacheServerName string) string {
225+
return fmt.Sprintf("%s-kubeconfig", cacheServerName)
226+
}
227+
224228
func GetRootShardProxyConfigName(r *operatorv1alpha1.RootShard) string {
225229
return fmt.Sprintf("%s-proxy-config", r.Name)
226230
}

internal/resources/rootshard/deployment.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ func getKubeconfigMountPath(certName operatorv1alpha1.Certificate) string {
6262
return fmt.Sprintf("/etc/kcp/%s-kubeconfig", certName)
6363
}
6464

65+
func getCacheServerKubeconfigMountPath() string {
66+
return "/etc/cache-server/kubeconfig"
67+
}
68+
69+
// getCacheServerCAMountPath has to match the code in the cacheserver package.
70+
func getCacheServerCAMountPath(caName operatorv1alpha1.CA) string {
71+
return fmt.Sprintf("/etc/cache-server/tls/ca/%s", caName)
72+
}
73+
6574
func DeploymentReconciler(rootShard *operatorv1alpha1.RootShard) reconciling.NamedDeploymentReconcilerFactory {
6675
return func() (string, reconciling.DeploymentReconciler) {
6776
return resources.GetRootShardDeploymentName(rootShard), func(dep *appsv1.Deployment) (*appsv1.Deployment, error) {
@@ -132,6 +141,22 @@ func DeploymentReconciler(rootShard *operatorv1alpha1.RootShard) reconciling.Nam
132141
})
133142
}
134143

144+
// If an external CacheServer is meant to be used, mount its kubeconfig and the
145+
// certificate referenced in it.
146+
if ref := rootShard.Spec.Cache.Reference; ref != nil {
147+
secretMounts = append(secretMounts, utils.SecretMount{
148+
VolumeName: "cache-server-kubeconfig",
149+
SecretName: resources.GetCacheServerKubeconfigName(ref.Name),
150+
MountPath: getCacheServerKubeconfigMountPath(),
151+
})
152+
153+
secretMounts = append(secretMounts, utils.SecretMount{
154+
VolumeName: "cache-server-ca",
155+
SecretName: resources.GetCacheServerCAName(ref.Name, operatorv1alpha1.RootCA),
156+
MountPath: getCacheServerCAMountPath(operatorv1alpha1.RootCA),
157+
})
158+
}
159+
135160
volumes := []corev1.Volume{}
136161
volumeMounts := []corev1.VolumeMount{}
137162

@@ -212,5 +237,9 @@ func getArgs(rootShard *operatorv1alpha1.RootShard) []string {
212237
args = append(args, rootShard.Spec.ExtraArgs...)
213238
}
214239

240+
if ref := rootShard.Spec.Cache.Reference; ref != nil {
241+
args = append(args, fmt.Sprintf("--cache-kubeconfig=%s/kubeconfig", getCacheServerKubeconfigMountPath()))
242+
}
243+
215244
return args
216245
}

0 commit comments

Comments
 (0)