Skip to content

Commit 5561519

Browse files
committed
[cli] reorganize runtime components and fix tunnel startup
1 parent 8d04f8b commit 5561519

File tree

7 files changed

+211
-129
lines changed

7 files changed

+211
-129
lines changed

api/config/v1alpha1/config_types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type TunnelConfig struct {
9999
Name string `json:"name,omitempty"`
100100
// Mode is the mode of the tunnel.
101101
Mode TunnelMode `json:"mode,omitempty"`
102-
// SocksPort, when running in userspace mode, is the port to listen on for
102+
// SocksPort, when running in user mode, is the port to listen on for
103103
// SOCKS5 proxy connections. If not specified it will default to 1080.
104104
SocksPort *int `json:"socksPort,omitempty"`
105105
// STUNServers is an optional list of STUN servers to use for determining the
@@ -108,7 +108,7 @@ type TunnelConfig struct {
108108
STUNServers []STUNServer `json:"stunServers,omitempty"`
109109
// PacketCapturePath is an optional path to write packet captures to.
110110
// If not specified, packet sniffing will be disabled.
111-
// This is only available in userspace mode and intended for debugging purposes.
111+
// This is only available in user mode and intended for debugging purposes.
112112
PacketCapturePath string `json:"packetCapturePath,omitempty"`
113113
// MinConns is the minimum number of concurrent tunnel connections to maintain.
114114
// Defaults to 1.
@@ -141,8 +141,8 @@ type TunnelMode string
141141
const (
142142
// Use the kernel implementation of WireGuard.
143143
TunnelModeKernel TunnelMode = "kernel"
144-
// Use an unprivileged userspace implementation of WireGuard.
145-
TunnelModeUserspace TunnelMode = "userspace"
144+
// Use an unprivileged user-mode implementation of WireGuard.
145+
TunnelModeUserspace TunnelMode = "user"
146146
)
147147

148148
// RuntimeConfig configures components started by `apoxy run`.

pkg/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/apoxy-dev/apoxy/pkg/cmd/alpha"
1919
"github.com/apoxy-dev/apoxy/pkg/cmd/domain"
2020
"github.com/apoxy-dev/apoxy/pkg/cmd/gateway"
21+
run "github.com/apoxy-dev/apoxy/pkg/cmd/run"
2122
"github.com/apoxy-dev/apoxy/pkg/cmd/tunnel"
2223
)
2324

@@ -60,6 +61,7 @@ func init() {
6061
RootCmd.AddCommand(alpha.Cmd())
6162
RootCmd.AddCommand(domain.Cmd())
6263
RootCmd.AddCommand(gateway.Cmd())
64+
RootCmd.AddCommand(run.Cmd())
6365
RootCmd.AddCommand(tunnel.Cmd())
6466
}
6567

pkg/cmd/run/aggregation.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package run
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"golang.org/x/sync/errgroup"
8+
"k8s.io/client-go/kubernetes"
9+
"k8s.io/client-go/rest"
10+
11+
configv1alpha1 "github.com/apoxy-dev/apoxy/api/config/v1alpha1"
12+
"github.com/apoxy-dev/apoxy/pkg/kube-controller/apiregistration"
13+
"github.com/apoxy-dev/apoxy/pkg/kube-controller/apiserviceproxy"
14+
"github.com/apoxy-dev/apoxy/pkg/log"
15+
)
16+
17+
func runKubeAggregation(ctx context.Context, cfg *configv1alpha1.Config, ac *configv1alpha1.KubeAggregationConfig) error {
18+
log.Infof("Starting kube-aggregation component (cluster=%s, namespace=%s)",
19+
ac.ClusterName, ac.Namespace)
20+
21+
kCluster, err := rest.InClusterConfig()
22+
if err != nil {
23+
return fmt.Errorf("failed to create in-cluster config: %w", err)
24+
}
25+
kc := kubernetes.NewForConfigOrDie(kCluster)
26+
27+
var proxyOpts []apiserviceproxy.Option
28+
proxyOpts = append(proxyOpts, apiserviceproxy.WithProjectID(cfg.CurrentProject.String()))
29+
proxyOpts = append(proxyOpts, apiserviceproxy.WithNamespace(ac.Namespace))
30+
proxyOpts = append(proxyOpts, apiserviceproxy.WithServiceName(ac.ServiceName))
31+
if ac.ClusterName != "" {
32+
proxyOpts = append(proxyOpts, apiserviceproxy.WithClusterName(ac.ClusterName))
33+
}
34+
if ac.BootstrapToken != "" {
35+
proxyOpts = append(proxyOpts, apiserviceproxy.WithToken(ac.BootstrapToken))
36+
}
37+
if ac.APIHost != "" {
38+
proxyOpts = append(proxyOpts, apiserviceproxy.WithAPIHost(ac.APIHost))
39+
}
40+
41+
apiSvc, err := apiserviceproxy.NewAPIServiceProxy(ctx, kc, proxyOpts...)
42+
if err != nil {
43+
return fmt.Errorf("failed to create API service proxy: %w", err)
44+
}
45+
46+
g, ctx := errgroup.WithContext(ctx)
47+
48+
g.Go(func() error {
49+
log.Infof("Starting API service proxy")
50+
return apiSvc.Run(ctx)
51+
})
52+
53+
g.Go(func() error {
54+
apiReg, err := apiregistration.NewAPIRegistration(kCluster)
55+
if err != nil {
56+
return fmt.Errorf("failed to create API registration client: %w", err)
57+
}
58+
if err := apiReg.RegisterAPIServices(ctx, ac.ServiceName, ac.Namespace, 443, apiSvc.CABundle()); err != nil {
59+
return fmt.Errorf("failed to register API services: %w", err)
60+
}
61+
log.Infof("API services registered")
62+
<-ctx.Done()
63+
return nil
64+
})
65+
66+
return g.Wait()
67+
}

pkg/cmd/run.go renamed to pkg/cmd/run/cmd.go

Lines changed: 6 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
1-
package cmd
1+
package run
22

33
import (
4-
"context"
54
"fmt"
65

76
"github.com/spf13/cobra"
87
"golang.org/x/sync/errgroup"
9-
"k8s.io/apimachinery/pkg/runtime"
10-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
11-
"k8s.io/client-go/kubernetes"
12-
"k8s.io/client-go/rest"
13-
ctrl "sigs.k8s.io/controller-runtime"
14-
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
15-
gwapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
168

179
configv1alpha1 "github.com/apoxy-dev/apoxy/api/config/v1alpha1"
18-
"github.com/apoxy-dev/apoxy/client/versioned"
1910
"github.com/apoxy-dev/apoxy/config"
20-
"github.com/apoxy-dev/apoxy/pkg/kube-controller/apiregistration"
21-
"github.com/apoxy-dev/apoxy/pkg/kube-controller/apiserviceproxy"
22-
"github.com/apoxy-dev/apoxy/pkg/kube-controller/controllers"
23-
"github.com/apoxy-dev/apoxy/pkg/log"
2411
)
2512

2613
var runCmd = &cobra.Command{
@@ -62,7 +49,7 @@ Components are defined under runtime.components in the config. Example:
6249
" mirror: \"gateway\"\n"+
6350
" - type: tunnel\n"+
6451
" tunnel:\n"+
65-
" mode: \"userspace\"\n",
52+
" mode: \"user\"\n",
6653
config.ConfigFile)
6754
}
6855

@@ -113,6 +100,10 @@ Components are defined under runtime.components in the config. Example:
113100
},
114101
}
115102

103+
func Cmd() *cobra.Command {
104+
return runCmd
105+
}
106+
116107
func resolveKubeMirrorConfig(in *configv1alpha1.KubeMirrorConfig) *configv1alpha1.KubeMirrorConfig {
117108
out := in.DeepCopy()
118109
if out.Mirror == "" {
@@ -156,90 +147,3 @@ func validateKubeAggregationConfig(cfg *configv1alpha1.Config, ac *configv1alpha
156147
}
157148
return nil
158149
}
159-
160-
func runKubeAggregation(ctx context.Context, cfg *configv1alpha1.Config, ac *configv1alpha1.KubeAggregationConfig) error {
161-
log.Infof("Starting kube-aggregation component (cluster=%s, namespace=%s)",
162-
ac.ClusterName, ac.Namespace)
163-
164-
kCluster, err := rest.InClusterConfig()
165-
if err != nil {
166-
return fmt.Errorf("failed to create in-cluster config: %w", err)
167-
}
168-
kc := kubernetes.NewForConfigOrDie(kCluster)
169-
170-
var proxyOpts []apiserviceproxy.Option
171-
proxyOpts = append(proxyOpts, apiserviceproxy.WithProjectID(cfg.CurrentProject.String()))
172-
proxyOpts = append(proxyOpts, apiserviceproxy.WithNamespace(ac.Namespace))
173-
proxyOpts = append(proxyOpts, apiserviceproxy.WithServiceName(ac.ServiceName))
174-
if ac.ClusterName != "" {
175-
proxyOpts = append(proxyOpts, apiserviceproxy.WithClusterName(ac.ClusterName))
176-
}
177-
if ac.BootstrapToken != "" {
178-
proxyOpts = append(proxyOpts, apiserviceproxy.WithToken(ac.BootstrapToken))
179-
}
180-
if ac.APIHost != "" {
181-
proxyOpts = append(proxyOpts, apiserviceproxy.WithAPIHost(ac.APIHost))
182-
}
183-
184-
apiSvc, err := apiserviceproxy.NewAPIServiceProxy(ctx, kc, proxyOpts...)
185-
if err != nil {
186-
return fmt.Errorf("failed to create API service proxy: %w", err)
187-
}
188-
189-
g, ctx := errgroup.WithContext(ctx)
190-
191-
g.Go(func() error {
192-
log.Infof("Starting API service proxy")
193-
return apiSvc.Run(ctx)
194-
})
195-
196-
g.Go(func() error {
197-
apiReg, err := apiregistration.NewAPIRegistration(kCluster)
198-
if err != nil {
199-
return fmt.Errorf("failed to create API registration client: %w", err)
200-
}
201-
if err := apiReg.RegisterAPIServices(ctx, ac.ServiceName, ac.Namespace, 443, apiSvc.CABundle()); err != nil {
202-
return fmt.Errorf("failed to register API services: %w", err)
203-
}
204-
log.Infof("API services registered")
205-
<-ctx.Done()
206-
return nil
207-
})
208-
209-
return g.Wait()
210-
}
211-
212-
func runKubeMirror(ctx context.Context, cfg *configv1alpha1.Config, mc *configv1alpha1.KubeMirrorConfig) error {
213-
log.Infof("Starting kube-mirror component (cluster=%s, mirror=%s, namespace=%s)",
214-
mc.ClusterName, mc.Mirror, mc.Namespace)
215-
216-
kCluster, err := rest.InClusterConfig()
217-
if err != nil {
218-
return fmt.Errorf("failed to create in-cluster config: %w", err)
219-
}
220-
221-
scheme := runtime.NewScheme()
222-
utilruntime.Must(gwapiv1.Install(scheme))
223-
utilruntime.Must(gwapiv1alpha2.Install(scheme))
224-
225-
mgr, err := ctrl.NewManager(kCluster, ctrl.Options{Scheme: scheme})
226-
if err != nil {
227-
return fmt.Errorf("failed to create controller manager: %w", err)
228-
}
229-
230-
apoxyClient, err := versioned.NewForConfig(kCluster)
231-
if err != nil {
232-
return fmt.Errorf("failed to create Apoxy client: %w", err)
233-
}
234-
235-
reconciler := controllers.NewMirrorReconciler(mgr.GetClient(), apoxyClient, mc)
236-
if err := reconciler.SetupWithManager(ctx, mgr); err != nil {
237-
return fmt.Errorf("failed to setup mirror reconciler: %w", err)
238-
}
239-
240-
return mgr.Start(ctx)
241-
}
242-
243-
func init() {
244-
RootCmd.AddCommand(runCmd)
245-
}

pkg/cmd/run/mirror.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package run
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"k8s.io/apimachinery/pkg/runtime"
8+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
9+
"k8s.io/client-go/rest"
10+
ctrl "sigs.k8s.io/controller-runtime"
11+
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
12+
gwapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
13+
14+
configv1alpha1 "github.com/apoxy-dev/apoxy/api/config/v1alpha1"
15+
"github.com/apoxy-dev/apoxy/client/versioned"
16+
"github.com/apoxy-dev/apoxy/pkg/kube-controller/controllers"
17+
"github.com/apoxy-dev/apoxy/pkg/log"
18+
)
19+
20+
func runKubeMirror(ctx context.Context, cfg *configv1alpha1.Config, mc *configv1alpha1.KubeMirrorConfig) error {
21+
log.Infof("Starting kube-mirror component (cluster=%s, mirror=%s, namespace=%s)",
22+
mc.ClusterName, mc.Mirror, mc.Namespace)
23+
24+
kCluster, err := rest.InClusterConfig()
25+
if err != nil {
26+
return fmt.Errorf("failed to create in-cluster config: %w", err)
27+
}
28+
29+
scheme := runtime.NewScheme()
30+
utilruntime.Must(gwapiv1.Install(scheme))
31+
utilruntime.Must(gwapiv1alpha2.Install(scheme))
32+
33+
mgr, err := ctrl.NewManager(kCluster, ctrl.Options{Scheme: scheme})
34+
if err != nil {
35+
return fmt.Errorf("failed to create controller manager: %w", err)
36+
}
37+
38+
apoxyClient, err := versioned.NewForConfig(kCluster)
39+
if err != nil {
40+
return fmt.Errorf("failed to create Apoxy client: %w", err)
41+
}
42+
43+
reconciler := controllers.NewMirrorReconciler(mgr.GetClient(), apoxyClient, mc)
44+
if err := reconciler.SetupWithManager(ctx, mgr); err != nil {
45+
return fmt.Errorf("failed to setup mirror reconciler: %w", err)
46+
}
47+
48+
return mgr.Start(ctx)
49+
}

0 commit comments

Comments
 (0)