go template
/*
Copyright 2022 cuisongliu@qq.com.
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 checker
import (
"os"
"github.com/labring/sealos/pkg/template"
"github.com/labring/image-cri-shim/pkg/types"
"github.com/pkg/errors"
"github.com/labring/sealos/pkg/buildimage"
v2 "github.com/labring/sealos/pkg/types/v1beta1"
"github.com/labring/sealos/pkg/utils/logger"
)
type CRIShimChecker struct {
}
type CRIShimStatus struct {
Config map[string]string
ImageList []string
Error string
}
func (n *CRIShimChecker) Check(cluster *v2.Cluster, phase string) error {
if phase != PhasePost {
return nil
}
status := &CRIShimStatus{}
defer func() {
err := n.Output(status)
if err != nil {
logger.Error("error output: %+v", err)
}
}()
criShimConfig := "/etc/image-cri-shim.yaml"
if shimCfg, err := types.Unmarshal(criShimConfig); err != nil {
status.Error = errors.Wrap(err, "read image-cri-shim config error").Error()
} else {
status.Config = map[string]string{}
status.Config["ShimSocket"] = shimCfg.ImageShimSocket
status.Config["CRISocket"] = shimCfg.RuntimeSocket
status.Config["RegistryAddress"] = shimCfg.Address
status.Config["CRIVersion"] = string(shimCfg.CRIVersion)
if status.Config["CRIVersion"] == "" {
delete(status.Config, "CRIVersion")
}
status.Config["RegistryAuth"] = shimCfg.Auth
status.Config["RootfsImageList"] = shimCfg.Image
if dir := status.Config["RootfsImageList"]; dir != "" {
images, err := buildimage.ParseShimImages(dir)
if err != nil {
status.Error = errors.Wrap(err, "read image-cri-shim config error").Error()
} else {
status.ImageList = images
}
}
}
status.Error = Nil
return nil
}
func (n *CRIShimChecker) Output(status *CRIShimStatus) error {
tpl, isOk, err := template.TryParse(`
CRIShim Service Status
Error: {{ .Error }}
Logger: journalctl -xeu image-cri-shim
Rootfs ImageList:
{{- range .ImageList }}
- {{ . }}
{{- end }}
{{ if .Config -}}
Config List:
{{- range $key,$val:= .Config }}
{{ $key }}: {{ $val }}
{{- end }}
{{ end }}`)
if err != nil || !isOk {
if err != nil {
logger.Error("failed to render cri-shim checkers template. error: %s", err.Error())
return err
}
return errors.New("convert cri-shim template failed")
}
if err = tpl.Execute(os.Stdout, status); err != nil {
return err
}
return nil
}
func NewCRIShimChecker() Interface {
return &CRIShimChecker{}
}
分类:
GO
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2022-02-15 免费好用的MySQL高可用方案
2022-02-15 git 查看分支、切换分支及合并分支 命令
2022-02-15 jenkins插件git获得过程