Skip to content

Commit

Permalink
Implement completion
Browse files Browse the repository at this point in the history
Signed-off-by: Daichi Sakaue <[email protected]>
  • Loading branch information
yokaze committed Dec 6, 2024
1 parent cb15a65 commit bec5062
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 16 deletions.
1 change: 1 addition & 0 deletions cmd/npv/app/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var dumpCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
return runDump(context.Background(), cmd.OutOrStdout(), args[0])
},
ValidArgsFunction: completePods,
}

func runDump(ctx context.Context, w io.Writer, name string) error {
Expand Down
20 changes: 20 additions & 0 deletions cmd/npv/app/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ func getPodIdentity(ctx context.Context, d *dynamic.DynamicClient, namespace, na
return identity, nil
}

func listRelevantPods(ctx context.Context, c *kubernetes.Clientset, namespace string) ([]corev1.Pod, error) {
pods, err := c.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}

ret := make([]corev1.Pod, 0)
for _, p := range pods.Items {
// Skip non-relevant pods
if p.Spec.HostNetwork {
continue
}
if p.Status.Phase != corev1.PodRunning {
continue
}
ret = append(ret, p)
}
return ret, nil
}

// key: identity number
// value: CiliumIdentity resource
func getIdentityResourceMap(ctx context.Context, d *dynamic.DynamicClient) (map[int]*unstructured.Unstructured, error) {
Expand Down
50 changes: 50 additions & 0 deletions cmd/npv/app/helper_completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package app

import (
"context"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func completeNamespaces(cmd *cobra.Command, args []string, toComplete string) (ret []string, directive cobra.ShellCompDirective) {
ret = make([]string, 0)
directive = cobra.ShellCompDirectiveNoFileComp

clientset, _, err := createK8sClients()
if err != nil {
ret = append(ret, "error1")
return
}

nss, err := clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
ret = append(ret, "error2")
return
}

for _, ns := range nss.Items {
ret = append(ret, ns.Name)
}
return
}

func completePods(cmd *cobra.Command, args []string, toComplete string) (ret []string, directive cobra.ShellCompDirective) {
ret = make([]string, 0)
directive = cobra.ShellCompDirectiveNoFileComp

clientset, _, err := createK8sClients()
if err != nil {
return
}

pods, err := listRelevantPods(context.Background(), clientset, rootOptions.namespace)
if err != nil {
return
}

for _, p := range pods {
ret = append(ret, p.Name)
}
return
}
1 change: 1 addition & 0 deletions cmd/npv/app/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var inspectCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
return runInspect(context.Background(), cmd.OutOrStdout(), args[0])
},
ValidArgsFunction: completePods,
}

type policyEntryKey struct {
Expand Down
1 change: 1 addition & 0 deletions cmd/npv/app/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var listCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
return runList(context.Background(), cmd.OutOrStdout(), args[0])
},
ValidArgsFunction: completePods,
}

type derivedFromEntry struct {
Expand Down
5 changes: 4 additions & 1 deletion cmd/npv/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ func init() {
rootCmd.PersistentFlags().Uint16Var(&rootOptions.proxyPort, "proxy-port", 8080, "port number of the proxy endpoints")
rootCmd.PersistentFlags().StringVarP(&rootOptions.output, "output", "o", OutputSimple, "output format")
rootCmd.PersistentFlags().BoolVar(&rootOptions.noHeaders, "no-headers", false, "stop printing header")
rootCmd.RegisterFlagCompletionFunc("namespace", completeNamespaces)
}

var rootCmd = &cobra.Command{}
var rootCmd = &cobra.Command{
Use: "npv",
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
Expand Down
14 changes: 2 additions & 12 deletions cmd/npv/app/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"strings"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func init() {
Expand Down Expand Up @@ -50,24 +48,16 @@ func runSummary(ctx context.Context, w io.Writer) error {
}

summary := make([]summaryEntry, 0)
pods, err := clientset.CoreV1().Pods(rootOptions.namespace).List(ctx, metav1.ListOptions{})
pods, err := listRelevantPods(ctx, clientset, rootOptions.namespace)
if err != nil {
return err
}

for _, p := range pods.Items {
for _, p := range pods {
var entry summaryEntry
entry.Namespace = p.Namespace
entry.Name = p.Name

// Skip non-relevant pods
if p.Spec.HostNetwork {
continue
}
if p.Status.Phase != corev1.PodRunning {
continue
}

policies, err := queryPolicyMap(ctx, clientset, dynamicClient, rootOptions.namespace, p.Name)
if err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions e2e/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,19 @@ install-test-pod:
install-policy-viewer:
$(MAKE) -C ../ build
PODNAME=$$(kubectl get po -l app=ubuntu -o name | cut -d'/' -f2); \
kubectl cp testdata/onboard $${PODNAME}:/tmp/; \
kubectl exec $${PODNAME} -- chmod +x /tmp/onboard; \
kubectl exec $${PODNAME} -- /tmp/onboard; \
kubectl cp $(POLICY_VIEWER) $${PODNAME}:/tmp/; \
kubectl exec $${PODNAME} -- chmod +x /tmp/npv; \
kubectl cp $$(aqua which kubectl) $${PODNAME}:/tmp/; \
kubectl exec $${PODNAME} -- chmod +x /tmp/kubectl

.PHONY: pilot
pilot:
@PODNAME=$$(kubectl get po -l app=ubuntu -o name | cut -d'/' -f2); \
kubectl exec -it $${PODNAME} -- bash

.PHONY: test
test:
go test -v -race . -ginkgo.v -ginkgo.fail-fast
Expand Down
8 changes: 8 additions & 0 deletions e2e/testdata/onboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

if ! grep boarded /root/.bashrc > /dev/null 2>&1; then
echo '. /usr/share/bash-completion/bash_completion' >> /root/.bashrc
echo 'export PATH=${PATH}:/tmp' >> /root/.bashrc
echo '. <(npv completion bash)' >> /root/.bashrc
echo '# boarded' >> /root/.bashrc
fi
4 changes: 1 addition & 3 deletions e2e/testdata/ubuntu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rules:
- apiGroups:
- ""
resources:
- namespaces
- pods
verbs:
- get
Expand Down Expand Up @@ -58,9 +59,6 @@ spec:
app: ubuntu
spec:
serviceAccountName: ubuntu
securityContext:
runAsUser: 1000
runAsGroup: 1000
containers:
- name: ubuntu
args:
Expand Down

0 comments on commit bec5062

Please sign in to comment.