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

Implement preparing manifest applier in kubernetes plugin #5389

Merged
merged 2 commits into from
Dec 4, 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
41 changes: 38 additions & 3 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package deployment

import (
"cmp"
"context"
"encoding/json"
"errors"
"time"

Expand All @@ -33,10 +35,20 @@
"google.golang.org/grpc/status"
)

const (
defaultKubectlVersion = "1.18.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is copied from current implementation.

defaultKubectlVersion = "1.18.2"

)

type toolClient interface {
InstallTool(ctx context.Context, name, version, script string) (string, error)
}

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

type loader interface {
// LoadManifests renders and loads all manifests for application.
LoadManifests(ctx context.Context, input provider.LoaderInput) ([]provider.Manifest, error)
Expand All @@ -60,7 +72,12 @@
type DeploymentService struct {
deployment.UnimplementedDeploymentServiceServer

// this field is set with the plugin configuration
// the plugin configuration is sent from piped while initializing the plugin
pluginConfig *config.PipedPlugin

logger *zap.Logger
toolRegistry toolRegistry
loader loader
logPersister logPersister
}
Expand All @@ -71,9 +88,12 @@
toolClient toolClient,
logPersister logPersister,
) *DeploymentService {
toolRegistry := toolregistry.NewRegistry(toolClient)

Check warning on line 92 in pkg/app/pipedv1/plugin/kubernetes/deployment/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L91-L92

Added lines #L91 - L92 were not covered by tests
return &DeploymentService{
logger: logger.Named("planner"),
loader: provider.NewLoader(toolregistry.NewRegistry(toolClient)),
toolRegistry: toolRegistry,
loader: provider.NewLoader(toolRegistry),

Check warning on line 96 in pkg/app/pipedv1/plugin/kubernetes/deployment/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L95-L96

Added lines #L95 - L96 were not covered by tests
logPersister: logPersister,
}
}
Expand Down Expand Up @@ -244,8 +264,23 @@

// TODO: implement annotateConfigHash to ensure restart of workloads when config changes

// Get the applier for the target cluster.
var applier applier // TODO: build applier from the plugin config
// Get the deploy target config.
var deployTargetConfig kubeconfig.KubernetesDeployTargetConfig
deployTarget := a.pluginConfig.FindDeployTarget(input.GetDeployment().GetDeployTargets()[0]) // TODO: check if there is a deploy target
if err := json.Unmarshal(deployTarget.Config, &deployTargetConfig); err != nil { // TODO: do not unmarshal the config here, but in the initialization of the plugin
lp.Errorf("Failed while unmarshalling deploy target config (%v)", err)
return nil, status.Error(codes.Internal, err.Error())
}

Check warning on line 273 in pkg/app/pipedv1/plugin/kubernetes/deployment/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L268-L273

Added lines #L268 - L273 were not covered by tests

// Get the kubectl tool path.
kubectlPath, err := a.toolRegistry.Kubectl(ctx, cmp.Or(cfg.Spec.Input.KubectlVersion, deployTargetConfig.KubectlVersion, defaultKubectlVersion))
if err != nil {
lp.Errorf("Failed while getting kubectl tool (%v)", err)
return nil, status.Error(codes.Internal, err.Error())
}

Check warning on line 280 in pkg/app/pipedv1/plugin/kubernetes/deployment/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L276-L280

Added lines #L276 - L280 were not covered by tests

// Create the applier for the target cluster.
applier := provider.NewApplier(provider.NewKubectl(kubectlPath), cfg.Spec.Input, deployTargetConfig, a.logger)

Check warning on line 283 in pkg/app/pipedv1/plugin/kubernetes/deployment/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L283

Added line #L283 was not covered by tests

// Start applying all manifests to add or update running resources.
if err := applyManifests(ctx, applier, manifests, cfg.Spec.Input.Namespace, lp); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/configv1/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -1310,3 +1310,13 @@
// The configuration of the deploy target.
Config json.RawMessage `json:"config"`
}

// FindDeployTarget finds the deploy target by the given name.
func (p *PipedPlugin) FindDeployTarget(name string) *PipedDeployTarget {
for _, dt := range p.DeployTargets {
if dt.Name == name {
return &dt
}

Check warning on line 1319 in pkg/configv1/piped.go

View check run for this annotation

Codecov / codecov/patch

pkg/configv1/piped.go#L1315-L1319

Added lines #L1315 - L1319 were not covered by tests
}
return nil

Check warning on line 1321 in pkg/configv1/piped.go

View check run for this annotation

Codecov / codecov/patch

pkg/configv1/piped.go#L1321

Added line #L1321 was not covered by tests
}
Loading