forked from crossplane/crossplane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
151 lines (126 loc) · 4.83 KB
/
provider.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
/*
Copyright 2018 The Crossplane 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 provider
import (
"context"
"golang.org/x/oauth2/google"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
gcpv1alpha1 "github.com/crossplaneio/crossplane/pkg/apis/gcp/v1alpha1"
"github.com/crossplaneio/crossplane/pkg/clients/gcp"
"github.com/crossplaneio/crossplane/pkg/logging"
)
const (
controllerName = "gcp.provider"
errorRetrievingSecret = "Failed retrieving provider secret"
errorInvalidCredentials = "Invalid provider credentials"
)
var (
log = logging.Logger.WithName("controller." + controllerName)
ctx = context.Background()
result = reconcile.Result{}
resultRequeue = reconcile.Result{Requeue: true}
)
// Add creates a new Provider Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr))
}
// Reconciler reconciles a Provider object
type Reconciler struct {
client.Client
scheme *runtime.Scheme
kubeclient kubernetes.Interface
recorder record.EventRecorder
validate func(*google.Credentials, []string) error
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
r := &Reconciler{
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
kubeclient: kubernetes.NewForConfigOrDie(mgr.GetConfig()),
recorder: mgr.GetRecorder(controllerName),
}
r.validate = r._validate
return r
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to Provider
err = c.Watch(&source.Kind{Type: &gcpv1alpha1.Provider{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
return nil
}
// fail - helper function to set fail condition with reason and message
func (r *Reconciler) fail(instance *gcpv1alpha1.Provider, reason, msg string) (reconcile.Result, error) {
instance.Status.UnsetAllConditions()
instance.Status.SetFailed(reason, msg)
return resultRequeue, r.Update(context.TODO(), instance)
}
func (r *Reconciler) _validate(creds *google.Credentials, permissions []string) error {
if len(permissions) == 0 {
return nil
}
return gcp.TestPermissions(creds, permissions)
}
// Reconcile reads that state of the cluster for a Provider object and makes changes based on the state read
// and what is in the Provider.Spec
// Automatically generate RBAC rules to allow the Controller to read and write Deployments
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=gcp.crossplane.io,resources=provider,verbs=get;list;watch;create;update;patch;delete
func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
log.V(logging.Debug).Info("reconciling", "kind", gcpv1alpha1.ProviderKindAPIVersion, "request", request)
// Fetch the Provider instance
instance := &gcpv1alpha1.Provider{}
err := r.Get(ctx, request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
return result, nil
}
// Error reading the object - requeue the request.
return result, err
}
creds, err := gcp.ProviderCredentials(r.kubeclient, instance)
if err != nil {
return r.fail(instance, errorRetrievingSecret, err.Error())
}
err = r.validate(creds, instance.Spec.RequiredPermissions)
if err != nil {
return r.fail(instance, errorInvalidCredentials, err.Error())
}
if instance.Status.IsReady() {
return result, nil
}
// Update status condition
instance.Status.UnsetAllConditions()
instance.Status.SetReady()
return result, r.Update(ctx, instance)
}