-
Notifications
You must be signed in to change notification settings - Fork 139
/
actuator.go
213 lines (184 loc) · 7.03 KB
/
actuator.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
/*
Copyright 2019 The OpenShift 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 openstack
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/client"
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
log "github.com/sirupsen/logrus"
minterv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
"github.com/openshift/cloud-credential-operator/pkg/operator/constants"
actuatoriface "github.com/openshift/cloud-credential-operator/pkg/operator/credentialsrequest/actuator"
"github.com/openshift/cloud-credential-operator/pkg/operator/utils"
)
type OpenStackActuator struct {
Client client.Client
RootCredClient client.Client
}
// NewOpenStackActuator creates a new OpenStack actuator.
func NewOpenStackActuator(client, rootCredClient client.Client) (*OpenStackActuator, error) {
return &OpenStackActuator{
Client: client,
RootCredClient: rootCredClient,
}, nil
}
// Exists checks if the credentials currently exist.
func (a *OpenStackActuator) Exists(ctx context.Context, cr *minterv1.CredentialsRequest) (bool, error) {
logger := a.getLogger(cr)
logger.Debug("running Exists")
var err error
existingSecret := &corev1.Secret{}
err = a.Client.Get(ctx, types.NamespacedName{Namespace: cr.Spec.SecretRef.Namespace, Name: cr.Spec.SecretRef.Name}, existingSecret)
if err != nil {
if errors.IsNotFound(err) {
logger.Debug("target secret does not exist")
return false, nil
}
return false, err
}
logger.Debug("target secret exists")
return true, nil
}
// Create the credentials.
func (a *OpenStackActuator) Create(ctx context.Context, cr *minterv1.CredentialsRequest) error {
logger := a.getLogger(cr)
logger.Debug("running create")
return a.sync(ctx, cr)
}
// Update the credentials to the provided definition.
func (a *OpenStackActuator) Update(ctx context.Context, cr *minterv1.CredentialsRequest) error {
logger := a.getLogger(cr)
logger.Debug("running update")
return a.sync(ctx, cr)
}
func (a *OpenStackActuator) sync(ctx context.Context, cr *minterv1.CredentialsRequest) error {
logger := a.getLogger(cr)
logger.Debug("running sync")
credentialsRootSecret, err := a.GetCredentialsRootSecret(ctx, cr)
if err != nil {
logger.WithError(err).Error("issue with cloud credentials secret")
return err
}
clouds, err := GetRootCloudCredentialsSecretData(credentialsRootSecret, logger)
if err != nil {
logger.WithError(err).Error("issue with cloud credentials secret")
return &actuatoriface.ActuatorError{
ErrReason: minterv1.InsufficientCloudCredentials,
Message: err.Error(),
}
}
logger.Debugf("provisioning secret")
err = a.syncCredentialSecret(ctx, cr, clouds, logger)
if err != nil {
msg := "error creating/updating secret"
logger.WithError(err).Error(msg)
return &actuatoriface.ActuatorError{
ErrReason: minterv1.CredentialsProvisionFailure,
Message: fmt.Sprintf("%v: %v", msg, err),
}
}
return nil
}
func (a *OpenStackActuator) syncCredentialSecret(ctx context.Context, cr *minterv1.CredentialsRequest, clouds string, logger log.FieldLogger) error {
sLog := logger.WithFields(log.Fields{
"targetSecret": fmt.Sprintf("%s/%s", cr.Spec.SecretRef.Namespace, cr.Spec.SecretRef.Name),
"cr": fmt.Sprintf("%s/%s", cr.Namespace, cr.Name),
})
sLog.Infof("processing secret")
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Spec.SecretRef.Name,
Namespace: cr.Spec.SecretRef.Namespace,
},
}
op, err := controllerutil.CreateOrPatch(ctx, a.Client, secret, func() error {
if secret.Labels == nil {
secret.Labels = map[string]string{}
}
secret.Labels[minterv1.LabelCredentialsRequest] = minterv1.LabelCredentialsRequestValue
if secret.Annotations == nil {
secret.Annotations = map[string]string{}
}
secret.Annotations[minterv1.AnnotationCredentialsRequest] = fmt.Sprintf("%s/%s", cr.Namespace, cr.Name)
if clouds != "" {
if secret.Data == nil {
secret.Data = map[string][]byte{}
}
secret.Data[RootOpenStackCredsSecretKey] = []byte(clouds)
}
return nil
})
sLog.WithField("operation", op).Info("processed secret")
if err != nil {
return &actuatoriface.ActuatorError{
ErrReason: minterv1.CredentialsProvisionFailure,
Message: "error processing secret",
}
}
return nil
}
// GetCredentialsRootSecretLocation returns the namespace and name where the parent credentials secret is stored.
func (a *OpenStackActuator) GetCredentialsRootSecretLocation() types.NamespacedName {
return types.NamespacedName{Namespace: constants.CloudCredSecretNamespace, Name: constants.OpenStackCloudCredsSecretName}
}
func (a *OpenStackActuator) GetCredentialsRootSecret(ctx context.Context, cr *minterv1.CredentialsRequest) (*corev1.Secret, error) {
logger := a.getLogger(cr)
cloudCredSecret := &corev1.Secret{}
if err := a.RootCredClient.Get(ctx, a.GetCredentialsRootSecretLocation(), cloudCredSecret); err != nil {
msg := "unable to fetch root cloud cred secret"
logger.WithError(err).Error(msg)
return nil, &actuatoriface.ActuatorError{
ErrReason: minterv1.CredentialsProvisionFailure,
Message: fmt.Sprintf("%v: %v", msg, err),
}
}
return cloudCredSecret, nil
}
// Delete credentials
func (a *OpenStackActuator) Delete(ctx context.Context, cr *minterv1.CredentialsRequest) error {
logger := a.getLogger(cr)
logger.Debug("running delete")
existingSecret := &corev1.Secret{}
err := a.Client.Get(context.TODO(), types.NamespacedName{Namespace: cr.Spec.SecretRef.Namespace, Name: cr.Spec.SecretRef.Name}, existingSecret)
if err != nil {
if errors.IsNotFound(err) {
logger.Debug("target secret does not exist")
return nil
}
return err
}
err = a.Client.Delete(context.TODO(), existingSecret)
if err != nil {
return err
}
return nil
}
func (a *OpenStackActuator) getLogger(cr *minterv1.CredentialsRequest) log.FieldLogger {
return log.WithFields(log.Fields{
"actuator": "openstack",
"cr": fmt.Sprintf("%s/%s", cr.Namespace, cr.Name),
})
}
// Upgradeable returns a ClusterOperator status condition for the upgradeable type
// if the system is considered not upgradeable. Otherwise, return nil as the default
// value is for things to be upgradeable.
func (a *OpenStackActuator) Upgradeable(mode operatorv1.CloudCredentialsMode) *configv1.ClusterOperatorStatusCondition {
return utils.UpgradeableCheck(a.RootCredClient, mode, a.GetCredentialsRootSecretLocation())
}