Skip to content

Commit cd9c3be

Browse files
authored
Notebook instance - Handling update only parameters (#82)
This pull request should be merged after #61, it contains code for handling cases where (lifecycleConfigName, AcceleratorTypes, DefaultCodeRepository, AdditionalCodeRepositories) are nil in desired and not nil in latest. In other words it allows the user to delete (lifecycleConfigName, AcceleratorTypes, DefaultCodeRepository, AdditionalCodeRepositories). TODO: In test/e2e/tests/test_notebook_instance.py, we should use replace_custom_resource when test-infra supports it. Files changed from #61 : **generator.yaml:** Added a sdk_update_post_build_request hook that calls the handleUpdateOnlyParameters function **pkg/resource/notebook_instance/sdk.go:** Added code that calls handleUpdateOnlyParameters **test/e2e/tests/test_notebook_instance.py:** Added a test for Update only parameters. **test/e2e/resources/notebook_instance.yaml:** Added the a field to test the update only parameters. **pkg/resource/notebook_instance/custom_set_input_update.go:** Contains the function handleUpdateOnlyParameters which checks if the (lifecycleConfigName, AcceleratorTypes, DefaultCodeRepository, AdditionalCodeRepositories) are nil, if they are nil in the desired resource and not nil in the latest resource it sets the respective Disassociate<field> to true for the update input.
1 parent 754b480 commit cd9c3be

File tree

7 files changed

+62
-5
lines changed

7 files changed

+62
-5
lines changed

apis/v1alpha1/generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ resources:
658658
code: customSetDefaults(a, b)
659659
sdk_update_pre_build_request:
660660
template_path: notebook_instance/sdk_update_pre_build_request.go.tpl
661+
sdk_update_post_build_request:
662+
code: handleUpdateOnlyParameters(desired, latest, input)
661663
sdk_update_post_set_output:
662664
code: rm.customSetOutputUpdate(ko, latest)
663665
sdk_delete_pre_build_request:

generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ resources:
658658
code: customSetDefaults(a, b)
659659
sdk_update_pre_build_request:
660660
template_path: notebook_instance/sdk_update_pre_build_request.go.tpl
661+
sdk_update_post_build_request:
662+
code: handleUpdateOnlyParameters(desired, latest, input)
661663
sdk_update_post_set_output:
662664
code: rm.customSetOutputUpdate(ko, latest)
663665
sdk_delete_pre_build_request:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package notebook_instance
2+
3+
import (
4+
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
5+
"github.com/aws/aws-sdk-go/aws"
6+
svcsdk "github.com/aws/aws-sdk-go/service/sagemaker"
7+
)
8+
9+
// handleUpdateOnlyParameters sets Disassociate<field> to true if corresponding value in desired is nil(ie not included in the spec)
10+
// and if the corresponding value in the latest spec is not nil (ie contains some value)
11+
// Ex if LifecycleConfigName was "a" in the latest spec but nil in the desired spec, DisassociateLifecycleConfig is set to true
12+
func handleUpdateOnlyParameters(
13+
desired *resource,
14+
latest *resource,
15+
update_input *svcsdk.UpdateNotebookInstanceInput) {
16+
if ackcompare.IsNil(desired.ko.Spec.AcceleratorTypes) && ackcompare.IsNotNil(latest.ko.Spec.AcceleratorTypes) {
17+
update_input.DisassociateAcceleratorTypes = aws.Bool(true)
18+
}
19+
if ackcompare.IsNil(desired.ko.Spec.AdditionalCodeRepositories) && ackcompare.IsNotNil(latest.ko.Spec.AdditionalCodeRepositories) {
20+
update_input.DisassociateAdditionalCodeRepositories = aws.Bool(true)
21+
}
22+
if ackcompare.IsNil(desired.ko.Spec.DefaultCodeRepository) && ackcompare.IsNotNil(latest.ko.Spec.DefaultCodeRepository) {
23+
update_input.DisassociateDefaultCodeRepository = aws.Bool(true)
24+
}
25+
if ackcompare.IsNil(desired.ko.Spec.LifecycleConfigName) && ackcompare.IsNotNil(latest.ko.Spec.LifecycleConfigName) {
26+
update_input.DisassociateLifecycleConfig = aws.Bool(true)
27+
}
28+
}

pkg/resource/notebook_instance/sdk.go

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

templates/notebook_instance/sdk_read_one_post_set_output.go.tpl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
// NotebookInstanceLifecycleConfigName does not get generated by the code generator.
2-
// because the field name in spec vs Describe response do not match
1+
// NotebookInstanceLifecycleConfigName and SecurityGroups do not get generated by the code generator.
2+
// Because the field name in spec vs Describe response do not match
33
if resp.NotebookInstanceLifecycleConfigName != nil {
44
ko.Spec.LifecycleConfigName = resp.NotebookInstanceLifecycleConfigName
55
} else {
66
ko.Spec.LifecycleConfigName = nil
77
}
8+
if resp.SecurityGroups != nil {
9+
ko.Spec.SecurityGroupIDs = resp.SecurityGroups
10+
} else {
11+
ko.Spec.SecurityGroupIDs = nil
12+
}
813
err = rm.customSetOutputDescribe(&resource{ko})
914
if err != nil{
1015
return nil, err

test/e2e/resources/notebook_instance.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ spec:
88
roleARN: $SAGEMAKER_EXECUTION_ROLE_ARN
99
volumeSizeInGB: 6
1010
directInternetAccess: Enabled
11-
rootAccess: Enabled
11+
rootAccess: Enabled
12+
defaultCodeRepository: $DEFAULT_CODE_REPOSITORY

test/e2e/tests/test_notebook_instance.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535

3636
@pytest.fixture(scope="module")
3737
def notebook_instance():
38+
default_code_repository = "https://github.com/aws-controllers-k8s/community"
3839
resource_name = random_suffix_name("nb", 32)
3940
replacements = REPLACEMENT_VALUES.copy()
4041
replacements["NOTEBOOK_INSTANCE_NAME"] = resource_name
42+
replacements["DEFAULT_CODE_REPOSITORY"] = default_code_repository
4143
reference, spec, resource = create_sagemaker_resource(
4244
resource_plural="notebookinstances",
4345
resource_name=resource_name,
@@ -160,8 +162,13 @@ def update_notebook_test(self, notebook_instance):
160162
(reference, resource, spec) = notebook_instance
161163
notebook_instance_name = resource["spec"].get("notebookInstanceName", None)
162164
volumeSizeInGB = 7
165+
additionalCodeRepositories = ["https://github.com/aws-controllers-k8s/runtime"]
163166

164167
spec["spec"]["volumeSizeInGB"] = volumeSizeInGB
168+
# TODO: Use del spec["spec"]["defaultCodeRepository"] instead once ack.testinfra supports replacement.
169+
# Patch only supports updating spec fields instead of fully getting rid of them.
170+
spec["spec"]["defaultCodeRepository"] = None
171+
spec["spec"]["additionalCodeRepositories"] = additionalCodeRepositories
165172
k8s.patch_custom_resource(reference, spec)
166173

167174
self._assert_notebook_status_in_sync(
@@ -183,6 +190,12 @@ def update_notebook_test(self, notebook_instance):
183190
resource = k8s.get_resource(reference)
184191
assert resource["spec"]["volumeSizeInGB"] == volumeSizeInGB
185192

193+
assert "DefaultCodeRepository" not in notebook_instance_desc
194+
assert "defaultCodeRepository" not in resource["spec"]
195+
196+
assert resource["spec"]["additionalCodeRepositories"] == additionalCodeRepositories
197+
assert notebook_instance_desc["AdditionalCodeRepositories"] == additionalCodeRepositories
198+
186199
assert "stoppedByControllerMetadata" not in resource["status"]
187200

188201
def delete_notebook_test(self, notebook_instance):

0 commit comments

Comments
 (0)