Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add plugin client params #237

Merged
merged 3 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions logging/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func Filter(logger *zap.SugaredLogger) func(req *restful.Request, resp *restful.
req.Request = req.Request.WithContext(logging.WithLogger(req.Request.Context(), log))
chain.ProcessFilter(req, resp)
if notHealthz(req.Request.URL.Path) {
if resp != nil {
log = log.With("status", resp.StatusCode)
l-qing marked this conversation as resolved.
Show resolved Hide resolved
}
log.Debugw("<== returned a response")
}
}
Expand Down
13 changes: 13 additions & 0 deletions plugin/client/client_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package client

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Client Suite")
}
93 changes: 93 additions & 0 deletions plugin/client/git_plugin_client_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2022 The Katanomi 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 client

import (
"context"
"fmt"
"net/url"
"strings"

corev1 "k8s.io/api/core/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/logging"

metav1alpha1 "github.com/katanomi/pkg/apis/meta/v1alpha1"
kclient "github.com/katanomi/pkg/client"
ksecret "github.com/katanomi/pkg/secret"
)

// GenerateGitPluginClientParams generate git plugin client params
func GenerateGitPluginClientParams(ctx context.Context, secretRef *corev1.ObjectReference,
gitRepoURL, integrationClassName string, classAddress *duckv1.Addressable) (
params *PluginClientParams, err error) {
log := logging.FromContext(ctx)
var secret *corev1.Secret
if secretRef != nil {
l-qing marked this conversation as resolved.
Show resolved Hide resolved
clt := kclient.Client(ctx)
if clt == nil {
err = fmt.Errorf("cannot get client from ctx")
return
}
secret, err = ksecret.GetSecretByRefOrLabel(ctx, clt, secretRef)
if err != nil {
err = fmt.Errorf("get secret for version steam failed: %w", err)
return
}
}

gitAddress, gitRepo, err := GetGitRepoInfo(gitRepoURL)
if err != nil {
err = fmt.Errorf("get git repo info failed: %w", err)
return
}

meta := Meta{
BaseURL: gitAddress,
}

params = NewPluginClientParams().
WithMeta(meta).
WithGitRepo(gitRepo).
WithClassAddress(classAddress).
WithSecret(secret).
WithIntegrationClassName(integrationClassName)

log.Debugw("generate git plugin client params", "BaseURL", gitAddress, "GitRepo", gitRepo,
"ClassAddress", classAddress, "IntegrationClassName", integrationClassName)
return params, nil
}

// GetGitRepoInfo try to get the project and repository from the git address.
func GetGitRepoInfo(gitAddress string) (host string, gitRepo metav1alpha1.GitRepo, err error) {
gitAddress = strings.TrimSuffix(gitAddress, ".git")
URL, err := url.Parse(gitAddress)
if err != nil {
return
}

projectRepo := strings.Split(strings.Trim(URL.Path, "/"), "/")
if len(projectRepo) < 2 {
err = fmt.Errorf("invaild git address %s which should have project and repository", gitAddress)
return
}

host = fmt.Sprintf("%s://%s", URL.Scheme, URL.Host)
gitRepo.Project = strings.Join(projectRepo[:len(projectRepo)-1], "/")
gitRepo.Repository = projectRepo[len(projectRepo)-1]
return
}
170 changes: 170 additions & 0 deletions plugin/client/git_plugin_client_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
Copyright 2022 The Katanomi 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 client

import (
"context"
"reflect"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"

metav1alpha1 "github.com/katanomi/pkg/apis/meta/v1alpha1"
)

var _ = Describe("Test.GenerateGitPluginClientParams", func() {
var (
ctx context.Context
secretRef *corev1.ObjectReference
gitRepoURL string
integrationClassName string
classAddress *duckv1.Addressable
params *PluginClientParams
err error
)
BeforeEach(func() {
ctx = context.TODO()
secretRef = nil
gitRepoURL = "https://github.com/katanomi/pkg"
integrationClassName = "github"
classAddress = &duckv1.Addressable{}
classAddress.URL, _ = apis.ParseURL(gitRepoURL)
})

JustBeforeEach(func() {
params, err = GenerateGitPluginClientParams(ctx, secretRef, gitRepoURL, integrationClassName, classAddress)
})

Context("ctx without client and secretRef is not nil", func() {
BeforeEach(func() {
secretRef = &corev1.ObjectReference{
Name: "name",
}
})
It("should return error", func() {
Expect(err).NotTo(BeNil())
})
})

Context("invalid git repository url", func() {
BeforeEach(func() {
gitRepoURL = "http:// github.com"
})
It("should return error", func() {
Expect(err).NotTo(BeNil())
})
})

Context("valid paramters", func() {
It("should generate success", func() {
Expect(err).To(BeNil())
Expect(params).ToNot(BeNil())
Expect(params.Meta.BaseURL).To(Equal("https://github.com"))
Expect(params.GitRepo).To(Equal(metav1alpha1.GitRepo{
Project: "katanomi",
Repository: "pkg",
}))
Expect(params.ClassAddress).To(Equal(classAddress))
Expect(params.Secret).To(BeNil())
Expect(params.IntegrationClassName).To(Equal(integrationClassName))
})
})
})

func TestGetGitRepoInfo(t *testing.T) {
tests := []struct {
name string
gitAddress string
wantHost string
wantGitRepo metav1alpha1.GitRepo
wantErr bool
}{
{
name: "invalid git repo url",
gitAddress: "http:// github.com/katanomi/pkg.git",
wantHost: "",
wantGitRepo: metav1alpha1.GitRepo{},
wantErr: true,
},
{
name: "shuffix with .git",
gitAddress: "http://github.com/katanomi/pkg.git",
wantHost: "http://github.com",
wantGitRepo: metav1alpha1.GitRepo{
Project: "katanomi",
Repository: "pkg",
},
wantErr: false,
},
{
name: "shuffix without .git",
gitAddress: "http://github.com/katanomi/pkg",
wantHost: "http://github.com",
wantGitRepo: metav1alpha1.GitRepo{
Project: "katanomi",
Repository: "pkg",
},
wantErr: false,
},
{
name: "shuffix without /",
gitAddress: "http://github.com/katanomi/pkg/",
wantHost: "http://github.com",
wantGitRepo: metav1alpha1.GitRepo{
Project: "katanomi",
Repository: "pkg",
},
wantErr: false,
},
{
name: "path too short",
gitAddress: "http://github.com/katanomi/",
wantHost: "",
wantGitRepo: metav1alpha1.GitRepo{},
wantErr: true,
},
{
name: "path with subgroup",
gitAddress: "http://github.com/katanomi/subgroup/pkg/",
wantHost: "http://github.com",
wantGitRepo: metav1alpha1.GitRepo{
Project: "katanomi/subgroup",
Repository: "pkg",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHost, gotGitRepo, err := GetGitRepoInfo(tt.gitAddress)
if (err != nil) != tt.wantErr {
t.Errorf("GetGitRepoInfo() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotHost != tt.wantHost {
t.Errorf("GetGitRepoInfo() gotHost = %v, want %v", gotHost, tt.wantHost)
}
if !reflect.DeepEqual(gotGitRepo, tt.wantGitRepo) {
t.Errorf("GetGitRepoInfo() gotGitRepo = %v, want %v", gotGitRepo, tt.wantGitRepo)
}
})
}
}
73 changes: 73 additions & 0 deletions plugin/client/plugin_client_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2022 The Katanomi 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 client

import (
corev1 "k8s.io/api/core/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"

metav1alpha1 "github.com/katanomi/pkg/apis/meta/v1alpha1"
)

// PluginClientParams used to store the parameters of the plugin client call.
// Avoid repeated generation some parameters
type PluginClientParams struct {
// Meta Plugin meta with base url and version info, for calling plugin api
Meta Meta

// ClassAddress is the address of the integration class
ClassAddress *duckv1.Addressable
l-qing marked this conversation as resolved.
Show resolved Hide resolved

// Secret is the secret to use for the plugin client
Secret *corev1.Secret

// IntegrationClassName is the name of the integration class
IntegrationClassName string

// GitRepo Repo base info, such as project, repository
GitRepo metav1alpha1.GitRepo
l-qing marked this conversation as resolved.
Show resolved Hide resolved
}

// NewPluginClientParams generate plugin client params
func NewPluginClientParams() *PluginClientParams {
return &PluginClientParams{}
}

func (p *PluginClientParams) WithMeta(meta Meta) *PluginClientParams {
p.Meta = meta
return p
}

func (p *PluginClientParams) WithClassAddress(classAddress *duckv1.Addressable) *PluginClientParams {
p.ClassAddress = classAddress
return p
}

func (p *PluginClientParams) WithSecret(secret *corev1.Secret) *PluginClientParams {
p.Secret = secret
return p
}

func (p *PluginClientParams) WithIntegrationClassName(integrationClassName string) *PluginClientParams {
p.IntegrationClassName = integrationClassName
return p
}

func (p *PluginClientParams) WithGitRepo(gitRepo metav1alpha1.GitRepo) *PluginClientParams {
p.GitRepo = gitRepo
return p
}