From 38c40221c400a0079c29560f613c9cc255c2e9ff Mon Sep 17 00:00:00 2001 From: cdjianghan <52602064+cdjianghan@users.noreply.github.com> Date: Tue, 3 Jan 2023 23:33:42 +0800 Subject: [PATCH] imagecrbuilder better code (#2334) * add imageCrdBuilder split some function to make the logic clear fit new buildah implement add image id arch and size read * add some imgeCrdBuilder change add CheckLoginStatus() to check wheather push image should appen ReadOrBuildAppConfig() add logic when user didn't write any image config add Image base info read such as image size image arch ... delete action content read now sealos push args is allow to be one or two ,when one ,this arg is both dst and src.when args has two , the secend is dest. * use config.GetAllCredentials to place CheckLoginStatus in imagecrdbuilder * change some func name to better understand add Success and Faild output * add some comment write ReadOrBuildImageCRFile better --- pkg/buildah/imagecrbuilder.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/pkg/buildah/imagecrbuilder.go b/pkg/buildah/imagecrbuilder.go index 8e1f61f63..4e006c931 100644 --- a/pkg/buildah/imagecrbuilder.go +++ b/pkg/buildah/imagecrbuilder.go @@ -59,7 +59,7 @@ const ( ) const ( - //Read Content Output + //Read Content Success Output SuccessCreateContainer = "Success create container" SuccessReadOrBuildImageCROutput = "Success get image cr" SuccessReadImageInfoOutput = "Success read image Info" @@ -69,7 +69,7 @@ const ( ) const ( - //Read Content Output + //Read Content Failed Output FailCreateContainer = "Fail to create container" FailReadOrBuildImageCROutput = "Fail to get image cr" FailReadImageInfoOutput = "Fail to read image Info" @@ -255,22 +255,19 @@ func (icb *ImageCRBuilder) ReadOrBuildImageCRFile(MountPoint string) (string, er if err = icb.ImageCRParse(c); err != nil { return FailReadOrBuildImageCROutput, fmt.Errorf("read ImageCR err : %v", err) } - } else { - //if app base config not find - //image name which contains "/" and ":" couldn't be used in meta name - MetaName := strings.Replace(icb.GetClearImagename(), ":", ".", -1) - MetaName = strings.Replace(MetaName, "/", ".", -1) - c := imagev1.Image{ - TypeMeta: metav1.TypeMeta{Kind: ImagehubKind, APIVersion: filepath.Join(ImagehubGroup, ImagehubVersion)}, - ObjectMeta: metav1.ObjectMeta{Name: MetaName}, - Spec: imagev1.ImageSpec{Name: imagev1.ImageName(icb.GetClearImagename())}, - Status: imagev1.ImageStatus{}, - } - icb.ImageCR = &c } + //if app base config not find + //image name which contains "/" and ":" couldn't be used in meta name + MetaName := strings.Replace(icb.GetClearImagename(), ":", ".", -1) + MetaName = strings.Replace(MetaName, "/", ".", -1) + // replace image cr spec and matename by imagename + icb.ImageCR.TypeMeta = metav1.TypeMeta{Kind: ImagehubKind, APIVersion: filepath.Join(ImagehubGroup, ImagehubVersion)} + icb.ImageCR.ObjectMeta.Name = MetaName + icb.ImageCR.Spec.Name = imagev1.ImageName(icb.GetClearImagename()) return SuccessReadOrBuildImageCROutput, nil } +// ImageCRParse parse image cr yaml file ot an image cr obj func (icb *ImageCRBuilder) ImageCRParse(c []byte) error { cr := imagev1.Image{} _, _, err := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(c, nil, &cr) @@ -282,6 +279,7 @@ func (icb *ImageCRBuilder) ImageCRParse(c []byte) error { return nil } +// ReadReadme gen image cr detail info: Readme func (icb *ImageCRBuilder) ReadReadme(MountPoint string) (string, error) { if file.IsExist(filepath.Join(MountPoint, READMEpath)) { c, err := file.ReadAll(filepath.Join(MountPoint, READMEpath)) @@ -293,6 +291,7 @@ func (icb *ImageCRBuilder) ReadReadme(MountPoint string) (string, error) { return SuccessReadReadmeOutput, nil } +// ReadInspectInfo gen image cr detail info: Id, Arch and Size func (icb *ImageCRBuilder) ReadInspectInfo(MountPoint string) (string, error) { realImpl, err := New("") if err != nil {