diff --git a/pkg/ignition/common.go b/pkg/ignition/common.go index a8f263bda3a9919f9976c6e61f773502db09913e..e778029c4cf39221a557868a5609200797f1c89c 100644 --- a/pkg/ignition/common.go +++ b/pkg/ignition/common.go @@ -16,10 +16,9 @@ limitations under the License. package ignition import ( + "fmt" "nestos-kubernetes-deployer/data" - "nestos-kubernetes-deployer/pkg/configmanager/asset/cluster" "nestos-kubernetes-deployer/pkg/utils" - "os" "path" "strings" @@ -28,48 +27,14 @@ import ( "github.com/sirupsen/logrus" ) -var ( - enabledServices = []string{ - "kubelet.service", - "set-kernel-para.service", - "disable-selinux.service", - "init-cluster.service", - "install-cni-plugin.service", - "join-master.service", - "join-worker.service", - "release-image-pivot.service", - } -) - -type tmplData struct { +type Common struct { + UserName string SSHKey string - APIServerURL string - Hsip string //HostName + IP - ImageRegistry string - PauseImageTag string - KubeVersion string - ServiceSubnet string - PodSubnet string - Token string + PassWord string NodeType string - NodeName string - CorednsImageTag string - IpSegment string - ReleaseImageURl string - PasswordHash string - CertificateKey string -} - -type Common struct { - Config *igntypes.Config - ClusterAsset cluster.ClusterAsset - Files []File -} - -type File struct { - Path string - Mode int - Content []byte + TmplData interface{} + EnabledServices []string + Config *igntypes.Config } func (c *Common) GenerateFile() error { @@ -80,11 +45,11 @@ func (c *Common) GenerateFile() error { Passwd: igntypes.Passwd{ Users: []igntypes.PasswdUser{ { - Name: "root", + Name: c.UserName, SSHAuthorizedKeys: []igntypes.SSHAuthorizedKey{ - igntypes.SSHAuthorizedKey("/*SSHKEY*/"), + igntypes.SSHAuthorizedKey(c.SSHKey), }, - PasswordHash: nil, /*PasswordHasH*/ + PasswordHash: &c.PassWord, }, }, }, @@ -99,53 +64,27 @@ func (c *Common) GenerateFile() error { }, }, } - //get template data - td := GetTmplData(c.ClusterAsset) - - //todo:对配置项参数解析,生成不同的Ignition文件 - - if err := AppendStorageFiles(c.Config, "/", "", td); err != nil { + nodeFilesPath := fmt.Sprintf("ignition/%s/files", c.NodeType) + if err := appendStorageFiles(c.Config, "/", nodeFilesPath, c.TmplData); err != nil { logrus.Errorf("failed to add files to a ignition config: %v", err) return err } - if err := AppendSystemdUnits(c.Config, "", td, enabledServices); err != nil { + nodeUnitPath := fmt.Sprintf("ignition/%s/systemd/", c.NodeType) + if err := appendSystemdUnits(c.Config, nodeUnitPath, c.TmplData, c.EnabledServices); err != nil { logrus.Errorf("failed to add systemd units to a ignition config: %v", err) return err } - for _, file := range c.Files { - ignFile := FileWithContents(file.Path, file.Mode, file.Content) - c.Config.Storage.Files = appendFiles(c.Config.Storage.Files, ignFile) - } return nil } -func (c *Common) SaveFile(filename string) error { - data, err := Marshal(c.Config) - if err != nil { - logrus.Errorf("failed to Marshal ignition config: %v", err) - return err - } - if err := os.WriteFile(filename, data, 0640); err != nil { - logrus.Errorf("failed to save ignition file: %v", err) - return err - } - return nil -} - -func GetTmplData(c cluster.ClusterAsset) *tmplData { - return &tmplData{ - KubeVersion: c.KubernetesVersion, - } -} - /* AppendStorageFiles add files to a ignition config Parameters: - config: the ignition config to be modified - tmplData: struct to used to render templates */ -func AppendStorageFiles(config *igntypes.Config, base string, uri string, tmplData interface{}) error { +func appendStorageFiles(config *igntypes.Config, base string, uri string, tmplData interface{}) error { file, err := data.Assets.Open(uri) if err != nil { return err @@ -167,7 +106,7 @@ func AppendStorageFiles(config *igntypes.Config, base string, uri string, tmplDa for _, childInfo := range children { name := childInfo.Name() - err = AppendStorageFiles(config, path.Join(base, name), path.Join(uri, name), tmplData) + err = appendStorageFiles(config, path.Join(base, name), path.Join(uri, name), tmplData) if err != nil { return err } @@ -179,21 +118,10 @@ func AppendStorageFiles(config *igntypes.Config, base string, uri string, tmplDa return err } ignFile := FileWithContents(strings.TrimSuffix(base, ".template"), 0755, data) - config.Storage.Files = appendFiles(config.Storage.Files, ignFile) + config.Storage.Files = AppendFiles(config.Storage.Files, ignFile) return nil } -func appendFiles(files []igntypes.File, file igntypes.File) []igntypes.File { - for i, f := range files { - if f.Node.Path == file.Node.Path { - files[i] = file - return files - } - } - files = append(files, file) - return files -} - /* Add systemd units to a ignition config Parameters: @@ -202,7 +130,7 @@ Parameters: - tmplData: struct to used to render templates - enabledServices: a list of systemd units to be enabled by default */ -func AppendSystemdUnits(config *igntypes.Config, uri string, tmplData interface{}, enabledServices []string) error { +func appendSystemdUnits(config *igntypes.Config, uri string, tmplData interface{}, enabledServices []string) error { enabled := make(map[string]struct{}, len(enabledServices)) for _, s := range enabledServices { enabled[s] = struct{}{} diff --git a/pkg/ignition/machine/master.go b/pkg/ignition/machine/master.go index 55388ffb6b6577890e38e25d8c0fef8c28d1cbed..259abdc137095685051347b4894f8881f490e5af 100644 --- a/pkg/ignition/machine/master.go +++ b/pkg/ignition/machine/master.go @@ -14,3 +14,84 @@ See the License for the specific language governing permissions and limitations under the License. */ package machine + +import ( + "nestos-kubernetes-deployer/pkg/configmanager/asset/cluster" + "nestos-kubernetes-deployer/pkg/ignition" + + igntypes "github.com/coreos/ignition/v2/config/v3_2/types" + "github.com/sirupsen/logrus" +) + +type masterTmplData struct { + APIServerURL string + Hsip string //HostName + IP + ImageRegistry string + PauseImageTag string + KubeVersion string + ServiceSubnet string + PodSubnet string + Token string + NodeType string + NodeName string + CorednsImageTag string + IpSegment string + ReleaseImageURl string + CertificateKey string +} + +var ( + enabledServices = []string{ + "kubelet.service", + "set-kernel-para.service", + "disable-selinux.service", + "init-cluster.service", + "install-cni-plugin.service", + "join-master.service", + "release-image-pivot.service", + } +) + +type Master struct { + ClusterAsset cluster.ClusterAsset + CertFiles []CertFile + IgnFiles []IgnFile +} + +type CertFile struct { + Path string + Mode int + Content []byte +} + +type IgnFile struct { + Data []byte +} + +func (m *Master) GenerateMasterIgnFiles() error { + mtd := getTmplData(m.ClusterAsset) + + generateFile := ignition.Common{ + UserName: m.ClusterAsset.NodeAsset[0].UserName, + SSHKey: m.ClusterAsset.NodeAsset[0].SSHKey, + PassWord: m.ClusterAsset.NodeAsset[0].PassWord, + NodeType: "controlplane", + TmplData: mtd, + EnabledServices: enabledServices, + Config: &igntypes.Config{}, + } + if err := generateFile.GenerateFile(); err != nil { + logrus.Errorf("failed to generate controlplane ignition file: %v", err) + return err + } + for _, file := range m.CertFiles { + ignFile := ignition.FileWithContents(file.Path, file.Mode, file.Content) + generateFile.Config.Storage.Files = ignition.AppendFiles(generateFile.Config.Storage.Files, ignFile) + } + //todo:generate master Ignition + return nil +} + +func getTmplData(c cluster.ClusterAsset) *masterTmplData { + return &masterTmplData{ /**/ } +} diff --git a/pkg/ignition/node.go b/pkg/ignition/node.go index d0843181cddd9caa8956a219f7256d2d19004280..8c941ab7cfb1f5f0fcfe6d87578e49b1d03e31f5 100644 --- a/pkg/ignition/node.go +++ b/pkg/ignition/node.go @@ -51,3 +51,14 @@ func FileWithContents(path string, mode int, contents []byte) igntypes.File { }, } } + +func AppendFiles(files []igntypes.File, file igntypes.File) []igntypes.File { + for i, f := range files { + if f.Node.Path == file.Node.Path { + files[i] = file + return files + } + } + files = append(files, file) + return files +}