Skip to content

Commit

Permalink
Add k8s plugin's toolregistry implementation (#5243)
Browse files Browse the repository at this point in the history
* Pass install script from arg

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Add k8s plugin's toolregistry

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

---------

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Oct 1, 2024
1 parent c16aebb commit 077b7b6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
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,
}
}

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)
}

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

func (r *registry) Helm(ctx context.Context, version string) (string, error) {
return r.client.InstallTool(ctx, "helm", version, helmInstallScript)
}
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 }}
`

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 @@ type ToolRegistry struct {
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) {
res, err := r.client.InstallTool(ctx, &service.InstallToolRequest{
Name: name,
Version: version,
Name: name,
Version: version,
InstallScript: script,
})

if err != nil {
Expand Down

0 comments on commit 077b7b6

Please sign in to comment.