-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
fix: kubeapi watch updates, allow configurable cidr (#1075)
## Description This PR contains two changes, both aimed at providing fixes for lingering issues with the KubeAPI watch: 1. NetworkPolicy updates based on changes to KubeAPI endpoints have never actually run as expected. The label we use to select existing KubeAPI network policies was never actually applied to policies in the first place. Previously we applied a `uds/generated` label but selected on `uds.dev/generated`, so these never lined up. Additionally our apply would have failed due to the existence of managed fields on the object. This has been the main cause of the problem with our auto-update logic. Pepr watcher restarts fixed the network policies not because of watch fixes, but because we re-reconcile all packages on startup. 2. While the watch does appear to be stable, this PR additionally adds a config option to manually set a CIDR to use instead of relying on the watch. This could be useful in some clusters (such as EKS) where the controlplane IPs update frequently to reduce churn on network policy modifications. ## Related Issue Fixes #821 ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide](https://github.com/defenseunicorns/uds-template-capability/blob/main/CONTRIBUTING.md) followed
Showing
9 changed files
with
483 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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 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 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
261 changes: 261 additions & 0 deletions
261
src/pepr/operator/controllers/network/generators/kubeAPI.spec.ts
This file contains 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,261 @@ | ||
/** | ||
* Copyright 2024 Defense Unicorns | ||
* SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Defense-Unicorns-Commercial | ||
*/ | ||
|
||
import { beforeEach, describe, expect, it, jest } from "@jest/globals"; | ||
import { K8s, kind } from "pepr"; | ||
import { updateAPIServerCIDR } from "./kubeAPI"; | ||
|
||
type KubernetesList<T> = { | ||
items: T[]; | ||
}; | ||
|
||
jest.mock("pepr", () => { | ||
const originalModule = jest.requireActual("pepr") as object; | ||
return { | ||
...originalModule, | ||
K8s: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe("updateAPIServerCIDR", () => { | ||
const mockApply = jest.fn(); | ||
const mockGet = jest.fn<() => Promise<KubernetesList<kind.NetworkPolicy>>>(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(K8s as jest.Mock).mockImplementation(() => ({ | ||
WithLabel: jest.fn(() => ({ | ||
Get: mockGet, | ||
})), | ||
Apply: mockApply, | ||
})); | ||
}); | ||
|
||
it("handles a static CIDR string", async () => { | ||
const mockService = { | ||
spec: { | ||
clusterIP: "10.0.0.1", | ||
}, | ||
} as kind.Service; | ||
|
||
const staticCIDR = "192.168.1.0/24"; | ||
|
||
// Mock the return of `Get` method | ||
mockGet.mockResolvedValue({ | ||
items: [ | ||
{ | ||
metadata: { | ||
name: "mock-netpol", | ||
namespace: "default", | ||
}, | ||
spec: { | ||
egress: [ | ||
{ | ||
to: [{ ipBlock: { cidr: "0.0.0.0/0" } }], | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
} as KubernetesList<kind.NetworkPolicy>); | ||
|
||
await updateAPIServerCIDR(mockService, staticCIDR); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(); | ||
expect(mockApply).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
metadata: { | ||
name: "mock-netpol", | ||
namespace: "default", | ||
}, | ||
spec: { | ||
egress: [ | ||
{ | ||
to: [{ ipBlock: { cidr: staticCIDR } }, { ipBlock: { cidr: "10.0.0.1/32" } }], | ||
}, | ||
], | ||
}, | ||
}), | ||
{ force: true }, // Include the second argument in the call | ||
); | ||
}); | ||
|
||
it("handles an EndpointSlice with multiple endpoints", async () => { | ||
const mockService = { | ||
spec: { | ||
clusterIP: "10.0.0.1", | ||
}, | ||
} as kind.Service; | ||
|
||
const mockSlice = { | ||
endpoints: [{ addresses: ["192.168.1.2"] }, { addresses: ["192.168.1.3"] }], | ||
} as kind.EndpointSlice; | ||
|
||
// Mock the return of `Get` method | ||
mockGet.mockResolvedValue({ | ||
items: [ | ||
{ | ||
metadata: { | ||
name: "mock-netpol", | ||
namespace: "default", | ||
}, | ||
spec: { | ||
egress: [ | ||
{ | ||
to: [{ ipBlock: { cidr: "0.0.0.0/0" } }], | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
} as KubernetesList<kind.NetworkPolicy>); | ||
|
||
await updateAPIServerCIDR(mockService, mockSlice); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(); | ||
expect(mockApply).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
metadata: { | ||
name: "mock-netpol", | ||
namespace: "default", | ||
}, | ||
spec: { | ||
egress: [ | ||
{ | ||
to: [ | ||
{ ipBlock: { cidr: "192.168.1.2/32" } }, | ||
{ ipBlock: { cidr: "192.168.1.3/32" } }, | ||
{ ipBlock: { cidr: "10.0.0.1/32" } }, | ||
], | ||
}, | ||
], | ||
}, | ||
}), | ||
{ force: true }, // Include the second argument in the call | ||
); | ||
}); | ||
|
||
it("handles an empty EndpointSlice", async () => { | ||
const mockService = { | ||
spec: { | ||
clusterIP: "10.0.0.1", | ||
}, | ||
} as kind.Service; | ||
|
||
const mockSlice = { | ||
endpoints: [{}], | ||
} as kind.EndpointSlice; | ||
|
||
// Mock the return of `Get` method | ||
mockGet.mockResolvedValue({ | ||
items: [ | ||
{ | ||
metadata: { | ||
name: "mock-netpol", | ||
namespace: "default", | ||
}, | ||
spec: { | ||
egress: [ | ||
{ | ||
to: [{ ipBlock: { cidr: "0.0.0.0/0" } }], | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
} as KubernetesList<kind.NetworkPolicy>); | ||
|
||
await updateAPIServerCIDR(mockService, mockSlice); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(); | ||
expect(mockApply).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
metadata: { | ||
name: "mock-netpol", | ||
namespace: "default", | ||
}, | ||
spec: { | ||
egress: [ | ||
{ | ||
to: [{ ipBlock: { cidr: "10.0.0.1/32" } }], | ||
}, | ||
], | ||
}, | ||
}), | ||
{ force: true }, // Include the second argument in the call | ||
); | ||
}); | ||
|
||
it("handles a Service with missing clusterIP", async () => { | ||
const mockService = { | ||
spec: {}, | ||
} as kind.Service; | ||
|
||
const mockSlice = { | ||
endpoints: [{ addresses: ["192.168.1.2"] }], | ||
} as kind.EndpointSlice; | ||
|
||
// Mock the return of `Get` method | ||
mockGet.mockResolvedValue({ | ||
items: [ | ||
{ | ||
metadata: { | ||
name: "mock-netpol", | ||
namespace: "default", | ||
}, | ||
spec: { | ||
egress: [ | ||
{ | ||
to: [{ ipBlock: { cidr: "0.0.0.0/0" } }], | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
} as KubernetesList<kind.NetworkPolicy>); | ||
|
||
await updateAPIServerCIDR(mockService, mockSlice); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(); | ||
expect(mockApply).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
metadata: { | ||
name: "mock-netpol", | ||
namespace: "default", | ||
}, | ||
spec: { | ||
egress: [ | ||
{ | ||
to: [{ ipBlock: { cidr: "192.168.1.2/32" } }], | ||
}, | ||
], | ||
}, | ||
}), | ||
{ force: true }, // Include the second argument in the call | ||
); | ||
}); | ||
|
||
it("handles no matching NetworkPolicies", async () => { | ||
const mockService = { | ||
spec: { | ||
clusterIP: "10.0.0.1", | ||
}, | ||
} as kind.Service; | ||
|
||
const mockSlice = { | ||
endpoints: [{ addresses: ["192.168.1.2"] }], | ||
} as kind.EndpointSlice; | ||
|
||
// Mock the return of `Get` method to return no items | ||
mockGet.mockResolvedValue({ | ||
items: [], | ||
} as KubernetesList<kind.NetworkPolicy>); | ||
|
||
await updateAPIServerCIDR(mockService, mockSlice); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(); | ||
expect(mockApply).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains 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 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 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 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 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