-
Notifications
You must be signed in to change notification settings - Fork 143
/
utils.go
107 lines (89 loc) · 3.26 KB
/
utils.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
/*
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 actuator
import (
"context"
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
minterv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
)
type gcpAuthJSON struct {
PrivateKeyID string `json:"private_key_id"`
}
func decodeProviderSpec(codec *minterv1.ProviderCodec, cr *minterv1.CredentialsRequest) (*minterv1.GCPProviderSpec, error) {
if cr.Spec.ProviderSpec != nil {
gcpSpec := minterv1.GCPProviderSpec{}
err := codec.DecodeProviderSpec(cr.Spec.ProviderSpec, &gcpSpec)
if err != nil {
return nil, fmt.Errorf("error decoding provider v1 spec: %v", err)
}
return &gcpSpec, nil
}
return nil, fmt.Errorf("no providerSpec defined")
}
func decodeProviderStatus(codec *minterv1.ProviderCodec, cr *minterv1.CredentialsRequest) (*minterv1.GCPProviderStatus, error) {
gcpStatus := minterv1.GCPProviderStatus{}
if cr.Status.ProviderStatus == nil {
return &gcpStatus, nil
}
err := codec.DecodeProviderStatus(cr.Status.ProviderStatus, &gcpStatus)
if err != nil {
return nil, fmt.Errorf("error decoding v1 provider status: %v", err)
}
return &gcpStatus, nil
}
func decodeGCPAuthStringToJSON(authJSONBytes []byte) (*gcpAuthJSON, error) {
authJSON := &gcpAuthJSON{}
err := json.Unmarshal(authJSONBytes, authJSON)
if err != nil {
return nil, err
}
return authJSON, nil
}
func extractKeyIDFromKeyName(keyName string) string {
return keyIDFromKeyName.ReplaceAllString(keyName, "")
}
func loadCredsFromSecret(kubeClient client.Client, namespace, secretName string) ([]byte, error) {
secret := &corev1.Secret{}
err := kubeClient.Get(context.TODO(),
types.NamespacedName{
Name: secretName,
Namespace: namespace,
},
secret)
if err != nil {
return nil, err
}
jsonBytes, ok := secret.Data[gcpSecretJSONKey]
if !ok {
return nil, fmt.Errorf("GCP credentials secret %s did not contain key %s",
secretName, gcpSecretJSONKey)
}
return jsonBytes, nil
}
// CalculateSliceDiff compares the two slices, and returns two new slices:
// The first slice is all the elements from new that are not present in original.
// The second slice is all the elements from original that are not present in new.
// To this effect, the first slice can be considered the elements that new has "added" over original,
// and the second slice can be considered the elements that new has "removed" from original.
func CalculateSliceDiff(original, new []string) ([]string, []string) {
originalSet := sets.New(original...)
newSet := sets.New(new...)
removed := originalSet.Difference(newSet)
added := newSet.Difference(originalSet)
return added.UnsortedList(), removed.UnsortedList()
}