Skip to content

Commit 4109488

Browse files
authored
Added plugin config support (#13)
Changed generated protobuf to use API_OPAQUE. Changed buf plugins from remote to local.
1 parent 83f4c6d commit 4109488

14 files changed

Lines changed: 855 additions & 354 deletions

File tree

‎Makefile‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
5252
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
5353

5454
.PHONY: generate
55-
generate: controller-gen buf ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
55+
generate: controller-gen protoc-gen-go protoc-gen-go-grpc buf ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
5656
$(BUF) generate
5757
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
5858

@@ -201,6 +201,8 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
201201
ENVTEST ?= $(LOCALBIN)/setup-envtest
202202
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
203203
BUF = $(LOCALBIN)/buf
204+
PROTOC_GEN_GO = $(LOCALBIN)/protoc-gen-go
205+
PROTOC_GEN_GO_GRPC = $(LOCALBIN)/protoc-gen-go-grpc
204206

205207
## Tool Versions
206208
KUSTOMIZE_VERSION ?= v5.6.0
@@ -209,8 +211,10 @@ CONTROLLER_TOOLS_VERSION ?= v0.18.0
209211
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
210212
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
211213
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
212-
GOLANGCI_LINT_VERSION ?= v2.3.0
213-
BUF_VERSION ?= v1.56.0
214+
GOLANGCI_LINT_VERSION ?= v2.4.0
215+
BUF_VERSION ?= v1.57.2
216+
PROTOC_GEN_GO_VERSION ?= v1.36.9
217+
PROTOC_GEN_GO_GRPC_VERSION ?= v1.5.1
214218

215219
.PHONY: kustomize
216220
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
@@ -227,6 +231,16 @@ buf: $(BUF) ## Download buf locally if necessary.
227231
$(BUF): $(LOCALBIN)
228232
$(call go-install-tool,$(BUF),github.com/bufbuild/buf/cmd/buf,$(BUF_VERSION))
229233

234+
.PHONY: protoc-gen-go
235+
protoc-gen-go: $(PROTOC_GEN_GO) ## Download protoc-gen-go locally if necessary.
236+
$(PROTOC_GEN_GO): $(LOCALBIN)
237+
$(call go-install-tool,$(PROTOC_GEN_GO),google.golang.org/protobuf/cmd/protoc-gen-go,$(PROTOC_GEN_GO_VERSION))
238+
239+
.PHONY: protoc-gen-go-grpc
240+
protoc-gen-go-grpc: $(PROTOC_GEN_GO_GRPC) ## Download protoc-gen-go-grpc locally if necessary.
241+
$(PROTOC_GEN_GO_GRPC): $(LOCALBIN)
242+
$(call go-install-tool,$(PROTOC_GEN_GO_GRPC),google.golang.org/grpc/cmd/protoc-gen-go-grpc,$(PROTOC_GEN_GO_GRPC_VERSION))
243+
230244
.PHONY: setup-envtest
231245
setup-envtest: envtest ## Download the binaries required for ENVTEST in the local bin directory.
232246
@echo "Setting up envtest binaries for Kubernetes version $(ENVTEST_K8S_VERSION)..."

‎api/plugins/drain-client.go‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,72 @@ import (
44
"context"
55

66
"github.com/hashicorp/go-hclog"
7-
proto "github.com/slyngdk/node-drain/api/plugins/proto/v1"
7+
pluginv1 "github.com/slyngdk/node-drain/api/plugins/proto/v1"
8+
"google.golang.org/protobuf/proto"
89
)
910

1011
var _ DrainPlugin = &DrainClient{}
1112

12-
type DrainClient struct{ client proto.DrainServiceClient }
13+
type DrainClient struct{ client pluginv1.DrainServiceClient }
1314

14-
func (c DrainClient) Init(ctx context.Context, logger hclog.Logger, settings DrainPluginSettings) (DrainPluginInfo, error) {
15-
resp, err := c.client.Init(ctx, &proto.InitRequest{})
15+
func (c DrainClient) PluginInfo(ctx context.Context) (DrainPluginInfo, error) {
16+
info, err := c.client.PluginInfo(ctx, &pluginv1.PluginInfoRequest{})
1617
if err != nil {
1718
return DrainPluginInfo{}, err
1819
}
19-
2020
return DrainPluginInfo{
21-
ID: resp.Id,
21+
ID: info.GetId(),
22+
ConfigFormat: info.GetConfigFormat(),
2223
}, nil
2324
}
2425

26+
func (c DrainClient) Init(ctx context.Context, _ hclog.Logger, settings DrainPluginSettings) error {
27+
_, err := c.client.Init(ctx, pluginv1.InitRequest_builder{
28+
Config: settings.Config,
29+
}.Build())
30+
if err != nil {
31+
return err
32+
}
33+
34+
return nil
35+
}
36+
2537
func (c DrainClient) IsSupported(ctx context.Context) (bool, error) {
26-
resp, err := c.client.IsSupported(ctx, &proto.IsSupportedRequest{})
38+
resp, err := c.client.IsSupported(ctx, &pluginv1.IsSupportedRequest{})
2739
if err != nil {
2840
return false, err
2941
}
30-
return resp.Supported, nil
42+
return resp.GetSupported(), nil
3143
}
3244

3345
func (c DrainClient) IsHealthy(ctx context.Context) (bool, error) {
34-
resp, err := c.client.IsHealthy(ctx, &proto.IsHealthyRequest{})
46+
resp, err := c.client.IsHealthy(ctx, &pluginv1.IsHealthyRequest{})
3547
if err != nil {
3648
return false, err
3749
}
38-
return resp.Healthy, nil
50+
return resp.GetHealthy(), nil
3951
}
4052

4153
func (c DrainClient) IsDrainOk(ctx context.Context, nodeName string) (bool, error) {
42-
resp, err := c.client.IsDrainOk(ctx, &proto.IsDrainOkRequest{NodeName: nodeName})
54+
resp, err := c.client.IsDrainOk(ctx, pluginv1.IsDrainOkRequest_builder{
55+
NodeName: proto.String(nodeName),
56+
}.Build())
4357
if err != nil {
4458
return false, err
4559
}
46-
return resp.Ok, nil
60+
return resp.GetOk(), nil
4761
}
4862

4963
func (c DrainClient) PreDrain(ctx context.Context, nodeName string) error {
50-
_, err := c.client.PreDrain(ctx, &proto.PreDrainRequest{NodeName: nodeName})
64+
_, err := c.client.PreDrain(ctx, pluginv1.PreDrainRequest_builder{
65+
NodeName: proto.String(nodeName),
66+
}.Build())
5167
return err
5268
}
5369

5470
func (c DrainClient) PostDrain(ctx context.Context, nodeName string) error {
55-
_, err := c.client.PostDrain(ctx, &proto.PostDrainRequest{NodeName: nodeName})
71+
_, err := c.client.PostDrain(ctx, pluginv1.PostDrainRequest_builder{
72+
NodeName: proto.String(nodeName),
73+
}.Build())
5674
return err
5775
}

‎api/plugins/drain-server.go‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,68 @@ import (
66

77
"github.com/hashicorp/go-hclog"
88
"github.com/hashicorp/go-plugin"
9-
proto "github.com/slyngdk/node-drain/api/plugins/proto/v1"
9+
pluginv1 "github.com/slyngdk/node-drain/api/plugins/proto/v1"
10+
"google.golang.org/protobuf/proto"
1011
)
1112

12-
var _ proto.DrainServiceServer = DrainServer{}
13+
var _ pluginv1.DrainServiceServer = DrainServer{}
1314

1415
type DrainServer struct {
1516
Logger hclog.Logger
1617
Impl DrainPlugin
1718
}
1819

19-
func (s DrainServer) Init(ctx context.Context, request *proto.InitRequest) (*proto.InitResponse, error) {
20-
info, err := s.Impl.Init(ctx, s.Logger, DrainPluginSettings{})
20+
func (s DrainServer) PluginInfo(ctx context.Context, _ *pluginv1.PluginInfoRequest) (*pluginv1.PluginInfoResponse, error) {
21+
info, err := s.Impl.PluginInfo(ctx)
22+
if err != nil {
23+
return nil, err
24+
}
25+
return pluginv1.PluginInfoResponse_builder{
26+
Id: proto.String(info.ID),
27+
ConfigFormat: &info.ConfigFormat,
28+
}.Build(), nil
29+
}
30+
31+
func (s DrainServer) Init(ctx context.Context, request *pluginv1.InitRequest) (*pluginv1.InitResponse, error) {
32+
err := s.Impl.Init(ctx, s.Logger, DrainPluginSettings{
33+
Config: request.GetConfig(),
34+
})
2135
if err != nil {
2236
return nil, err
2337
}
2438

25-
return &proto.InitResponse{
26-
Id: info.ID,
27-
}, nil
39+
return &pluginv1.InitResponse{}, nil
2840
}
2941

30-
func (s DrainServer) IsSupported(ctx context.Context, request *proto.IsSupportedRequest) (*proto.IsSupportedResponse, error) {
42+
func (s DrainServer) IsSupported(ctx context.Context, _ *pluginv1.IsSupportedRequest) (*pluginv1.IsSupportedResponse, error) {
3143
supported, err := s.Impl.IsSupported(ctx)
32-
return &proto.IsSupportedResponse{Supported: supported}, err
44+
return pluginv1.IsSupportedResponse_builder{
45+
Supported: proto.Bool(supported),
46+
}.Build(), err
3347
}
3448

35-
func (s DrainServer) IsHealthy(ctx context.Context, request *proto.IsHealthyRequest) (*proto.IsHealthyResponse, error) {
49+
func (s DrainServer) IsHealthy(ctx context.Context, _ *pluginv1.IsHealthyRequest) (*pluginv1.IsHealthyResponse, error) {
3650
healthy, err := s.Impl.IsHealthy(ctx)
37-
return &proto.IsHealthyResponse{Healthy: healthy}, err
51+
return pluginv1.IsHealthyResponse_builder{
52+
Healthy: proto.Bool(healthy),
53+
}.Build(), err
3854
}
3955

40-
func (s DrainServer) IsDrainOk(ctx context.Context, request *proto.IsDrainOkRequest) (*proto.IsDrainOkResponse, error) {
41-
ok, err := s.Impl.IsDrainOk(ctx, request.NodeName)
42-
return &proto.IsDrainOkResponse{Ok: ok}, err
56+
func (s DrainServer) IsDrainOk(ctx context.Context, request *pluginv1.IsDrainOkRequest) (*pluginv1.IsDrainOkResponse, error) {
57+
ok, err := s.Impl.IsDrainOk(ctx, request.GetNodeName())
58+
return pluginv1.IsDrainOkResponse_builder{
59+
Ok: proto.Bool(ok),
60+
}.Build(), err
4361
}
4462

45-
func (s DrainServer) PreDrain(ctx context.Context, request *proto.PreDrainRequest) (*proto.PreDrainResponse, error) {
46-
err := s.Impl.PreDrain(ctx, request.NodeName)
47-
return &proto.PreDrainResponse{}, err
63+
func (s DrainServer) PreDrain(ctx context.Context, request *pluginv1.PreDrainRequest) (*pluginv1.PreDrainResponse, error) {
64+
err := s.Impl.PreDrain(ctx, request.GetNodeName())
65+
return &pluginv1.PreDrainResponse{}, err
4866
}
4967

50-
func (s DrainServer) PostDrain(ctx context.Context, request *proto.PostDrainRequest) (*proto.PostDrainResponse, error) {
51-
err := s.Impl.PostDrain(ctx, request.NodeName)
52-
return &proto.PostDrainResponse{}, err
68+
func (s DrainServer) PostDrain(ctx context.Context, request *pluginv1.PostDrainRequest) (*pluginv1.PostDrainResponse, error) {
69+
err := s.Impl.PostDrain(ctx, request.GetNodeName())
70+
return &pluginv1.PostDrainResponse{}, err
5371
}
5472

5573
func Serve(impl DrainPlugin) {

‎api/plugins/drain.go‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/hashicorp/go-plugin"
88
"google.golang.org/grpc"
99

10-
proto "github.com/slyngdk/node-drain/api/plugins/proto/v1"
10+
pluginv1 "github.com/slyngdk/node-drain/api/plugins/proto/v1"
1111
)
1212

1313
var Handshake = plugin.HandshakeConfig{
@@ -16,12 +16,17 @@ var Handshake = plugin.HandshakeConfig{
1616
}
1717

1818
type DrainPluginSettings struct {
19+
Config []byte
1920
}
21+
2022
type DrainPluginInfo struct {
21-
ID string
23+
ID string
24+
ConfigFormat pluginv1.ConfigFormat
2225
}
26+
2327
type DrainPlugin interface {
24-
Init(ctx context.Context, logger hclog.Logger, settings DrainPluginSettings) (DrainPluginInfo, error)
28+
PluginInfo(ctx context.Context) (DrainPluginInfo, error)
29+
Init(ctx context.Context, logger hclog.Logger, settings DrainPluginSettings) error
2530
IsSupported(ctx context.Context) (bool, error)
2631
IsHealthy(ctx context.Context) (bool, error)
2732
IsDrainOk(ctx context.Context, nodeName string) (bool, error)
@@ -37,14 +42,14 @@ type GRPCDrainPlugin struct {
3742
Impl DrainPlugin
3843
}
3944

40-
func (p GRPCDrainPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
41-
proto.RegisterDrainServiceServer(s, &DrainServer{
45+
func (p GRPCDrainPlugin) GRPCServer(_ *plugin.GRPCBroker, s *grpc.Server) error {
46+
pluginv1.RegisterDrainServiceServer(s, &DrainServer{
4247
Logger: p.Logger,
4348
Impl: p.Impl,
4449
})
4550
return nil
4651
}
4752

48-
func (p GRPCDrainPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
49-
return &DrainClient{client: proto.NewDrainServiceClient(c)}, nil
53+
func (p GRPCDrainPlugin) GRPCClient(_ context.Context, _ *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
54+
return &DrainClient{client: pluginv1.NewDrainServiceClient(c)}, nil
5055
}

0 commit comments

Comments
 (0)