Skip to content

Commit cbda0df

Browse files
Merge pull request #5 from nmvk/secret
SecretKeyRef - Runtime Implementation
2 parents 96355b6 + fb79ea8 commit cbda0df

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

apis/core/v1alpha1/secret.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package v1alpha1
15+
16+
import (
17+
k8scorev1 "k8s.io/api/core/v1"
18+
)
19+
20+
// SecretKeyReference combines a k8s corev1.SecretReference with a
21+
// specific key within the referred-to Secret
22+
type SecretKeyReference struct {
23+
// Empty JSON tag is required to solve encountered struct field "" without JSON tag error.
24+
k8scorev1.SecretReference `json:""`
25+
// Key is the key within the secret
26+
Key string `json:"key"`
27+
}

mocks/pkg/types/aws_resource_reconciler.go

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/errors/error.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ var (
4242
// Terminal is returned with resource is in Terminal Condition
4343
Terminal = fmt.Errorf(
4444
"resource is in terminal condition")
45+
// SecretTypeNotSupported is returned if non opaque secret is used.
46+
SecretTypeNotSupported = fmt.Errorf(
47+
"only opaque secrets can be used")
4548
)
4649

4750
// AWSError returns the type conversion for the supplied error to an aws-sdk-go

pkg/runtime/reconciler.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,41 @@ func (r *reconciler) BindControllerManager(mgr ctrlrt.Manager) error {
8585
}
8686

8787
// SecretValueFromReference fetches the value of a Secret given a
88-
// SecretReference
88+
// SecretKeyReference.
8989
func (r *reconciler) SecretValueFromReference(
90-
ref *corev1.SecretReference,
90+
ctx context.Context,
91+
ref *ackv1alpha1.SecretKeyReference,
9192
) (string, error) {
92-
// TODO(alina-kim): Implement this method :)
93-
return "", ackerr.NotImplemented
93+
94+
if ref == nil {
95+
return "", nil
96+
}
97+
98+
namespace := ref.Namespace
99+
if namespace == "" {
100+
namespace = "default"
101+
}
102+
103+
nsn := client.ObjectKey{
104+
Namespace: namespace,
105+
Name: ref.Name,
106+
}
107+
var secret corev1.Secret
108+
if err := r.kc.Get(ctx, nsn, &secret); err != nil {
109+
return "", err
110+
}
111+
112+
// Currently we have only Opaque secrets in scope.
113+
if secret.Type != corev1.SecretTypeOpaque {
114+
return "", ackerr.SecretTypeNotSupported
115+
}
116+
117+
if value, ok := secret.Data[ref.Key]; ok {
118+
valuestr := string(value)
119+
return valuestr, nil
120+
}
121+
122+
return "", ackerr.NotFound
94123
}
95124

96125
// Reconcile implements `controller-runtime.Reconciler` and handles reconciling

pkg/types/aws_resource_reconciler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
package types
1515

1616
import (
17-
corev1 "k8s.io/api/core/v1"
17+
"context"
18+
"github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
1819
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1920
ctrlrt "sigs.k8s.io/controller-runtime"
2021
ctrlreconcile "sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -39,6 +40,6 @@ type AWSResourceReconciler interface {
3940
// of an upstream controller-runtime.Manager
4041
BindControllerManager(ctrlrt.Manager) error
4142
// SecretValueFromReference fetches the value of a Secret given a
42-
// SecretReference
43-
SecretValueFromReference(*corev1.SecretReference) (string, error)
43+
// SecretKeyReference
44+
SecretValueFromReference(context.Context, *v1alpha1.SecretKeyReference) (string, error)
4445
}

0 commit comments

Comments
 (0)