github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/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  	"strings"
    19  
    20  	"github.com/pkg/errors"
    21  	v1 "k8s.io/api/core/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/client-go/kubernetes/scheme"
    25  
    26  	"github.com/alibaba/sealer/logger"
    27  )
    28  
    29  const (
    30  	LvsCareStaticPodName = "kube-lvscare"
    31  	LvsCareCommand       = "/usr/bin/lvscare"
    32  	DefaultLvsCareImage  = "sea.hub:5000/fanux/lvscare:latest"
    33  )
    34  
    35  // return lvs care static pod yaml
    36  func LvsStaticPodYaml(vip string, masters []string, image string) string {
    37  	if vip == "" || len(masters) == 0 {
    38  		return ""
    39  	}
    40  	if image == "" {
    41  		image = DefaultLvsCareImage
    42  	}
    43  	args := []string{"care", "--vs", vip + ":6443", "--health-path", "/healthz", "--health-schem", "https"}
    44  	for _, m := range masters {
    45  		if strings.Contains(m, ":") {
    46  			m = strings.Split(m, ":")[0]
    47  		}
    48  		args = append(args, "--rs")
    49  		args = append(args, m+":6443")
    50  	}
    51  	flag := true
    52  	pod := componentPod(v1.Container{
    53  		Name:            LvsCareStaticPodName,
    54  		Image:           image,
    55  		Command:         []string{LvsCareCommand},
    56  		Args:            args,
    57  		ImagePullPolicy: v1.PullIfNotPresent,
    58  		SecurityContext: &v1.SecurityContext{Privileged: &flag},
    59  	})
    60  	yaml, err := podToYaml(pod)
    61  	if err != nil {
    62  		logger.Error("decode lvs care static pod yaml failed %s", err)
    63  		return ""
    64  	}
    65  	return string(yaml)
    66  }
    67  
    68  func podToYaml(pod v1.Pod) ([]byte, error) {
    69  	codecs := scheme.Codecs
    70  	gv := v1.SchemeGroupVersion
    71  	const mediaType = runtime.ContentTypeYAML
    72  	info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
    73  	if !ok {
    74  		return []byte{}, errors.Errorf("unsupported media type %q", mediaType)
    75  	}
    76  
    77  	encoder := codecs.EncoderForVersion(info.Serializer, gv)
    78  	return runtime.Encode(encoder, &pod)
    79  }
    80  
    81  // componentPod returns a Pod object from the container and volume specifications
    82  func componentPod(container v1.Container) v1.Pod {
    83  	hostPathType := v1.HostPathUnset
    84  	mountName := "lib-modules"
    85  	volumes := []v1.Volume{
    86  		{Name: mountName, VolumeSource: v1.VolumeSource{
    87  			HostPath: &v1.HostPathVolumeSource{
    88  				Path: "/lib/modules",
    89  				Type: &hostPathType,
    90  			},
    91  		}},
    92  	}
    93  	container.VolumeMounts = []v1.VolumeMount{
    94  		{Name: mountName, ReadOnly: true, MountPath: "/lib/modules"},
    95  	}
    96  
    97  	return v1.Pod{
    98  		TypeMeta: metav1.TypeMeta{
    99  			APIVersion: "v1",
   100  			Kind:       "Pod",
   101  		},
   102  		ObjectMeta: metav1.ObjectMeta{
   103  			Name:      container.Name,
   104  			Namespace: metav1.NamespaceSystem,
   105  		},
   106  		Spec: v1.PodSpec{
   107  			Containers:  []v1.Container{container},
   108  			HostNetwork: true,
   109  			Volumes:     volumes,
   110  		},
   111  	}
   112  }