diff --git a/cmd/command/opts.go b/cmd/command/opts.go index b5f9f305e8054c54b708f792e23acad5b45e1a8d..b2437117a4297a964ebd0c26f3d466c7ac19c851 100644 --- a/cmd/command/opts.go +++ b/cmd/command/opts.go @@ -1,3 +1,19 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package command var ( diff --git a/data/data/housekeeper/housekeeper.io_updates.yaml b/data/data/housekeeper/housekeeper.io_updates.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/data/data/housekeeper/manager.yaml.template b/data/data/housekeeper/manager.yaml.template new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/data/data/housekeeper/role.yaml b/data/data/housekeeper/role.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/data/data/housekeeper/role_binding.yaml b/data/data/housekeeper/role_binding.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/pkg/housekeeper/upgrade/apply.go b/pkg/housekeeper/upgrade/apply.go index 189842ac92d39d624e6ad4f17381849f835a7411..be20ef4b9602e0fdfa4bd04a9fb615383f5e9ac6 100644 --- a/pkg/housekeeper/upgrade/apply.go +++ b/pkg/housekeeper/upgrade/apply.go @@ -1,3 +1,19 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package upgrade import ( @@ -6,11 +22,19 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" + "nestos-kubernetes-deployer/data" ) const namespaces = "housekeeper-system" -func deployOperator(kubeconfig string){ +type yamlTmlpData struct{ + operatorImageUrl string + controllerImageUrl string +} + +//todo:获取yamlTmlpData数据 + +func DeployOperator(kubeconfig string){ kubeconfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { logrus.Errorf("Error building kubeconfig: %v", err) @@ -21,7 +45,8 @@ func deployOperator(kubeconfig string){ logrus.Errorf("Error creating kubernetes client: %v", err) return err } - // todo: 部署CRD + //todo:获取yaml内容,部署资源 + } func applyYAML(clientset *kubernetes.Clientset, yamlContent) error { @@ -36,6 +61,56 @@ func applyYAML(clientset *kubernetes.Clientset, yamlContent) error { logrus.ErrorF("Error applying YAML: %v\n", err) return err } - return nil } + +func getYamlContent(uri string, yamlTmlpData interface{}) ([]byte, error){ + file, err := data.Assets.Open(uri) + if err != nil { + logrus.Errorf("Error opening file %s: %v\n", uri, err) + return nil,err + } + defer file.Close() + info, err := file.Stat() + if err != nil { + logrus.Errorf("Error getting file info for %s: %v\n", uri, err) + return nil,err + } + _, data, err := readFile(info.Name(), file, yamlTmlpData) + if err != nil { + logrus.Errorf("Error reading file %s: %v\n", uri, err) + return nil, err + } + return data,nil +} + +// Read data from the file +func readFile(name string, file io.Reader, tmplData interface{}) (realName string, data []byte, err error) { + data, err = io.ReadAll(file) + if err != nil { + logrus.Errorf("Error reading file %s: %v\n", name, err) + return "", nil, err + } + if filepath.Ext(name) == ".template" { + name = strings.TrimSuffix(name, ".template") + tmpl := template.New(name) + tmpl, err := tmpl.Parse(string(data)) + if err != nil { + logrus.Errorf("Error parsing template for file %s: %v\n", name, err) + return "", nil, err + } + stringData := applyTmplData(tmpl, tmplData) + data = []byte(stringData) + } + + return name, data, nil +} + +func applyTmplData(tmpl *template.Template, data interface{}) string { + buf := &bytes.Buffer{} + if err := tmpl.Execute(buf, data); err != nil { + logrus.Errorf("Error applying template: %v\n", err) + panic(err) + } + return buf.String() +} \ No newline at end of file