/
skupper.go
1171 lines (1026 loc) · 37.5 KB
/
skupper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"fmt"
"github.com/skupperproject/skupper/pkg/network"
"github.com/skupperproject/skupper/pkg/utils/configs"
"github.com/skupperproject/skupper/pkg/utils/validator"
"os"
"reflect"
"strconv"
"strings"
"time"
routev1 "github.com/openshift/api/route/v1"
"github.com/skupperproject/skupper/pkg/domain"
"github.com/skupperproject/skupper/pkg/qdr"
"github.com/skupperproject/skupper/pkg/utils/formatter"
"github.com/skupperproject/skupper/pkg/version"
"github.com/spf13/cobra/doc"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"github.com/spf13/cobra"
"github.com/skupperproject/skupper/api/types"
"github.com/skupperproject/skupper/client"
"github.com/skupperproject/skupper/pkg/config"
"github.com/skupperproject/skupper/pkg/utils"
)
type SkupperClientManager interface {
Create(cmd *cobra.Command, args []string) error
CreateFlags(cmd *cobra.Command)
Delete(cmd *cobra.Command, args []string) error
DeleteFlags(cmd *cobra.Command)
List(cmd *cobra.Command, args []string) error
ListFlags(cmd *cobra.Command)
Status(cmd *cobra.Command, args []string) error
StatusFlags(cmd *cobra.Command)
SkupperClientCommon
}
type SkupperSiteClient interface {
SkupperClientManager
Update(cmd *cobra.Command, args []string) error
UpdateFlags(cmd *cobra.Command)
Version(cmd *cobra.Command, args []string) error
RevokeAccess(cmd *cobra.Command, args []string) error
}
type SkupperServiceClient interface {
SkupperClientManager
Label(cmd *cobra.Command, args []string) error
Bind(cmd *cobra.Command, args []string) error
BindArgs(cmd *cobra.Command, args []string) error
BindFlags(cmd *cobra.Command)
Unbind(cmd *cobra.Command, args []string) error
Expose(cmd *cobra.Command, args []string) error
ExposeArgs(cmd *cobra.Command, args []string) error
ExposeFlags(cmd *cobra.Command)
UnbindFlags(cmd *cobra.Command)
Unexpose(cmd *cobra.Command, args []string) error
UnexposeFlags(cmd *cobra.Command) error
UnexposeArgs(cmd *cobra.Command, args []string) error
}
type SkupperDebugClient interface {
Dump(cmd *cobra.Command, args []string) error
Events(cmd *cobra.Command, args []string) error
Service(cmd *cobra.Command, args []string) error
Policies(cmd *cobra.Command, args []string) error
SkupperClientCommon
}
type SkupperLinkClient interface {
SkupperClientManager
LinkHandler() domain.LinkHandler
}
type SkupperTokenClient interface {
Create(cmd *cobra.Command, args []string) error
CreateFlags(cmd *cobra.Command)
SkupperClientCommon
}
type SkupperNetworkClient interface {
GetCurrentSiteId(ctx context.Context) (string, error)
Status(cmd *cobra.Command, args []string, ctx context.Context) (*network.NetworkStatusInfo, error)
StatusFlags(cmd *cobra.Command)
SkupperClientCommon
}
type SkupperClientCommon interface {
NewClient(cmd *cobra.Command, args []string)
Platform() types.Platform
}
type SkupperClient interface {
Options(cmd *cobra.Command)
SupportedCommands() []string
Site() SkupperSiteClient
Service() SkupperServiceClient
Debug() SkupperDebugClient
Link() SkupperLinkClient
Token() SkupperTokenClient
Network() SkupperNetworkClient
}
type ExposeOptions struct {
Protocol string
Address string
Ports []int
TargetPorts []string
Headless bool
ProxyTuning types.Tuning
GeneratedCerts bool
TlsCredentials string
TlsCertAuthority string
PublishNotReadyAddresses bool
IngressMode string
BridgeImage string
Aggregate string
EventChannel bool
Namespace string
}
type BindOptions struct {
TargetPorts []string
PublishNotReadyAddresses bool
tlsCertAuthority string
Namespace string
Headless bool
ProxyTuning types.Tuning
}
func SkupperNotInstalledError(namespace string) error {
return fmt.Errorf("Skupper is not installed in Namespace: '" + namespace + "`")
}
func parseTargetTypeAndName(args []string) (string, string) {
// this functions assumes it is called with the right arguments, wrong
// argument verification is done on the "Args:" functions
targetType := args[0]
var targetName string
if len(args) == 2 {
targetName = args[1]
} else {
parts := strings.Split(args[0], "/")
targetType = parts[0]
targetName = parts[1]
}
return targetType, targetName
}
func configureHeadlessProxy(spec *types.Headless, options *types.Tuning) error {
var err error
if options.Affinity != "" {
spec.Affinity = utils.LabelToMap(options.Affinity)
}
if options.AntiAffinity != "" {
spec.AntiAffinity = utils.LabelToMap(options.AntiAffinity)
}
if options.NodeSelector != "" {
spec.NodeSelector = utils.LabelToMap(options.NodeSelector)
}
if options.Cpu != "" {
cpuQuantity, err := resource.ParseQuantity(options.Cpu)
if err == nil {
spec.CpuRequest = &cpuQuantity
} else {
err = fmt.Errorf("Invalid value for cpu: %s", err)
}
}
if options.Memory != "" {
memoryQuantity, err := resource.ParseQuantity(options.Memory)
if err == nil {
spec.MemoryRequest = &memoryQuantity
} else {
err = fmt.Errorf("Invalid value for memory: %s", err)
}
}
if options.CpuLimit != "" {
cpuQuantity, err := resource.ParseQuantity(options.CpuLimit)
if err == nil {
spec.CpuLimit = &cpuQuantity
} else {
err = fmt.Errorf("Invalid value for cpu: %s", err)
}
}
if options.MemoryLimit != "" {
memoryQuantity, err := resource.ParseQuantity(options.MemoryLimit)
if err == nil {
spec.MemoryLimit = &memoryQuantity
} else {
err = fmt.Errorf("Invalid value for memory: %s", err)
}
}
return err
}
func expose(cli types.VanClientInterface, ctx context.Context, targetType string, targetName string, options ExposeOptions) (string, error) {
serviceName := options.Address
service, err := cli.ServiceInterfaceInspect(ctx, serviceName)
if err != nil {
return "", err
}
vanClient, realClient := cli.(*client.VanClient)
var policy *client.PolicyAPIClient
if realClient {
policy = client.NewPolicyValidatorAPI(vanClient)
res, err := policy.Expose(targetType, targetName)
if err != nil {
return "", err
}
if !res.Allowed {
return "", res.Err()
}
}
if service == nil {
if options.Headless {
if targetType != "statefulset" {
return "", fmt.Errorf("The headless option is only supported for statefulsets")
}
service, err = cli.GetHeadlessServiceConfiguration(targetName, options.Protocol, options.Address, options.Ports, options.PublishNotReadyAddresses, options.Namespace)
if err != nil {
return "", err
}
err = configureHeadlessProxy(service.Headless, &options.ProxyTuning)
return service.Address, cli.ServiceInterfaceUpdate(ctx, service)
} else {
if realClient {
res, err := policy.Service(serviceName)
if err != nil {
return "", err
}
if !res.Allowed {
return "", res.Err()
}
}
service = &types.ServiceInterface{
Address: serviceName,
Ports: options.Ports,
Protocol: options.Protocol,
TlsCredentials: options.TlsCredentials,
TlsCertAuthority: options.TlsCertAuthority,
PublishNotReadyAddresses: options.PublishNotReadyAddresses,
BridgeImage: options.BridgeImage,
}
err := service.SetIngressMode(options.IngressMode)
if err != nil {
return "", err
}
}
} else if service.Headless != nil {
return "", fmt.Errorf("Service already exposed as headless")
} else if options.Headless {
return "", fmt.Errorf("Service already exposed, cannot reconfigure as headless")
} else if options.Protocol != "" && service.Protocol != options.Protocol {
return "", fmt.Errorf("Invalid protocol %s for service with mapping %s", options.Protocol, service.Protocol)
} else if options.BridgeImage != "" && service.BridgeImage != options.BridgeImage {
return "", fmt.Errorf("Service %s already exists with a different bridge image: %s", serviceName, service.BridgeImage)
} else if options.TlsCredentials != "" && service.TlsCredentials == "" {
return "", fmt.Errorf("Service already exposed without TLS support")
} else if options.TlsCertAuthority != "" && service.TlsCertAuthority == "" {
return "", fmt.Errorf("Service already exposed without TLS support")
} else if options.TlsCredentials == "" && service.TlsCredentials != "" {
return "", fmt.Errorf("Service already exposed with TLS support")
} else if options.TlsCertAuthority == "" && service.TlsCertAuthority != "" {
return "", fmt.Errorf("Service already exposed with TLS support")
} else if options.IngressMode != "" && options.IngressMode != string(service.ExposeIngress) {
return "", fmt.Errorf("Service already exposed with different ingress mode")
}
// service may exist from remote origin
service.Origin = ""
targetPorts, err := parsePortMapping(service, options.TargetPorts)
if err != nil {
return "", err
}
err = cli.ServiceInterfaceBind(ctx, service, targetType, targetName, targetPorts, options.Namespace)
if errors.IsNotFound(err) {
return "", SkupperNotInstalledError(cli.GetNamespace())
} else if err != nil {
return "", fmt.Errorf("Unable to create skupper service: %w", err)
}
return options.Address, nil
}
func createServiceArgs(cmd *cobra.Command, args []string) error {
if len(args) < 1 || (len(args) == 1 && !strings.Contains(args[0], ":")) {
return fmt.Errorf("Name and port(s) must be specified")
}
if len(args) > 1 {
for _, v := range args[1:] {
if _, err := strconv.Atoi(v); err != nil {
return fmt.Errorf("%s is not a valid port", v)
}
}
}
return nil
}
func exposeGatewayArgs(cmd *cobra.Command, args []string) error {
if len(args) < 2 || (!strings.Contains(args[1], ":") && len(args) < 3) {
return fmt.Errorf("Gateway service address, target host and port must all be specified")
}
if len(args) > 2 && strings.Contains(args[1], ":") {
return fmt.Errorf("extra argument: %s", args[2])
}
if len(args) > 2 {
for _, v := range args[2:] {
ports := strings.Split(v, ":")
if len(ports) > 2 {
return fmt.Errorf("%s is not a valid port", v)
} else if len(ports) == 2 {
if _, err := strconv.Atoi(ports[1]); err != nil {
return fmt.Errorf("%s is not a valid port", v)
}
}
if _, err := strconv.Atoi(ports[0]); err != nil {
return fmt.Errorf("%s is not a valid port", v)
}
}
}
return nil
}
func bindGatewayArgs(cmd *cobra.Command, args []string) error {
if len(args) < 2 || (!strings.Contains(args[1], ":") && len(args) < 3) {
return fmt.Errorf("Service address, target host and port must all be specified")
}
if len(args) > 2 && strings.Contains(args[1], ":") {
return fmt.Errorf("extra argument: %s", args[2])
}
if len(args) > 2 {
for _, v := range args[2:] {
if _, err := strconv.Atoi(v); err != nil {
return fmt.Errorf("%s is not a valid port", v)
}
}
}
return nil
}
func silenceCobra(cmd *cobra.Command) {
cmd.SilenceUsage = true
}
func NewClient(namespace string, context string, kubeConfigPath string) types.VanClientInterface {
return NewClientHandleError(namespace, context, kubeConfigPath, true)
}
func NewClientHandleError(namespace string, context string, kubeConfigPath string, exitOnError bool) types.VanClientInterface {
var cli types.VanClientInterface
var err error
cli, err = client.NewClient(namespace, context, kubeConfigPath)
if err != nil {
if exitOnError {
if strings.Contains(err.Error(), "invalid configuration: no configuration has been provided") {
fmt.Printf("%s. Please point to an existing, valid kubeconfig file.\n", err.Error())
} else {
fmt.Println(err.Error())
}
os.Exit(1)
} else {
return nil
}
}
return cli
}
var routerCreateOpts types.SiteConfigSpec
var routerLogging string
func asMap(entries []string) map[string]string {
result := map[string]string{}
for _, entry := range entries {
parts := strings.Split(entry, "=")
if len(parts) > 1 {
result[parts[0]] = parts[1]
} else {
result[parts[0]] = ""
}
}
return result
}
var LoadBalancerTimeout time.Duration
type InitFlags struct {
routerMode string
labels []string
}
var initFlags InitFlags
func NewCmdInit(skupperCli SkupperSiteClient) *cobra.Command {
platform := skupperCli.Platform()
cmd := &cobra.Command{
Use: "init",
Short: "Initialise skupper installation",
Long: `Setup a router and other supporting objects to provide a functional skupper
installation that can then be connected to other skupper installations`,
Args: cobra.NoArgs,
PreRun: skupperCli.NewClient,
RunE: func(cmd *cobra.Command, args []string) error {
routerModeFlag := cmd.Flag("router-mode")
if routerModeFlag.Changed {
options := []string{string(types.TransportModeInterior), string(types.TransportModeEdge)}
if !utils.StringSliceContains(options, initFlags.routerMode) {
return fmt.Errorf(`invalid "--router-mode=%v", it must be one of "%v"`, initFlags.routerMode, strings.Join(options, ", "))
}
routerCreateOpts.RouterMode = initFlags.routerMode
} else {
routerCreateOpts.RouterMode = string(types.TransportModeInterior)
}
if routerLogging != "" {
logConfig, err := qdr.ParseRouterLogConfig(routerLogging)
if err != nil {
return fmt.Errorf("Bad value for --router-logging: %s", err)
}
routerCreateOpts.Router.Logging = logConfig
}
if routerCreateOpts.EnableFlowCollector && routerCreateOpts.FlowCollector.FlowRecordTtl != 0 && routerCreateOpts.FlowCollector.FlowRecordTtl < time.Minute {
return fmt.Errorf("The minimum value for flow-collector-record-ttl is 1 minute")
}
if routerCreateOpts.EnableConsole && !routerCreateOpts.EnableFlowCollector {
return fmt.Errorf("The --enable-flow-collector option must be used with the --enable-console option")
}
if len(routerCreateOpts.AuthMode) > 0 && !utils.StringSliceContains(types.ValidAuthOptions(platform), routerCreateOpts.AuthMode) {
return fmt.Errorf("the --console-auth option must contain one of these values: %v", types.ValidAuthOptions(platform))
}
if routerCreateOpts.AuthMode != "internal" && (len(routerCreateOpts.User) > 0 || len(routerCreateOpts.Password) > 0) {
return fmt.Errorf("for the console to work with this user or password, the --console-auth option must be set to internal")
}
ok, err := validator.NewStringValidator().Evaluate(routerCreateOpts.SkupperName)
if !ok {
return fmt.Errorf("invalid skupper site name: %s", err.Error())
}
ok, err = validator.NewNumberValidator().Evaluate(routerCreateOpts.Routers)
if !ok {
return fmt.Errorf("invalid number for routers: %s", err.Error())
}
return skupperCli.Create(cmd, args)
},
}
routerCreateOpts.EnableController = true
cmd.Flags().StringVarP(&routerCreateOpts.SkupperName, "site-name", "", "", "Provide a specific name for this skupper installation")
cmd.Flags().StringVarP(&routerCreateOpts.Ingress, "ingress", "", "", "Setup Skupper ingress to one of: ["+strings.Join(types.ValidIngressOptions(platform), "|")+"].")
cmd.Flags().StringVarP(&initFlags.routerMode, "router-mode", "", string(types.TransportModeInterior), "Skupper router-mode")
cmd.Flags().StringSliceVar(&initFlags.labels, "labels", []string{}, "Labels to add to resources created by skupper")
cmd.Flags().StringVarP(&routerLogging, "router-logging", "", "", "Logging settings for router. 'trace', 'debug', 'info' (default), 'notice', 'warning', and 'error' are valid values.")
cmd.Flags().StringVarP(&routerCreateOpts.PrometheusServer.ExternalServer, "external-prometheus-server", "", "", "External prometheus server for metric aggregation. Valid only when --enable-flow-collector")
cmd.Flags().StringVarP(&routerCreateOpts.PrometheusServer.AuthMode, "prometheus-auth", "", "", "Authentication mode for skupper prometheus server. One of: 'tls', 'basic', 'unsecured'")
cmd.Flags().StringVarP(&routerCreateOpts.PrometheusServer.User, "prometheus-user", "", "", "Skupper prometheus user. Valid only when --prometheus-auth=basic")
cmd.Flags().StringVarP(&routerCreateOpts.PrometheusServer.Password, "prometheus-password", "", "", "Skupper prometheus user. Valid only when --prometheus-auth=basic")
// hide prometheus flags until external server supported
f := cmd.Flag("external-prometheus-server")
f.Hidden = true
f = cmd.Flag("prometheus-auth")
f.Hidden = true
f = cmd.Flag("prometheus-user")
f.Hidden = true
f = cmd.Flag("prometheus-password")
f.Hidden = true
cmd.Flags().SortFlags = false
// platform specific flags
skupperCli.CreateFlags(cmd)
return cmd
}
func hideFlag(cmd *cobra.Command, name string) {
f := cmd.Flag(name)
f.Hidden = true
}
func NewCmdDelete(skupperCli SkupperSiteClient) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete skupper installation",
Long: `delete will delete any skupper related objects from the namespace`,
Args: cobra.NoArgs,
PreRun: skupperCli.NewClient,
RunE: skupperCli.Delete,
}
return cmd
}
var forceHup bool
func NewCmdUpdate(skupperCli SkupperSiteClient) *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "Update skupper installation version",
Long: "Update the skupper site to " + version.Version,
Args: cobra.NoArgs,
PreRun: skupperCli.NewClient,
RunE: skupperCli.Update,
}
skupperCli.UpdateFlags(cmd)
return cmd
}
var clientIdentity string
var verboseStatus bool
func NewCmdStatus(skupperCli SkupperSiteClient) *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Report the status of the current Skupper site",
Args: cobra.NoArgs,
PreRun: skupperCli.NewClient,
RunE: skupperCli.Status,
}
cmd.Flags().BoolVarP(&verboseStatus, "verbose", "v", false, "Detailed information about Skupper status")
return cmd
}
var exposeOpts ExposeOptions
func NewCmdExpose(skupperCli SkupperServiceClient) *cobra.Command {
cmd := &cobra.Command{
Use: "expose",
Short: "Expose a set of pods through a Skupper address",
Args: skupperCli.ExposeArgs,
PreRun: skupperCli.NewClient,
RunE: skupperCli.Expose,
}
cmd.Flags().StringVar(&(exposeOpts.Protocol), "protocol", "tcp", "The protocol to proxy (tcp, http, or http2)")
cmd.Flags().StringVar(&(exposeOpts.Address), "address", "", "The Skupper address to expose")
cmd.Flags().IntSliceVar(&(exposeOpts.Ports), "port", []int{}, "The ports to expose on")
cmd.Flags().StringSliceVar(&(exposeOpts.TargetPorts), "target-port", []string{}, "The ports to target on pods")
cmd.Flags().StringVar(&(exposeOpts.IngressMode), "enable-ingress-from-target-site", "", "Determines whether access to the Skupper service is enabled in the site the target was exposed through. Always (default) or Never are valid values.")
cmd.Flags().StringVar(&exposeOpts.Aggregate, "aggregate", "", "The aggregation strategy to use. One of 'json' or 'multipart'. If specified requests to this service will be sent to all registered implementations and the responses aggregated.")
cmd.Flags().BoolVar(&exposeOpts.EventChannel, "event-channel", false, "If specified, this service will be a channel for multicast events.")
cmd.Flags().BoolVar(&exposeOpts.GeneratedCerts, "enable-tls", false, "If specified, the service will be exposed over TLS")
cmd.Flags().BoolVar(&exposeOpts.GeneratedCerts, "generate-tls-secrets", false, "If specified, the service will be exposed over TLS")
f := cmd.Flag("enable-tls")
f.Deprecated = "use 'generate-tls-secrets' instead"
f.Hidden = true
skupperCli.ExposeFlags(cmd)
return cmd
}
var unexposeAddress string
var unexposeNamespace string
func NewCmdUnexpose(skupperCli SkupperServiceClient) *cobra.Command {
cmd := &cobra.Command{
Use: "unexpose",
Short: "Unexpose a set of pods previously exposed through a Skupper address",
Args: skupperCli.UnexposeArgs,
PreRun: skupperCli.NewClient,
RunE: skupperCli.Unexpose,
}
cmd.Flags().StringVar(&unexposeAddress, "address", "", "Skupper address the target was exposed as")
skupperCli.UnexposeFlags(cmd)
return cmd
}
var showLabels bool
var verboseServiceStatus bool
func NewCmdServiceStatus(skupperClient SkupperServiceClient) *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "List services exposed over the service network",
Args: cobra.NoArgs,
PreRun: skupperClient.NewClient,
RunE: func(cmd *cobra.Command, args []string) error {
silenceCobra(cmd)
return skupperClient.Status(cmd, args)
},
}
cmd.Flags().BoolVar(&showLabels, "show-labels", false, "show service labels")
skupperClient.StatusFlags(cmd)
return cmd
}
var addLabels, removeLabels []string
func NewCmdServiceLabel(skupperClient SkupperServiceClient) *cobra.Command {
labels := &cobra.Command{
Use: "label <service> [labels...]",
Short: "Manage service labels",
Example: `
# show labels for my-service
skupper service label my-service
# add label1=value1 and label2=value2 to my-service
skupper service label my-service label1=value1 label2=value2
# add label1=value1 and remove label2 to/from my-service
skupper service label my-service label1=value1 label2-`,
PreRun: skupperClient.NewClient,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("service name is required")
} else if len(args) == 1 {
showLabels = true
}
if len(args) > 1 {
for i := 1; i < len(args); i++ {
label := args[i]
labelFields := len(strings.Split(label, "="))
if labelFields == 2 {
addLabels = append(addLabels, label)
} else if labelFields == 1 {
if !strings.HasSuffix(label, "-") {
return fmt.Errorf("no value provided to label %s", label)
}
removeLabels = append(removeLabels, strings.TrimSuffix(label, "-"))
} else {
return fmt.Errorf("invalid label [%s], use key=value or key- (to remove)", label)
}
}
}
return nil
},
RunE: skupperClient.Label,
}
return labels
}
func updateServiceLabels(si *types.ServiceInterface) {
curLabels := si.Labels
if curLabels == nil {
curLabels = map[string]string{}
}
// removing labels
for _, rmlabel := range removeLabels {
delete(curLabels, rmlabel)
}
// adding labels
for _, addLabels := range addLabels {
for k, v := range utils.LabelToMap(addLabels) {
curLabels[k] = v
}
}
si.Labels = curLabels
}
func showServiceLabels(si *types.ServiceInterface, name string) {
if si.Labels != nil && len(si.Labels) > 0 {
l := formatter.NewList()
l.Item(name)
labels := l.NewChild("Labels:")
for k, v := range si.Labels {
labels.NewChild(fmt.Sprintf("%s=%s", k, v))
}
l.Print()
} else {
fmt.Printf("%s has no labels", name)
fmt.Println()
}
}
func NewCmdService() *cobra.Command {
cmd := &cobra.Command{
Use: "service create <name> <port> or service delete port",
Short: "Manage skupper service definitions",
}
return cmd
}
var serviceToCreate types.ServiceInterface
var serviceIngressMode string
var createSvcWithGeneratedTlsCerts bool
func NewCmdCreateService(skupperClient SkupperServiceClient) *cobra.Command {
cmd := &cobra.Command{
Use: "create <name> <port...>",
Short: "Create a skupper service",
Args: createServiceArgs,
PreRun: skupperClient.NewClient,
RunE: func(cmd *cobra.Command, args []string) error {
silenceCobra(cmd)
var sPorts []string
if len(args) == 1 {
parts := strings.Split(args[0], ":")
serviceToCreate.Address = parts[0]
sPorts = []string{parts[1]}
} else {
serviceToCreate.Address = args[0]
sPorts = args[1:]
}
for _, p := range sPorts {
servicePort, err := strconv.Atoi(p)
if err != nil {
return fmt.Errorf("%s is not a valid port", p)
}
serviceToCreate.Ports = append(serviceToCreate.Ports, servicePort)
}
createServiceOverTlsWithCustomAndGeneratedCerts := createSvcWithGeneratedTlsCerts && serviceToCreate.TlsCredentials != ""
if createServiceOverTlsWithCustomAndGeneratedCerts {
return fmt.Errorf("the option --generate-tls-secrets can not be used with custom certificates")
}
err := serviceToCreate.SetIngressMode(serviceIngressMode)
if err != nil {
return err
}
if createSvcWithGeneratedTlsCerts {
serviceToCreate.TlsCredentials = types.SkupperServiceCertPrefix + serviceToCreate.Address
}
return skupperClient.Create(cmd, args)
},
}
cmd.Flags().StringVar(&serviceToCreate.Protocol, "protocol", "tcp", "The mapping in use for this service address (tcp, http, http2)")
cmd.Flags().StringVar(&serviceToCreate.Aggregate, "aggregate", "", "The aggregation strategy to use. One of 'json' or 'multipart'. If specified requests to this service will be sent to all registered implementations and the responses aggregated.")
cmd.Flags().StringVar(&serviceIngressMode, "enable-ingress", "", "Determines whether access to the Skupper service is enabled in this site. Valid values are Always (default) or Never.")
cmd.Flags().BoolVar(&serviceToCreate.EventChannel, "event-channel", false, "If specified, this service will be a channel for multicast events.")
cmd.Flags().BoolVar(&createSvcWithGeneratedTlsCerts, "enable-tls", false, "If specified, the service communication will be encrypted using TLS")
cmd.Flags().StringVar(&serviceToCreate.Protocol, "mapping", "tcp", "The mapping in use for this service address (currently one of tcp or http)")
cmd.Flags().BoolVar(&createSvcWithGeneratedTlsCerts, "generate-tls-secrets", false, "If specified, the service communication will be encrypted using TLS")
cmd.Flags().StringVar(&serviceToCreate.BridgeImage, "bridge-image", "", "The image to use for a bridge running external to the skupper router")
cmd.Flags().StringVar(&serviceToCreate.TlsCredentials, "tls-cert", "", "K8s secret name with custom certificates to encrypt the communication using TLS")
// platform specific flags
skupperClient.CreateFlags(cmd)
f := cmd.Flag("mapping")
f.Deprecated = "protocol is now the flag to set the mapping"
f.Hidden = true
f = cmd.Flag("enable-tls")
f.Deprecated = "use 'generate-tls-secrets' instead"
f.Hidden = true
return cmd
}
func NewCmdDeleteService(skupperClient SkupperServiceClient) *cobra.Command {
cmd := &cobra.Command{
Use: "delete <name>",
Short: "Delete a skupper service",
Args: cobra.ExactArgs(1),
PreRun: skupperClient.NewClient,
RunE: func(cmd *cobra.Command, args []string) error {
silenceCobra(cmd)
return skupperClient.Delete(cmd, args)
},
}
return cmd
}
var bindOptions BindOptions
func NewCmdBind(skupperClient SkupperServiceClient) *cobra.Command {
cmd := &cobra.Command{
Use: "bind <service-name> <target-type> <target-name>",
Short: "Bind a target to a service",
Args: skupperClient.BindArgs,
PreRun: skupperClient.NewClient,
RunE: func(cmd *cobra.Command, args []string) error {
silenceCobra(cmd)
return skupperClient.Bind(cmd, args)
},
}
cmd.Flags().StringSliceVar(&bindOptions.TargetPorts, "target-port", []string{}, "The port the target is listening on (you can also use colon to map source-port to a target-port).")
cmd.Flags().StringVar(&bindOptions.tlsCertAuthority, "tls-trust", "", "K8s secret name with the CA to expose the service over TLS")
skupperClient.BindFlags(cmd)
return cmd
}
func parsePortMapping(service *types.ServiceInterface, targetPorts []string) (map[int]int, error) {
if len(targetPorts) > 0 && len(service.Ports) != len(targetPorts) {
return nil, fmt.Errorf("service defines %d ports but only %d mapped (all ports must be mapped)",
len(service.Ports), len(targetPorts))
}
ports := map[int]int{}
for _, port := range service.Ports {
ports[port] = port
}
for i, port := range targetPorts {
portSplit := strings.SplitN(port, ":", 2)
var sPort, tPort string
sPort = portSplit[0]
tPort = sPort
mapping := false
if len(portSplit) == 2 {
tPort = portSplit[1]
mapping = true
}
var isp, itp int
var err error
if isp, err = strconv.Atoi(sPort); err != nil {
return nil, fmt.Errorf("invalid source port: %s", sPort)
}
if itp, err = strconv.Atoi(tPort); err != nil {
return nil, fmt.Errorf("invalid target port: %s", tPort)
}
if _, ok := ports[isp]; mapping && !ok {
return nil, fmt.Errorf("source port not defined in service: %d", isp)
}
// if target port not mapped, use positional index to determine it
if !mapping {
isp = service.Ports[i]
}
ports[isp] = itp
}
return ports, nil
}
var unbindNamespace string
func NewCmdUnbind(skupperClient SkupperServiceClient) *cobra.Command {
cmd := &cobra.Command{
Use: "unbind <service-name> <target-type> <target-name>",
Short: "Unbind a target from a service",
Args: skupperClient.BindArgs,
PreRun: skupperClient.NewClient,
RunE: skupperClient.Unbind,
}
skupperClient.UnbindFlags(cmd)
return cmd
}
func IsZero(v reflect.Value) bool {
return !v.IsValid() || reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}
func NewCmdVersion(skupperClient SkupperSiteClient) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Report the version of the Skupper CLI and services",
Args: cobra.NoArgs,
PreRun: skupperClient.NewClient,
RunE: func(cmd *cobra.Command, args []string) error {
silenceCobra(cmd)
fmt.Printf("%-30s %s\n", "client version", version.Version)
return skupperClient.Version(cmd, args)
},
}
return cmd
}
func NewCmdVersionManifest() *cobra.Command {
cmd := &cobra.Command{
Use: "manifest",
Long: "Generate a JSON file containing the version of the Skupper images by default and the value of the environment variables in the current directory.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
silenceCobra(cmd)
manifestManager := configs.ManifestManager{}
return manifestManager.CreateFile(manifestManager.GetDefaultManifestWithEnv())
},
}
return cmd
}
func NewCmdDebug() *cobra.Command {
cmd := &cobra.Command{
Use: "debug dump <file>, debug events or debug service <service-name>",
Short: "Debug skupper installation",
}
return cmd
}
func NewCmdDebugDump(skupperCli SkupperDebugClient) *cobra.Command {
cmd := &cobra.Command{
Use: "dump <filename>.tar.gz",
Short: "Collect and store skupper logs, config, etc. to compressed archive file",
Args: cobra.ExactArgs(1),
PreRun: skupperCli.NewClient,
RunE: skupperCli.Dump,
}
return cmd
}
var verbose bool
func NewCmdDebugEvents(skupperClient SkupperDebugClient) *cobra.Command {
cmd := &cobra.Command{
Use: "events",
Short: "Show events",
Args: cobra.NoArgs,
PreRun: skupperClient.NewClient,
RunE: skupperClient.Events,
}
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "More detailed output (in json)")
return cmd
}
func NewCmdDebugService(skupperClient SkupperDebugClient) *cobra.Command {
cmd := &cobra.Command{
Use: "service <service-name>",
Short: "Check the internal state of a skupper exposed service",
Args: cobra.ExactArgs(1),
PreRun: skupperClient.NewClient,
RunE: skupperClient.Service,
}
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "More detailed output (in json)")
return cmd
}
func NewCmdDebugPolicies(skupperClient SkupperDebugClient) *cobra.Command {
cmd := &cobra.Command{
Use: "policies",
Short: "List active SkupperClusterPolicies",
Args: cobra.NoArgs,
PreRun: skupperClient.NewClient,
RunE: skupperClient.Policies,
}
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "More detailed output (in json)")
return cmd
}
func NewCmdRevokeaccess(skupperClient SkupperSiteClient) *cobra.Command {
cmd := &cobra.Command{
Use: "revoke-access",
Short: "Revoke all previously granted access to the site.",
Long: `This will invalidate all previously issued tokens and require that all
links to this site be re-established with new tokens.`,
Args: cobra.ExactArgs(0),
PreRun: skupperClient.NewClient,
RunE: skupperClient.RevokeAccess,
}
return cmd
}
func NewCmdCompletion() *cobra.Command {
completionLong := `
Output shell completion code for bash.
The shell code must be evaluated to provide interactive
completion of skupper commands. This can be done by sourcing it from
the .bash_profile. i.e.: $ source <(skupper completion)
`
cmd := &cobra.Command{
Use: "completion",
Short: "Output shell completion code for bash",
Long: completionLong,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenBashCompletion(os.Stdout)
},
}
return cmd
}
func NewCmdMan() *cobra.Command {
var outDir string
cmd := &cobra.Command{
Use: "man",
Short: "generate man pages",
RunE: func(cmd *cobra.Command, args []string) error {
info, err := os.Stat(outDir)
if err != nil && os.IsNotExist(err) {
err = os.MkdirAll(outDir, 0755)
if err != nil {
return fmt.Errorf("error creating output directory - %w", err)
}