-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move sync / rollback method to the separate file (#5479)
* Move sync / rollback method to the separate file Signed-off-by: Shinnosuke Sawada-Dazai <shin@warashi.dev> * Fix formatting Signed-off-by: Shinnosuke Sawada-Dazai <shin@warashi.dev> --------- Signed-off-by: Shinnosuke Sawada-Dazai <shin@warashi.dev>
- Loading branch information
Showing
5 changed files
with
840 additions
and
760 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
pkg/app/pipedv1/plugin/kubernetes/deployment/rollback.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// 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 deployment | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
|
||
kubeconfig "github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/config" | ||
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/provider" | ||
config "github.com/pipe-cd/pipecd/pkg/configv1" | ||
"github.com/pipe-cd/pipecd/pkg/model" | ||
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment" | ||
"github.com/pipe-cd/pipecd/pkg/plugin/logpersister" | ||
) | ||
|
||
func (a *DeploymentService) executeK8sRollbackStage(ctx context.Context, lp logpersister.StageLogPersister, input *deployment.ExecutePluginInput) model.StageStatus { | ||
if input.GetDeployment().GetRunningCommitHash() == "" { | ||
lp.Errorf("Unable to determine the last deployed commit to rollback. It seems this is the first deployment.") | ||
return model.StageStatus_STAGE_FAILURE | ||
} | ||
|
||
lp.Info("Start rolling back the deployment") | ||
|
||
cfg, err := config.DecodeYAML[*kubeconfig.KubernetesApplicationSpec](input.GetRunningDeploymentSource().GetApplicationConfig()) | ||
if err != nil { | ||
lp.Errorf("Failed while decoding application config (%v)", err) | ||
return model.StageStatus_STAGE_FAILURE | ||
} | ||
|
||
lp.Infof("Loading manifests at commit %s for handling", input.GetDeployment().GetRunningCommitHash()) | ||
manifests, err := a.loadManifests(ctx, input.GetDeployment(), cfg.Spec, input.GetRunningDeploymentSource()) | ||
if err != nil { | ||
lp.Errorf("Failed while loading manifests (%v)", err) | ||
return model.StageStatus_STAGE_FAILURE | ||
} | ||
lp.Successf("Successfully loaded %d manifests", len(manifests)) | ||
|
||
// Because the loaded manifests are read-only | ||
// we duplicate them to avoid updating the shared manifests data in cache. | ||
// TODO: implement duplicateManifests function | ||
|
||
// When addVariantLabelToSelector is true, ensure that all workloads | ||
// have the variant label in their selector. | ||
var ( | ||
variantLabel = cfg.Spec.VariantLabel.Key | ||
primaryVariant = cfg.Spec.VariantLabel.PrimaryValue | ||
) | ||
// TODO: handle cfg.Spec.QuickSync.AddVariantLabelToSelector | ||
|
||
// Add variant annotations to all manifests. | ||
for i := range manifests { | ||
manifests[i].AddAnnotations(map[string]string{ | ||
variantLabel: primaryVariant, | ||
}) | ||
} | ||
|
||
if err := annotateConfigHash(manifests); err != nil { | ||
lp.Errorf("Unable to set %q annotation into the workload manifest (%v)", provider.AnnotationConfigHash, err) | ||
return model.StageStatus_STAGE_FAILURE | ||
} | ||
|
||
// Get the deploy target config. | ||
deployTargetConfig, err := kubeconfig.FindDeployTarget(a.pluginConfig, input.GetDeployment().GetDeployTargets()[0]) // TODO: check if there is a deploy target | ||
if err != nil { | ||
lp.Errorf("Failed while unmarshalling deploy target config (%v)", err) | ||
return model.StageStatus_STAGE_FAILURE | ||
} | ||
|
||
// 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 model.StageStatus_STAGE_FAILURE | ||
} | ||
|
||
// Create the applier for the target cluster. | ||
applier := provider.NewApplier(provider.NewKubectl(kubectlPath), cfg.Spec.Input, deployTargetConfig, a.logger) | ||
|
||
// Start applying all manifests to add or update running resources. | ||
if err := applyManifests(ctx, applier, manifests, cfg.Spec.Input.Namespace, lp); err != nil { | ||
lp.Errorf("Failed while applying manifests (%v)", err) | ||
return model.StageStatus_STAGE_FAILURE | ||
} | ||
|
||
// TODO: implement prune resources | ||
// TODO: delete all resources of CANARY variant | ||
// TODO: delete all resources of BASELINE variant | ||
|
||
return model.StageStatus_STAGE_SUCCESS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.