Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Rearrange bundleresolver code, add example test for demo resolver
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>
  • Loading branch information
abayer committed May 25, 2022
1 parent 58315be commit 137e577
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 56 deletions.
57 changes: 1 addition & 56 deletions bundleresolver/cmd/bundleresolver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,17 @@ package main

import (
"context"
"time"

"github.com/google/go-containerregistry/pkg/authn/k8schain"
"github.com/tektoncd/resolution/bundleresolver/pkg/bundle"
"github.com/tektoncd/resolution/pkg/apis/resolution/v1alpha1"
"github.com/tektoncd/resolution/pkg/common"
"github.com/tektoncd/resolution/pkg/resolver/framework"
"k8s.io/client-go/kubernetes"
kubeclient "knative.dev/pkg/client/injection/kube/client"
filteredinformerfactory "knative.dev/pkg/client/injection/kube/informers/factory/filtered"
"knative.dev/pkg/injection/sharedmain"
)

// TODO(sbwsg): This should be exposed as a configurable option for
// admins (e.g. via ConfigMap)
const timeoutDuration = time.Minute

func main() {
ctx := filteredinformerfactory.WithSelectors(context.Background(), v1alpha1.ManagedByLabelKey)
sharedmain.MainWithContext(ctx, "controller",
framework.NewController(ctx, &resolver{}),
framework.NewController(ctx, &bundle.Resolver{}),
)
}

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 = kubeclient.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 := bundle.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 := bundle.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 bundle.GetEntry(ctx, kc, opts)
}
79 changes: 79 additions & 0 deletions bundleresolver/pkg/bundle/resolver.go
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)
}
67 changes: 67 additions & 0 deletions docs/resolver-template/cmd/demoresolver/main_test.go
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)
}

0 comments on commit 137e577

Please sign in to comment.