mirror of
https://github.com/imjasonh/client-go2
synced 2026-07-20 04:48:31 +00:00
Add expansion methods and missing client-go operations
This PR adds support for resource-specific expansion methods and implements
missing standard client-go operations to achieve feature parity.
## New Features
### Expansion Methods
- Added PodClient with methods: GetLogs, Bind, Evict, ProxyGet
- Added ServiceClient with method: ProxyGet
- Both clients implement the official k8s.io/client-go expansion interfaces
- Runtime type assertions ensure type safety (panic if wrong type)
### Missing Standard Methods
- Watch: Watch resources with optional label/field selectors
- DeleteCollection: Delete multiple resources matching criteria
- UpdateStatus: Update only the status subresource
### Other Improvements
- Refactored all tests to use inline configuration pattern
- Added comprehensive e2e tests for new functionality
- Updated documentation with examples
- Fixed linting error in stream.Close() handling
## Design Decisions
The expansion methods follow a namespace-scoped pattern where calling
`client.PodClient("namespace")` returns a client that implements the
standard client-go PodExpansion interface. This maintains full API
compatibility while providing type safety through runtime assertions.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
63505cf9fd
commit
e79b66caf5
9 changed files with 1458 additions and 366 deletions
71
main.go
71
main.go
|
|
@ -243,4 +243,75 @@ func main() {
|
|||
for _, cm := range cms {
|
||||
log.Println("-", cm.Name)
|
||||
}
|
||||
|
||||
// Demonstrate Pod expansion methods
|
||||
log.Println("\n=== POD EXPANSION EXAMPLE ===")
|
||||
|
||||
// Get a namespace-scoped PodClient with expansion methods from the existing pod client
|
||||
expandedPodClient := podClient.PodClient("kube-system")
|
||||
|
||||
// List pods to find one to get logs from
|
||||
dnsPods, err := podClient.List(ctx, "kube-system", &metav1.ListOptions{
|
||||
LabelSelector: "k8s-app=kube-dns",
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error listing pods: %v", err)
|
||||
} else if len(dnsPods) > 0 {
|
||||
// Get logs from the first CoreDNS pod
|
||||
pod := dnsPods[0]
|
||||
log.Printf("\nGETTING LOGS FROM POD %s", pod.Name)
|
||||
|
||||
// Get last 5 lines of logs
|
||||
tailLines := int64(5)
|
||||
logOpts := &corev1.PodLogOptions{
|
||||
TailLines: &tailLines,
|
||||
}
|
||||
|
||||
req := expandedPodClient.GetLogs(pod.Name, logOpts)
|
||||
logs, err := req.DoRaw(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error getting logs: %v", err)
|
||||
} else {
|
||||
log.Println("Last 5 lines of logs:")
|
||||
log.Println(string(logs))
|
||||
}
|
||||
|
||||
// If the pod has multiple containers, get logs from a specific container
|
||||
if len(pod.Spec.Containers) > 0 {
|
||||
containerName := pod.Spec.Containers[0].Name
|
||||
log.Printf("\nGETTING LOGS FROM CONTAINER %s", containerName)
|
||||
|
||||
containerLogOpts := &corev1.PodLogOptions{
|
||||
Container: containerName,
|
||||
TailLines: &tailLines,
|
||||
}
|
||||
|
||||
req := expandedPodClient.GetLogs(pod.Name, containerLogOpts)
|
||||
logs, err := req.DoRaw(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error getting container logs: %v", err)
|
||||
} else {
|
||||
log.Printf("Last 5 lines from container %s:", containerName)
|
||||
log.Println(string(logs))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Println("No CoreDNS pods found to demonstrate GetLogs")
|
||||
}
|
||||
|
||||
// Demonstrate using the generic SubResource method
|
||||
log.Println("\n=== SUBRESOURCE EXAMPLE ===")
|
||||
if len(dnsPods) > 0 {
|
||||
pod := dnsPods[0]
|
||||
log.Printf("Getting status subresource for pod %s", pod.Name)
|
||||
|
||||
req := podClient.SubResource(pod.Namespace, pod.Name, "status")
|
||||
statusBytes, err := req.DoRaw(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error getting pod status: %v", err)
|
||||
} else {
|
||||
// Just show that we got data (full status would be verbose)
|
||||
log.Printf("Got pod status (%d bytes)", len(statusBytes))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue