/
cfservicebinding_controller.go
250 lines (224 loc) · 9.21 KB
/
cfservicebinding_controller.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
/*
Copyright 2021.
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 services
import (
"context"
"fmt"
"time"
servicesv1alpha1 "code.cloudfoundry.org/cf-k8s-controllers/controllers/apis/services/v1alpha1"
workloadsv1alpha1 "code.cloudfoundry.org/cf-k8s-controllers/controllers/apis/workloads/v1alpha1"
"github.com/go-logr/logr"
servicebindingv1beta1 "github.com/servicebinding/service-binding-controller/apis/v1beta1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)
// CFServiceBindingReconciler reconciles a CFServiceBinding object
type CFServiceBindingReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
}
const (
BindingSecretAvailableCondition = "BindingSecretAvailable"
ServiceBindingGUIDLabel = "services.cloudfoundry.org/service-binding-guid"
ServiceCredentialBindingTypeLabel = "services.cloudfoundry.org/service-credential-binding-type"
)
//+kubebuilder:rbac:groups=services.cloudfoundry.org,resources=cfservicebindings,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=services.cloudfoundry.org,resources=cfservicebindings/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=services.cloudfoundry.org,resources=cfservicebindings/finalizers,verbs=update
//+kubebuilder:rbac:groups=servicebinding.io,resources=servicebindings,verbs=get;list;create;update;patch;watch
func (r *CFServiceBindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
cfServiceBinding := new(servicesv1alpha1.CFServiceBinding)
err := r.Client.Get(ctx, types.NamespacedName{Name: req.Name, Namespace: req.Namespace}, cfServiceBinding)
if err != nil {
if !apierrors.IsNotFound(err) {
r.Log.Error(err, "unable to fetch CFServiceBinding", req.Name, req.Namespace)
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
cfApp := new(workloadsv1alpha1.CFApp)
err = r.Client.Get(ctx, types.NamespacedName{Name: cfServiceBinding.Spec.AppRef.Name, Namespace: cfServiceBinding.Namespace}, cfApp)
if err != nil {
r.Log.Error(err, "Error when fetching CFApp")
return ctrl.Result{}, err
}
originalCfServiceBinding := cfServiceBinding.DeepCopy()
err = controllerutil.SetOwnerReference(cfApp, cfServiceBinding, r.Scheme)
if err != nil {
r.Log.Error(err, "Unable to set owner reference on CfServiceBinding")
return ctrl.Result{}, err
}
err = r.Client.Patch(ctx, cfServiceBinding, client.MergeFrom(originalCfServiceBinding))
if err != nil {
r.Log.Error(err, fmt.Sprintf("Error setting owner reference on the CFServiceBinding %s/%s", req.Namespace, cfServiceBinding.Name))
return ctrl.Result{}, err
}
instance := new(servicesv1alpha1.CFServiceInstance)
err = r.Client.Get(ctx, types.NamespacedName{Name: cfServiceBinding.Spec.Service.Name, Namespace: req.Namespace}, instance)
if err != nil {
var result ctrl.Result
if apierrors.IsNotFound(err) {
cfServiceBinding.Status.Binding.Name = ""
meta.SetStatusCondition(&cfServiceBinding.Status.Conditions, metav1.Condition{
Type: BindingSecretAvailableCondition,
Status: metav1.ConditionFalse,
Reason: "ServiceInstanceNotFound",
Message: "Service instance does not exist",
})
result = ctrl.Result{RequeueAfter: 2 * time.Second}
err = nil
} else {
meta.SetStatusCondition(&cfServiceBinding.Status.Conditions, metav1.Condition{
Type: BindingSecretAvailableCondition,
Status: metav1.ConditionFalse,
Reason: "UnknownError",
Message: "Error occurred while fetching service instance: " + err.Error(),
})
result = ctrl.Result{}
}
statusErr := r.setStatus(ctx, cfServiceBinding)
if statusErr != nil {
return ctrl.Result{}, statusErr
}
return result, err
}
secret := new(corev1.Secret)
// Note: is there a reason to fetch the secret name from the service instance spec?
err = r.Client.Get(ctx, types.NamespacedName{Name: instance.Spec.SecretName, Namespace: req.Namespace}, secret)
if err != nil {
var result ctrl.Result
if apierrors.IsNotFound(err) {
cfServiceBinding.Status.Binding.Name = ""
meta.SetStatusCondition(&cfServiceBinding.Status.Conditions, metav1.Condition{
Type: BindingSecretAvailableCondition,
Status: metav1.ConditionFalse,
Reason: "SecretNotFound",
Message: "Binding secret does not exist",
})
result = ctrl.Result{RequeueAfter: 2 * time.Second}
err = nil
} else {
meta.SetStatusCondition(&cfServiceBinding.Status.Conditions, metav1.Condition{
Type: BindingSecretAvailableCondition,
Status: metav1.ConditionFalse,
Reason: "UnknownError",
Message: "Error occurred while fetching secret: " + err.Error(),
})
result = ctrl.Result{}
}
statusErr := r.setStatus(ctx, cfServiceBinding)
if statusErr != nil {
return ctrl.Result{}, statusErr
}
return result, err
} else {
cfServiceBinding.Status.Binding.Name = instance.Spec.SecretName
meta.SetStatusCondition(&cfServiceBinding.Status.Conditions, metav1.Condition{
Type: BindingSecretAvailableCondition,
Status: metav1.ConditionTrue,
Reason: "SecretFound",
Message: "",
})
err = r.setStatus(ctx, cfServiceBinding)
if err != nil {
return ctrl.Result{}, err
}
}
actualSBServiceBinding := servicebindingv1beta1.ServiceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("cf-binding-%s", cfServiceBinding.Name),
Namespace: cfServiceBinding.Namespace,
},
}
desiredSBServiceBinding := generateDesiredServiceBinding(&actualSBServiceBinding, cfServiceBinding, cfApp, secret)
_, err = controllerutil.CreateOrPatch(ctx, r.Client, &actualSBServiceBinding, sbServiceBindingMutateFn(&actualSBServiceBinding, desiredSBServiceBinding))
if err != nil {
r.Log.Error(err, "Error calling Create on servicebinding.io ServiceBinding")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func sbServiceBindingMutateFn(actualSBServiceBinding, desiredSBServiceBinding *servicebindingv1beta1.ServiceBinding) controllerutil.MutateFn {
return func() error {
actualSBServiceBinding.ObjectMeta.Labels = desiredSBServiceBinding.ObjectMeta.Labels
actualSBServiceBinding.ObjectMeta.OwnerReferences = desiredSBServiceBinding.ObjectMeta.OwnerReferences
actualSBServiceBinding.Spec = desiredSBServiceBinding.Spec
return nil
}
}
func generateDesiredServiceBinding(actualServiceBinding *servicebindingv1beta1.ServiceBinding, cfServiceBinding *servicesv1alpha1.CFServiceBinding, cfApp *workloadsv1alpha1.CFApp, secret *corev1.Secret) *servicebindingv1beta1.ServiceBinding {
var desiredServiceBinding servicebindingv1beta1.ServiceBinding
actualServiceBinding.DeepCopyInto(&desiredServiceBinding)
desiredServiceBinding.ObjectMeta.Labels = map[string]string{
ServiceBindingGUIDLabel: cfServiceBinding.Name,
workloadsv1alpha1.CFAppGUIDLabelKey: cfApp.Name,
ServiceCredentialBindingTypeLabel: "app",
}
desiredServiceBinding.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: "services.cloudfoundry.org/v1alpha1",
Kind: "CFServiceBinding",
Name: cfServiceBinding.Name,
UID: cfServiceBinding.UID,
},
}
desiredServiceBinding.Spec = servicebindingv1beta1.ServiceBindingSpec{
Name: secret.Name,
Type: "user-provided",
Workload: servicebindingv1beta1.ServiceBindingWorkloadReference{
APIVersion: "apps/v1",
Kind: "StatefulSet",
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
workloadsv1alpha1.CFAppGUIDLabelKey: cfApp.Name,
},
},
},
Service: servicebindingv1beta1.ServiceBindingServiceReference{
APIVersion: "services.cloudfoundry.org/v1alpha1",
Kind: "CFServiceBinding",
Name: cfServiceBinding.Name,
},
}
secretType, ok := secret.Data["type"]
if ok && len(secretType) > 0 {
desiredServiceBinding.Spec.Type = string(secretType)
}
secretProvider, ok := secret.Data["provider"]
if ok {
desiredServiceBinding.Spec.Provider = string(secretProvider)
}
return &desiredServiceBinding
}
func (r *CFServiceBindingReconciler) setStatus(ctx context.Context, cfServiceBinding *servicesv1alpha1.CFServiceBinding) error {
if statusErr := r.Client.Status().Update(ctx, cfServiceBinding); statusErr != nil {
r.Log.Error(statusErr, "unable to update CFServiceBinding status")
return statusErr
}
return nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *CFServiceBindingReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&servicesv1alpha1.CFServiceBinding{}).
Complete(r)
}