-
Notifications
You must be signed in to change notification settings - Fork 0
/
applicationconfig_controller.go
157 lines (129 loc) · 4.25 KB
/
applicationconfig_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
/*
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 controllers
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/go-logr/logr"
feistyv1alpha1 "github.com/mrferos/feisty/api/v1alpha1"
"github.com/mrferos/feisty/revisions"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ApplicationConfigReconciler reconciles a ApplicationConfig object
type ApplicationConfigReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
type upsertResult struct {
secret *v1.Secret
created bool
}
// +kubebuilder:rbac:groups=feisty.paas.feisty.dev,resources=applicationconfigs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=feisty.paas.feisty.dev,resources=applicationconfigs/status,verbs=get;update;patch
func (r *ApplicationConfigReconciler) getSecretId(cfg feistyv1alpha1.ApplicationConfig) (string, error) {
keyValueJson, err := json.Marshal(cfg.Spec.KeyValuePairs)
if err != nil {
return "", err
}
md5bytes := md5.Sum([]byte(keyValueJson))
return fmt.Sprintf("%x", md5bytes), nil
}
func (r *ApplicationConfigReconciler) upsertSecret(cfg feistyv1alpha1.ApplicationConfig, req ctrl.Request, ctx context.Context) (upsertResult, error) {
log := r.Log.WithValues("application", req.NamespacedName)
secretId, err := r.getSecretId(cfg)
upsertState := upsertResult{}
if err != nil {
return upsertState, err
}
secretName := cfg.Name + "-" + secretId
objKey := client.ObjectKey{
Namespace: cfg.Namespace,
Name: secretName,
}
doCreate := false
var secret v1.Secret
upsertState.secret = &secret
if err := r.Get(ctx, objKey, &secret); err != nil {
doCreate = true
secret = v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: cfg.Namespace,
},
}
}
secretData := map[string][]byte{}
for k, v := range cfg.Spec.KeyValuePairs {
secretData[k] = []byte(v)
}
secret.Data = secretData
if doCreate {
_ = ctrl.SetControllerReference(&cfg, &secret, r.Scheme)
if err := r.Create(ctx, &secret); err != nil {
upsertState.created = true
log.Error(err, "Could not create secret")
return upsertState, err
}
} else {
if err := r.Update(ctx, &secret); err != nil {
upsertState.created = false
log.Error(err, "Could not update secret")
return upsertState, err
}
}
return upsertState, nil
}
func (r *ApplicationConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("applicationconfig", req.NamespacedName)
rev := revisions.Revision{
Client: r.Client,
Log: r.Log,
}
var cfg feistyv1alpha1.ApplicationConfig
if err := r.Get(ctx, req.NamespacedName, &cfg); err != nil {
log.Error(err, "Unable to fetch ApplicationConfig")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if upsertState, err := r.upsertSecret(cfg, req, ctx); err != nil {
log.Error(err, "There was an error doing secret handling")
return ctrl.Result{}, err
} else {
var app feistyv1alpha1.Application
if err := r.Get(ctx, req.NamespacedName, &app); err != nil {
log.Error(err, "Unable to fetch Application")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
app.Spec.AppConfigRef = upsertState.secret.Name
if err := r.Update(ctx, &app); err != nil {
log.Error(err, "Could not update Application with target ApplicationConfig")
return ctrl.Result{}, err
}
if upsertState.created {
_ = rev.CreateIfNeeded(req.NamespacedName, ctx)
}
}
return ctrl.Result{}, nil
}
func (r *ApplicationConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&feistyv1alpha1.ApplicationConfig{}).
Owns(&v1.Secret{}).
Complete(r)
}