-
Notifications
You must be signed in to change notification settings - Fork 785
/
create_addon_gloo.go
187 lines (165 loc) · 5.54 KB
/
create_addon_gloo.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
package cmd
import (
"fmt"
"time"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"github.com/jenkins-x/jx/pkg/jx/cmd/opts"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
glooNamespace = "gloo-system"
glooClusterIngressProxy = "clusteringress-proxy"
knativeServeNamespace = "knative-serving"
knativeServeConfig = "config-domain"
)
var (
createAddonGlooLong = templates.LongDesc(`
Create a Gloo and Knative Serve addon for creating serverless applications
`)
createAddonGlooExample = templates.Examples(`
# Create the Gloo addon
jx create addon gloo
`)
)
// CreateAddonGlooOptions the options for the create spring command
type CreateAddonGlooOptions struct {
CreateAddonOptions
GlooNamespace string
ClusterIngressProxy string
KnativeServeNamespace string
KnativeServeConfigMap string
}
// NewCmdCreateAddonGloo creates a command object for the "create" command
func NewCmdCreateAddonGloo(commonOpts *opts.CommonOptions) *cobra.Command {
options := &CreateAddonGlooOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOpts,
},
},
}
cmd := &cobra.Command{
Use: "gloo",
Short: "Create a Gloo and Knative Serve addon for creating serverless applications",
Aliases: []string{"knative-serve"},
Long: createAddonGlooLong,
Example: createAddonGlooExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.GlooNamespace, "namespace", "n", glooNamespace, "The gloo system namespace")
cmd.Flags().StringVarP(&options.ClusterIngressProxy, "ingress", "i", glooClusterIngressProxy, "The name of the gloo cluster ingress proxy Service")
cmd.Flags().StringVarP(&options.KnativeServeNamespace, "knative-namespace", "k", knativeServeNamespace, "The knative serving namespace")
cmd.Flags().StringVarP(&options.KnativeServeConfigMap, "knative-configmap", "c", knativeServeConfig, "The knative serving ConfigMap name")
return cmd
}
// Run implements the command
func (o *CreateAddonGlooOptions) Run() error {
if o.GlooNamespace == "" {
o.GlooNamespace = glooNamespace
}
if o.ClusterIngressProxy == "" {
o.ClusterIngressProxy = glooClusterIngressProxy
}
if o.KnativeServeNamespace == "" {
o.KnativeServeNamespace = knativeServeNamespace
}
if o.KnativeServeConfigMap == "" {
o.KnativeServeConfigMap = knativeServeConfig
}
// lets ensure glooctl is installed
_, shouldInstall, err := opts.ShouldInstallBinary("glooctl")
if err != nil {
return errors.Wrapf(err, "failed to check if we need to install glooctl")
}
if shouldInstall {
log.Infof("installing %s\n", util.ColorInfo("glooctl"))
err = o.InstallGlooctl()
if err != nil {
return errors.Wrapf(err, "failed to install glooctl")
}
}
kubeClient, err := o.KubeClient()
if err != nil {
return err
}
// lets try load the gloo cluster service
_, err = kubeClient.CoreV1().Services(o.GlooNamespace).Get(o.ClusterIngressProxy, metav1.GetOptions{})
if err != nil {
// we may not have installed gloo yet so lets do that now...
err = o.RunCommandVerbose("glooctl", "install", "knative")
if err != nil {
return errors.Wrapf(err, "failed to install gloo")
}
}
ip, err := o.getGlooDomain(kubeClient)
if err != nil {
return err
}
if ip == "" {
return fmt.Errorf("failed to find the external LoadBalancer IP of the Gloo cluster ingress proxy service %s in namespace %s", o.ClusterIngressProxy, o.GlooNamespace)
}
externalDomain := ip + ".nip.io"
err = o.updateKnativeServeDomain(kubeClient, externalDomain)
if err != nil {
return errors.Wrapf(err, "failed to update the gloo domain")
}
eo := &EditDeployKindOptions{}
eo.CommonOptions = o.CommonOptions
eo.Team = true
eo.Kind = DeployKindKnative
return eo.Run()
}
func (o *CreateAddonGlooOptions) getGlooDomain(kubeClient kubernetes.Interface) (string, error) {
loggedWait := false
ip := ""
fn := func() (bool, error) {
svc, err := kubeClient.CoreV1().Services(o.GlooNamespace).Get(o.ClusterIngressProxy, metav1.GetOptions{})
if err != nil {
return false, err
}
// lets get the gloo ingress service
for _, lb := range svc.Status.LoadBalancer.Ingress {
ip = lb.IP
if ip != "" {
return true, nil
}
}
if !loggedWait {
loggedWait = true
log.Infof("waiting for external IP on Gloo cluster ingress proxy service %s in namespace %s ...\n", o.ClusterIngressProxy, o.GlooNamespace)
}
return false, nil
}
err := o.RetryUntilTrueOrTimeout(time.Minute*5, time.Second*3, fn)
if ip == "" || err != nil {
return "", err
}
log.Infof("using external IP of gloo LoadBalancer: %s\n", util.ColorInfo(ip))
return ip, nil
}
func (o *CreateAddonGlooOptions) updateKnativeServeDomain(kubeClient kubernetes.Interface, domain string) error {
// lets get the knative serving ConfigMap
knativeConfigMaps := kubeClient.CoreV1().ConfigMaps(o.KnativeServeNamespace)
cm, err := knativeConfigMaps.Get(o.KnativeServeConfigMap, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "failed to load the Knative Serve ConfigMap %s in namespace %s", o.KnativeServeConfigMap, o.KnativeServeNamespace)
}
cm.Data = map[string]string{
domain: "",
}
_, err = knativeConfigMaps.Update(cm)
if err != nil {
return errors.Wrapf(err, "failed to save the Knative Serve ConfigMap %s in namespace %s", o.KnativeServeConfigMap, o.KnativeServeNamespace)
}
return nil
}