Skip to content

Commit

Permalink
Merge pull request #27 from keisku/issue26
Browse files Browse the repository at this point in the history
Fix: Cannot explain if a filed contains uppercase letters
  • Loading branch information
keisku authored Apr 27, 2024
2 parents 52e9f8a + a86a90b commit be9a469
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
24 changes: 0 additions & 24 deletions .github/workflows/go_test.yaml

This file was deleted.

38 changes: 38 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
on:
pull_request:
push:
branches:
- main
name: test
jobs:
test:
strategy:
matrix:
microk8s: [1.29/stable, 1.30/stable]
os: [ubuntu-latest]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install microk8s
run: |
sudo snap install microk8s --classic --channel=${{ matrix.microk8s }}
- name: Install Go
uses: actions/setup-go@v5
- name: go vet
run: go vet ./...
- name: go test
run: go test -v ./...
- name: Build kubectl-explore
run: |
go build
chmod +x kubectl-explore
sudo install kubectl-explore /usr/local/bin
- name: Run kubectl-explore
run: |
sudo microk8s kubectl explore hpa.*own.*id > /dev/null
sudo microk8s kubectl explore provider > /dev/null
sudo microk8s kubectl explore statefulset.spec.template.spec.volumes.projected.sources.serviceAccountToken.expirationSeconds > /dev/null
sudo microk8s kubectl explore sts.spec.template.spec.volumes.projected.sources.serviceAccountToken.expirationSeconds > /dev/null
sudo microk8s kubectl explore sts.spec.template.spec.volumes.projected.sources.serviceAccountToken.expir > /dev/null
35 changes: 21 additions & 14 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type Options struct {
Discovery discovery.CachedDiscoveryInterface
Schema openapi.Resources

inputFieldPath *regexp.Regexp
resource string
gvks []schema.GroupVersionKind
inputFieldPathRegex *regexp.Regexp
inputFieldPath string
gvks []schema.GroupVersionKind
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -93,13 +93,13 @@ func NewOptions(streams genericclioptions.IOStreams) *Options {
func (o *Options) Complete(f cmdutil.Factory, args []string) error {
var err error
if len(args) == 0 {
o.inputFieldPath = regexp.MustCompile(".*")
o.inputFieldPathRegex = regexp.MustCompile(".*")
} else {
o.inputFieldPath, err = regexp.Compile(args[0])
o.inputFieldPathRegex, err = regexp.Compile(args[0])
if err != nil {
return err
}
o.resource = args[0]
o.inputFieldPath = args[0]
}
o.Discovery, err = f.ToDiscoveryClient()
if err != nil {
Expand All @@ -113,7 +113,7 @@ func (o *Options) Complete(f cmdutil.Factory, args []string) error {
if err != nil {
return err
}
if o.resource == "" {
if o.inputFieldPath == "" {
gvk, err := o.findGVK()
if err != nil {
return err
Expand All @@ -123,8 +123,8 @@ func (o *Options) Complete(f cmdutil.Factory, args []string) error {
var gvk schema.GroupVersionKind
var err error
var idx int
for i := 1; i <= len(o.resource); i++ {
gvk, err = o.getGVK(o.resource[:i])
for i := 1; i <= len(o.inputFieldPath); i++ {
gvk, err = o.getGVK(o.inputFieldPath[:i])
if err != nil {
continue
}
Expand All @@ -137,10 +137,17 @@ func (o *Options) Complete(f cmdutil.Factory, args []string) error {
return err
}
} else {
// The left part of the input should be the resource name. E.g., "hpa", "sts", etc
// The right part of the input should be the field name or regex. E.g., "spec.replicas", "spec.*containers", etc
right := strings.TrimLeft(o.resource, o.resource[:idx])
o.inputFieldPath, err = regexp.Compile(strings.ToLower(right))
// In this case, the input includes the resource name.

// The left part of the input should be the resource name.
// E.g., "hpa", "statefulset", "node", etc.
left := o.inputFieldPath[:idx]

// The right part of the input should be the field or regex.
// E.g., "spec.template.spec.volumes.projected.sources.serviceAcc ", "spec.*containers", "spec.providerID", etc.
right := strings.TrimLeft(o.inputFieldPath, left)

o.inputFieldPathRegex, err = regexp.Compile(right)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,7 +175,7 @@ func (o *Options) Run() error {
return visitor.err
}
filteredPaths := visitor.listPaths(func(s string) bool {
return o.inputFieldPath.MatchString(s)
return o.inputFieldPathRegex.MatchString(s)
})
for _, p := range filteredPaths {
pathExplainers[p] = explainer{
Expand Down

0 comments on commit be9a469

Please sign in to comment.