Skip to content

Commit 531099e

Browse files
authored
Merge pull request #14 from RedbackThomson/resource-adoption
Adopted resource template changes
2 parents b6d98fa + 21564b1 commit 531099e

File tree

11 files changed

+103
-17
lines changed

11 files changed

+103
-17
lines changed

pkg/generate/code/set_sdk.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func SetSDK(
223223
out += fmt.Sprintf(
224224
"%s} else {\n", indent,
225225
)
226-
nameField := r.NameField()
226+
nameField := *r.SpecIdentifierField()
227227
out += fmt.Sprintf(
228228
"%s\t%s.Set%s(rm.ARNFromName(*%s.Spec.%s))\n",
229229
indent, targetVarName, memberName, sourceVarName, nameField,
@@ -444,7 +444,7 @@ func SetSDKGetAttributes(
444444
out += fmt.Sprintf(
445445
"%s} else {\n", indent,
446446
)
447-
nameField := r.NameField()
447+
nameField := *r.SpecIdentifierField()
448448
out += fmt.Sprintf(
449449
"%s\t%s.Set%s(rm.ARNFromName(*%s.Spec.%s))\n",
450450
indent, targetVarName, memberName, sourceVarName, nameField,
@@ -617,7 +617,7 @@ func SetSDKSetAttributes(
617617
out += fmt.Sprintf(
618618
"%s} else {\n", indent,
619619
)
620-
nameField := r.NameField()
620+
nameField := *r.SpecIdentifierField()
621621
out += fmt.Sprintf(
622622
"%s\t%s.Set%s(rm.ARNFromName(*%s.Spec.%s))\n",
623623
indent, targetVarName, memberName, sourceVarName, nameField,

pkg/generate/config/resource.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type ResourceConfig struct {
7272
// All ShortNames must be distinct from any other ShortNames installed into the cluster,
7373
// otherwise the CRD will fail to install.
7474
ShortNames []string `json:"shortNames,omitempty"`
75+
// IsAdoptable determines whether the CRD should be accepted by the adoption reconciler.
76+
// If set to false, the user will be given an error if they attempt to adopt a resource
77+
// with this type.
78+
IsAdoptable *bool `json:"is_adoptable,omitempty"`
7579
}
7680

7781
// HooksConfig instructs the code generator how to inject custom callback hooks
@@ -383,3 +387,19 @@ func (c *Config) ResourceShortNames(resourceName string) []string {
383387
}
384388
return rConfig.ShortNames
385389
}
390+
391+
// ResourceIsAdoptable returns whether the given CRD is adoptable
392+
func (c *Config) ResourceIsAdoptable(resourceName string) bool {
393+
if c == nil {
394+
return true
395+
}
396+
rConfig, ok := c.Resources[resourceName]
397+
if !ok {
398+
return true
399+
}
400+
// Default to True
401+
if rConfig.IsAdoptable == nil {
402+
return true
403+
}
404+
return *rConfig.IsAdoptable
405+
}

pkg/model/crd.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,14 @@ func (r *CRD) UpdateConditionsCustomMethodName() string {
336336
return resGenConfig.UpdateConditionsCustomMethodName
337337
}
338338

339-
// NameField returns the name of the "Name" or string identifier field in the Spec
340-
func (r *CRD) NameField() string {
339+
// SpecIdentifierField returns the name of the "Name" or string identifier field in the Spec
340+
func (r *CRD) SpecIdentifierField() *string {
341341
if r.cfg != nil {
342342
rConfig, found := r.cfg.Resources[r.Names.Original]
343343
if found {
344344
for fName, fConfig := range rConfig.Fields {
345345
if fConfig.IsName {
346-
return fName
346+
return &fName
347347
}
348348
}
349349
}
@@ -355,10 +355,19 @@ func (r *CRD) NameField() string {
355355
}
356356
for memberName := range r.SpecFields {
357357
if util.InStrings(memberName, lookup) {
358-
return memberName
358+
return &memberName
359359
}
360360
}
361-
return "???"
361+
return nil
362+
}
363+
364+
// IsAdoptable returns true if the resource can be adopted
365+
func (r *CRD) IsAdoptable() bool {
366+
if r.cfg == nil {
367+
// Should never reach this condition
368+
return false
369+
}
370+
return r.cfg.ResourceIsAdoptable(r.Names.Original)
362371
}
363372

364373
// CustomUpdateMethodName returns the name of the custom resourceManager method

scripts/build-controller.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ ACK_GENERATE_BIN_PATH=${ACK_GENERATE_BIN_PATH:-$DEFAULT_ACK_GENERATE_BIN_PATH}
2727
ACK_GENERATE_API_VERSION=${ACK_GENERATE_API_VERSION:-"v1alpha1"}
2828
ACK_GENERATE_CONFIG_PATH=${ACK_GENERATE_CONFIG_PATH:-""}
2929
AWS_SDK_GO_VERSION=${AWS_SDK_GO_VERSION:-""}
30+
DEFAULT_RUNTIME_CRD_DIR="$ROOT_DIR/../../aws-controllers-k8s/runtime/config"
31+
RUNTIME_CRD_DIR=${RUNTIME_CRD_DIR:-$DEFAULT_RUNTIME_CRD_DIR}
3032

3133
USAGE="
3234
Usage:
@@ -117,6 +119,12 @@ TEMPLATE_DIRS=${TEMPLATE_DIRS:-$DEFAULT_TEMPLATE_DIRS}
117119

118120
K8S_RBAC_ROLE_NAME=${K8S_RBAC_ROLE_NAME:-"ack-$SERVICE-controller"}
119121

122+
config_output_dir="$SERVICE_CONTROLLER_SOURCE_PATH/config/"
123+
124+
echo "Copying common custom resource definitions into $SERVICE"
125+
mkdir -p $config_output_dir/crd/common
126+
cp -r $RUNTIME_CRD_DIR/crd/* $config_output_dir/crd/common/
127+
120128
# If there's a generator.yaml in the service's directory and the caller hasn't
121129
# specified an override, use that.
122130
if [ -z "$ACK_GENERATE_CONFIG_PATH" ]; then
@@ -151,8 +159,6 @@ if [ $? -ne 0 ]; then
151159
exit 2
152160
fi
153161

154-
config_output_dir="$SERVICE_CONTROLLER_SOURCE_PATH/config/"
155-
156162
pushd $SERVICE_CONTROLLER_SOURCE_PATH/apis/$ACK_GENERATE_API_VERSION 1>/dev/null
157163

158164
echo "Generating deepcopy code for $SERVICE"
@@ -186,4 +192,4 @@ mv $config_output_dir/rbac/role.yaml $config_output_dir/rbac/cluster-role-contro
186192
popd 1>/dev/null
187193

188194
echo "Running gofmt against generated code for $SERVICE"
189-
gofmt -w "$SERVICE_CONTROLLER_SOURCE_PATH"
195+
gofmt -w "$SERVICE_CONTROLLER_SOURCE_PATH"

templates/cmd/controller/main.go.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
ctrlrt "sigs.k8s.io/controller-runtime"
1414
ctrlrtmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
1515

16+
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
1617
svcresource "github.com/aws-controllers-k8s/{{ .ServiceIDClean }}-controller/pkg/resource"
1718
svctypes "github.com/aws-controllers-k8s/{{ .ServiceIDClean }}-controller/apis/{{ .APIVersion }}"
1819

@@ -30,6 +31,7 @@ var (
3031
func init() {
3132
_ = clientgoscheme.AddToScheme(scheme)
3233
_ = svctypes.AddToScheme(scheme)
34+
_ = ackv1alpha1.AddToScheme(scheme)
3335
}
3436

3537
func main() {

templates/config/crd/kustomization.yaml.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
apiVersion: kustomize.config.k8s.io/v1beta1
22
kind: Kustomization
3+
bases:
4+
- common
35
resources:
46
{{- range .CRDNames }}
57
- bases/{{ $.APIGroup }}_{{ . }}.yaml

templates/pkg/resource/descriptor.go.tpl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package {{ .CRD.Names.Snake }}
44

55
import (
6+
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
67
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
78
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -129,3 +130,21 @@ func (d *resourceDescriptor) MarkUnmanaged(
129130
}
130131
k8sctrlutil.RemoveFinalizer(obj, finalizerString)
131132
}
133+
134+
// MarkAdopted places descriptors on the custom resource that indicate the
135+
// resource was not created from within ACK.
136+
func (d *resourceDescriptor) MarkAdopted(
137+
res acktypes.AWSResource,
138+
) {
139+
obj := res.RuntimeMetaObject()
140+
if obj == nil {
141+
// Should not happen. If it does, there is a bug in the code
142+
panic("nil RuntimeMetaObject in AWSResource")
143+
}
144+
curr := obj.GetAnnotations()
145+
if curr == nil {
146+
curr = make(map[string]string)
147+
}
148+
curr[ackv1alpha1.AnnotationAdopted] = "true"
149+
obj.SetAnnotations(curr)
150+
}

templates/pkg/resource/manager.go.tpl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222

2323
// +kubebuilder:rbac:groups={{ .APIGroup }},resources={{ ToLower .CRD.Plural }},verbs=get;list;watch;create;update;patch;delete
2424
// +kubebuilder:rbac:groups={{ .APIGroup }},resources={{ ToLower .CRD.Plural }}/status,verbs=get;update;patch
25-
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
26-
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch
2725

2826
// resourceManager is responsible for providing a consistent way to perform
2927
// CRUD operations in a backend AWS service API for Book custom resources.
@@ -37,9 +35,9 @@ type resourceManager struct {
3735
// metrics contains a collection of Prometheus metric objects that the
3836
// service controller and its reconcilers track
3937
metrics *ackmetrics.Metrics
40-
// rr is the AWSResourceReconciler which can be used for various utility
38+
// rr is the Reconciler which can be used for various utility
4139
// functions such as querying for Secret values given a SecretReference
42-
rr acktypes.AWSResourceReconciler
40+
rr acktypes.Reconciler
4341
// awsAccountID is the AWS account identifier that contains the resources
4442
// managed by this resource manager
4543
awsAccountID ackv1alpha1.AWSAccountID
@@ -160,7 +158,7 @@ func newResourceManager(
160158
cfg ackcfg.Config,
161159
log logr.Logger,
162160
metrics *ackmetrics.Metrics,
163-
rr acktypes.AWSResourceReconciler,
161+
rr acktypes.Reconciler,
164162
sess *session.Session,
165163
id ackv1alpha1.AWSAccountID,
166164
region ackv1alpha1.AWSRegion,

templates/pkg/resource/manager_factory.go.tpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (f *resourceManagerFactory) ManagerFor(
3636
cfg ackcfg.Config,
3737
log logr.Logger,
3838
metrics *ackmetrics.Metrics,
39-
rr acktypes.AWSResourceReconciler,
39+
rr acktypes.Reconciler,
4040
sess *session.Session,
4141
id ackv1alpha1.AWSAccountID,
4242
region ackv1alpha1.AWSRegion,
@@ -61,6 +61,11 @@ func (f *resourceManagerFactory) ManagerFor(
6161
return rm, nil
6262
}
6363

64+
// IsAdoptable returns true if the resource is able to be adopted
65+
func (f *resourceManagerFactory) IsAdoptable() bool {
66+
return {{ .CRD.IsAdoptable }}
67+
}
68+
6469
func newResourceManagerFactory() *resourceManagerFactory {
6570
return &resourceManagerFactory{
6671
rmCache: map[string]*resourceManager{},

templates/pkg/resource/registry.go.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import (
77
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
88
)
99

10+
// +kubebuilder:rbac:groups=services.k8s.aws,resources=adoptedresources,verbs=get;list;watch;create;update;patch;delete
11+
// +kubebuilder:rbac:groups=services.k8s.aws,resources=adoptedresources/status,verbs=get;update;patch
12+
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
13+
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch
14+
1015
var (
1116
reg = ackrt.NewRegistry()
1217
)

0 commit comments

Comments
 (0)