Skip to content

Commit ee9ffa0

Browse files
danegstaCopilot
andcommitted
Simplify V2 volume policy and preserve physical log completion
Flatten physical volumes into one create-or-reuse contract, remove replacement and reference-only options, and retain explicit cleanup policy. Preserve physical log completion independently of logical lifecycle, including output drain after exit. Co-authored-by: Copilot App <[email protected]>
1 parent 70e9b1e commit ee9ffa0

18 files changed

Lines changed: 686 additions & 687 deletions

‎api/v2/physical_container_volume_types.go‎

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,12 @@ const (
5050
// PhysicalContainerVolumeReasonCreating indicates that runtime volume creation is in progress.
5151
PhysicalContainerVolumeReasonCreating ConditionReason = "Creating"
5252

53-
// PhysicalContainerVolumeReasonCreated indicates that runtime volume creation completed.
53+
// PhysicalContainerVolumeReasonCreated indicates that a runtime volume was created or selected for reuse.
5454
PhysicalContainerVolumeReasonCreated ConditionReason = "Created"
5555

5656
// PhysicalContainerVolumeReasonCreateFailed indicates that runtime volume creation failed.
5757
PhysicalContainerVolumeReasonCreateFailed ConditionReason = "CreateFailed"
5858

59-
// PhysicalContainerVolumeReasonExistingVolumeReplacementFailed indicates that an existing runtime volume could not be replaced.
60-
PhysicalContainerVolumeReasonExistingVolumeReplacementFailed ConditionReason = "ExistingVolumeReplacementFailed"
61-
6259
// PhysicalContainerVolumeReasonVolumeAvailable indicates that the runtime volume is available.
6360
PhysicalContainerVolumeReasonVolumeAvailable ConditionReason = "VolumeAvailable"
6461

@@ -81,31 +78,16 @@ const (
8178
PhysicalContainerVolumeReasonRuntimeVolumeRemovalAbandoned ConditionReason = "RuntimeVolumeRemovalAbandoned"
8279
)
8380

84-
// PhysicalContainerVolumeSpec describes either an existing runtime volume or how to create one.
81+
// PhysicalContainerVolumeSpec describes a named runtime volume to create or reuse.
8582
// +k8s:openapi-gen=true
8683
type PhysicalContainerVolumeSpec struct {
87-
// VolumeID identifies an existing runtime volume to track. Exactly one of volumeID or volume must be set.
88-
// Container runtimes commonly use the volume name as its identifier.
89-
VolumeID string `json:"volumeID,omitempty"`
90-
91-
// Volume describes a runtime volume to create. Exactly one of volumeID or volume must be set.
92-
Volume *PhysicalContainerVolumeConfig `json:"volume,omitempty"`
93-
}
94-
95-
// PhysicalContainerVolumeConfig describes a runtime volume to create.
96-
// +k8s:openapi-gen=true
97-
type PhysicalContainerVolumeConfig struct {
98-
// VolumeName is the runtime name to use when creating a new volume.
99-
VolumeName string `json:"volumeName,omitempty"`
84+
// VolumeName is the runtime volume name to create or reuse.
85+
VolumeName string `json:"volumeName"`
10086

101-
// RemoveRuntimeVolumeOnDelete removes the created runtime volume when this resource is deleted.
102-
// Created runtime volumes are retained by default.
87+
// RemoveRuntimeVolumeOnDelete removes the created or reused runtime volume when this resource is deleted.
88+
// Runtime volumes are retained by default.
10389
RemoveRuntimeVolumeOnDelete bool `json:"removeRuntimeVolumeOnDelete,omitempty"`
10490

105-
// ReplaceExisting removes an existing runtime volume with volumeName before creating a new one.
106-
// Replacement retries non-forced removal while the existing volume is in use and never removes attached containers.
107-
ReplaceExisting bool `json:"replaceExisting,omitempty"`
108-
10991
// Labels contains labels to apply to a newly-created runtime volume.
11092
// DCP harvesting and physical resource UID labels are reserved and set by the controller.
11193
// +listType=map
@@ -210,27 +192,14 @@ func (pv *PhysicalContainerVolume) Validate(ctx context.Context) field.ErrorList
210192

211193
errorList = append(errorList, commonapi.ValidateAnnotationsSize(pv.Annotations, field.NewPath("metadata", "annotations"))...)
212194

213-
if pv.Spec.VolumeID == "" && pv.Spec.Volume == nil {
214-
errorList = append(errorList, field.Required(specPath, "exactly one of volumeID or volume must be set"))
215-
return errorList
216-
}
217-
if pv.Spec.VolumeID != "" && pv.Spec.Volume != nil {
218-
errorList = append(errorList, field.Forbidden(specPath.Child("volume"), "volume cannot be set when volumeID is set"))
219-
return errorList
220-
}
221-
if pv.Spec.Volume == nil {
222-
return errorList
223-
}
224-
225-
volume := pv.Spec.Volume
226-
volumePath := specPath.Child("volume")
195+
volume := &pv.Spec
227196
if volume.VolumeName == "" {
228-
errorList = append(errorList, field.Required(volumePath.Child("volumeName"), "volumeName must be set"))
197+
errorList = append(errorList, field.Required(specPath.Child("volumeName"), "volumeName must be set"))
229198
} else if !validVolumeNameRegexp.MatchString(volume.VolumeName) {
230-
errorList = append(errorList, field.Invalid(volumePath.Child("volumeName"), volume.VolumeName, fmt.Sprintf("volumeName must match regex '%s'", validVolumeName)))
199+
errorList = append(errorList, field.Invalid(specPath.Child("volumeName"), volume.VolumeName, fmt.Sprintf("volumeName must match regex '%s'", validVolumeName)))
231200
}
232201

233-
errorList = append(errorList, validateLabels(volume.Labels, volumePath.Child("labels"))...)
202+
errorList = append(errorList, validateLabels(volume.Labels, specPath.Child("labels"))...)
234203
return errorList
235204
}
236205

‎api/v2/physical_container_volume_types_test.go‎

Lines changed: 84 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package v2
77

88
import (
99
"context"
10+
"encoding/json"
1011
"testing"
1112

1213
"github.com/stretchr/testify/require"
@@ -15,129 +16,33 @@ import (
1516
"github.com/microsoft/dcp/pkg/commonapi"
1617
)
1718

19+
// Verifies that PhysicalContainerVolume.Validate requires a valid runtime volume name and labels.
1820
func TestPhysicalContainerVolumeValidate(t *testing.T) {
19-
testCases := []struct {
21+
t.Parallel()
22+
for _, testCase := range []struct {
2023
name string
21-
volume PhysicalContainerVolume
24+
spec PhysicalContainerVolumeSpec
2225
expectedError string
2326
}{
24-
{
25-
name: "valid created volume",
26-
volume: PhysicalContainerVolume{
27-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
28-
Spec: PhysicalContainerVolumeSpec{
29-
Volume: &PhysicalContainerVolumeConfig{
30-
VolumeName: "test-runtime-volume",
31-
Labels: []commonapi.Label{{Key: "test-label", Value: "test-value"}},
32-
},
33-
},
34-
},
35-
},
36-
{
37-
name: "valid tracked volume",
38-
volume: PhysicalContainerVolume{
39-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
40-
Spec: PhysicalContainerVolumeSpec{VolumeID: "test-runtime-volume"},
41-
},
42-
},
43-
{
44-
name: "valid replacement volume",
45-
volume: PhysicalContainerVolume{
46-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
47-
Spec: PhysicalContainerVolumeSpec{
48-
Volume: &PhysicalContainerVolumeConfig{
49-
VolumeName: "test-runtime-volume",
50-
ReplaceExisting: true,
51-
},
52-
},
53-
},
54-
},
55-
{
56-
name: "missing namespace",
57-
volume: PhysicalContainerVolume{
58-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume"},
59-
Spec: PhysicalContainerVolumeSpec{
60-
Volume: &PhysicalContainerVolumeConfig{VolumeName: "test-runtime-volume"},
61-
},
62-
},
63-
expectedError: "metadata.namespace",
64-
},
65-
{
66-
name: "missing volume source",
67-
volume: PhysicalContainerVolume{
68-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
69-
},
70-
expectedError: "spec",
71-
},
72-
{
73-
name: "volume ID and config",
74-
volume: PhysicalContainerVolume{
75-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
76-
Spec: PhysicalContainerVolumeSpec{
77-
VolumeID: "test-runtime-volume",
78-
Volume: &PhysicalContainerVolumeConfig{VolumeName: "another-runtime-volume"},
79-
},
80-
},
81-
expectedError: "spec.volume",
82-
},
83-
{
84-
name: "missing volume name",
85-
volume: PhysicalContainerVolume{
86-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
87-
Spec: PhysicalContainerVolumeSpec{
88-
Volume: &PhysicalContainerVolumeConfig{},
89-
},
90-
},
91-
expectedError: "spec.volume.volumeName",
92-
},
93-
{
94-
name: "whitespace-only volume name",
95-
volume: PhysicalContainerVolume{
96-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
97-
Spec: PhysicalContainerVolumeSpec{
98-
Volume: &PhysicalContainerVolumeConfig{VolumeName: " "},
99-
},
100-
},
101-
expectedError: "spec.volume.volumeName",
102-
},
103-
{
104-
name: "invalid volume name",
105-
volume: PhysicalContainerVolume{
106-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
107-
Spec: PhysicalContainerVolumeSpec{
108-
Volume: &PhysicalContainerVolumeConfig{VolumeName: "invalid volume"},
109-
},
110-
},
111-
expectedError: "volumeName must match regex",
112-
},
113-
{
114-
name: "single-character volume name",
115-
volume: PhysicalContainerVolume{
116-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
117-
Spec: PhysicalContainerVolumeSpec{
118-
Volume: &PhysicalContainerVolumeConfig{VolumeName: "v"},
119-
},
120-
},
121-
expectedError: "volumeName must match regex",
122-
},
123-
{
124-
name: "missing label key",
125-
volume: PhysicalContainerVolume{
126-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
127-
Spec: PhysicalContainerVolumeSpec{
128-
Volume: &PhysicalContainerVolumeConfig{
129-
VolumeName: "test-runtime-volume",
130-
Labels: []commonapi.Label{{Value: "test-value"}},
131-
},
132-
},
133-
},
134-
expectedError: "spec.volume.labels[0].key",
135-
},
136-
}
137-
138-
for _, testCase := range testCases {
27+
{name: "retained", spec: PhysicalContainerVolumeSpec{VolumeName: "test-runtime-volume"}},
28+
{name: "removable", spec: PhysicalContainerVolumeSpec{VolumeName: "test-runtime-volume", RemoveRuntimeVolumeOnDelete: true}},
29+
{name: "labelled", spec: PhysicalContainerVolumeSpec{
30+
VolumeName: "test-runtime-volume", Labels: []commonapi.Label{{Key: "test-label", Value: "test-value"}},
31+
}},
32+
{name: "missing name", expectedError: "spec.volumeName"},
33+
{name: "whitespace name", spec: PhysicalContainerVolumeSpec{VolumeName: " "}, expectedError: "spec.volumeName"},
34+
{name: "invalid name", spec: PhysicalContainerVolumeSpec{VolumeName: "invalid volume"}, expectedError: "spec.volumeName"},
35+
{name: "single character", spec: PhysicalContainerVolumeSpec{VolumeName: "v"}, expectedError: "spec.volumeName"},
36+
{name: "missing label key", spec: PhysicalContainerVolumeSpec{
37+
VolumeName: "test-runtime-volume", Labels: []commonapi.Label{{Value: "test-value"}},
38+
}, expectedError: "spec.labels[0].key"},
39+
} {
13940
t.Run(testCase.name, func(t *testing.T) {
140-
errorList := testCase.volume.Validate(context.Background())
41+
volume := &PhysicalContainerVolume{
42+
ObjectMeta: metav1.ObjectMeta{Name: "volume", Namespace: "test-namespace"},
43+
Spec: testCase.spec,
44+
}
45+
errorList := volume.Validate(context.Background())
14146
if testCase.expectedError == "" {
14247
require.Empty(t, errorList)
14348
} else {
@@ -146,37 +51,78 @@ func TestPhysicalContainerVolumeValidate(t *testing.T) {
14651
}
14752
})
14853
}
54+
volume := &PhysicalContainerVolume{
55+
ObjectMeta: metav1.ObjectMeta{Name: "volume"},
56+
Spec: PhysicalContainerVolumeSpec{VolumeName: "runtime-volume"},
57+
}
58+
require.Contains(t, volume.Validate(context.Background()).ToAggregate().Error(), "metadata.namespace")
14959
}
15060

61+
// Verifies that PhysicalContainerVolume.ValidateUpdate keeps every top-level spec field immutable.
15162
func TestPhysicalContainerVolumeValidateUpdateRejectsSpecChanges(t *testing.T) {
152-
oldVolume := &PhysicalContainerVolume{
153-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
154-
Spec: PhysicalContainerVolumeSpec{
155-
Volume: &PhysicalContainerVolumeConfig{VolumeName: "test-runtime-volume"},
156-
},
63+
t.Parallel()
64+
original := &PhysicalContainerVolume{
65+
ObjectMeta: metav1.ObjectMeta{Name: "volume", Namespace: "test-namespace"},
66+
Spec: PhysicalContainerVolumeSpec{VolumeName: "runtime-volume"},
67+
}
68+
for _, testCase := range []struct {
69+
name string
70+
change func(*PhysicalContainerVolume)
71+
}{
72+
{name: "name", change: func(volume *PhysicalContainerVolume) { volume.Spec.VolumeName = "another-volume" }},
73+
{name: "cleanup", change: func(volume *PhysicalContainerVolume) { volume.Spec.RemoveRuntimeVolumeOnDelete = true }},
74+
{name: "labels", change: func(volume *PhysicalContainerVolume) {
75+
volume.Spec.Labels = []commonapi.Label{{Key: "key", Value: "value"}}
76+
}},
77+
} {
78+
t.Run(testCase.name, func(t *testing.T) {
79+
updated := original.DeepCopy()
80+
testCase.change(updated)
81+
updateErrors := updated.ValidateUpdate(context.Background(), original)
82+
require.Len(t, updateErrors, 1)
83+
require.Equal(t, "spec", updateErrors[0].Field)
84+
})
15785
}
158-
newVolume := oldVolume.DeepCopy()
159-
newVolume.Spec.Volume.RemoveRuntimeVolumeOnDelete = true
160-
161-
errorList := newVolume.ValidateUpdate(context.Background(), oldVolume)
162-
163-
require.NotEmpty(t, errorList)
164-
require.Contains(t, errorList.ToAggregate().Error(), "spec")
16586
}
16687

88+
// Verifies that PhysicalContainerVolume.ValidateUpdate permits status updates during shutdown.
16789
func TestPhysicalContainerVolumeValidateUpdateAllowsStatusUpdateDuringShutdown(t *testing.T) {
16890
commonapi.ResourceCreationProhibited.Store(true)
16991
defer commonapi.ResourceCreationProhibited.Store(false)
92+
original := &PhysicalContainerVolume{
93+
ObjectMeta: metav1.ObjectMeta{Name: "volume", Namespace: "test-namespace"},
94+
Spec: PhysicalContainerVolumeSpec{VolumeName: "runtime-volume"},
95+
}
96+
updated := original.DeepCopy()
97+
updated.Status.Phase = PhysicalContainerVolumePhaseReady
98+
require.Error(t, updated.Validate(context.Background()).ToAggregate())
99+
require.Empty(t, updated.ValidateUpdate(context.Background(), original))
100+
}
170101

171-
oldVolume := &PhysicalContainerVolume{
172-
ObjectMeta: metav1.ObjectMeta{Name: "test-volume", Namespace: "test-namespace"},
102+
// Verifies that PhysicalContainerVolume serializes a flat spec, retains observed volumeID in status,
103+
// and deep copies isolate the configured labels.
104+
func TestPhysicalContainerVolumeJSONAndDeepCopy(t *testing.T) {
105+
t.Parallel()
106+
volume := &PhysicalContainerVolume{
173107
Spec: PhysicalContainerVolumeSpec{
174-
Volume: &PhysicalContainerVolumeConfig{VolumeName: "test-runtime-volume"},
108+
VolumeName: "runtime-volume", RemoveRuntimeVolumeOnDelete: true,
109+
Labels: []commonapi.Label{{Key: "key", Value: "value"}},
175110
},
111+
Status: PhysicalContainerVolumeStatus{VolumeID: "runtime-volume"},
176112
}
177-
newVolume := oldVolume.DeepCopy()
178-
newVolume.Status.Phase = PhysicalContainerVolumePhaseReady
179-
180-
require.Error(t, newVolume.Validate(context.Background()).ToAggregate())
181-
require.Empty(t, newVolume.ValidateUpdate(context.Background(), oldVolume))
113+
encoded, encodeErr := json.Marshal(volume)
114+
require.NoError(t, encodeErr)
115+
var document struct {
116+
Spec map[string]json.RawMessage `json:"spec"`
117+
Status map[string]json.RawMessage `json:"status"`
118+
}
119+
require.NoError(t, json.Unmarshal(encoded, &document))
120+
require.Len(t, document.Spec, 3)
121+
require.JSONEq(t, `"runtime-volume"`, string(document.Spec["volumeName"]))
122+
require.JSONEq(t, `true`, string(document.Spec["removeRuntimeVolumeOnDelete"]))
123+
require.JSONEq(t, `[{"key":"key","value":"value"}]`, string(document.Spec["labels"]))
124+
require.JSONEq(t, `"runtime-volume"`, string(document.Status["volumeID"]))
125+
cloned := volume.DeepCopy()
126+
cloned.Spec.Labels[0].Value = "changed"
127+
require.Equal(t, "value", volume.Spec.Labels[0].Value)
182128
}

‎api/v2/zz_generated.deepcopy.go‎

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

‎api/v2/zz_generated.model_name.go‎

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

0 commit comments

Comments
 (0)