-
Notifications
You must be signed in to change notification settings - Fork 3
Cache 404 responses for unknown artifacts #66
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
bdehamer
wants to merge
2
commits into
main
Choose a base branch
from
bdehamer/no-artifact-cache
base: main
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.
+211
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,179 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/github/deployment-tracker/pkg/deploymentrecord" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| amcache "k8s.io/apimachinery/pkg/util/cache" | ||
| ) | ||
|
|
||
| // mockPoster records all PostOne calls and returns a configurable error. | ||
| type mockPoster struct { | ||
| mu sync.Mutex | ||
| calls int | ||
| lastErr error | ||
| } | ||
|
|
||
| func (m *mockPoster) PostOne(_ context.Context, _ *deploymentrecord.DeploymentRecord) error { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
| m.calls++ | ||
| return m.lastErr | ||
| } | ||
|
|
||
| func (m *mockPoster) getCalls() int { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
| return m.calls | ||
| } | ||
|
|
||
| // newTestController creates a minimal Controller suitable for unit-testing | ||
| // recordContainer without a real Kubernetes cluster. | ||
| func newTestController(poster *mockPoster) *Controller { | ||
| return &Controller{ | ||
| apiClient: poster, | ||
| cfg: &Config{ | ||
| Template: "{{namespace}}/{{deploymentName}}/{{containerName}}", | ||
| LogicalEnvironment: "test", | ||
| PhysicalEnvironment: "test", | ||
| Cluster: "test", | ||
| }, | ||
| observedDeployments: amcache.NewExpiring(), | ||
| unknownArtifacts: amcache.NewExpiring(), | ||
| } | ||
| } | ||
|
|
||
| // testPod returns a pod with a single container and a known digest. | ||
| func testPod(digest string) (*corev1.Pod, corev1.Container) { | ||
| container := corev1.Container{ | ||
| Name: "app", | ||
| Image: "nginx:latest", | ||
| } | ||
| pod := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-pod", | ||
| Namespace: "default", | ||
| OwnerReferences: []metav1.OwnerReference{{ | ||
| APIVersion: "apps/v1", | ||
| Kind: "ReplicaSet", | ||
| Name: "test-deployment-abc123", | ||
| }}, | ||
| }, | ||
| Status: corev1.PodStatus{ | ||
| ContainerStatuses: []corev1.ContainerStatus{{ | ||
| Name: "app", | ||
| ImageID: fmt.Sprintf("docker-pullable://nginx@%s", digest), | ||
| }}, | ||
| }, | ||
| } | ||
| return pod, container | ||
| } | ||
|
|
||
| func TestRecordContainer_UnknownArtifactCachePopulatedOn404(t *testing.T) { | ||
| t.Parallel() | ||
| digest := "sha256:unknown404digest" | ||
| poster := &mockPoster{ | ||
| lastErr: &deploymentrecord.NoArtifactError{}, | ||
| } | ||
| ctrl := newTestController(poster) | ||
| pod, container := testPod(digest) | ||
|
|
||
| // First call should hit the API and get a 404 | ||
| err := ctrl.recordContainer(context.Background(), pod, container, deploymentrecord.StatusDeployed, EventCreated, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, poster.getCalls()) | ||
|
|
||
| // Digest should now be in the unknown artifacts cache | ||
| _, exists := ctrl.unknownArtifacts.Get(digest) | ||
| assert.True(t, exists, "digest should be cached after 404") | ||
| } | ||
|
|
||
| func TestRecordContainer_UnknownArtifactCacheSkipsAPICall(t *testing.T) { | ||
| t.Parallel() | ||
| digest := "sha256:cacheddigest" | ||
| poster := &mockPoster{ | ||
| lastErr: &deploymentrecord.NoArtifactError{}, | ||
| } | ||
| ctrl := newTestController(poster) | ||
| pod, container := testPod(digest) | ||
|
|
||
| // First call — API returns 404, populates cache | ||
| err := ctrl.recordContainer(context.Background(), pod, container, deploymentrecord.StatusDeployed, EventCreated, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, poster.getCalls()) | ||
|
|
||
| // Second call — should be served from cache, no API call | ||
| err = ctrl.recordContainer(context.Background(), pod, container, deploymentrecord.StatusDeployed, EventCreated, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, poster.getCalls(), "API should not be called for cached unknown artifact") | ||
| } | ||
|
|
||
| func TestRecordContainer_UnknownArtifactCacheAppliesToDecommission(t *testing.T) { | ||
| t.Parallel() | ||
| digest := "sha256:decommission404" | ||
| poster := &mockPoster{ | ||
| lastErr: &deploymentrecord.NoArtifactError{}, | ||
| } | ||
| ctrl := newTestController(poster) | ||
| pod, container := testPod(digest) | ||
|
|
||
| // Deploy call — 404, populates cache | ||
| err := ctrl.recordContainer(context.Background(), pod, container, deploymentrecord.StatusDeployed, EventCreated, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, poster.getCalls()) | ||
|
|
||
| // Decommission call for same digest — should skip API | ||
| err = ctrl.recordContainer(context.Background(), pod, container, deploymentrecord.StatusDecommissioned, EventDeleted, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, poster.getCalls(), "decommission should also be skipped for cached unknown artifact") | ||
| } | ||
|
|
||
| func TestRecordContainer_UnknownArtifactCacheExpires(t *testing.T) { | ||
| t.Parallel() | ||
| digest := "sha256:expiringdigest" | ||
| poster := &mockPoster{ | ||
| lastErr: &deploymentrecord.NoArtifactError{}, | ||
| } | ||
| ctrl := newTestController(poster) | ||
| pod, container := testPod(digest) | ||
|
|
||
| // Seed the cache with a very short TTL to test expiry | ||
| ctrl.unknownArtifacts.Set(digest, true, 50*time.Millisecond) | ||
|
|
||
| // Immediately — should be cached | ||
| err := ctrl.recordContainer(context.Background(), pod, container, deploymentrecord.StatusDeployed, EventCreated, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 0, poster.getCalls(), "should skip API while cached") | ||
|
|
||
| // Wait for expiry | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| // After expiry — should call API again | ||
| err = ctrl.recordContainer(context.Background(), pod, container, deploymentrecord.StatusDeployed, EventCreated, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, poster.getCalls(), "should call API after cache expiry") | ||
| } | ||
|
|
||
| func TestRecordContainer_SuccessfulPostDoesNotPopulateUnknownCache(t *testing.T) { | ||
| t.Parallel() | ||
| digest := "sha256:knowndigest" | ||
| poster := &mockPoster{lastErr: nil} // success | ||
| ctrl := newTestController(poster) | ||
| pod, container := testPod(digest) | ||
|
|
||
| err := ctrl.recordContainer(context.Background(), pod, container, deploymentrecord.StatusDeployed, EventCreated, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, poster.getCalls()) | ||
|
|
||
| // Digest should NOT be in the unknown artifacts cache | ||
| _, exists := ctrl.unknownArtifacts.Get(digest) | ||
| assert.False(t, exists, "successful post should not cache digest as unknown") | ||
| } | ||
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
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
Oops, something went wrong.
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.