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

Split deployment configuration into multiple files #560

Merged
merged 2 commits into from
Jul 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ go_library(
"config.go",
"control_plane.go",
"deployment.go",
"deployment_cloudrun.go",
"deployment_kubernetes.go",
"deployment_lambda.go",
"deployment_terraform.go",
"duration.go",
"piped.go",
"replicas.go",
Expand Down
230 changes: 0 additions & 230 deletions pkg/config/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,6 @@ type Pipelineable interface {
GetStage(index int32) (PipelineStage, bool)
}

// KubernetesDeploymentSpec represents a deployment configuration for Kubernetes application.
type KubernetesDeploymentSpec struct {
Input KubernetesDeploymentInput `json:"input"`
CommitMatcher DeploymentCommitMatcher `json:"commitMatcher"`
Sync K8sSyncStageOptions `json:"sync"`
Pipeline *DeploymentPipeline `json:"pipeline"`

// Which resource should be considered as the Service of application.
// Empty means the first Service resource will be used.
Service K8sResourceReference `json:"service"`
// Which resources should be considered as the Workload of application.
// Empty means all Deployments.
// e.g. "Deployment/deployment-name", "ReplicaSet/replicaset-name"
Workloads []K8sResourceReference `json:"workloads"`
// Which method should be used for traffic routing.
TrafficRouting *TrafficRouting `json:"trafficRouting"`
}

func (s *KubernetesDeploymentSpec) GetStage(index int32) (PipelineStage, bool) {
if s.Pipeline == nil {
return PipelineStage{}, false
}
if int(index) >= len(s.Pipeline.Stages) {
return PipelineStage{}, false
}
return s.Pipeline.Stages[index], true
}

// Validate returns an error if any wrong configuration value was found.
func (s *KubernetesDeploymentSpec) Validate() error {
return nil
}

// TerraformDeploymentSpec represents a deployment configuration for Terraform application.
type TerraformDeploymentSpec struct {
Input TerraformDeploymentInput `json:"input"`
Pipeline *DeploymentPipeline `json:"pipeline"`
}

func (s *TerraformDeploymentSpec) GetStage(index int32) (PipelineStage, bool) {
if s.Pipeline == nil {
return PipelineStage{}, false
}
if int(index) >= len(s.Pipeline.Stages) {
return PipelineStage{}, false
}
return s.Pipeline.Stages[index], true
}

// Validate returns an error if any wrong configuration value was found.
func (s *TerraformDeploymentSpec) Validate() error {
return nil
}

// DeploymentCommitMatcher provides a way to decide how to deploy.
type DeploymentCommitMatcher struct {
// It makes sure to perform syncing if the commit message matches this regular expression.
Expand All @@ -95,42 +41,6 @@ type DeploymentPipeline struct {
Stages []PipelineStage `json:"stages"`
}

type TrafficRoutingMethod string

const (
TrafficRoutingMethodPodSelector TrafficRoutingMethod = "podselector"
TrafficRoutingMethodIstio TrafficRoutingMethod = "istio"
)

type TrafficRouting struct {
Method TrafficRoutingMethod `json:"method"`
Istio *IstioTrafficRouting `json:"istio"`
}

// DetermineTrafficRoutingMethod determines the routing method should be used based on the TrafficRouting config.
// The default is PodSelector: the way by updating the selector in Service to switching all of traffic.
func DetermineTrafficRoutingMethod(cfg *TrafficRouting) TrafficRoutingMethod {
if cfg == nil {
return TrafficRoutingMethodPodSelector
}
if cfg.Method == "" {
return TrafficRoutingMethodPodSelector
}
return cfg.Method
}

type IstioTrafficRouting struct {
EditableRoutes []string `json:"editableRoutes"`
// TODO: Add a validate to ensure this was configured or using the default value by service name.
Host string `json:"host"`
VirtualService K8sResourceReference `json:"virtualService"`
}

type K8sResourceReference struct {
Kind string `json:"kind"`
Name string `json:"name"`
}

// PiplineStage represents a single stage of a pipeline.
// This is used as a generic struct for all stage type.
type PipelineStage struct {
Expand Down Expand Up @@ -243,97 +153,6 @@ type WaitApprovalStageOptions struct {
Approvers []string `json:"approvers"`
}

// K8sSyncStageOptions contains all configurable values for a K8S_SYNC stage.
type K8sSyncStageOptions struct {
// Whether the PRIMARY variant label should be added to manifests if they were missing.
AddVariantLabelToSelector bool `json:"addVariantLabelToSelector"`
// Whether the resources that are no longer defined in Git will be removed.
Prune bool `json:"prune"`
}

// K8sPrimaryRolloutStageOptions contains all configurable values for a K8S_PRIMARY_ROLLOUT stage.
type K8sPrimaryRolloutStageOptions struct {
// Suffix that should be used when naming the PRIMARY variant's resources.
// Default is "primary".
Suffix string `json:"suffix"`
// Whether the PRIMARY service should be created.
CreateService bool `json:"createService"`
// Whether the PRIMARY variant label should be added to manifests if they were missing.
AddVariantLabelToSelector bool `json:"addVariantLabelToSelector"`
// Whether the resources that are no longer defined in Git will be removed.
Prune bool `json:"prune"`
}

// K8sCanaryRolloutStageOptions contains all configurable values for a K8S_CANARY_ROLLOUT stage.
type K8sCanaryRolloutStageOptions struct {
// How many pods for CANARY workloads.
// An integer value can be specified to indicate an absolute value of pod number.
// Or a string suffixed by "%" to indicate an percentage value compared to the pod number of PRIMARY.
// Default is 1 pod.
Replicas Replicas `json:"replicas"`
// Suffix that should be used when naming the CANARY variant's resources.
// Default is "canary".
Suffix string `json:"suffix"`
// Whether the CANARY service should be created.
CreateService bool `json:"createService"`
}

// K8sCanaryCleanStageOptions contains all configurable values for a K8S_CANARY_CLEAN stage.
type K8sCanaryCleanStageOptions struct {
}

// K8sBaselineRolloutStageOptions contains all configurable values for a K8S_BASELINE_ROLLOUT stage.
type K8sBaselineRolloutStageOptions struct {
// How many pods for BASELINE workloads.
// An integer value can be specified to indicate an absolute value of pod number.
// Or a string suffixed by "%" to indicate an percentage value compared to the pod number of PRIMARY.
// Default is 1 pod.
Replicas Replicas `json:"replicas"`
// Suffix that should be used when naming the BASELINE variant's resources.
// Default is "baseline".
Suffix string `json:"suffix"`
// Whether the BASELINE service should be created.
CreateService bool `json:"createService"`
}

// K8sBaselineCleanStageOptions contains all configurable values for a K8S_BASELINE_CLEAN stage.
type K8sBaselineCleanStageOptions struct {
}

// K8sTrafficRoutingStageOptions contains all configurable values for a K8S_TRAFFIC_ROUTING stage.
type K8sTrafficRoutingStageOptions struct {
// Which variant should receive all traffic.
All string `json:"all"`
// The percentage of traffic should be routed to PRIMARY variant.
Primary int `json:"primary"`
// The percentage of traffic should be routed to CANARY variant.
Canary int `json:"canary"`
// The percentage of traffic should be routed to BASELINE variant.
Baseline int `json:"baseline"`
}

func (opts K8sTrafficRoutingStageOptions) Percentages() (primary, canary, baseline int) {
switch opts.All {
case "primary":
return 100, 0, 0
case "canary":
return 0, 100, 0
case "baseline":
return 0, 0, 100
}
return opts.Primary, opts.Canary, opts.Baseline
}

// TerraformPlanStageOptions contains all configurable values for a K8S_TERRAFORM_PLAN stage.
type TerraformPlanStageOptions struct {
}

// TerraformApplyStageOptions contains all configurable values for a K8S_TERRAFORM_APPLY stage.
type TerraformApplyStageOptions struct {
// How many times to retry applying terraform changes.
Retries int `json:"retries"`
}

// AnalysisStageOptions contains all configurable values for a K8S_ANALYSIS stage.
type AnalysisStageOptions struct {
// How long the analysis process should be executed.
Expand Down Expand Up @@ -369,52 +188,3 @@ type TemplatableAnalysisHTTP struct {
AnalysisHTTP
Template AnalysisTemplateRef `json:"template"`
}

type KubernetesDeploymentInput struct {
Manifests []string `json:"manifests"`
KubectlVersion string `json:"kubectlVersion"`

KustomizeVersion string `json:"kustomizeVersion"`

HelmChart *InputHelmChart `json:"helmChart"`
HelmOptions *InputHelmOptions `json:"helmOptions"`
HelmVersion string `json:"helmVersion"`

// The namespace where manifests will be applied.
Namespace string `json:"namespace"`
// Automatically reverts all changes from all stages when one of them failed.
// Default is true.
AutoRollback bool `json:"autoRollback"`
Dependencies []string `json:"dependencies,omitempty"`
}

type TerraformDeploymentInput struct {
Workspace string `json:"workspace,omitempty"`
TerraformVersion string `json:"terraformVersion,omitempty"`
// Automatically reverts all changes from all stages when one of them failed.
// Default is false.
AutoRollback bool `json:"autoRollback"`
Dependencies []string `json:"dependencies,omitempty"`
}

type InputHelmChart struct {
// Empty means current repository.
GitRemote string `json:"gitRemote"`
// The commit SHA or tag for remote git.
Ref string `json:"ref"`
// Relative path from the repository root directory to the chart directory.
Path string `json:"path"`

// The name of an added Helm chart repository.
Repository string `json:"repository"`
Name string `json:"name"`
Version string `json:"version"`
}

type InputHelmOptions struct {
// By default the release name is equal to the application name.
ReleaseName string `json:"releaseName"`
// List of value files should be loaded.
ValueFiles []string `json:"valueFiles"`
SetFiles map[string]string
}
15 changes: 15 additions & 0 deletions pkg/config/deployment_cloudrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2020 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 config