Skip to content

Commit 32f5aba

Browse files
authored
Add osutils, grpcutils, requestutils (#84)
These were part of the brokers tree in the ironcore repository and widely used by providers. Instead of maintaining a dependency to the borkers the helpers are split into this repository instead. Signed-off-by: Maximilian Moehl <maximilian@moehl.eu>
1 parent bc9e5ff commit 32f5aba

9 files changed

Lines changed: 457 additions & 1 deletion

File tree

‎go.mod‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/opencontainers/image-spec v1.1.1
1414
github.com/prometheus/procfs v0.22.0
1515
go.uber.org/zap v1.28.0
16+
google.golang.org/grpc v1.83.2
1617
k8s.io/apimachinery v0.33.4
1718
sigs.k8s.io/controller-runtime v0.21.0
1819
)
@@ -21,6 +22,7 @@ require (
2122
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect
2223
github.com/Masterminds/semver/v3 v3.4.0 // indirect
2324
github.com/Microsoft/hcsshim v0.13.0 // indirect
25+
github.com/beorn7/perks v1.0.1 // indirect
2426
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2527
github.com/containerd/cgroups/v3 v3.0.5 // indirect
2628
github.com/containerd/continuity v0.4.5 // indirect
@@ -33,6 +35,7 @@ require (
3335
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
3436
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
3537
github.com/felixge/httpsnoop v1.0.4 // indirect
38+
github.com/fsnotify/fsnotify v1.9.0 // indirect
3639
github.com/fxamacker/cbor/v2 v2.8.0 // indirect
3740
github.com/go-logr/stdr v1.2.2 // indirect
3841
github.com/go-logr/zapr v1.3.0 // indirect
@@ -42,6 +45,7 @@ require (
4245
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
4346
github.com/gogo/protobuf v1.3.2 // indirect
4447
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
48+
github.com/google/btree v1.1.3 // indirect
4549
github.com/google/gnostic-models v0.6.9 // indirect
4650
github.com/google/go-cmp v0.7.0 // indirect
4751
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 // indirect
@@ -56,7 +60,12 @@ require (
5660
github.com/modern-go/reflect2 v1.0.2 // indirect
5761
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5862
github.com/opencontainers/go-digest v1.0.0 // indirect
63+
github.com/pkg/errors v0.9.1 // indirect
64+
github.com/prometheus/client_golang v1.22.0 // indirect
65+
github.com/prometheus/client_model v0.6.2 // indirect
66+
github.com/prometheus/common v0.64.0 // indirect
5967
github.com/sirupsen/logrus v1.9.3 // indirect
68+
github.com/spf13/pflag v1.0.9 // indirect
6069
github.com/x448/float16 v0.8.4 // indirect
6170
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
6271
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0 // indirect
@@ -75,12 +84,14 @@ require (
7584
golang.org/x/text v0.41.0 // indirect
7685
golang.org/x/time v0.12.0 // indirect
7786
golang.org/x/tools v0.48.0 // indirect
87+
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
7888
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect
79-
google.golang.org/grpc v1.83.2 // indirect
8089
google.golang.org/protobuf v1.36.11 // indirect
90+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
8191
gopkg.in/inf.v0 v0.9.1 // indirect
8292
gopkg.in/yaml.v3 v3.0.1 // indirect
8393
k8s.io/api v0.33.4 // indirect
94+
k8s.io/apiextensions-apiserver v0.33.3 // indirect
8495
k8s.io/client-go v0.33.4 // indirect
8596
k8s.io/klog/v2 v2.130.1 // indirect
8697
k8s.io/kube-openapi v0.0.0-20250610211856-8b98d1ed966a // indirect

‎go.sum‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo
66
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
77
github.com/Microsoft/hcsshim v0.13.0 h1:/BcXOiS6Qi7N9XqUcv27vkIuVOkBEcWstd2pMlWSeaA=
88
github.com/Microsoft/hcsshim v0.13.0/go.mod h1:9KWJ/8DgU+QzYGupX4tzMhRQE8h6w90lH6HAaclpEok=
9+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
10+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
911
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
1012
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
1113
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@@ -34,10 +36,14 @@ github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5Qvfr
3436
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
3537
github.com/emicklei/go-restful/v3 v3.12.2 h1:DhwDP0vY3k8ZzE0RunuJy8GhNpPL6zqLkDf9B/a0/xU=
3638
github.com/emicklei/go-restful/v3 v3.12.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
39+
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
40+
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
3741
github.com/evanphx/json-patch/v5 v5.9.11 h1:/8HVnzMq13/3x9TPvjG08wUGqBTmZBsCWzjTM0wiaDU=
3842
github.com/evanphx/json-patch/v5 v5.9.11/go.mod h1:3j+LviiESTElxA4p3EMKAB9HXj3/XEtnUf6OZxqIQTM=
3943
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
4044
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
45+
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
46+
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
4147
github.com/fxamacker/cbor/v2 v2.8.0 h1:fFtUGXUzXPHTIUdne5+zzMPTfffl3RD5qYnkY40vtxU=
4248
github.com/fxamacker/cbor/v2 v2.8.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
4349
github.com/gkampitakis/ciinfo v0.3.2 h1:JcuOPk8ZU7nZQjdUhctuhQofk7BGHuIy0c9Ez8BNhXs=
@@ -69,12 +75,16 @@ github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8J
6975
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
7076
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
7177
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
78+
github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg=
79+
github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
7280
github.com/google/gnostic-models v0.6.9 h1:MU/8wDLif2qCXZmzncUQ/BOfxWfthHi63KqpoNbWqVw=
7381
github.com/google/gnostic-models v0.6.9/go.mod h1:CiWsm0s6BSQd1hRn8/QmxqB6BesYcbSZxsz9b0KuDBw=
7482
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7583
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
7684
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
7785
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
86+
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
87+
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
7888
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 h1:EwtI+Al+DeppwYX2oXJCETMO23COyaKGP6fHVpkpWpg=
7989
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI=
8090
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -99,6 +109,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
99109
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
100110
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
101111
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
112+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
113+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
102114
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
103115
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
104116
github.com/maruel/natural v1.1.1 h1:Hja7XhhmvEFhcByqDoHz9QZbkWey+COd9xWfCfn1ioo=
@@ -131,6 +143,12 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
131143
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
132144
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
133145
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
146+
github.com/prometheus/client_golang v1.22.0 h1:rb93p9lokFEsctTys46VnV1kLCDpVZ0a/Y92Vm0Zc6Q=
147+
github.com/prometheus/client_golang v1.22.0/go.mod h1:R7ljNsLXhuQXYZYtw6GAE9AZg8Y7vEW5scdCXrWRXC0=
148+
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
149+
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
150+
github.com/prometheus/common v0.64.0 h1:pdZeA+g617P7oGv1CzdTzyeShxAGrTBsolKNOLQPGO4=
151+
github.com/prometheus/common v0.64.0/go.mod h1:0gZns+BLRQ3V6NdaerOhMbwwRbNh9hkGINtQAsP5GS8=
134152
github.com/prometheus/procfs v0.22.0 h1:6q9+/JL9IKAPbCmBrv9n5O5Ty3NKnciV5X7YGw0oics=
135153
github.com/prometheus/procfs v0.22.0/go.mod h1:CvmFr/GVhIjIvWJZW3tgkODBQMRIf0EyWMQLHCHab58=
136154
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
@@ -230,6 +248,10 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
230248
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
231249
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
232250
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
251+
gomodules.xyz/jsonpatch/v2 v2.5.0 h1:JELs8RLM12qJGXU4u/TO3V25KW8GreMKl9pdkk14RM0=
252+
gomodules.xyz/jsonpatch/v2 v2.5.0/go.mod h1:AH3dM2RI6uoBZxn3LVrfvJ3E0/9dG4cSrbuBJT4moAY=
253+
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
254+
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
233255
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa h1:mZHHdPZl0dbGHCflZgAq/Q468DWVFcU2whhB2KAo8fk=
234256
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
235257
google.golang.org/grpc v1.83.2 h1:EManeRomTObA0BU7I8vXgg/78uE5MJ9M8B39EX2WscU=

‎grpcutils/grpc.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package grpcutils
5+
6+
import (
7+
"context"
8+
9+
"github.com/go-logr/logr"
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/status"
13+
ctrl "sigs.k8s.io/controller-runtime"
14+
)
15+
16+
// InjectLogger injects the given logr.Logger into the context using ctrl.LoggerInto.
17+
func InjectLogger(log logr.Logger) grpc.UnaryServerInterceptor {
18+
return grpc.UnaryServerInterceptor(func(
19+
ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler,
20+
) (resp interface{}, err error) {
21+
log := log.WithName(info.FullMethod)
22+
ctx = ctrl.LoggerInto(ctx, log)
23+
return handler(ctx, req)
24+
})
25+
}
26+
27+
// LogRequest logs grpc requests. In case any request returns with status.Code == codes.Unknown, the error is logged.
28+
var LogRequest = grpc.UnaryServerInterceptor(func(
29+
ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler,
30+
) (resp interface{}, err error) {
31+
log := ctrl.LoggerFrom(ctx)
32+
log.V(1).Info("Request")
33+
resp, err = handler(ctx, req)
34+
if err != nil {
35+
if code := status.Code(err); code == codes.Unknown {
36+
log.Error(err, "Unknown error handling request")
37+
}
38+
}
39+
return resp, err
40+
})

‎osutils/osutils_suite_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package osutils_test
5+
6+
import (
7+
"testing"
8+
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
)
12+
13+
func TestOsutils(t *testing.T) {
14+
RegisterFailHandler(Fail)
15+
RunSpecs(t, "Osutils Suite")
16+
}

‎osutils/socket.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package osutils
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"os"
10+
)
11+
12+
// CleanupSocketIfExists deletes any leftover socket at the given address, if any.
13+
//
14+
// If the file at the given address is no socket, an error is returned.
15+
func CleanupSocketIfExists(address string) error {
16+
stat, err := os.Stat(address)
17+
if err != nil {
18+
if !errors.Is(err, os.ErrNotExist) {
19+
return fmt.Errorf("error stat-ing socket %q: %w", address, err)
20+
}
21+
return nil
22+
}
23+
24+
if stat.Mode().Type()&os.ModeSocket == 0 {
25+
return fmt.Errorf("file at %s is not a socket", address)
26+
}
27+
28+
if err := os.Remove(address); err != nil && !errors.Is(err, os.ErrNotExist) {
29+
return fmt.Errorf("error removing socket: %w", err)
30+
}
31+
return nil
32+
}

‎osutils/socket_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package osutils_test
5+
6+
import (
7+
"net"
8+
"os"
9+
"path/filepath"
10+
11+
. "github.com/onsi/ginkgo/v2"
12+
. "github.com/onsi/gomega"
13+
14+
. "github.com/ironcore-dev/provider-utils/osutils"
15+
)
16+
17+
var _ = Describe("CleanupSocketIfExists", func() {
18+
var dir string
19+
20+
BeforeEach(func() {
21+
dir = GinkgoT().TempDir()
22+
})
23+
24+
It("should succeed if the socket does not exist", func() {
25+
Expect(CleanupSocketIfExists(filepath.Join(dir, "foo.sock"))).To(Succeed())
26+
})
27+
28+
It("should remove an existing socket", func() {
29+
address := filepath.Join(dir, "foo.sock")
30+
l, err := net.Listen("unix", address)
31+
Expect(err).NotTo(HaveOccurred())
32+
defer func() { _ = l.Close() }()
33+
34+
Expect(CleanupSocketIfExists(address)).To(Succeed())
35+
Expect(address).NotTo(BeAnExistingFile())
36+
})
37+
38+
It("should error on an existing non-socket file", func() {
39+
address := filepath.Join(dir, "foo.sock")
40+
Expect(os.WriteFile(address, []byte("foo"), 0600)).To(Succeed())
41+
42+
Expect(CleanupSocketIfExists(address)).To(MatchError(ContainSubstring("is not a socket")))
43+
Expect(address).To(BeAnExistingFile())
44+
})
45+
})

0 commit comments

Comments
 (0)