Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify jq filter initialization and remove unused libpath #743

Merged
merged 16 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,11 @@ jobs:
go mod download
echo -n "Go modules unpacked size is: " && du -sh $HOME/go/pkg/mod

- name: Download prebuilt libjq static libraries
run: |
curl -sSfL https://github.com/flant/libjq-go/releases/download/jq-b6be13d5-0/libjq-glibc-amd64.tgz | tar zxf -

- name: Build binary
run: |
export CGO_ENABLED=1
export CGO_CFLAGS="-I$GITHUB_WORKSPACE/libjq/include"
export CGO_LDFLAGS="-L$GITHUB_WORKSPACE/libjq/lib"
export GOOS=linux

go build -tags use_libjq ./cmd/shell-operator
go build ./cmd/shell-operator

# MacOS build works fine because jq package already has static libraries.
# Windows build requires jq compilation, this should be done in libjq-go.
Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/tests-labeled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ jobs:
go mod download
echo -n "Go modules unpacked size is: " && du -sh $HOME/go/pkg/mod

- name: Download prebuilt libjq static libraries
run: |
curl -sSfL https://github.com/flant/libjq-go/releases/download/jq-b6be13d5-0/libjq-glibc-amd64.tgz | tar zxf -

- name: Install ginkgo
run: |
go install github.com/onsi/ginkgo/ginkgo
Expand All @@ -109,9 +105,6 @@ jobs:
env:
CLUSTER_NAME: ${{ matrix.cluster_name }}
run: |
export CGO_ENABLED=1
export CGO_CFLAGS="-I$GITHUB_WORKSPACE/libjq/include"
export CGO_LDFLAGS="-L$GITHUB_WORKSPACE/libjq/lib"
export GOOS=linux

ginkgo \
Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@ jobs:
go mod download
echo -n "Go modules unpacked size is: " && du -sh $HOME/go/pkg/mod

- name: Download prebuilt libjq static libraries
run: |
curl -sSfL https://github.com/flant/libjq-go/releases/download/jq-b6be13d5-0/libjq-glibc-amd64.tgz | tar zxf -

- name: Run unit tests
run: |
export CGO_ENABLED=1
export CGO_CFLAGS="-I$GITHUB_WORKSPACE/libjq/include"
export CGO_LDFLAGS="-L$GITHUB_WORKSPACE/libjq/lib"
export GOOS=linux

go test \
Expand Down
17 changes: 4 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Prebuilt libjq.
FROM --platform=${TARGETPLATFORM:-linux/amd64} flant/jq:b6be13d5-musl as libjq

# Go builder.
FROM --platform=${TARGETPLATFORM:-linux/amd64} golang:1.23-alpine3.21 AS builder

Expand All @@ -12,15 +9,10 @@ ADD go.mod go.sum /app/
WORKDIR /app
RUN go mod download

COPY --from=libjq /libjq /libjq
ADD . /app

RUN CGO_ENABLED=1 \
CGO_CFLAGS="-I/libjq/include" \
CGO_LDFLAGS="-L/libjq/lib" \
GOOS=linux \
go build -ldflags="-linkmode external -extldflags '-static' -s -w -X 'github.com/flant/shell-operator/pkg/app.Version=$appVersion'" \
-tags use_libjq \
RUN GOOS=linux \
go build -ldflags="-s -w -X 'github.com/flant/shell-operator/pkg/app.Version=$appVersion'" \
-o shell-operator \
./cmd/shell-operator

Expand All @@ -35,10 +27,9 @@ RUN apk --no-cache add ca-certificates bash sed tini && \
mkdir /hooks
ADD frameworks/shell /frameworks/shell
ADD shell_lib.sh /
COPY --from=libjq /bin/jq /usr/bin
COPY --from=builder /app/shell-operator /
WORKDIR /
ENV SHELL_OPERATOR_HOOKS_DIR /hooks
ENV LOG_TYPE json
ENV SHELL_OPERATOR_HOOKS_DIR=/hooks
ENV LOG_TYPE=json
ENTRYPOINT ["/sbin/tini", "--", "/shell-operator"]
CMD ["start"]
2 changes: 1 addition & 1 deletion cmd/shell-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
// print version
kpApp.Command("version", "Show version.").Action(func(_ *kingpin.ParseContext) error {
fmt.Printf("%s %s\n", app.AppName, app.Version)
fl := jq.NewFilter(app.JqLibraryPath)
fl := jq.NewFilter()
fmt.Println(fl.FilterInfo())
return nil
})
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.23.1
require (
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241205040953-7b376bae249c
github.com/flant/kube-client v1.2.2
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee // branch: master
github.com/go-chi/chi/v5 v5.2.1
github.com/go-openapi/spec v0.19.8
github.com/go-openapi/strfmt v0.19.5
Expand Down Expand Up @@ -36,6 +35,7 @@ replace github.com/go-openapi/validate => github.com/flant/go-openapi-validate v
require (
github.com/deckhouse/module-sdk v0.2.0
github.com/gojuno/minimock/v3 v3.4.5
github.com/itchyny/gojq v0.12.17
)

require (
Expand Down Expand Up @@ -71,6 +71,7 @@ require (
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ github.com/flant/go-openapi-validate v0.19.12-flant.0 h1:xk6kvc9fHKMgUdB6J7kbpbL
github.com/flant/go-openapi-validate v0.19.12-flant.0/go.mod h1:Rzou8hA/CBw8donlS6WNEUQupNvUZ0waH08tGe6kAQ4=
github.com/flant/kube-client v1.2.2 h1:27LBs+PKJEFnkQXjPU9eIps7a7iyI13AKcSYj897DCU=
github.com/flant/kube-client v1.2.2/go.mod h1:eMa3aJ6V1PRWSQ/RCROkObDpY4S74uM84SJS4G/LINg=
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee h1:evii83J+/6QGNvyf6tjQ/p27DPY9iftxIBb37ALJRTg=
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee/go.mod h1:f+REaGl/+pZR97rbTcwHEka/MAipoQQ2Mc0iQUj4ak0=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down Expand Up @@ -176,6 +174,10 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/itchyny/gojq v0.12.17 h1:8av8eGduDb5+rvEdaOO+zQUjA04MS0m3Ps8HiD+fceg=
github.com/itchyny/gojq v0.12.17/go.mod h1:WBrEMkgAfAGO1LUcGOckBl5O726KPp+OlkKug0I/FEY=
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
Expand Down
1 change: 0 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func DefineStartCommandFlags(kpApp *kingpin.Application, cmd *kingpin.CmdClause)
DefineKubeClientFlags(cmd)
DefineValidatingWebhookFlags(cmd)
DefineConversionWebhookFlags(cmd)
DefineJqFlags(cmd)
DefineLoggingFlags(cmd)
DefineDebugFlags(kpApp, cmd)
}
Expand Down
13 changes: 0 additions & 13 deletions pkg/app/jq.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/filter/filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package filter

type Filter interface {
ApplyFilter(filterStr string, data []byte) (string, error)
ApplyFilter(filterStr string, data map[string]any) (map[string]any, error)
FilterInfo() string
}
61 changes: 61 additions & 0 deletions pkg/filter/jq/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package jq

import (
"encoding/json"
"errors"
"maps"

"github.com/itchyny/gojq"

"github.com/flant/shell-operator/pkg/filter"
)

var _ filter.Filter = (*Filter)(nil)

func NewFilter() *Filter {
return &Filter{}
}

type Filter struct{}

// ApplyFilter runs jq expression provided in jqFilter with jsonData as input.
func (f *Filter) ApplyFilter(jqFilter string, data map[string]any) (map[string]any, error) {
query, err := gojq.Parse(jqFilter)
if err != nil {
return nil, err
}

// gojs will normalize numbers in the input data, we should create new map for prevent changes in input data
workData := deepCopy(data)
iter := query.Run(workData)
result := make(map[string]any)
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
var errGoJq *gojq.HaltError
if errors.As(err, &errGoJq) && errGoJq.Value() == nil {
break
}
return nil, err
}
if resultMap, ok := v.(map[string]any); ok {
maps.Copy(result, resultMap)
}
}

return result, nil
}

func (f *Filter) FilterInfo() string {
return "jqFilter implementation: using itchyny/gojq"
}

func deepCopy(input map[string]any) map[string]any {
data, _ := json.Marshal(input)
var output map[string]any
_ = json.Unmarshal(data, &output)
return output
}
29 changes: 0 additions & 29 deletions pkg/filter/jq/apply_jq_exec.go

This file was deleted.

49 changes: 0 additions & 49 deletions pkg/filter/jq/apply_libjq_go.go

This file was deleted.

Loading
Loading