forked from crossplane-contrib/provider-upjet-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
182 lines (165 loc) · 6.8 KB
/
Copy pathcommon.go
File metadata and controls
182 lines (165 loc) · 6.8 KB
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
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: CC0-1.0
package common
import (
"context"
v1 "github.com/crossplane/crossplane-runtime/v2/apis/common/v1"
"github.com/crossplane/crossplane-runtime/v2/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/v2/pkg/meta"
"github.com/crossplane/crossplane-runtime/v2/pkg/password"
"github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/v2/pkg/reference"
xpresource "github.com/crossplane/crossplane-runtime/v2/pkg/resource"
"github.com/crossplane/upjet/v2/pkg/config"
"github.com/crossplane/upjet/v2/pkg/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// SelfPackagePath is the golang path for this package.
SelfPackagePath = "github.com/upbound/provider-aws/config/namespaced/common"
// PathARNExtractor is the golang path to ARNExtractor function
// in this package.
PathARNExtractor = SelfPackagePath + ".ARNExtractor()"
// PathTerraformIDExtractor is the golang path to TerraformID extractor
// function in this package.
PathTerraformIDExtractor = SelfPackagePath + ".TerraformID()"
// VersionV1Beta1 is used for resources that meet the v1beta1 criteria
// here: https://github.com/upbound/arch/pull/33
VersionV1Beta1 = "v1beta1"
// ErrGetPasswordSecret is an error string for failing to get password secret
ErrGetPasswordSecret = "cannot get password secret"
// errManagedNotNamespaced is an error string for non-namespaced MRs
errManagedNotNamespaced = "managed resource is not namespaced"
)
// ARNExtractor extracts ARN of the resources from "status.atProvider.arn" which
// is quite common among all AWS resources.
func ARNExtractor() reference.ExtractValueFn {
return func(mg xpresource.Managed) string {
paved, err := fieldpath.PaveObject(mg)
if err != nil {
// todo(hasan): should we log this error?
return ""
}
r, err := paved.GetString("status.atProvider.arn")
if err != nil {
// todo(hasan): should we log this error?
return ""
}
return r
}
}
// TerraformID returns the Terraform ID string of the resource without any
// manipulation.
func TerraformID() reference.ExtractValueFn {
return func(mr xpresource.Managed) string {
tr, ok := mr.(resource.Terraformed)
if !ok {
return ""
}
return tr.GetID()
}
}
// PasswordGenerator returns an InitializerFn that will generate a password
// for a resource if the toggle field is set to true and the secret referenced
// by the secretRefFieldPath is not found or does not have content corresponding
// to the password key.
func PasswordGenerator(secretRefFieldPath, toggleFieldPath string) config.NewInitializerFn { //nolint:gocyclo
// NOTE(muvaf): This function is just 1 point over the cyclo limit but there
// is no easy way to reduce it without making it harder to read.
return func(client client.Client) managed.Initializer {
return managed.InitializerFn(func(ctx context.Context, mg xpresource.Managed) error {
// this would be a programming/configuration error
// should be used only for namespaced MRs
if mg.GetNamespace() == "" {
return errors.New(errManagedNotNamespaced)
}
paved, err := fieldpath.PaveObject(mg)
if err != nil {
return errors.Wrap(err, "cannot pave object")
}
sel := &v1.LocalSecretKeySelector{}
if err := paved.GetValueInto(secretRefFieldPath, sel); err != nil {
return errors.Wrapf(xpresource.Ignore(fieldpath.IsNotFound, err), "cannot unmarshal %s into a secret key selector", secretRefFieldPath)
}
s := &corev1.Secret{}
if err := client.Get(ctx, types.NamespacedName{Namespace: mg.GetNamespace(), Name: sel.Name}, s); xpresource.IgnoreNotFound(err) != nil {
return errors.Wrap(err, ErrGetPasswordSecret)
}
if err == nil && len(s.Data[sel.Key]) != 0 {
// Password is already set.
return nil
}
// At this point, either the secret doesn't exist, or it doesn't
// have the password filled.
if gen, err := paved.GetBool(toggleFieldPath); err != nil || !gen {
// If there is error, then we return that.
// If the toggle field is not set to true, then we return nil.
// Because we don't want to generate a password if the user
// doesn't want to.
return errors.Wrapf(xpresource.Ignore(fieldpath.IsNotFound, err), "cannot get the value of %s", toggleFieldPath)
}
pw, err := password.Generate()
if err != nil {
return errors.Wrap(err, "cannot generate password")
}
s.SetName(sel.Name)
s.SetNamespace(mg.GetNamespace())
if !meta.WasCreated(s) {
// We don't want to own the Secret if it is created by someone
// else, otherwise the deletion of the managed resource will
// delete the Secret that we didn't create in the first place.
meta.AddOwnerReference(s, meta.AsOwner(meta.TypedReferenceTo(mg, mg.GetObjectKind().GroupVersionKind())))
}
if s.Data == nil {
s.Data = make(map[string][]byte, 1)
}
s.Data[sel.Key] = []byte(pw)
return errors.Wrap(xpresource.NewAPIPatchingApplicator(client).Apply(ctx, s), "cannot apply password secret")
})
}
}
// RemovePolicyVersion removes the "Version" field from a JSON-encoded policy string.
func RemovePolicyVersion(p string) (string, error) {
var policy any
if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(p), &policy); err != nil {
return "", errors.Wrap(err, "failed to unmarshal the policy from JSON")
}
m, ok := policy.(map[string]any)
if !ok {
return p, nil
}
delete(m, "Version")
r, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(m)
return string(r), errors.Wrap(err, "failed to marshal the policy map as JSON")
}
// RemoveDiffIfEmpty removes supplied keys from Terraform diffs, if old and new
// values for the key are empty (""). It is probably safe to delete all such
// keys in the diff. Until we decide to do so, we'll specify the keys, to be on
// the safe side.
func RemoveDiffIfEmpty(keys []string) config.CustomDiff { //nolint:gocyclo // The implementation is pretty straightforward as of this writing.
return func(diff *terraform.InstanceDiff, state *terraform.InstanceState, config *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
// Skip diff customization on create
if state == nil || state.Empty() {
return diff, nil
}
if config == nil {
return nil, errors.New("resource config cannot be nil")
}
// Skip no diff or destroy diffs
if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil {
return diff, nil
}
for _, key := range keys {
if diff.Attributes[key] != nil && diff.Attributes[key].Old == "" && diff.Attributes[key].New == "" {
delete(diff.Attributes, key)
}
}
return diff, nil
}
}