-
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.
Refactor to the remote image library and task references
This change is part of larger effort to support OCI image references in task and pipeline refs. This intial change is a refactor of existing logic for clarity and in preparation of adding the new API fields into the Tekton objects. For more details see the design: https://docs.google.com/document/d/1lXF_SvLwl6OqqGy8JbpSXRj4hWJ6CSImlxlIl4V9rnM/edit# Principle changes: - Refactor the remote library to operate more like an independent API. It allows the user to Get and List objects in a remote image. Decoupling the API of fetching and inspecting images allows this to be reused by external clients (like `tkn`). - Create a new taskref library resolver that knows how to resolve local references. This mirrors the remote resolver and now establishes an ability for the next PR to create a factory function that assigns the correct resolver based on the TaskRun. It also allows us to test this logic by putting it into a function. - Likewise, we have created a new pipelineref library for all the same reasons above. Signed-off-by: Sunil Thaha <sthaha@redhat.com>
- Loading branch information
1 parent
52a14b4
commit b435bd9
Showing
17 changed files
with
766 additions
and
304 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,41 @@ | ||
/* | ||
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 resources | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// LocalPipelineRefResolver uses the current cluster to resolve a pipeline reference. | ||
type LocalPipelineRefResolver struct { | ||
Namespace string | ||
Tektonclient clientset.Interface | ||
} | ||
|
||
// GetPipeline will resolve a Pipeline from the local cluster using a versioned Tekton client. It will | ||
// return an error if it can't find an appropriate Pipeline for any reason. | ||
func (l *LocalPipelineRefResolver) GetPipeline(name string) (v1alpha1.PipelineInterface, error) { | ||
// If we are going to resolve this reference locally, we need a namespace scope. | ||
if l.Namespace == "" { | ||
return nil, fmt.Errorf("Must specify namespace to resolve reference to pipeline %s", name) | ||
} | ||
return l.Tektonclient.TektonV1alpha1().Pipelines(l.Namespace).Get(name, metav1.GetOptions{}) | ||
} |
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,82 @@ | ||
/* | ||
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 resources_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned/fake" | ||
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources" | ||
tb "github.com/tektoncd/pipeline/test/builder" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func TestPipelineRef(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
pipelines []runtime.Object | ||
ref *v1alpha1.PipelineRef | ||
expected runtime.Object | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "local-pipeline", | ||
pipelines: []runtime.Object{ | ||
tb.Pipeline("simple", tb.PipelineNamespace("default")), | ||
tb.Pipeline("dummy", tb.PipelineNamespace("default")), | ||
}, | ||
ref: &v1alpha1.PipelineRef{ | ||
Name: "simple", | ||
}, | ||
expected: tb.Pipeline("simple", tb.PipelineNamespace("default")), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "pipeline-not-found", | ||
pipelines: []runtime.Object{}, | ||
ref: &v1alpha1.PipelineRef{ | ||
Name: "simple", | ||
}, | ||
expected: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tektonclient := fake.NewSimpleClientset(tc.pipelines...) | ||
|
||
lc := &resources.LocalPipelineRefResolver{ | ||
Namespace: "default", | ||
Tektonclient: tektonclient, | ||
} | ||
|
||
task, err := lc.GetPipeline(tc.ref.Name) | ||
if tc.wantErr && err == nil { | ||
t.Fatal("Expected error but found nil instead") | ||
} else if !tc.wantErr && err != nil { | ||
t.Fatalf("Received unexpected error ( %#v )", err) | ||
} | ||
|
||
if diff := cmp.Diff(task, tc.expected); tc.expected != nil && diff != "" { | ||
t.Error(diff) | ||
} | ||
}) | ||
} | ||
} |
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,50 @@ | ||
/* | ||
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 resources | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// LocalTaskRefResolver uses the current cluster to resolve a task reference. | ||
type LocalTaskRefResolver struct { | ||
Namespace string | ||
Kind v1alpha1.TaskKind | ||
Tektonclient clientset.Interface | ||
} | ||
|
||
// GetTask will resolve either a Task or ClusterTask from the local cluster using a versioned Tekton client. It will | ||
// return an error if it can't find an appropriate Task for any reason. | ||
func (l *LocalTaskRefResolver) GetTask(name string) (v1alpha1.TaskInterface, error) { | ||
if l.Kind == v1alpha1.ClusterTaskKind { | ||
task, err := l.Tektonclient.TektonV1alpha1().ClusterTasks().Get(name, metav1.GetOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return task, nil | ||
} | ||
|
||
// If we are going to resolve this reference locally, we need a namespace scope. | ||
if l.Namespace == "" { | ||
return nil, fmt.Errorf("Must specify namespace to resolve reference to task %s", name) | ||
} | ||
return l.Tektonclient.TektonV1alpha1().Tasks(l.Namespace).Get(name, metav1.GetOptions{}) | ||
} |
Oops, something went wrong.