github.com/docker/compose-on-kubernetes@v0.5.0/internal/convert/stateful-set.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 ) 9 10 // toStatefulSet converts a Compose Service to a Kube StatefulSet if its replica mode is NOT `global 11 // and it has persistent volumes. 12 func toStatefulSet(s latest.ServiceConfig, objectMeta metav1.ObjectMeta, podTemplate apiv1.PodTemplateSpec, 13 labelSelector map[string]string, original appsv1.StatefulSet) *appsv1.StatefulSet { 14 revisionHistoryLimit := int32(3) 15 res := original.DeepCopy() 16 res.ObjectMeta = objectMeta 17 res.Spec.Replicas = toReplicas(s.Deploy.Replicas) 18 res.Spec.RevisionHistoryLimit = &revisionHistoryLimit 19 res.Spec.Template = forceRestartPolicy(podTemplate, apiv1.RestartPolicyAlways) 20 res.Spec.UpdateStrategy = toStatefulSetUpdateStrategy(s, res.Spec.UpdateStrategy) 21 res.Spec.VolumeClaimTemplates = toPersistentVolumeClaims(s, res.Spec.VolumeClaimTemplates) 22 res.Spec.Selector = &metav1.LabelSelector{ 23 MatchLabels: labelSelector, 24 } 25 return res 26 } 27 28 func toStatefulSetUpdateStrategy(s latest.ServiceConfig, original appsv1.StatefulSetUpdateStrategy) appsv1.StatefulSetUpdateStrategy { 29 config := s.Deploy.UpdateConfig 30 if config == nil { 31 return original 32 } 33 34 if config.Parallelism == nil { 35 return original 36 } 37 38 return appsv1.StatefulSetUpdateStrategy{ 39 Type: appsv1.RollingUpdateStatefulSetStrategyType, 40 RollingUpdate: &appsv1.RollingUpdateStatefulSetStrategy{ 41 Partition: int32Ptr(config.Parallelism), 42 }, 43 } 44 } 45 46 func int32Ptr(value *uint64) *int32 { 47 if value == nil { 48 return nil 49 } 50 51 result := int32(*value) 52 return &result 53 }