Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit61d7d29

Browse files
authored
fix: remove state information from apply (#21373)
Delete builds were not deleting resources as the tf state being sent in the apply request was empty.State removed from apply request and read from the session instead.
1 parent0af038b commit61d7d29

File tree

6 files changed

+226
-212
lines changed

6 files changed

+226
-212
lines changed

‎enterprise/coderd/workspaces_test.go‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"fmt"
99
"net/http"
10+
"os"
1011
"path/filepath"
1112
"strings"
1213
"sync/atomic"
@@ -3074,7 +3075,8 @@ func TestWorkspaceProvisionerdServerMetrics(t *testing.T) {
30743075
// This is testing that dynamic params defers input validation to terraform.
30753076
// It does not try to do this in coder/coder.
30763077
funcTestWorkspaceTemplateParamsChange(t*testing.T) {
3077-
mainTfTemplate:=`
3078+
indicatorFile:=filepath.Join(t.TempDir(),"workspace_indicator.txt")
3079+
mainTfTemplate:=fmt.Sprintf(`
30783080
terraform {
30793081
required_providers {
30803082
coder = {
@@ -3100,7 +3102,12 @@ func TestWorkspaceTemplateParamsChange(t *testing.T) {
31003102
min = data.coder_parameter.param_min.value
31013103
}
31023104
}
3103-
`
3105+
3106+
resource "local_file" "workspace_indicator" {
3107+
content = "I exist"
3108+
filename = "%s"
3109+
}
3110+
`,indicatorFile)
31043111
tfCliConfigPath:=downloadProviders(t,mainTfTemplate)
31053112
t.Setenv("TF_CLI_CONFIG_FILE",tfCliConfigPath)
31063113

@@ -3176,13 +3183,30 @@ func TestWorkspaceTemplateParamsChange(t *testing.T) {
31763183
createBuild:=coderdtest.AwaitWorkspaceBuildJobCompleted(t,member,ws.LatestBuild.ID)
31773184
require.Equal(t,createBuild.Status,codersdk.WorkspaceStatusRunning)
31783185

3186+
// File should exist
3187+
_,err=os.Stat(indicatorFile)
3188+
require.NoError(t,err,"file created for workspace build")
3189+
31793190
// Now delete the workspace
31803191
build,err:=member.CreateWorkspaceBuild(ctx,ws.ID, codersdk.CreateWorkspaceBuildRequest{
31813192
Transition:codersdk.WorkspaceTransitionDelete,
31823193
})
31833194
require.NoError(t,err)
31843195
build=coderdtest.AwaitWorkspaceBuildJobCompleted(t,member,build.ID)
31853196
require.Equal(t,codersdk.WorkspaceStatusDeleted,build.Status)
3197+
3198+
logsCh,closeLogs,err:=member.WorkspaceBuildLogsAfter(ctx,build.ID,0)
3199+
require.NoError(t,err)
3200+
t.Cleanup(func() {
3201+
_=closeLogs.Close()
3202+
})
3203+
forlog:=rangelogsCh {
3204+
assert.NotContains(t,log.Output,"there is nothing to do")
3205+
}
3206+
3207+
// File should be deleted from terraform apply
3208+
_,err=os.Stat(indicatorFile)
3209+
require.ErrorIs(t,err,os.ErrNotExist)
31863210
}
31873211

31883212
typetestWorkspaceTagsTerraformCasestruct {

‎provisioner/terraform/provision.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package terraform
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
89
"net"
@@ -284,18 +285,21 @@ func (s *server) Apply(
284285
}
285286
logTerraformEnvVars(sess)
286287

287-
// Exit early if there is no plan file. This is necessary to
288+
// Earlier in the session, Plan() will have written the state file and the plan file.
289+
statefilePath:=sess.Files.StateFilePath()
290+
291+
// Exit early if there is no state file. This is necessary to
288292
// avoid any cases where a workspace is "locked out" of terraform due to
289293
// e.g. bad template param values and cannot be deleted. This is just for
290294
// contingency, in the future we will try harder to prevent workspaces being
291295
// broken this hard.
292-
ifrequest.Metadata.GetWorkspaceTransition()==proto.WorkspaceTransition_DESTROY&&len(request.GetState())==0 {
293-
sess.ProvisionLog(proto.LogLevel_INFO,"The terraform plan does not exist, there is nothing to do")
294-
return&proto.ApplyComplete{}
296+
ifrequest.Metadata.GetWorkspaceTransition()==proto.WorkspaceTransition_DESTROY {
297+
if_,err:=os.Stat(statefilePath);errors.Is(err,os.ErrNotExist) {
298+
sess.ProvisionLog(proto.LogLevel_INFO,"The terraform state does not exist, there is nothing to do")
299+
return&proto.ApplyComplete{}
300+
}
295301
}
296302

297-
// Earlier in the session, Plan() will have written the state file and the plan file.
298-
statefilePath:=sess.Files.StateFilePath()
299303
env,err:=provisionEnv(sess.Config,request.Metadata,nil,nil,nil)
300304
iferr!=nil {
301305
returnprovisionersdk.ApplyErrorf("provision env: %s",err)

‎provisioner/terraform/provision_test.go‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ func configure(ctx context.Context, t *testing.T, client proto.DRPCProvisionerCl
110110
returnsess
111111
}
112112

113-
funcreadProvisionLog(t*testing.T,response proto.DRPCProvisioner_SessionClient)string {
113+
funcreadProvisionLog(t*testing.T,response proto.DRPCProvisioner_SessionClient) (string,*proto.Response) {
114+
varlast*proto.Response
114115
varlogBuf strings.Builder
115116
for {
116117
msg,err:=response.Recv()
@@ -122,9 +123,10 @@ func readProvisionLog(t *testing.T, response proto.DRPCProvisioner_SessionClient
122123
require.NoError(t,err)
123124
continue
124125
}
126+
last=msg
125127
break
126128
}
127-
returnlogBuf.String()
129+
returnlogBuf.String(),last
128130
}
129131

130132
funcsendInit(sess proto.DRPCProvisioner_SessionClient,archive []byte)error {
@@ -1287,15 +1289,18 @@ func TestProvision_SafeEnv(t *testing.T) {
12871289
err:=sendPlan(sess,proto.WorkspaceTransition_START)
12881290
require.NoError(t,err)
12891291

1290-
_=readProvisionLog(t,sess)
1292+
_,_=readProvisionLog(t,sess)
12911293

12921294
err=sendApply(sess,proto.WorkspaceTransition_START)
12931295
require.NoError(t,err)
12941296

1295-
log:=readProvisionLog(t,sess)
1297+
log,applyComplete:=readProvisionLog(t,sess)
12961298
require.Contains(t,log,passedValue)
12971299
require.NotContains(t,log,secretValue)
12981300
require.Contains(t,log,"CODER_")
1301+
1302+
apply:=applyComplete.Type.(*proto.Response_Apply)
1303+
require.NotEmpty(t,apply.Apply.State,"state exists")
12991304
}
13001305

13011306
funcTestProvision_MalformedModules(t*testing.T) {
@@ -1310,6 +1315,6 @@ func TestProvision_MalformedModules(t *testing.T) {
13101315
}))
13111316
require.NoError(t,err)
13121317

1313-
log:=readProvisionLog(t,sess)
1318+
log,_:=readProvisionLog(t,sess)
13141319
require.Contains(t,log,"Invalid block definition")
13151320
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp