-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V1: Add conversion for Task.Resources
This commit adds support for Task.Resources when converting between v1beta1 and v1 versions of Tasks. This will allow us to release v1 Task in a backwards compatible way, by ensuring that v1beta1 Tasks with Resources will have the Resources serialized into annotations on the v1 Task on conversion.
- Loading branch information
1 parent
3562d6f
commit 73f4e0d
Showing
4 changed files
with
150 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2022 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// SerializeToMetadata serializes the input field and adds it as an annotation to | ||
// the metadata under the input key. | ||
func SerializeToMetadata(meta *metav1.ObjectMeta, field interface{}, key string) error { | ||
bytes, err := json.Marshal(field) | ||
if err != nil { | ||
return fmt.Errorf("error serializing field: %s", err) | ||
} | ||
if meta.Annotations == nil { | ||
meta.Annotations = make(map[string]string) | ||
} | ||
meta.Annotations[key] = string(bytes) | ||
return nil | ||
} | ||
|
||
// DeserializeFromMetadata takes the value of the input key from the metadata's annotations, | ||
// deserializes it into "to", and removes the key from the metadata's annotations. | ||
// Returns nil if the key is not present in the annotations. | ||
func DeserializeFromMetadata(meta *metav1.ObjectMeta, to interface{}, key string) error { | ||
if meta.Annotations == nil { | ||
return nil | ||
} | ||
if str, ok := meta.Annotations[key]; ok { | ||
if err := json.Unmarshal([]byte(str), to); err != nil { | ||
return fmt.Errorf("error deserializing key %s from metadata: %s", key, err) | ||
} | ||
delete(meta.Annotations, key) | ||
if len(meta.Annotations) == 0 { | ||
meta.Annotations = nil | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2022 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package version_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/tektoncd/pipeline/pkg/apis/version" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type testStruct struct { | ||
Field string | ||
} | ||
|
||
func TestSerializationRoundTrip(t *testing.T) { | ||
meta := metav1.ObjectMeta{} | ||
source := testStruct{Field: "foo"} | ||
key := "my-key" | ||
err := version.SerializeToMetadata(&meta, source, key) | ||
if err != nil { | ||
t.Fatalf("Serialization error: %s", err) | ||
} | ||
|
||
sink := testStruct{} | ||
err = version.DeserializeFromMetadata(&meta, &sink, key) | ||
if err != nil { | ||
t.Fatalf("Deserialization error: %s", err) | ||
} | ||
|
||
_, ok := meta.Annotations[key] | ||
if ok { | ||
t.Errorf("Expected key %s not to be present in annotations but it was", key) | ||
} | ||
|
||
if d := cmp.Diff(source, sink); d != "" { | ||
t.Errorf("Unexpected diff after serialization/deserialization round trip: %s", d) | ||
} | ||
} |