github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/runtime/utils.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 runtime 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "net" 21 "os" 22 "path/filepath" 23 "strings" 24 25 ocispecs "github.com/opencontainers/image-spec/specs-go/v1" 26 "github.com/sealerio/sealer/common" 27 osi "github.com/sealerio/sealer/utils/os" 28 ) 29 30 // Deprecated 31 type Metadata struct { 32 Version string `json:"version"` 33 Arch string `json:"arch"` 34 Variant string `json:"variant"` 35 // KubeVersion is a SemVer constraint specifying the version of Kubernetes required. 36 KubeVersion string `json:"kubeVersion"` 37 NydusFlag bool `json:"NydusFlag"` 38 // ClusterRuntime is a flag to distinguish the runtime for k0s、k8s、k3s 39 ClusterRuntime ClusterRuntime `json:"ClusterRuntime"` 40 } 41 42 type ClusterRuntime string 43 44 // Deprecated 45 func LoadMetadata(rootfs string) (*Metadata, error) { 46 metadataPath := filepath.Join(rootfs, common.DefaultMetadataName) 47 var metadataFile []byte 48 var err error 49 var md Metadata 50 if !osi.IsFileExist(metadataPath) { 51 return nil, nil 52 } 53 54 metadataFile, err = os.ReadFile(filepath.Clean(metadataPath)) 55 if err != nil { 56 return nil, fmt.Errorf("failed to read ClusterImage metadata: %v", err) 57 } 58 err = json.Unmarshal(metadataFile, &md) 59 if err != nil { 60 return nil, fmt.Errorf("failed to load ClusterImage metadata: %v", err) 61 } 62 return &md, nil 63 } 64 65 // Deprecated 66 func GetClusterImagePlatform(rootfs string) (cp ocispecs.Platform) { 67 // current we only support build on linux 68 cp = ocispecs.Platform{ 69 Architecture: "amd64", 70 OS: "linux", 71 Variant: "", 72 OSVersion: "", 73 } 74 meta, err := LoadMetadata(rootfs) 75 if err != nil { 76 return 77 } 78 if meta == nil { 79 return 80 } 81 if meta.Arch != "" { 82 cp.Architecture = meta.Arch 83 } 84 if meta.Variant != "" { 85 cp.Variant = meta.Variant 86 } 87 return 88 } 89 90 func RemoteCertCmd(altNames []string, hostIP net.IP, hostName, serviceCIRD, DNSDomain string) string { 91 cmd := "seautil cert gen " 92 if hostIP != nil { 93 cmd += fmt.Sprintf(" --node-ip %s", hostIP.String()) 94 } 95 96 if hostName != "" { 97 cmd += fmt.Sprintf(" --node-name %s", hostName) 98 } 99 100 if serviceCIRD != "" { 101 cmd += fmt.Sprintf(" --service-cidr %s", serviceCIRD) 102 } 103 104 if DNSDomain != "" { 105 cmd += fmt.Sprintf(" --dns-domain %s", DNSDomain) 106 } 107 108 for _, name := range append(altNames, common.APIServerDomain) { 109 if name != "" { 110 cmd += fmt.Sprintf(" --alt-names %s", name) 111 } 112 } 113 114 return cmd 115 } 116 117 func IsInContainer() bool { 118 data, err := osi.NewFileReader("/proc/1/environ").ReadAll() 119 if err != nil { 120 return false 121 } 122 return strings.Contains(string(data), "container=docker") 123 }