-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update for default resource tags (#335)
Issue #, if available: aws-controllers-k8s/community#1261 Description of changes: * Removes `service.k8s.aws/managed=true` and `services.k8s.aws/created=%UTC_NOW%` tags and adds the new `services.k8s.aws/controller-version=%CONTROLLER_VERSION%` tag as resource-tags input for ACK controller. * Updates ACK runtime to `v0.19.0` * Adds new `TagConfig` field inside `ResourceConfig` * Adds the code-generation for implementation of `AWSResourceManager.EnsureTags()` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
- Loading branch information
Showing
18 changed files
with
556 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 code | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws-controllers-k8s/code-generator/pkg/fieldpath" | ||
|
||
"github.com/aws-controllers-k8s/code-generator/pkg/model" | ||
) | ||
|
||
// InitializeNestedStructField returns the go code for initializing a nested | ||
// struct field. Currently this method only supports the struct shape for | ||
// nested elements. | ||
// | ||
// TODO(vijtrip2): Refactor the code out of set_resource.go for generating | ||
// constructors and reuse here. This method is currently being used for handling | ||
// nested Tagging fields. | ||
// | ||
// Example: generated code for "Logging.LoggingEnabled.TargetBucket" field | ||
// inside "s3" "bucket" crd looks like: | ||
// | ||
// ``` | ||
// r.ko.Spec.Logging = &svcapitypes.BucketLoggingStatus{} | ||
// r.ko.Spec.Logging.LoggingEnabled = &svcapitypes.LoggingEnabled{} | ||
// ``` | ||
func InitializeNestedStructField( | ||
r *model.CRD, | ||
sourceVarName string, | ||
field *model.Field, | ||
// apiPkgAlias contains the imported package alias where the type definition | ||
// for nested structs is present. | ||
// ex: svcapitypes "github.com/aws-controllers-k8s/s3-controller/apis/v1alpha1" | ||
apiPkgAlias string, | ||
// Number of levels of indentation to use | ||
indentLevel int, | ||
) string { | ||
out := "" | ||
indent := strings.Repeat("\t", indentLevel) | ||
fieldPath := field.Path | ||
if fieldPath != "" { | ||
fp := fieldpath.FromString(fieldPath) | ||
if fp.Size() > 1 { | ||
// replace the front field name with front field shape name inside | ||
// the field path to construct the fieldShapePath | ||
front := fp.Front() | ||
frontField := r.Fields[front] | ||
if frontField == nil { | ||
panic(fmt.Sprintf("unable to find the field with name %s"+ | ||
" for fieldpath %s", front, fieldPath)) | ||
} | ||
if frontField.ShapeRef == nil { | ||
panic(fmt.Sprintf("nil ShapeRef for field %s", front)) | ||
} | ||
fieldShapePath := strings.Replace(fieldPath, front, | ||
frontField.ShapeRef.ShapeName, 1) | ||
fsp := fieldpath.FromString(fieldShapePath) | ||
var index int | ||
// Build the prefix to access elements in field path. | ||
// Use the front of fieldpath to determine whether the field is | ||
// a spec field or status field. | ||
elemAccessPrefix := sourceVarName | ||
if _, found := r.SpecFields[front]; found { | ||
elemAccessPrefix = fmt.Sprintf("%s%s", elemAccessPrefix, | ||
r.Config().PrefixConfig.SpecField) | ||
} else { | ||
elemAccessPrefix = fmt.Sprintf("%s%s", elemAccessPrefix, | ||
r.Config().PrefixConfig.StatusField) | ||
} | ||
var importPath string | ||
if apiPkgAlias != "" { | ||
importPath = fmt.Sprintf("%s.", apiPkgAlias) | ||
} | ||
// traverse over the fieldShapePath and initialize every element | ||
// except the last. | ||
for index < fsp.Size()-1 { | ||
elemName := fp.At(index) | ||
elemShapeRef := fsp.ShapeRefAt(frontField.ShapeRef, index) | ||
if elemShapeRef.Shape.Type != "structure" { | ||
panic(fmt.Sprintf("only nested structures are supported."+ | ||
" Shape type for %s is %s inside fieldpath %s", elemName, | ||
elemShapeRef.Shape.Type, fieldPath)) | ||
} | ||
out += fmt.Sprintf("%s%s.%s = &%s%s{}\n", | ||
indent, elemAccessPrefix, elemName, importPath, | ||
elemShapeRef.GoTypeElem()) | ||
elemAccessPrefix = fmt.Sprintf("%s.%s", elemAccessPrefix, | ||
elemName) | ||
index++ | ||
} | ||
} | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package code_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws-controllers-k8s/code-generator/pkg/generate/code" | ||
|
||
"github.com/aws-controllers-k8s/code-generator/pkg/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestInitializeNestedStructField(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
g := testutil.NewModelForServiceWithOptions(t, "s3", | ||
&testutil.TestingModelOptions{GeneratorConfigFile: "generator-with-tags.yaml"}) | ||
|
||
crd := testutil.GetCRDByName(t, g, "Bucket") | ||
assert.NotNil(crd) | ||
|
||
f := crd.Fields["Logging.LoggingEnabled.TargetBucket"] | ||
|
||
s := code.InitializeNestedStructField(crd, "r.ko", f, | ||
"svcapitypes", 1) | ||
expected := | ||
` r.ko.Spec.Logging = &svcapitypes.BucketLoggingStatus{} | ||
r.ko.Spec.Logging.LoggingEnabled = &svcapitypes.LoggingEnabled{} | ||
` | ||
assert.Equal(expected, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.