Skip to content

Commit 66c4fcc

Browse files
authored
Merge pull request #5 from teamcode-inc/feature/local-development
create cli tool to support local development (linked to #3)
2 parents 03aadf4 + b0d916a commit 66c4fcc

20 files changed

Lines changed: 949 additions & 2 deletions

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ RUN go mod download
1212
# Copy the go source
1313
COPY main.go main.go
1414
COPY api/ api/
15-
COPY controllers/ controllers/
15+
COPY pkg/controllers/ controllers/
1616

1717
# Build
1818
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ It solves the following problems during integration tests:
2929
## Features
3030
From now on, stop testing your application in local infra naively. Also, no more endless efforts in managing various cloud-based test environments.
3131
- **Workload tag**: tag your workload by creating a new channel. Then your request can be routed to the right workload replica, where you can work with your mates to test&debug the same feature together.
32+
- **KubeOrbit CLI**: just using one command, forward the traffic from in-cluster service to local service in a flash, no matter your service discovery is based on Eureka, Consul, Nacos or Kubernetes SVC.
3233
- **Protocol support**: various protocols based on Layer-7 are supported. HTTP, gRPC, Thrift, Dubbo ...
3334
- **Cost-effective**: when test your app, you don't need to configure the *entire* infra environment. Save your costs by using less resource according to your *incremental* environment request only. That is, you can have an isolated environment built from your base environment, which is achieved by KubeOrbit's high-scalability.
3435

cmd/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cli:
2+
GOARCH=amd64 GOOS=linux go build -o "orbitctl-linux" .
3+
GOARCH=amd64 GOOS=darwin go build -o "orbitctl-darwin" .
4+
GOARCH=amd64 GOOS=windows go build -o "orbitctl.exe" .

cmd/orbitctl.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2022 The TeamCode authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package main
14+
15+
import (
16+
"github.com/spf13/cobra"
17+
"kubeorbit.io/pkg/cli/command"
18+
)
19+
20+
var rootCmd = &cobra.Command{
21+
Use: "orbitctl",
22+
Long: `Orbitctl can forward traffic intended for a service in-clsuter to your local workload`,
23+
CompletionOptions: cobra.CompletionOptions{
24+
DisableDefaultCmd: true,
25+
},
26+
Example: "orbitctl forward --deployment depolyment-a --namespace ns-a --containerPort 8080 --localPort 8080",
27+
Version: "0.2.0",
28+
}
29+
30+
func init() {
31+
rootCmd.AddCommand(command.ForwardCommand())
32+
rootCmd.AddCommand(command.UninstallCommand())
33+
}
34+
35+
func main() {
36+
cobra.CheckErr(rootCmd.Execute())
37+
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ require (
88
github.com/google/go-cmp v0.5.5
99
github.com/onsi/ginkgo v1.16.5
1010
github.com/onsi/gomega v1.17.0
11+
github.com/satori/go.uuid v1.2.0
12+
github.com/sirupsen/logrus v1.8.1
13+
github.com/spf13/cobra v1.2.1
14+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
1115
istio.io/api v0.0.0-20220113014359-2bcfbc334255
1216
istio.io/client-go v1.12.1
1317
k8s.io/api v0.23.0

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8
126126
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
127127
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
128128
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
129+
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
129130
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
130131
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
131132
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
@@ -302,6 +303,7 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
302303
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
303304
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
304305
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
306+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
305307
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
306308
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
307309
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
@@ -353,6 +355,7 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
353355
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
354356
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
355357
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
358+
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
356359
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
357360
github.com/moby/term v0.0.0-20210610120745-9d4ed1856297/go.mod h1:vgPCkQMyxTZ7IDy8SXRufE172gr8+K/JE/7hHFxHW3A=
358361
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -431,12 +434,15 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
431434
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
432435
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
433436
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
437+
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
438+
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
434439
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
435440
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
436441
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
437442
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
438443
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
439444
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
445+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
440446
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
441447
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
442448
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
@@ -449,6 +455,7 @@ github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z
449455
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
450456
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
451457
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
458+
github.com/spf13/cobra v1.2.1 h1:+KmjbUw1hriSNMF55oPrkZcb27aECyrj8V2ytv7kWDw=
452459
github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk=
453460
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
454461
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"flag"
2121
v1 "kubeorbit.io/api/v1"
22+
controllers "kubeorbit.io/pkg/controllers"
2223
"os"
2324
"sigs.k8s.io/controller-runtime/pkg/webhook"
2425

@@ -36,7 +37,6 @@ import (
3637

3738
orbitv1alpha1 "kubeorbit.io/api/v1alpha1"
3839
routev1alpha1 "kubeorbit.io/api/v1alpha1"
39-
"kubeorbit.io/controllers"
4040
//+kubebuilder:scaffold:imports
4141
)
4242

pkg/cli/client/kubernetes.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2022 The TeamCode authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package client
14+
15+
import (
16+
"k8s.io/client-go/kubernetes"
17+
"k8s.io/client-go/rest"
18+
"k8s.io/client-go/tools/clientcmd"
19+
log "kubeorbit.io/pkg/cli/logger"
20+
)
21+
22+
var kubeConfig *rest.Config
23+
var kubeClient *kubernetes.Clientset
24+
25+
func init() {
26+
clusterConfig, err := newClusterConfig()
27+
kubeConfig = clusterConfig
28+
if err != nil {
29+
log.Fatalf("error loading kubeconfig: %v", err)
30+
}
31+
clientSet, err := kubernetes.NewForConfig(clusterConfig)
32+
if err != nil {
33+
log.Fatalf("error loading kubeconfig: %v", err)
34+
}
35+
kubeClient = clientSet
36+
}
37+
38+
func KubeConfig() *rest.Config {
39+
return kubeConfig
40+
}
41+
42+
func KubeClient() *kubernetes.Clientset {
43+
return kubeClient
44+
}
45+
46+
func newClusterConfig() (*rest.Config, error) {
47+
var cfg *rest.Config
48+
var err error
49+
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
50+
cfg, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}).ClientConfig()
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
cfg.QPS = 100
56+
cfg.Burst = 100
57+
58+
return cfg, nil
59+
}

pkg/cli/command/forward.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2022 The TeamCode authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package command
14+
15+
import (
16+
"github.com/spf13/cobra"
17+
"kubeorbit.io/pkg/cli/core"
18+
)
19+
20+
func ForwardCommand() *cobra.Command {
21+
request := &core.ForwardRequest{}
22+
cmd := &cobra.Command{
23+
Use: "forward",
24+
Long: `Forward a deployment to local`,
25+
Run: func(cmd *cobra.Command, args []string) {
26+
err := core.Forward(request)
27+
if err != nil {
28+
cmd.PrintErr(err)
29+
}
30+
},
31+
}
32+
cmd.Flags().StringVarP(&request.Namespace, "namespace", "n", "default", "Namespace for forwarding")
33+
cmd.Flags().StringVar(&request.DeploymentName, "deployment", "", "Deployment Name")
34+
cmd.Flags().IntVar(&request.LocalPort, "localPort", 0, "Local Port")
35+
cmd.Flags().IntVar(&request.ContainerPort, "containerPort", 0, "Container Port")
36+
cmd.MarkFlagRequired("deployment")
37+
cmd.MarkFlagRequired("localPort")
38+
cmd.MarkFlagRequired("containerPort")
39+
return cmd
40+
}

pkg/cli/command/uninstall.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2022 The TeamCode authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package command
14+
15+
import (
16+
"github.com/spf13/cobra"
17+
"kubeorbit.io/pkg/cli/core"
18+
)
19+
20+
func UninstallCommand() *cobra.Command {
21+
request := &core.UninstallRequest{}
22+
cmd := &cobra.Command{
23+
Use: "uninstall",
24+
Long: `Uninstall orbit agent and resources`,
25+
Run: func(cmd *cobra.Command, args []string) {
26+
err := core.Uninstall(request)
27+
if err != nil {
28+
cmd.PrintErr(err)
29+
}
30+
},
31+
}
32+
cmd.Flags().StringVar(&request.Namespace, "namespace", "default", "Namespace for uninstall")
33+
cmd.Flags().StringVar(&request.DeploymentName, "deployment", "", "Deployment name for uninstall")
34+
return cmd
35+
}

0 commit comments

Comments
 (0)