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

Add k8s plugin's toolregistry implementation #5243

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2024 The PipeCD 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 toolregistry installs and manages the needed tools
// such as kubectl, helm... for executing tasks in pipeline.
package toolregistry

import (
"context"

"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/toolregistry"
)

// Registry provides functions to get path to the needed tools.
type Registry interface {
Kubectl(ctx context.Context, version string) (string, error)
Kustomize(ctx context.Context, version string) (string, error)
Helm(ctx context.Context, version string) (string, error)
}

func NewRegistry(client toolregistry.ToolRegistry) Registry {
return &registry{
client: client,
}

Check warning on line 35 in pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go#L32-L35

Added lines #L32 - L35 were not covered by tests
}

type registry struct {
client toolregistry.ToolRegistry
}

func (r *registry) Kubectl(ctx context.Context, version string) (string, error) {
return r.client.InstallTool(ctx, "kubectl", version, kubectlInstallScript)

Check warning on line 43 in pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go#L42-L43

Added lines #L42 - L43 were not covered by tests
}

func (r *registry) Kustomize(ctx context.Context, version string) (string, error) {
return r.client.InstallTool(ctx, "kustomize", version, kustomizeInstallScript)

Check warning on line 47 in pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

func (r *registry) Helm(ctx context.Context, version string) (string, error) {
return r.client.InstallTool(ctx, "helm", version, helmInstallScript)

Check warning on line 51 in pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}
33 changes: 33 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/toolregistry/scripts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 The PipeCD 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 toolregistry

const kubectlInstallScript = `
cd {{ .TmpDir }}
curl -LO https://storage.googleapis.com/kubernetes-release/release/v{{ .Version }}/bin/{{ .Os }}/{{ .Arch }}/kubectl
mv kubectl {{ .OutPath }}
Comment on lines +18 to +20
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These template variables come from the below comments.

// - {{ .Name }}: name of the tool.
// - {{ .Version }}: version of the tool.
// - {{ .OutPath }}: file path where the tool will be installed.
// - {{ .TmpDir }}: directory where the tool will be downloaded and extracted.
// - {{ .Arch }}: GOARCH of the current machine.
// - {{ .Os }}: GOOS of the current machine.

`

const kustomizeInstallScript = `
cd {{ .TmpDir }}
curl -L https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v{{ .Version }}/kustomize_v{{ .Version }}_{{ .Os }}_{{ .Arch }}.tar.gz | tar xvz
mv kustomize {{ .OutPath }}
`

const helmInstallScript = `
cd {{ .TmpDir }}
curl -L https://get.helm.sh/helm-v{{ .Version }}-{{ .Os }}-{{ .Arch }}.tar.gz | tar xvz
mv {{ .Os }}-{{ .Arch }}/helm {{ .OutPath }}
`
7 changes: 4 additions & 3 deletions pkg/app/pipedv1/plugin/toolregistry/toolregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
client service.PluginServiceClient
}

func (r *ToolRegistry) InstallTool(ctx context.Context, name, version string) (path string, err error) {
func (r *ToolRegistry) InstallTool(ctx context.Context, name, version, script string) (path string, err error) {

Check warning on line 27 in pkg/app/pipedv1/plugin/toolregistry/toolregistry.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistry.go#L27

Added line #L27 was not covered by tests
res, err := r.client.InstallTool(ctx, &service.InstallToolRequest{
Name: name,
Version: version,
Name: name,
Version: version,
InstallScript: script,

Check warning on line 31 in pkg/app/pipedv1/plugin/toolregistry/toolregistry.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistry.go#L29-L31

Added lines #L29 - L31 were not covered by tests
})

if err != nil {
Expand Down
Loading