Skip to content

Commit

Permalink
Merge pull request #1851 from deads2k/apply-16-add-generate-name-for-…
Browse files Browse the repository at this point in the history
…mutation

API-1835: add generateName for mutation: came up on CSRs
  • Loading branch information
openshift-merge-bot[bot] authored Oct 23, 2024
2 parents abb8c75 + 698f089 commit e59f1b6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 15 deletions.
9 changes: 5 additions & 4 deletions pkg/manifestclient/mutation_directory_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ func serializedRequestFromFile(action Action, actionFS fs.FS, bodyFilename strin
Version: versionName,
Resource: resourceName,
},
KindType: retObj.(*unstructured.Unstructured).GroupVersionKind(),
Namespace: retObj.(*unstructured.Unstructured).GetNamespace(),
Name: metadataName,
Body: bodyContent,
KindType: retObj.(*unstructured.Unstructured).GroupVersionKind(),
Namespace: retObj.(*unstructured.Unstructured).GetNamespace(),
Name: metadataName,
GenerateName: retObj.(*unstructured.Unstructured).GetGenerateName(),
Body: bodyContent,
},
}
if optionsExist {
Expand Down
9 changes: 5 additions & 4 deletions pkg/manifestclient/mutation_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ type AllActionsTracker[T SerializedRequestish] struct {
}

type ActionMetadata struct {
Action Action
GVR schema.GroupVersionResource
Namespace string
Name string
Action Action
GVR schema.GroupVersionResource
Namespace string
Name string
GenerateName string
}

type actionTracker[T SerializedRequestish] struct {
Expand Down
20 changes: 13 additions & 7 deletions pkg/manifestclient/serialized_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type SerializedRequest struct {
KindType schema.GroupVersionKind
Namespace string
Name string
GenerateName string

Options []byte
Body []byte
Expand Down Expand Up @@ -206,6 +207,9 @@ func CompareSerializedRequest(lhs, rhs *SerializedRequest) int {
if cmp := strings.Compare(lhs.Name, rhs.Name); cmp != 0 {
return cmp
}
if cmp := strings.Compare(lhs.GenerateName, rhs.GenerateName); cmp != 0 {
return cmp
}

if cmp := bytes.Compare(lhs.Body, rhs.Body); cmp != 0 {
return cmp
Expand Down Expand Up @@ -262,7 +266,7 @@ func suggestedFilenames(a SerializedRequest, uniqueNumber int) (string, string)
scopingString,
groupName,
a.ResourceType.Resource,
fmt.Sprintf("%03d-body-%s.yaml", uniqueNumber, a.Name),
fmt.Sprintf("%03d-body-%s%s.yaml", uniqueNumber, a.Name, a.GenerateName),
),
)
optionsFilename := ""
Expand All @@ -273,7 +277,7 @@ func suggestedFilenames(a SerializedRequest, uniqueNumber int) (string, string)
scopingString,
groupName,
a.ResourceType.Resource,
fmt.Sprintf("%03d-options-%s.yaml", uniqueNumber, a.Name),
fmt.Sprintf("%03d-options-%s%s.yaml", uniqueNumber, a.Name, a.GenerateName),
),
)
}
Expand Down Expand Up @@ -302,20 +306,22 @@ func (a SerializedRequest) DeepCopy() SerializedRequestish {
KindType: a.KindType,
Namespace: a.Namespace,
Name: a.Name,
GenerateName: a.GenerateName,
Options: bytes.Clone(a.Options),
Body: bytes.Clone(a.Body),
}
}

func (a SerializedRequest) StringID() string {
return fmt.Sprintf("%s-%s.%s.%s/%s[%s]", a.Action, a.KindType.Kind, a.KindType.Version, a.KindType.Group, a.Name, a.Namespace)
return fmt.Sprintf("%s-%s.%s.%s/%s%s[%s]", a.Action, a.KindType.Kind, a.KindType.Version, a.KindType.Group, a.Name, a.GenerateName, a.Namespace)
}

func (a SerializedRequest) GetLookupMetadata() ActionMetadata {
return ActionMetadata{
Action: a.Action,
GVR: a.ResourceType,
Namespace: a.Namespace,
Name: a.Name,
Action: a.Action,
GVR: a.ResourceType,
Namespace: a.Namespace,
Name: a.Name,
GenerateName: a.GenerateName,
}
}
82 changes: 82 additions & 0 deletions pkg/manifestclient/serialized_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,88 @@ func TestDifferenceOfSerializedRequests(t *testing.T) {
},
},
},
{
name: "diff in generateName",
args: args{
lhs: []FileOriginatedSerializedRequest{
{
BodyFilename: "foo.yaml",
OptionsFilename: "foo-options.yaml",
SerializedRequest: SerializedRequest{
Namespace: "foo-ns",
GenerateName: "bar-",
Options: nil,
Body: []byte("content"),
},
},
},
rhs: []TrackedSerializedRequest{
{
RequestNumber: 6,
SerializedRequest: SerializedRequest{
Namespace: "foo-ns",
Name: "bar",
Options: nil,
Body: []byte("content"),
},
},
},
},
want: []FileOriginatedSerializedRequest{
{
BodyFilename: "foo.yaml",
OptionsFilename: "foo-options.yaml",
SerializedRequest: SerializedRequest{
Namespace: "foo-ns",
GenerateName: "bar-",
Options: nil,
Body: []byte("content"),
},
},
},
},
{
name: "diff in generateName with same name",
args: args{
lhs: []FileOriginatedSerializedRequest{
{
BodyFilename: "foo.yaml",
OptionsFilename: "foo-options.yaml",
SerializedRequest: SerializedRequest{
Namespace: "foo-ns",
Name: "bar-2", // this happens on updates for instance
GenerateName: "bar-",
Options: nil,
Body: []byte("content"),
},
},
},
rhs: []TrackedSerializedRequest{
{
RequestNumber: 6,
SerializedRequest: SerializedRequest{
Namespace: "foo-ns",
Name: "bar-2",
Options: nil,
Body: []byte("content"),
},
},
},
},
want: []FileOriginatedSerializedRequest{
{
BodyFilename: "foo.yaml",
OptionsFilename: "foo-options.yaml",
SerializedRequest: SerializedRequest{
Namespace: "foo-ns",
Name: "bar-2", // this happens on updates for instance
GenerateName: "bar-",
Options: nil,
Body: []byte("content"),
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/manifestclient/write_roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (mrt *writeTrackingRoundTripper) roundTrip(req *http.Request) ([]byte, erro
KindType: bodyObj.GetObjectKind().GroupVersionKind(),
Namespace: requestInfo.Namespace,
Name: metadataName,
GenerateName: bodyObj.(*unstructured.Unstructured).GetGenerateName(),
Options: optionsBytes,
Body: bodyYAMLBytes,
}
Expand Down

0 comments on commit e59f1b6

Please sign in to comment.