/
channel_types.go
120 lines (106 loc) · 4.76 KB
/
channel_types.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
// Copyright © 2020 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package flags
import (
"fmt"
"strings"
duckv1 "knative.dev/pkg/apis/duck/v1"
messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/client/pkg/kn/config"
)
type ChannelTypeFlags struct {
ctype string
}
type ChannelRef struct {
Cref string
}
// ctypeMappings maps aliases used for channel types to their GroupVersionKind
var ctypeMappings = map[string]schema.GroupVersionKind{
"imc": {
Group: "messaging.knative.dev",
Version: "v1",
Kind: "InMemoryChannel",
},
}
// Add sets channel type flag definition to given flagset
func (i *ChannelTypeFlags) Add(f *pflag.FlagSet) {
f.StringVar(&i.ctype,
"type",
"",
"Override channel type to create, in the format '--type Group:Version:Kind'. "+
"If flag is not specified, it uses default messaging layer settings for channel type, cluster wide or specific namespace. "+
"You can configure aliases for channel types in kn config and refer the aliases with this flag. "+
"You can also refer inbuilt channel type InMemoryChannel using an alias 'imc' like '--type imc'. "+
"Examples: '--type messaging.knative.dev:v1beta1:KafkaChannel' for specifying explicit Group:Version:Kind.")
for _, p := range config.GlobalConfig.ChannelTypeMappings() {
//user configuration might override the default configuration
ctypeMappings[p.Alias] = schema.GroupVersionKind{
Kind: p.Kind,
Group: p.Group,
Version: p.Version,
}
}
}
// Parse parses the CLI value for channel type flag and populates GVK or returns error
func (i *ChannelTypeFlags) Parse() (*schema.GroupVersionKind, error) {
parts := strings.Split(i.ctype, ":")
switch len(parts) {
case 1:
if typ, ok := ctypeMappings[i.ctype]; ok {
return &typ, nil
}
return nil, fmt.Errorf("Error: unknown channel type alias: '%s'", i.ctype)
case 3:
if parts[0] == "" || parts[1] == "" || parts[2] == "" {
return nil, fmt.Errorf("Error: incorrect value '%s' for '--type', must be in the format 'Group:Version:Kind' or configure an alias in kn config", i.ctype)
}
return &schema.GroupVersionKind{Group: parts[0], Version: parts[1], Kind: parts[2]}, nil
default:
return nil, fmt.Errorf("Error: incorrect value '%s' for '--type', must be in the format 'Group:Version:Kind' or configure an alias in kn config", i.ctype)
}
}
// Add sets channel reference flag definition to given flagset
func (i *ChannelRef) Add(f *pflag.FlagSet) {
f.StringVar(&i.Cref,
"channel",
"",
"Specify the channel to subscribe to. For the default channel, "+
"just use the name (e.g. 'mychannel'). A mapped channel type like 'imc' "+
"can be used as a prefix (e.g. 'imc:mychannel'). "+
"Finally you can specify the full coordinates to the referenced channel "+
"with Group:Version:Kind:Name (e.g. 'messaging.knative.dev:v1beta1:KafkaChannel:mychannel').")
}
// Parse parses the CLI value for channel ref flag and populates object reference or return error
func (i *ChannelRef) Parse() (*duckv1.KReference, error) {
parts := strings.Split(i.Cref, ":")
switch len(parts) {
// if no prefix is given, defer to "messaging.knative.dev/v1:Channel"
case 1:
return &duckv1.KReference{Kind: "Channel", APIVersion: messagingv1.SchemeGroupVersion.String(), Name: parts[0]}, nil
case 2:
if typ, ok := ctypeMappings[parts[0]]; ok {
return &duckv1.KReference{Kind: typ.Kind, APIVersion: typ.GroupVersion().String(), Name: parts[1]}, nil
}
return nil, fmt.Errorf("Error: unknown alias '%s' for '--channel', please configure the alias in kn config or specify in the format '--channel Group:Version:Kind:Name'", parts[0])
case 4:
if parts[0] == "" || parts[1] == "" || parts[2] == "" || parts[3] == "" {
return nil, fmt.Errorf("Error: incorrect value '%s' for '--channel', must be in the format 'Group:Version:Kind:Name' or configure an alias in kn config and refer as: '--channel ALIAS:NAME'", i.Cref)
}
return &duckv1.KReference{Kind: parts[2], APIVersion: parts[0] + "/" + parts[1], Name: parts[3]}, nil
default:
return nil, fmt.Errorf("Error: incorrect value '%s' for '--channel', must be in the format 'Group:Version:Kind:Name' or configure an alias in kn config and refer as: '--channel ALIAS:NAME'", i.Cref)
}
}