-
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.
Task auto-conversion from v1alpha1 to v1alpha2 🎋
This adds auto-conversion methods and tests for Task types in v1alpha1 and v1alpha2. In order to reduce the number of duplicated fields, we are using v1alpha2.TaskSpec embedded in v1alpha1.TaskSpec so that both types are compatible. Signed-off-by: Vincent Demeester <[email protected]>
- Loading branch information
1 parent
d782021
commit 15a7bd5
Showing
26 changed files
with
1,369 additions
and
563 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2020 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 v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"knative.dev/pkg/apis" | ||
) | ||
|
||
const ( | ||
// ConditionTypeConvertible is a Warning condition that is set on | ||
// resources when they cannot be converted to warn of a forthcoming | ||
// breakage. | ||
ConditionTypeConvertible apis.ConditionType = "Convertible" | ||
) | ||
|
||
// CannotConvertError is returned when a field cannot be converted. | ||
type CannotConvertError struct { | ||
Message string | ||
Field string | ||
} | ||
|
||
var _ error = (*CannotConvertError)(nil) | ||
|
||
// Error implements error | ||
func (cce *CannotConvertError) Error() string { | ||
return cce.Message | ||
} | ||
|
||
// ConvertErrorf creates a CannotConvertError from the field name and format string. | ||
func ConvertErrorf(field, msg string, args ...interface{}) error { | ||
return &CannotConvertError{ | ||
Message: fmt.Sprintf(msg, args...), | ||
Field: field, | ||
} | ||
} |
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,49 @@ | ||
/* | ||
Copyright 2020 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 v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
var _ apis.Convertible = (*Pipeline)(nil) | ||
|
||
// ConvertUp implements api.Convertible | ||
func (source *Pipeline) ConvertUp(ctx context.Context, obj apis.Convertible) error { | ||
switch sink := obj.(type) { | ||
case *v1alpha2.Pipeline: | ||
sink.ObjectMeta = source.ObjectMeta | ||
return nil // source.Spec.ConvertUp(ctx, &sink.Spec) | ||
default: | ||
return fmt.Errorf("unknown version, got: %T", sink) | ||
} | ||
} | ||
|
||
// ConvertDown implements api.Convertible | ||
func (sink *Pipeline) ConvertDown(ctx context.Context, obj apis.Convertible) error { | ||
switch source := obj.(type) { | ||
case *v1alpha2.Pipeline: | ||
sink.ObjectMeta = source.ObjectMeta | ||
return nil // sink.Spec.ConvertDown(ctx, &source.Spec) | ||
default: | ||
return fmt.Errorf("unknown version, got: %T", sink) | ||
} | ||
} |
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,123 @@ | ||
/* | ||
Copyright 2020 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 v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
var _ apis.Convertible = (*Task)(nil) | ||
|
||
// ConvertUp implements api.Convertible | ||
func (source *Task) ConvertUp(ctx context.Context, obj apis.Convertible) error { | ||
switch sink := obj.(type) { | ||
case *v1alpha2.Task: | ||
sink.ObjectMeta = source.ObjectMeta | ||
return source.Spec.ConvertUp(ctx, &sink.Spec) | ||
default: | ||
return fmt.Errorf("unknown version, got: %T", sink) | ||
} | ||
} | ||
|
||
func (source *TaskSpec) ConvertUp(ctx context.Context, sink *v1alpha2.TaskSpec) error { | ||
sink.Steps = source.Steps | ||
sink.Volumes = source.Volumes | ||
sink.StepTemplate = source.StepTemplate | ||
sink.Sidecars = source.Sidecars | ||
sink.Workspaces = source.Workspaces | ||
sink.Results = source.Results | ||
sink.Resources = source.Resources | ||
sink.Params = source.Params | ||
if source.Inputs != nil { | ||
if len(source.Inputs.Params) > 0 && len(source.Params) > 0 { | ||
// This shouldn't happen as it shouldn't pass validation | ||
return apis.ErrMultipleOneOf("inputs.params", "params") | ||
} | ||
if len(source.Inputs.Params) > 0 { | ||
sink.Params = make([]v1alpha2.ParamSpec, len(source.Inputs.Params)) | ||
for i, param := range source.Inputs.Params { | ||
sink.Params[i] = *param.DeepCopy() | ||
} | ||
} | ||
if len(source.Inputs.Resources) > 0 { | ||
if sink.Resources == nil { | ||
sink.Resources = &v1alpha2.TaskResources{} | ||
} | ||
if len(source.Inputs.Resources) > 0 && source.Resources != nil && len(source.Resources.Inputs) > 0 { | ||
// This shouldn't happen as it shouldn't pass validation but just in case | ||
return apis.ErrMultipleOneOf("inputs.resources", "resources.inputs") | ||
} | ||
sink.Resources.Inputs = make([]v1alpha2.TaskResource, len(source.Inputs.Resources)) | ||
for i, resource := range source.Inputs.Resources { | ||
sink.Resources.Inputs[i] = v1alpha2.TaskResource{ResourceDeclaration: v1alpha2.ResourceDeclaration{ | ||
Name: resource.Name, | ||
Type: resource.Type, | ||
Description: resource.Description, | ||
TargetPath: resource.TargetPath, | ||
Optional: resource.Optional, | ||
}} | ||
} | ||
} | ||
} | ||
if source.Outputs != nil && len(source.Outputs.Resources) > 0 { | ||
if sink.Resources == nil { | ||
sink.Resources = &v1alpha2.TaskResources{} | ||
} | ||
if len(source.Outputs.Resources) > 0 && source.Resources != nil && len(source.Resources.Outputs) > 0 { | ||
// This shouldn't happen as it shouldn't pass validation but just in case | ||
return apis.ErrMultipleOneOf("outputs.resources", "resources.outputs") | ||
} | ||
sink.Resources.Outputs = make([]v1alpha2.TaskResource, len(source.Outputs.Resources)) | ||
for i, resource := range source.Outputs.Resources { | ||
sink.Resources.Outputs[i] = v1alpha2.TaskResource{ResourceDeclaration: v1alpha2.ResourceDeclaration{ | ||
Name: resource.Name, | ||
Type: resource.Type, | ||
Description: resource.Description, | ||
TargetPath: resource.TargetPath, | ||
Optional: resource.Optional, | ||
}} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// ConvertDown implements api.Convertible | ||
func (sink *Task) ConvertDown(ctx context.Context, obj apis.Convertible) error { | ||
switch source := obj.(type) { | ||
case *v1alpha2.Task: | ||
sink.ObjectMeta = source.ObjectMeta | ||
return sink.Spec.ConvertDown(ctx, &source.Spec) | ||
default: | ||
return fmt.Errorf("unknown version, got: %T", sink) | ||
} | ||
} | ||
|
||
func (sink *TaskSpec) ConvertDown(ctx context.Context, source *v1alpha2.TaskSpec) error { | ||
sink.Steps = source.Steps | ||
sink.Volumes = source.Volumes | ||
sink.StepTemplate = source.StepTemplate | ||
sink.Sidecars = source.Sidecars | ||
sink.Workspaces = source.Workspaces | ||
sink.Results = source.Results | ||
sink.Params = source.Params | ||
sink.Resources = source.Resources | ||
return nil | ||
} |
Oops, something went wrong.