Skip to content

Commit

Permalink
Merge pull request #18 from cybozu-go/bash-completion
Browse files Browse the repository at this point in the history
Implement completion
  • Loading branch information
yokaze authored Dec 6, 2024
2 parents 10f88e5 + 43751d7 commit 995d2e4
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 29 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
24 changes: 22 additions & 2 deletions cmd/npv/app/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
)

var cachedCiliumClients map[string]*client.Client
Expand All @@ -27,7 +27,7 @@ func init() {
}

func createK8sClients() (*kubernetes.Clientset, *dynamic.DynamicClient, error) {
config, err := rest.InClusterConfig()
config, err := ctrl.GetConfig()
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -121,6 +121,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
48 changes: 48 additions & 0 deletions cmd/npv/app/helper_completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 {
return
}

nss, err := clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
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
19 changes: 16 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ require (
k8s.io/api v0.31.2
k8s.io/apimachinery v0.31.2
k8s.io/client-go v0.31.2
sigs.k8s.io/controller-runtime v0.19.3
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.2 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand All @@ -36,6 +40,7 @@ require (
github.com/go-openapi/validate v0.22.3 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand All @@ -44,6 +49,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -58,7 +64,12 @@ require (
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
Expand All @@ -77,9 +88,9 @@ require (
github.com/x448/float16 v0.8.4 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.mongodb.org/mongo-driver v1.13.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/net v0.30.0 // indirect
Expand All @@ -90,11 +101,13 @@ require (
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.26.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
Expand Down
Loading

0 comments on commit 995d2e4

Please sign in to comment.