Skip to content

Commit 032142c

Browse files
authored
Merge pull request kubernetes#133242 from ahmedtd/fix-podcerts
Pod Certificates: Fix kubelet volume host arg order; improve logging
2 parents 640c5b8 + 4874d41 commit 032142c

File tree

3 files changed

+107
-32
lines changed

3 files changed

+107
-32
lines changed

pkg/kubelet/podcertificate/podcertificatemanager.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ type IssuingManager struct {
109109
}
110110

111111
type projectionKey struct {
112-
namespace string
113-
podName string
114-
podUID string
115-
volumeName string
116-
sourceIndex int
112+
Namespace string
113+
PodName string
114+
PodUID string
115+
VolumeName string
116+
SourceIndex int
117117
}
118118

119119
type projectionRecord struct {
@@ -259,13 +259,14 @@ func (m *IssuingManager) queueAllProjectionsForPod(uid types.UID) {
259259
continue
260260
}
261261

262-
m.projectionQueue.Add(projectionKey{
263-
namespace: pod.ObjectMeta.Namespace,
264-
podName: pod.ObjectMeta.Name,
265-
podUID: string(pod.ObjectMeta.UID),
266-
volumeName: v.Name,
267-
sourceIndex: sourceIndex,
268-
})
262+
key := projectionKey{
263+
Namespace: pod.ObjectMeta.Namespace,
264+
PodName: pod.ObjectMeta.Name,
265+
PodUID: string(pod.ObjectMeta.UID),
266+
VolumeName: v.Name,
267+
SourceIndex: sourceIndex,
268+
}
269+
m.projectionQueue.Add(key)
269270
}
270271
}
271272
}
@@ -298,7 +299,7 @@ func (m *IssuingManager) processNextProjection(ctx context.Context) bool {
298299

299300
err := m.handleProjection(ctx, key)
300301
if err != nil {
301-
utilruntime.HandleErrorWithContext(ctx, err, "while handling podCertificate projected volume source", "namespace", key.namespace, "pod", key.podName, "volume", key.volumeName, "sourceIndex", key.sourceIndex)
302+
utilruntime.HandleErrorWithContext(ctx, err, "while handling podCertificate projected volume source", "namespace", key.Namespace, "pod", key.PodName, "volume", key.VolumeName, "sourceIndex", key.SourceIndex)
302303
m.projectionQueue.AddRateLimited(key)
303304
return true
304305
}
@@ -311,7 +312,7 @@ func (m *IssuingManager) handleProjection(ctx context.Context, key projectionKey
311312
// Remember, returning nil from this function indicates that the work item
312313
// was successfully processed, and should be dropped from the queue.
313314

314-
pod, ok := m.podManager.GetPodByUID(types.UID(key.podUID))
315+
pod, ok := m.podManager.GetPodByUID(types.UID(key.PodUID))
315316
if !ok {
316317
// If we can't find the pod anymore, it's been deleted. Clear all our
317318
// internal state associated with the pod and return a nil error so it
@@ -320,7 +321,7 @@ func (m *IssuingManager) handleProjection(ctx context.Context, key projectionKey
320321
m.lock.Lock()
321322
defer m.lock.Unlock()
322323
for k := range m.credStore {
323-
if k.namespace == key.namespace && k.podName == key.podName && k.podUID == key.podUID {
324+
if k.Namespace == key.Namespace && k.PodName == key.PodName && k.PodUID == key.PodUID {
324325
delete(m.credStore, k)
325326
}
326327
}
@@ -330,9 +331,9 @@ func (m *IssuingManager) handleProjection(ctx context.Context, key projectionKey
330331

331332
var source *corev1.PodCertificateProjection
332333
for _, vol := range pod.Spec.Volumes {
333-
if vol.Name == key.volumeName && vol.Projected != nil {
334+
if vol.Name == key.VolumeName && vol.Projected != nil {
334335
for i, volumeSource := range vol.Projected.Sources {
335-
if i == key.sourceIndex && volumeSource.PodCertificate != nil {
336+
if i == key.SourceIndex && volumeSource.PodCertificate != nil {
336337
source = volumeSource.PodCertificate
337338
}
338339
}
@@ -409,7 +410,7 @@ func (m *IssuingManager) handleProjection(ctx context.Context, key projectionKey
409410
// We are working through the initial issuance. We created a PCR, now
410411
// we need to wait for it to reach a terminal state.
411412

412-
pcr, err := m.pcrLister.PodCertificateRequests(key.namespace).Get(state.pcrName)
413+
pcr, err := m.pcrLister.PodCertificateRequests(key.Namespace).Get(state.pcrName)
413414
if k8serrors.IsNotFound(err) && m.clock.Now().After(state.pcrAbandonAt) {
414415
// "Not Found" could be due to informer lag, or because someone
415416
// deleted the PodCertificateRequest. In the first case, the
@@ -423,7 +424,7 @@ func (m *IssuingManager) handleProjection(ctx context.Context, key projectionKey
423424
rec.curState = &credStateInitial{}
424425
return fmt.Errorf("PodCertificateRequest %q appears to have been deleted", pcr.ObjectMeta.Namespace+"/"+pcr.ObjectMeta.Name)
425426
} else if err != nil {
426-
return fmt.Errorf("while getting PodCertificateRequest %q: %w", key.namespace+"/"+state.pcrName, err)
427+
return fmt.Errorf("while getting PodCertificateRequest %q: %w", key.Namespace+"/"+state.pcrName, err)
427428
}
428429

429430
// If the PodCertificateRequest has moved to a terminal state, update
@@ -525,7 +526,7 @@ func (m *IssuingManager) handleProjection(ctx context.Context, key projectionKey
525526

526527
case *credStateWaitRefresh:
527528
// Check the refresh PodCertificateRequest
528-
pcr, err := m.pcrLister.PodCertificateRequests(key.namespace).Get(state.refreshPCRName)
529+
pcr, err := m.pcrLister.PodCertificateRequests(key.Namespace).Get(state.refreshPCRName)
529530
if k8serrors.IsNotFound(err) && m.clock.Now().After(state.refreshPCRAbandonAt) {
530531
// "Not Found" could be due to informer lag, or because someone
531532
// deleted the PodCertificateRequest. In the first case, the
@@ -543,7 +544,7 @@ func (m *IssuingManager) handleProjection(ctx context.Context, key projectionKey
543544
}
544545
return fmt.Errorf("PodCertificateRequest appears to have been deleted")
545546
} else if err != nil {
546-
return fmt.Errorf("while getting PodCertificateRequest %q: %w", key.namespace+"/"+state.refreshPCRName, err)
547+
return fmt.Errorf("while getting PodCertificateRequest %q: %w", key.Namespace+"/"+state.refreshPCRName, err)
547548
}
548549

549550
// If the PodCertificateRequest has moved to a terminal state, update
@@ -675,24 +676,23 @@ func (m *IssuingManager) createPodCertificateRequest(
675676
}
676677

677678
func (m *IssuingManager) GetPodCertificateCredentialBundle(ctx context.Context, namespace, podName, podUID, volumeName string, sourceIndex int) ([]byte, []byte, error) {
679+
credKey := projectionKey{
680+
Namespace: namespace,
681+
PodName: podName,
682+
PodUID: podUID,
683+
VolumeName: volumeName,
684+
SourceIndex: sourceIndex,
685+
}
686+
678687
var rec *projectionRecord
679688
func() {
680689
m.lock.Lock()
681690
defer m.lock.Unlock()
682-
683-
credKey := projectionKey{
684-
namespace: namespace,
685-
podName: podName,
686-
podUID: podUID,
687-
volumeName: volumeName,
688-
sourceIndex: sourceIndex,
689-
}
690691
rec = m.credStore[credKey]
691-
692692
}()
693693

694694
if rec == nil {
695-
return nil, nil, fmt.Errorf("no credentials yet")
695+
return nil, nil, fmt.Errorf("no credentials yet for key=%v", credKey)
696696
}
697697

698698
rec.lock.Lock()

pkg/kubelet/volume_host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (kvh *kubeletVolumeHost) GetTrustAnchorsBySigner(signerName string, labelSe
266266
}
267267

268268
func (kvh *kubeletVolumeHost) GetPodCertificateCredentialBundle(ctx context.Context, namespace, podName, podUID, volumeName string, sourceIndex int) ([]byte, []byte, error) {
269-
return kvh.podCertificateManager.GetPodCertificateCredentialBundle(ctx, namespace, podName, volumeName, podUID, sourceIndex)
269+
return kvh.podCertificateManager.GetPodCertificateCredentialBundle(ctx, namespace, podName, podUID, volumeName, sourceIndex)
270270
}
271271

272272
func (kvh *kubeletVolumeHost) GetNodeLabels() (map[string]string, error) {

pkg/kubelet/volume_host_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2025 The Kubernetes 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 kubelet
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/google/go-cmp/cmp"
24+
corev1 "k8s.io/api/core/v1"
25+
)
26+
27+
type recordingPodCertificateManager struct {
28+
Namespace string
29+
PodName string
30+
PodUID string
31+
VolumeName string
32+
SourceIndex int
33+
}
34+
35+
func (f *recordingPodCertificateManager) GetPodCertificateCredentialBundle(ctx context.Context, namespace, podName, podUID, volumeName string, sourceIndex int) ([]byte, []byte, error) {
36+
f.Namespace = namespace
37+
f.PodName = podName
38+
f.PodUID = podUID
39+
f.VolumeName = volumeName
40+
f.SourceIndex = sourceIndex
41+
42+
return nil, nil, nil
43+
}
44+
45+
func (f *recordingPodCertificateManager) TrackPod(ctx context.Context, pod *corev1.Pod) {}
46+
47+
func (f *recordingPodCertificateManager) ForgetPod(ctx context.Context, pod *corev1.Pod) {}
48+
49+
// Check that GetPodCertificateCredentialBundle forwards its arguments in the
50+
// correct order. Seems excessive, but we got here because I put the arguments
51+
// in the wrong order...
52+
func TestGetPodCertificateCredentialBundle(t *testing.T) {
53+
recorder := &recordingPodCertificateManager{}
54+
55+
kvh := &kubeletVolumeHost{
56+
podCertificateManager: recorder,
57+
}
58+
59+
_, _, err := kvh.GetPodCertificateCredentialBundle(context.Background(), "namespace", "pod-name", "pod-uid", "volume-name", 10)
60+
if err != nil {
61+
t.Fatalf("Unexpected error calling GetPodCertificateCredentialBundle: %v", err)
62+
}
63+
64+
want := &recordingPodCertificateManager{
65+
Namespace: "namespace",
66+
PodName: "pod-name",
67+
PodUID: "pod-uid",
68+
VolumeName: "volume-name",
69+
SourceIndex: 10,
70+
}
71+
72+
if diff := cmp.Diff(recorder, want); diff != "" {
73+
t.Errorf("Wrong input to GetPodCertificateCredentialBundle; diff (-got +want)\n%s", diff)
74+
}
75+
}

0 commit comments

Comments
 (0)