github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/ipvs/ipvs.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ipvs 16 17 import ( 18 "fmt" 19 "path" 20 21 v1 "k8s.io/api/core/v1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "sigs.k8s.io/yaml" 24 25 "github.com/sealerio/sealer/common" 26 ) 27 28 const ( 29 LvsCareCommand = "/usr/bin/lvscare" 30 ) 31 32 func GetCreateLvscareStaticPodCmd(content, fileName string) string { 33 return fmt.Sprintf("mkdir -p %s && echo \"%s\" > %s", 34 common.StaticPodDir, 35 content, 36 path.Join(common.StaticPodDir, fileName), 37 ) 38 } 39 40 // LvsStaticPodYaml return lvs care static pod yaml 41 func LvsStaticPodYaml(podName, virtualEndpoint string, realEndpoints []string, image string, 42 healthPath string, healthSchem string) (string, error) { 43 if virtualEndpoint == "" || len(realEndpoints) == 0 || image == "" { 44 return "", fmt.Errorf("invalid args to create Lvs static pod") 45 } 46 47 args := []string{"care", "--vs", virtualEndpoint, "--health-path", healthPath, "--health-schem", healthSchem} 48 for _, re := range realEndpoints { 49 args = append(args, "--rs", re) 50 } 51 flag := true 52 pod := componentPod(podName, v1.Container{ 53 Name: "main", 54 Image: image, 55 Command: []string{LvsCareCommand}, 56 Args: args, 57 ImagePullPolicy: v1.PullIfNotPresent, 58 SecurityContext: &v1.SecurityContext{Privileged: &flag}, 59 }) 60 61 yml, err := yaml.Marshal(pod) 62 if err != nil { 63 return "", fmt.Errorf("failed to decode lvs care static pod yaml: %s", err) 64 } 65 66 return string(yml), nil 67 } 68 69 // componentPod returns a Pod object from the container and volume specifications 70 func componentPod(podName string, container v1.Container) v1.Pod { 71 hostPathType := v1.HostPathUnset 72 mountName := "lib-modules" 73 volumes := []v1.Volume{ 74 {Name: mountName, VolumeSource: v1.VolumeSource{ 75 HostPath: &v1.HostPathVolumeSource{ 76 Path: "/lib/modules", 77 Type: &hostPathType, 78 }, 79 }}, 80 } 81 container.VolumeMounts = []v1.VolumeMount{ 82 {Name: mountName, ReadOnly: true, MountPath: "/lib/modules"}, 83 } 84 85 return v1.Pod{ 86 TypeMeta: metav1.TypeMeta{ 87 APIVersion: "v1", 88 Kind: "Pod", 89 }, 90 ObjectMeta: metav1.ObjectMeta{ 91 Name: podName, 92 Namespace: metav1.NamespaceSystem, 93 }, 94 Spec: v1.PodSpec{ 95 Containers: []v1.Container{container}, 96 HostNetwork: true, 97 Volumes: volumes, 98 }, 99 } 100 }