forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 143
UPSTREAM: 141913: restore FileRefreshDuration poll for file-backed CAs #2771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arpitbhagat
wants to merge
2
commits into
openshift:master
Choose a base branch
from
arpitbhagat:UPSTREAM-141913-file-ca-poll
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
.../src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_cafile_content_linux_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //go:build linux | ||
|
|
||
| /* | ||
| Copyright 2026 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package dynamiccertificates | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // TestDynamicFileCAContentPollReloadsAfterBindMountHidesInode covers the miss | ||
| // class from atomic replace / overlay / bind-mount: fsnotify stays on the old | ||
| // inode while os.ReadFile of the path returns new bytes. Deleting the file is | ||
| // not a valid stand-in — loadCABundle errors and the workqueue retries, which | ||
| // can reload without FileRefreshDuration. | ||
| func TestDynamicFileCAContentPollReloadsAfterBindMountHidesInode(t *testing.T) { | ||
| orig := FileRefreshDuration | ||
| FileRefreshDuration = 50 * time.Millisecond | ||
| t.Cleanup(func() { FileRefreshDuration = orig }) | ||
|
|
||
| dir := t.TempDir() | ||
| filename := filepath.Join(dir, "ca.crt") | ||
| shadow := filepath.Join(dir, "ca.shadow") | ||
| ca1 := mustCreateCA(t, "ca-one") | ||
| ca2 := mustCreateCA(t, "ca-two") | ||
| if err := os.WriteFile(filename, ca1, 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(shadow, ca1, 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| c, err := NewDynamicCAContentFromFile("test", filename) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got := c.CurrentCABundleContent(); !bytes.Equal(got, ca1) { | ||
| t.Fatalf("initial bundle mismatch") | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| go c.Run(ctx, 1) | ||
| time.Sleep(150 * time.Millisecond) | ||
|
|
||
| if err := syscall.Mount(shadow, filename, "", syscall.MS_BIND, ""); err != nil { | ||
| t.Skipf("bind mount not permitted in this environment: %v", err) | ||
| } | ||
| t.Cleanup(func() { | ||
| if err := syscall.Unmount(filename, syscall.MNT_DETACH); err != nil { | ||
| t.Errorf("unmount %s: %v", filename, err) | ||
| } | ||
| }) | ||
|
|
||
| // Append on the new inode. The watch still holds the hidden original inode, | ||
| // so fsnotify should not fire; FileRefreshDuration must reload from the path. | ||
| f, err := os.OpenFile(shadow, os.O_APPEND|os.O_WRONLY, 0) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := f.Write(ca2); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := f.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| want, err := os.ReadFile(filename) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| waitForCABundle(t, c, want, 2*time.Second) | ||
| } |
106 changes: 106 additions & 0 deletions
106
staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_cafile_content_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| Copyright 2026 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package dynamiccertificates | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "crypto/rand" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "encoding/pem" | ||
| "math/big" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/wait" | ||
| ) | ||
|
|
||
| func mustCreateCA(t *testing.T, cn string) []byte { | ||
| t.Helper() | ||
| key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| tmpl := &x509.Certificate{ | ||
| SerialNumber: big.NewInt(1), | ||
| Subject: pkix.Name{CommonName: cn}, | ||
| NotBefore: time.Now().Add(-time.Hour), | ||
| NotAfter: time.Now().Add(time.Hour), | ||
| KeyUsage: x509.KeyUsageCertSign, | ||
| BasicConstraintsValid: true, | ||
| IsCA: true, | ||
| } | ||
| der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) | ||
| } | ||
|
|
||
| func waitForCABundle(t *testing.T, c *DynamicFileCAContent, want []byte, timeout time.Duration) { | ||
| t.Helper() | ||
| deadline := time.Now().Add(timeout) | ||
| var last []byte | ||
| for time.Now().Before(deadline) { | ||
| last = c.CurrentCABundleContent() | ||
| if bytes.Equal(last, want) { | ||
| return | ||
| } | ||
| time.Sleep(20 * time.Millisecond) | ||
| } | ||
| t.Fatalf("timed out waiting for CA bundle update: got %d bytes, want %d bytes", len(last), len(want)) | ||
| } | ||
|
|
||
| // TestFileRefreshPollReloadsWithoutFsnotify starts only the worker + FileRefreshDuration | ||
| // enqueue loop from Run (not watchCAFile). A rewrite of the CA file then cannot be | ||
| // attributed to inotify; the poll must pick it up. Run() must keep that loop. | ||
| func TestFileRefreshPollReloadsWithoutFsnotify(t *testing.T) { | ||
| orig := FileRefreshDuration | ||
| FileRefreshDuration = 50 * time.Millisecond | ||
| t.Cleanup(func() { FileRefreshDuration = orig }) | ||
|
|
||
| dir := t.TempDir() | ||
| filename := filepath.Join(dir, "ca.crt") | ||
| ca1 := mustCreateCA(t, "ca-one") | ||
| ca2 := mustCreateCA(t, "ca-two") | ||
| if err := os.WriteFile(filename, ca1, 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| c, err := NewDynamicCAContentFromFile("test", filename) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| t.Cleanup(func() { | ||
| cancel() | ||
| c.queue.ShutDown() | ||
| }) | ||
| go wait.Until(c.runWorker, time.Second, ctx.Done()) | ||
| go wait.Until(func() { c.queue.Add(workItemKey) }, FileRefreshDuration, ctx.Done()) | ||
|
|
||
| if err := os.WriteFile(filename, ca2, 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| waitForCABundle(t, c, ca2, 2*time.Second) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.