Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions manifest/provider/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ func (s *RawProviderServer) ApplyResourceChange(ctx context.Context, req *tfprot
if !isComputed {
return v, nil
}
if v.IsKnown() {
return tftypes.NewValue(v.Type(), tftypes.UnknownValue), nil
}

ppMan, restPath, err := tftypes.WalkAttributePath(plannedStateVal["manifest"], ap)
if err != nil {
if len(restPath.Steps()) > 0 {
Expand Down
1 change: 0 additions & 1 deletion manifest/provider/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ func (s *RawProviderServer) PlanResourceChange(ctx context.Context, req *tfproto
}
nowCfg, restPath, err := tftypes.WalkAttributePath(ppMan, ap)
hasChanged = err == nil && len(restPath.Steps()) == 0 && wasCfg.(tftypes.Value).IsKnown() && !wasCfg.(tftypes.Value).Equal(nowCfg.(tftypes.Value))

if hasChanged {
h, ok := hints[morph.ValueToTypePath(ap).String()]
if ok && h == manifest.PreserveUnknownFieldsLabel {
Expand Down
16 changes: 11 additions & 5 deletions manifest/test/acceptance/computed_attr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestKubernetesManifest_ComputedFields(t *testing.T) {
step1.Close()
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "secrets", namespace, name)
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "services", namespace, name)
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "deployments", namespace, name)
k8shelper.AssertNamespacedResourceDoesNotExist(t, "apps/v1", "deployments", namespace, name)
k8shelper.AssertResourceDoesNotExist(t, "admissionregistration.k8s.io", "mutatingwebhookconfigurations", name)
}()

Expand All @@ -66,13 +66,15 @@ func TestKubernetesManifest_ComputedFields(t *testing.T) {
defer func() {
step2.Destroy(ctx)
step2.Close()
k8shelper.AssertNamespacedResourceDoesNotExist(t, "apps/v1", "deployments", namespace, name)
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "configmaps", namespace, name)
}()

tfconfig = loadTerraformConfig(t, "ComputedFields/computed.tf", tfvars)
step2.SetConfig(ctx, string(tfconfig))
step2.Init(ctx)
step2.Apply(ctx)
k8shelper.AssertNamespacedResourceExists(t, "apps/v1", "deployments", namespace, name)
k8shelper.AssertNamespacedResourceExists(t, "v1", "configmaps", namespace, name)

s2, err := step2.State(ctx)
Expand All @@ -81,9 +83,13 @@ func TestKubernetesManifest_ComputedFields(t *testing.T) {
}
tfstate := tfstatehelper.NewHelper(s2)
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.annotations.tf-k8s-acc": "true",
"kubernetes_manifest.test.object.metadata.annotations.mutated": "true",
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.annotations.tf-k8s-acc": "true",
"kubernetes_manifest.test.object.metadata.annotations.mutated": "true",
"kubernetes_manifest.deployment_resource_diff.object.metadata.name": name,
"kubernetes_manifest.deployment_resource_diff.object.metadata.namespace": namespace,
"kubernetes_manifest.deployment_resource_diff.object.spec.template.spec.containers[0].resources.limits[\"cpu\"]": "250m",
"kubernetes_manifest.deployment_resource_diff.object.spec.template.spec.containers[0].resources.limits[\"memory\"]": "512Mi",
})
}
50 changes: 50 additions & 0 deletions manifest/test/acceptance/testdata/ComputedFields/computed.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,53 @@ resource "kubernetes_manifest" "test" {
}
}
}

resource "kubernetes_manifest" "deployment_resource_diff" {
computed_fields = ["spec.template.spec.containers[0].resources.limits"]
manifest = {
apiVersion = "apps/v1"
kind = "Deployment"

metadata = {
name = var.name
namespace = var.namespace
}

spec = {
replicas = 3

selector = {
matchLabels = {
test = "MyExampleApp"
}
}

template = {
metadata= {
labels = {
test = "MyExampleApp"
}
}


spec = {
containers = [{
image = "nginx:1.21.6"
name = "example"

resources = {
limits = {
cpu = "0.25"
memory = "512Mi"
}
requests = {
cpu = "250m"
memory = "50Mi"
}
}
}]
}
}
}
}
}