github.com/docker/compose-on-kubernetes@v0.5.0/internal/convert/deployment.go (about)

     1  package convert
     2  
     3  import (
     4  	"github.com/docker/compose-on-kubernetes/api/compose/latest"
     5  	appsv1 "k8s.io/api/apps/v1"
     6  	apiv1 "k8s.io/api/core/v1"
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  	"k8s.io/apimachinery/pkg/util/intstr"
     9  )
    10  
    11  // toDeployment converts a Compose Service to a Kube Deployment if its replica mode is NOT `global`.
    12  func toDeployment(s latest.ServiceConfig, objectMeta metav1.ObjectMeta, podTemplate apiv1.PodTemplateSpec, labelSelector map[string]string, original appsv1.Deployment) *appsv1.Deployment {
    13  	revisionHistoryLimit := int32(3)
    14  	dep := original.DeepCopy()
    15  	dep.ObjectMeta = objectMeta
    16  	dep.Spec.Replicas = toReplicas(s.Deploy.Replicas)
    17  	dep.Spec.RevisionHistoryLimit = &revisionHistoryLimit
    18  	dep.Spec.Template = forceRestartPolicy(podTemplate, apiv1.RestartPolicyAlways)
    19  	dep.Spec.Strategy = toDeploymentStrategy(s, original.Spec.Strategy)
    20  	dep.Spec.Selector = &metav1.LabelSelector{
    21  		MatchLabels: labelSelector,
    22  	}
    23  	return dep
    24  }
    25  
    26  func isGlobal(srv latest.ServiceConfig) bool {
    27  	return srv.Deploy.Mode == "global"
    28  }
    29  
    30  func toDeploymentStrategy(s latest.ServiceConfig, original appsv1.DeploymentStrategy) appsv1.DeploymentStrategy {
    31  	config := s.Deploy.UpdateConfig
    32  	if config == nil {
    33  		return original
    34  	}
    35  
    36  	if config.Parallelism == nil {
    37  		return original
    38  	}
    39  
    40  	return appsv1.DeploymentStrategy{
    41  		Type: appsv1.RollingUpdateDeploymentStrategyType,
    42  		RollingUpdate: &appsv1.RollingUpdateDeployment{
    43  			MaxUnavailable: &intstr.IntOrString{
    44  				Type:   intstr.Int,
    45  				IntVal: int32(*config.Parallelism),
    46  			},
    47  			MaxSurge: nil,
    48  		},
    49  	}
    50  }
    51  
    52  func toReplicas(replicas *uint64) *int32 {
    53  	v := int32(1)
    54  
    55  	if replicas != nil {
    56  		v = int32(*replicas)
    57  	}
    58  
    59  	return &v
    60  }