-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathbind.go
More file actions
221 lines (192 loc) · 6.92 KB
/
bind.go
File metadata and controls
221 lines (192 loc) · 6.92 KB
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
package cluster
import (
"crypto/rand"
"fmt"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/color"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/shared/cluster/constants"
"github.com/redhat-developer/app-services-cli/pkg/shared/cluster/kubeclient"
"github.com/redhat-developer/app-services-cli/pkg/shared/cluster/v1alpha"
bindv1alpha1 "github.com/redhat-developer/service-binding-operator/apis/binding/v1alpha1"
"github.com/AlecAivazis/survey/v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func (c *KubernetesClusterAPIImpl) ExecuteServiceBinding(options *v1alpha.BindOperationOptions) error {
clients := c.KubernetesClients
cliEnv := c.CommandEnvironment
if options.Namespace == "" {
newNamespace, _, err := clients.ClientConfig.Namespace()
if err != nil {
return kubeclient.TranslatedKubernetesErrors(cliEnv, err)
}
options.Namespace = newNamespace
cliEnv.Logger.Info(cliEnv.Localizer.MustLocalize("cluster.serviceBinding.namespaceInfo",
localize.NewEntry("Namespace", color.Info(options.Namespace))))
}
var appResource schema.GroupVersionResource
if options.DeploymentConfigEnabled {
appResource = constants.DeploymentConfigResource
} else {
cliEnv.Logger.Info(cliEnv.Localizer.MustLocalize("cluster.serviceBinding.using.deployment"))
appResource = constants.DeploymentResource
}
// Get deployment/app name if needed
if options.AppName == "" {
appName, err := fetchAppNameFromCluster(c, appResource, options.Namespace)
if err != nil {
return kubeclient.TranslatedKubernetesErrors(cliEnv, err)
}
options.AppName = appName
} else {
_, err := clients.DynamicClient.Resource(appResource).Namespace(options.Namespace).Get(cliEnv.Context, options.AppName, metav1.GetOptions{})
if err != nil {
return kubeclient.TranslatedKubernetesErrors(cliEnv, err)
}
}
// Execute binding
err := c.performBinding(options, options.Namespace, appResource)
if err != nil {
// Important to handle errors from API and handle missing prerequisites
return kubeclient.TranslatedKubernetesErrors(cliEnv, err)
}
cliEnv.Logger.Info(icon.SuccessPrefix(), fmt.Sprintf(cliEnv.Localizer.MustLocalize("cluster.serviceBinding.bindingSuccess"), options.ServiceName, options.AppName))
return nil
}
func (c *KubernetesClusterAPIImpl) performBinding(
options *v1alpha.BindOperationOptions, ns string, appResource schema.GroupVersionResource) error {
clients := c.KubernetesClients
cliEnv := c.CommandEnvironment
logger := cliEnv.Logger
localizer := cliEnv.Localizer
// Check of operator is installed
installed, err := c.KubernetesClients.IsResourceAvailableOnCluster(&bindv1alpha1.GroupVersionResource, ns)
if !installed {
return fmt.Errorf("%s: %w", localizer.MustLocalizeError("cluster.serviceBinding.operatorMissing"), err)
}
if err != nil {
return err
}
// Create service instance (it can be kafka/registry etc.)
service, err := c.createServiceInstance(options.ServiceType)
if err != nil {
return err
}
// Build service metadata
serviceMetadata, err := service.BuildServiceDetails(options.ServiceName, ns, options.IgnoreContext)
if err != nil {
return err
}
// Validate if service exist on the cluster
_, err = clients.DynamicClient.Resource(serviceMetadata.GroupMetadata).Namespace(ns).Get(cliEnv.Context,
serviceMetadata.Name, metav1.GetOptions{})
if err != nil {
return cliEnv.Localizer.MustLocalizeError("cluster.serviceBinding.serviceMissing.message")
}
cliEnv.Logger.Info(fmt.Sprintf(cliEnv.Localizer.MustLocalize("cluster.serviceBinding.status.message"),
serviceMetadata.Name, options.AppName))
if !options.ForceCreationWithoutAsk {
var shouldContinue bool
confirm := &survey.Confirm{
Message: cliEnv.Localizer.MustLocalize("cluster.serviceBinding.confirm.message"),
}
errAsk := survey.AskOne(confirm, &shouldContinue)
if errAsk != nil {
return errAsk
}
if !shouldContinue {
return nil
}
}
if options.ServiceName == "" {
options.ServiceName = serviceMetadata.Name
}
sb, err := createBindingCR(options, serviceMetadata.GroupMetadata, appResource, ns)
if err != nil {
return err
}
logger.Info(localizer.MustLocalize("cluster.serviceBinding.usingOperator"))
sbData, err := runtime.DefaultUnstructuredConverter.ToUnstructured(sb)
if err != nil {
return err
}
unstructuredSB := unstructured.Unstructured{Object: sbData}
_, err = clients.DynamicClient.Resource(bindv1alpha1.GroupVersionResource).Namespace(ns).
Create(cliEnv.Context, &unstructuredSB, metav1.CreateOptions{})
return err
}
func createBindingCR(options *v1alpha.BindOperationOptions, serviceMetadata schema.GroupVersionResource,
appResource schema.GroupVersionResource, namespace string) (*bindv1alpha1.ServiceBinding, error) {
serviceRef := bindv1alpha1.Service{
NamespacedRef: bindv1alpha1.NamespacedRef{
Ref: bindv1alpha1.Ref{
Group: serviceMetadata.Group,
Version: serviceMetadata.Version,
Resource: serviceMetadata.Resource,
Name: options.ServiceName,
},
},
}
appRef := bindv1alpha1.Application{
Ref: bindv1alpha1.Ref{
Group: appResource.Group,
Version: appResource.Version,
Resource: appResource.Resource,
Name: options.AppName,
},
}
if options.BindingName == "" {
randomValue := make([]byte, 2)
_, errRand := rand.Read(randomValue)
if errRand != nil {
return nil, errRand
}
options.BindingName = fmt.Sprintf("%v-%x", options.ServiceName, randomValue)
}
sb := &bindv1alpha1.ServiceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: options.BindingName,
Namespace: namespace,
},
Spec: bindv1alpha1.ServiceBindingSpec{
BindAsFiles: options.BindAsFiles,
Services: []bindv1alpha1.Service{serviceRef},
Application: appRef,
},
}
sb.SetGroupVersionKind(bindv1alpha1.GroupVersionKind)
return sb, nil
}
func fetchAppNameFromCluster(c *KubernetesClusterAPIImpl, resource schema.GroupVersionResource, ns string) (string, error) {
clients := c.KubernetesClients
opts := c.CommandEnvironment
list, err := clients.DynamicClient.Resource(resource).Namespace(ns).List(opts.Context, metav1.ListOptions{})
if err != nil {
return "", err
}
var appNames []string
for _, d := range list.Items {
name, found, err2 := unstructured.NestedString(d.Object, "metadata", "name")
if err2 != nil || !found {
continue
}
appNames = append(appNames, name)
}
if len(appNames) == 0 {
return "", opts.Localizer.MustLocalizeError("cluster.serviceBinding.error.noapps")
}
prompt := &survey.Select{
Message: opts.Localizer.MustLocalize("cluster.serviceBinding.connect.survey.message"),
Options: appNames,
PageSize: 10,
}
var selectedAppIndex int
err = survey.AskOne(prompt, &selectedAppIndex)
if err != nil {
return "", err
}
return appNames[selectedAppIndex], nil
}