-
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.
Define a new type of PipelineResource for cloud events. This resource can be used as an output only. The resource is only defined here, it's not functional yet. Signed-off-by: Andrea Frittoli <andrea.frittoli@uk.ibm.com>
- Loading branch information
Showing
4 changed files
with
246 additions
and
1 deletion.
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,105 @@ | ||
/* | ||
Copyright 2019 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" | ||
"strings" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// CloudEventResource is an event sink to which events are delivered when a TaskRun has finished | ||
type CloudEventResource struct { | ||
// Name is the name used to reference to the PipelineResource | ||
Name string `json:"name"` | ||
// Type must be `PipelineResourceTypeCloudEvent` | ||
Type PipelineResourceType `json:"type"` | ||
// TargetURI is the URI of the sink which the cloud event is develired to | ||
TargetURI string `json:"targetURI"` | ||
} | ||
|
||
// NewCloudEventResource creates a new CloudEvent resource to pass to a Task | ||
func NewCloudEventResource(r *PipelineResource) (*CloudEventResource, error) { | ||
if r.Spec.Type != PipelineResourceTypeCloudEvent { | ||
return nil, fmt.Errorf("CloudEventResource: Cannot create a Cloud Event resource from a %s Pipeline Resource", r.Spec.Type) | ||
} | ||
var targetURI string | ||
var targetURISpecified bool | ||
|
||
for _, param := range r.Spec.Params { | ||
switch { | ||
case strings.EqualFold(param.Name, "TargetURI"): | ||
targetURI = param.Value | ||
if param.Value != "" { | ||
targetURISpecified = true | ||
} | ||
} | ||
} | ||
|
||
if !targetURISpecified { | ||
return nil, fmt.Errorf("CloudEventResource: Need URI to be specified in order to create a CloudEvent resource %s", r.Name) | ||
} | ||
return &CloudEventResource{ | ||
Name: r.Name, | ||
Type: r.Spec.Type, | ||
TargetURI: targetURI, | ||
}, nil | ||
} | ||
|
||
// GetName returns the name of the resource | ||
func (s CloudEventResource) GetName() string { | ||
return s.Name | ||
} | ||
|
||
// GetType returns the type of the resource, in this case "storage" | ||
func (s CloudEventResource) GetType() PipelineResourceType { | ||
return PipelineResourceTypeCloudEvent | ||
} | ||
|
||
// Replacements is used for template replacement on an CloudEventResource inside of a Taskrun. | ||
func (s *CloudEventResource) Replacements() map[string]string { | ||
return map[string]string{ | ||
"name": s.Name, | ||
"type": string(s.Type), | ||
"target-uri": s.TargetURI, | ||
} | ||
} | ||
|
||
// GetUploadContainerSpec returns nothing as the cloud event is sent by the controller once the POD execution is completed | ||
func (s *CloudEventResource) GetUploadContainerSpec() ([]corev1.Container, error) { | ||
return nil, nil | ||
} | ||
|
||
// GetDownloadContainerSpec returns nothing, cloud events cannot be used as input resource | ||
func (s *CloudEventResource) GetDownloadContainerSpec() ([]corev1.Container, error) { | ||
return nil, nil | ||
} | ||
|
||
// SetDestinationDirectory required by the interface but not really used | ||
func (s *CloudEventResource) SetDestinationDirectory(path string) { | ||
} | ||
|
||
// GetUploadVolumeSpec - no upload from volume for CloudEvent resource | ||
func (s *CloudEventResource) GetUploadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) { | ||
return nil, nil | ||
} | ||
|
||
// GetDownloadVolumeSpec - no download from volume for CloudEvent resource | ||
func (s *CloudEventResource) GetDownloadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) { | ||
return nil, nil | ||
} |
119 changes: 119 additions & 0 deletions
119
pkg/apis/pipeline/v1alpha1/cloud_event_resource_test.go
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,119 @@ | ||
/* | ||
Copyright 2019 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_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
|
||
tb "github.com/tektoncd/pipeline/test/builder" | ||
) | ||
|
||
func Test_NewCloudEventResource_Invalid(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
pipelineResource *v1alpha1.PipelineResource | ||
}{{ | ||
name: "create resource with no parameter", | ||
pipelineResource: tb.PipelineResource("cloud-event-resource-no-uri", "default", tb.PipelineResourceSpec( | ||
v1alpha1.PipelineResourceTypeCloudEvent, | ||
)), | ||
}, { | ||
name: "create resource with invalid type", | ||
pipelineResource: tb.PipelineResource("git-resource", "default", tb.PipelineResourceSpec( | ||
v1alpha1.PipelineResourceTypeGit, | ||
tb.PipelineResourceSpecParam("URL", "git://fake/repo"), | ||
tb.PipelineResourceSpecParam("Revision", "fake_rev"), | ||
)), | ||
}} | ||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
_, err := v1alpha1.NewCloudEventResource(tc.pipelineResource) | ||
if err == nil { | ||
t.Error("Expected error creating CloudEvent resource") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_NewCloudEventResource_Valid(t *testing.T) { | ||
pr := tb.PipelineResource("cloud-event-resource-uri", "default", tb.PipelineResourceSpec( | ||
v1alpha1.PipelineResourceTypeCloudEvent, | ||
tb.PipelineResourceSpecParam("TargetURI", "http://fake-sink"), | ||
)) | ||
expectedCloudEventResource := &v1alpha1.CloudEventResource{ | ||
Name: "cloud-event-resource-uri", | ||
TargetURI: "http://fake-sink", | ||
Type: v1alpha1.PipelineResourceTypeCloudEvent, | ||
} | ||
|
||
r, err := v1alpha1.NewCloudEventResource(pr) | ||
if err != nil { | ||
t.Fatalf("Unexpected error creating CloudEvent resource: %s", err) | ||
} | ||
if d := cmp.Diff(expectedCloudEventResource, r); d != "" { | ||
t.Errorf("Mismatch of CloudEvent resource: %s", d) | ||
} | ||
} | ||
|
||
func Test_CloudEventGetReplacements(t *testing.T) { | ||
r := &v1alpha1.CloudEventResource{ | ||
Name: "cloud-event-resource", | ||
TargetURI: "http://fake-uri", | ||
Type: v1alpha1.PipelineResourceTypeCloudEvent, | ||
} | ||
expectedReplacementMap := map[string]string{ | ||
"name": "cloud-event-resource", | ||
"type": "cloudEvent", | ||
"target-uri": "http://fake-uri", | ||
} | ||
if d := cmp.Diff(r.Replacements(), expectedReplacementMap); d != "" { | ||
t.Errorf("CloudEvent Replacement map mismatch: %s", d) | ||
} | ||
} | ||
|
||
func Test_CloudEventDownloadContainerSpec(t *testing.T) { | ||
r := &v1alpha1.CloudEventResource{ | ||
Name: "cloud-event-resource", | ||
TargetURI: "http://fake-uri", | ||
Type: v1alpha1.PipelineResourceTypeCloudEvent, | ||
} | ||
d, e := r.GetDownloadContainerSpec(); | ||
if d != nil { | ||
t.Errorf("Did not expect a download container for CloudEventResource") | ||
} | ||
if e != nil { | ||
t.Errorf("Did not expect an error %s when getting a download container for CloudEventResource", e) | ||
} | ||
} | ||
|
||
func Test_CloudEventUploadContainerSpec(t *testing.T) { | ||
r := &v1alpha1.CloudEventResource{ | ||
Name: "cloud-event-resource", | ||
TargetURI: "http://fake-uri", | ||
Type: v1alpha1.PipelineResourceTypeCloudEvent, | ||
} | ||
d, e := r.GetUploadContainerSpec(); | ||
if d != nil { | ||
t.Errorf("Did not expect an upload container for CloudEventResource") | ||
} | ||
if e != nil { | ||
t.Errorf("Did not expect an error %s when getting an upload container for CloudEventResource", e) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.