Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manifest: change computed_field logic to mark all as unknown instead of only on changes #2432

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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 v, nil
}

ppMan, restPath, err := tftypes.WalkAttributePath(plannedStateVal["manifest"], ap)
if err != nil {
if len(restPath.Steps()) > 0 {
Expand Down
12 changes: 3 additions & 9 deletions manifest/provider/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ func (s *RawProviderServer) PlanResourceChange(ctx context.Context, req *tfproto
}
updatedObj, err := tftypes.Transform(completePropMan, func(ap *tftypes.AttributePath, v tftypes.Value) (tftypes.Value, error) {
_, isComputed := computedFields[ap.String()]
if isComputed {
return tftypes.NewValue(v.Type(), tftypes.UnknownValue), nil
}
if v.IsKnown() { // this is a value from current configuration - include it in the plan
hasChanged := false
wasCfg, restPath, err := tftypes.WalkAttributePath(priorMan, ap)
Expand All @@ -437,15 +440,6 @@ func (s *RawProviderServer) PlanResourceChange(ctx context.Context, req *tfproto
resp.RequiresReplace = append(resp.RequiresReplace, tftypes.NewAttributePathWithSteps(apm))
}
}
if isComputed {
if hasChanged {
return tftypes.NewValue(v.Type(), tftypes.UnknownValue), nil
}
nowVal, restPath, err := tftypes.WalkAttributePath(proposedVal["object"], ap)
if err == nil && len(restPath.Steps()) == 0 {
return nowVal.(tftypes.Value), nil
}
}
return v, nil
}
// check if value was present in the previous configuration
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"
}
}
}]
}
}
}
}
}
Loading