This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rearrange bundleresolver code, add example test for demo resolver
Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>
- Loading branch information
Showing
3 changed files
with
147 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
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 bundle | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn/k8schain" | ||
"github.com/tektoncd/resolution/pkg/common" | ||
"github.com/tektoncd/resolution/pkg/resolver/framework" | ||
"k8s.io/client-go/kubernetes" | ||
"knative.dev/pkg/client/injection/kube/client" | ||
) | ||
|
||
// TODO(sbwsg): This should be exposed as a configurable option for | ||
// admins (e.g. via ConfigMap) | ||
const timeoutDuration = time.Minute | ||
|
||
// Resolver implements a framework.Resolver that can fetch files from OCI bundles. | ||
type Resolver struct { | ||
kubeClientSet kubernetes.Interface | ||
} | ||
|
||
// Initialize sets up any dependencies needed by the Resolver. None atm. | ||
func (r *Resolver) Initialize(ctx context.Context) error { | ||
r.kubeClientSet = client.Get(ctx) | ||
return nil | ||
} | ||
|
||
// GetName returns a string name to refer to this Resolver by. | ||
func (r *Resolver) GetName(context.Context) string { | ||
return "bundleresolver" | ||
} | ||
|
||
// GetSelector returns a map of labels to match requests to this Resolver. | ||
func (r *Resolver) GetSelector(context.Context) map[string]string { | ||
return map[string]string{ | ||
common.LabelKeyResolverType: "bundles", | ||
} | ||
} | ||
|
||
// ValidateParams ensures parameters from a request are as expected. | ||
func (r *Resolver) ValidateParams(ctx context.Context, params map[string]string) error { | ||
if _, err := OptionsFromParams(params); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Resolve uses the given params to resolve the requested file or resource. | ||
func (r *Resolver) Resolve(ctx context.Context, params map[string]string) (framework.ResolvedResource, error) { | ||
opts, err := OptionsFromParams(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
namespace := common.RequestNamespace(ctx) | ||
kc, err := k8schain.New(ctx, r.kubeClientSet, k8schain.Options{ | ||
Namespace: namespace, | ||
ServiceAccountName: opts.ServiceAccount, | ||
}) | ||
ctx, cancelFn := context.WithTimeout(ctx, timeoutDuration) | ||
defer cancelFn() | ||
return GetEntry(ctx, kc, opts) | ||
} |
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,67 @@ | ||
/* | ||
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 main | ||
|
||
import ( | ||
"encoding/base64" | ||
"testing" | ||
"time" | ||
|
||
"github.com/tektoncd/resolution/pkg/apis/resolution/v1alpha1" | ||
resolutioncommon "github.com/tektoncd/resolution/pkg/common" | ||
ttesting "github.com/tektoncd/resolution/pkg/reconciler/testing" | ||
frtesting "github.com/tektoncd/resolution/pkg/resolver/framework/testing" | ||
"github.com/tektoncd/resolution/test" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
_ "knative.dev/pkg/system/testing" | ||
) | ||
|
||
func TestResolver(t *testing.T) { | ||
ctx, _ := ttesting.SetupFakeContext(t) | ||
|
||
r := &resolver{} | ||
|
||
request := &v1alpha1.ResolutionRequest{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "resolution.tekton.dev/v1alpha1", | ||
Kind: "ResolutionRequest", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "rr", | ||
Namespace: "foo", | ||
CreationTimestamp: metav1.Time{Time: time.Now()}, | ||
Labels: map[string]string{ | ||
resolutioncommon.LabelKeyResolverType: "demo", | ||
}, | ||
}, | ||
Spec: v1alpha1.ResolutionRequestSpec{}, | ||
} | ||
d := test.Data{ | ||
ResolutionRequests: []*v1alpha1.ResolutionRequest{request}, | ||
} | ||
|
||
expectedStatus := &v1alpha1.ResolutionRequestStatus{ | ||
ResolutionRequestStatusFields: v1alpha1.ResolutionRequestStatusFields{ | ||
Data: base64.StdEncoding.Strict().EncodeToString([]byte(pipeline)), | ||
}, | ||
} | ||
|
||
// If you want to test scenarios where an error should occur, pass a non-nil error to RunResolverReconcileTest | ||
var expectedErr error | ||
|
||
frtesting.RunResolverReconcileTest(ctx, t, d, r, request, expectedStatus, expectedErr) | ||
} |