Skip to content

Commit fd3c06f

Browse files
K8SPS-281: Ability to run the operator locally (#386)
* Log * Mysqlsh with exec. * Fix mysqlshexec. * Implement replicatorexec. * Implement orchestrator exec functions and fix replicatorexec. * Implementing replicatorexec - parsing sql results. * Add query method for replicatorexe. * Wrong images * Finish implementin replicatorexec. * Revert some changes. * Add pods/exec resource to the operator role. * Improve queries. * Refactor and cleanup. * Fix getting mysql and orc pods. * Cleanup. * Improve getting mysql and orc pods. * Update cr initImage and update readme. * Fix sql syntax error. * Implement usersexec and get pod index from hostname. * Fox usersexec * Fix getting hostname. * Fix getting replication status. * Use replicatorexec in the rest of the places. * Use propper primary pod. * Update updating uperator user password. * Discard old passwords with new operator pass. * Fix replicatorexec. * Fix ChangeReplicationSource syntax. * Cleanup. * Correctly start async replication. * Escape passwords. --------- Co-authored-by: Ege Güneş <[email protected]>
1 parent 8ba3b2e commit fd3c06f

File tree

15 files changed

+1291
-50
lines changed

15 files changed

+1291
-50
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ Contributions to the source tree should follow the workflow described below:
169169

170170
By default kuttl will delete created test namespace after test is finished, you can add `--skip-delete` flag in order to prevent deletion for potential troubleshooting.
171171

172+
**Running the operator locally**
173+
174+
1. Save your changes
175+
2. Tell the operator which namespace to watch by running `export WATCH_NAMESPACE=my-namespace`
176+
3. Run the command `make install run`.
172177

173178
### 2. Contributing to documentation
174179

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ gen-versionservice-client: swagger
120120
build: generate ## Build docker image with the manager.
121121
ROOT_REPO=$(ROOT_REPO) VERSION=$(VERSION) IMAGE=$(IMAGE) $(ROOT_REPO)/e2e-tests/build
122122

123+
.PHONY: run
124+
run: manifests generate fmt vet ## Run a controller from your host.
125+
go run ./cmd/manager/main.go
126+
123127
##@ Deployment
124128

125129
install: manifests ## Install CRDs, rbac

config/rbac/role.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rules:
1010
resources:
1111
- configmaps
1212
- pods
13+
- pods/exec
1314
- secrets
1415
- services
1516
verbs:

deploy/bundle.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8477,6 +8477,7 @@ rules:
84778477
resources:
84788478
- configmaps
84798479
- pods
8480+
- pods/exec
84808481
- secrets
84818482
- services
84828483
verbs:

deploy/cr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ spec:
1414
upgradeOptions:
1515
versionServiceEndpoint: https://check.percona.com
1616
apply: disabled
17+
initImage: perconalab/percona-server-mysql-operator:main
1718
# ignoreAnnotations:
1819
# - service.beta.kubernetes.io/aws-load-balancer-backend-protocol
1920
# ignoreLabels:
@@ -27,7 +28,6 @@ spec:
2728
# name: special-selfsigned-issuer
2829
# kind: ClusterIssuer
2930
# group: cert-manager.io
30-
# initImage: percona/percona-server-mysql-operator:0.5.0
3131

3232
mysql:
3333
clusterType: group-replication

deploy/rbac.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ rules:
5656
resources:
5757
- configmaps
5858
- pods
59+
- pods/exec
5960
- secrets
6061
- services
6162
verbs:

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/go-openapi/strfmt v0.21.7
1313
github.com/go-openapi/swag v0.22.3
1414
github.com/go-openapi/validate v0.22.1
15+
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d
1516
github.com/go-sql-driver/mysql v1.7.1
1617
github.com/minio/minio-go/v7 v7.0.56
1718
github.com/onsi/ginkgo/v2 v2.9.7
@@ -30,6 +31,7 @@ require (
3031
)
3132

3233
require (
34+
github.com/moby/spdystream v0.2.0 // indirect
3335
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
3436
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
3537
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ github.com/Percona-Lab/percona-version-service v0.0.0-20230324081000-27de445df23
5353
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
5454
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
5555
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
56+
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
5657
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
5758
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
5859
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
@@ -177,6 +178,8 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe
177178
github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ=
178179
github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0=
179180
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
181+
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d h1:KbPOUXFUDJxwZ04vbmDOc3yuruGvVO+LOa7cVER3yWw=
182+
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
180183
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
181184
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
182185
github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c=
@@ -255,6 +258,7 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
255258
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
256259
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
257260
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
261+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
258262
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
259263
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
260264
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
@@ -321,6 +325,8 @@ github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
321325
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
322326
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
323327
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
328+
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
329+
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
324330
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
325331
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
326332
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

pkg/clientcmd/clientcmd.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package clientcmd
22

33
import (
4+
"context"
5+
"io"
6+
7+
corev1 "k8s.io/api/core/v1"
8+
"k8s.io/client-go/kubernetes/scheme"
49
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
510
restclient "k8s.io/client-go/rest"
611
"k8s.io/client-go/tools/clientcmd"
12+
"k8s.io/client-go/tools/remotecommand"
713
)
814

915
type Client struct {
@@ -39,6 +45,45 @@ func NewClient() (*Client, error) {
3945
}, nil
4046
}
4147

48+
func (c *Client) Exec(
49+
ctx context.Context,
50+
pod *corev1.Pod,
51+
containerName string,
52+
command []string,
53+
stdin io.Reader,
54+
stdout, stderr io.Writer,
55+
tty bool) error {
56+
57+
req := c.client.RESTClient().
58+
Post().
59+
Namespace(pod.Namespace).
60+
Resource("pods").
61+
Name(pod.Name).
62+
SubResource("exec").
63+
VersionedParams(&corev1.PodExecOptions{
64+
Container: containerName,
65+
Command: command,
66+
Stdin: stdin != nil,
67+
Stdout: stdout != nil,
68+
Stderr: stderr != nil,
69+
TTY: tty,
70+
}, scheme.ParameterCodec)
71+
72+
exec, err := remotecommand.NewSPDYExecutor(c.restconfig, "POST", req.URL())
73+
if err != nil {
74+
return err
75+
}
76+
77+
// Connect this process' std{in,out,err} to the remote shell process.
78+
return exec.StreamWithContext(ctx, remotecommand.StreamOptions{
79+
Stdin: stdin,
80+
Stdout: stdout,
81+
Stderr: stderr,
82+
Tty: tty,
83+
})
84+
85+
}
86+
4287
func (c *Client) REST() restclient.Interface {
4388
return c.client.RESTClient()
4489
}

0 commit comments

Comments
 (0)