From 250115c3e3baa8330257cf680860a60d93eb6db2 Mon Sep 17 00:00:00 2001 From: lingdie <56745951+lingdie@users.noreply.github.com> Date: Wed, 11 Jan 2023 17:51:36 +0800 Subject: [PATCH] feat.imagehub mutate image cr. (#2377) * mutate image cr. * add mutate func * add image type. --- controllers/imagehub/api/v1/datapack_types.go | 17 ++++++++++---- controllers/imagehub/api/v1/image_types.go | 23 +++++++++++++++++++ controllers/imagehub/api/v1/image_webhook.go | 13 ++++++++++- .../imagehub/api/v1/zz_generated.deepcopy.go | 15 ------------ .../bases/imagehub.sealos.io_datapacks.yaml | 2 ++ .../crd/bases/imagehub.sealos.io_images.yaml | 2 ++ .../imagehub/deploy/manifests/deploy.yaml | 4 ++++ 7 files changed, 56 insertions(+), 20 deletions(-) diff --git a/controllers/imagehub/api/v1/datapack_types.go b/controllers/imagehub/api/v1/datapack_types.go index a551a30dd..657b5ba54 100644 --- a/controllers/imagehub/api/v1/datapack_types.go +++ b/controllers/imagehub/api/v1/datapack_types.go @@ -36,6 +36,7 @@ const ( type Data struct { // base Name ImageName `json:"name,omitempty"` + Type ImageType `json:"type,omitempty"` // grid RepoName RepoName `json:"repoName,omitempty"` Rating int `json:"rating,omitempty"` @@ -63,19 +64,23 @@ type FullData struct { type ImageBaseData struct { Name ImageName `json:"name,omitempty"` + Type ImageType `json:"type,omitempty"` } func (i *ImageBaseData) New(fd *FullData) { i.Name = fd.ImageInfo.Name + i.Type = fd.ImageInfo.Type } func (i *ImageBaseData) ToData() Data { return Data{ Name: i.Name, + Type: i.Type, } } type ImageGridData struct { Name ImageName `json:"name,omitempty"` + Type ImageType `json:"type,omitempty"` RepoName RepoName `json:"repoName,omitempty"` Rating int `json:"rating,omitempty"` Icon string `json:"icon,omitempty"` @@ -85,18 +90,19 @@ type ImageGridData struct { } func (i *ImageGridData) New(fd *FullData) { - i.RepoName = fd.RepoInfo.Name i.Name = fd.ImageInfo.Name + i.Type = fd.ImageInfo.Type + i.RepoName = fd.RepoInfo.Name i.Icon = fd.ImageInfo.DetailInfo.Icon i.Keywords = fd.ImageInfo.DetailInfo.Keywords i.Size = fd.ImageInfo.DetailInfo.Size i.Description = fd.ImageInfo.DetailInfo.Description } - func (i *ImageGridData) ToData() Data { return Data{ - RepoName: i.RepoName, Name: i.Name, + Type: i.Type, + RepoName: i.RepoName, Icon: i.Icon, Keywords: i.Keywords, Rating: i.Rating, @@ -107,6 +113,7 @@ func (i *ImageGridData) ToData() Data { type ImageDetailData struct { Name ImageName `json:"name,omitempty"` + Type ImageType `json:"type,omitempty"` RepoName RepoName `json:"repoName,omitempty"` Rating int `json:"rating,omitempty"` Icon string `json:"icon,omitempty"` @@ -121,6 +128,7 @@ type ImageDetailData struct { func (i *ImageDetailData) New(fd *FullData) { i.Name = fd.ImageInfo.Name + i.Type = fd.ImageInfo.Type i.RepoName = fd.RepoInfo.Name i.Icon = fd.ImageInfo.DetailInfo.Icon @@ -135,8 +143,9 @@ func (i *ImageDetailData) New(fd *FullData) { } func (i *ImageDetailData) ToData() Data { return Data{ - RepoName: i.RepoName, Name: i.Name, + Type: i.Type, + RepoName: i.RepoName, Icon: i.Icon, Keywords: i.Keywords, Size: i.Size, diff --git a/controllers/imagehub/api/v1/image_types.go b/controllers/imagehub/api/v1/image_types.go index 0944f715b..cc1e0f85f 100644 --- a/controllers/imagehub/api/v1/image_types.go +++ b/controllers/imagehub/api/v1/image_types.go @@ -32,6 +32,13 @@ type TagData struct { CTime metav1.Time `json:"creatTime"` // todo inspect image and get time } +type ImageType string + +const ( + CloudImageType ImageType = "cloud-image" + ClusterImageType ImageType = "cluster-image" +) + type ImageName string // IsLegal check name is legal @@ -120,6 +127,7 @@ type ImageSpec struct { //+kubebuilder:validation:Required Name ImageName `json:"name,omitempty"` + Type ImageType `json:"type,omitempty"` DetailInfo ImageDetailInfo `json:"detail,omitempty"` } @@ -162,6 +170,21 @@ func (i *Image) getName() string { return i.Name } +func (i *Image) MulateFromOldobj(old *Image) { + if i.Spec.DetailInfo.Docs == "" { + i.Spec.DetailInfo.Docs = old.Spec.DetailInfo.Docs + } + if i.Spec.DetailInfo.Icon == "" { + i.Spec.DetailInfo.Icon = old.Spec.DetailInfo.Icon + } + if i.Spec.DetailInfo.Description == "" { + i.Spec.DetailInfo.Description = old.Spec.DetailInfo.Description + } + if len(i.Spec.DetailInfo.Keywords) == 0 { + i.Spec.DetailInfo.Keywords = old.Spec.DetailInfo.Keywords + } +} + //+kubebuilder:object:root=true // ImageList contains a list of Image diff --git a/controllers/imagehub/api/v1/image_webhook.go b/controllers/imagehub/api/v1/image_webhook.go index 60dbdfab1..f30fdff3b 100644 --- a/controllers/imagehub/api/v1/image_webhook.go +++ b/controllers/imagehub/api/v1/image_webhook.go @@ -35,7 +35,7 @@ import ( var imagelog = logf.Log.WithName("image-resource") func (i *Image) SetupWebhookWithManager(mgr ctrl.Manager) error { - m := &ImageMutator{} + m := &ImageMutator{Client: mgr.GetClient()} v := &ImageValidator{Client: mgr.GetClient()} return ctrl.NewWebhookManagedBy(mgr). For(i). @@ -45,8 +45,10 @@ func (i *Image) SetupWebhookWithManager(mgr ctrl.Manager) error { } //+kubebuilder:webhook:path=/mutate-imagehub-sealos-io-v1-image,mutating=true,failurePolicy=fail,sideEffects=None,groups=imagehub.sealos.io,resources=images,verbs=create;update,versions=v1,name=mimage.kb.io,admissionReviewVersions=v1 +//+kubebuilder:object:generate=false type ImageMutator struct { + client.Client } func (m *ImageMutator) Default(ctx context.Context, obj runtime.Object) error { @@ -59,6 +61,15 @@ func (m *ImageMutator) Default(ctx context.Context, obj runtime.Object) error { img.ObjectMeta.Labels[SealosOrgLable] = img.Spec.Name.GetOrg() img.ObjectMeta.Labels[SealosRepoLabel] = img.Spec.Name.GetRepo() img.ObjectMeta.Labels[SealosTagLabel] = img.Spec.Name.GetTag() + + oldimg := &Image{} + oldimg.Name = img.Name + err := m.Get(ctx, client.ObjectKeyFromObject(oldimg), oldimg) + if err != nil { + return client.IgnoreNotFound(err) + } + // mulate image cr + img.MulateFromOldobj(oldimg) return nil } diff --git a/controllers/imagehub/api/v1/zz_generated.deepcopy.go b/controllers/imagehub/api/v1/zz_generated.deepcopy.go index 8b62fa0bc..607716cfe 100644 --- a/controllers/imagehub/api/v1/zz_generated.deepcopy.go +++ b/controllers/imagehub/api/v1/zz_generated.deepcopy.go @@ -371,21 +371,6 @@ func (in *ImageList) DeepCopyObject() runtime.Object { return nil } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ImageMutator) DeepCopyInto(out *ImageMutator) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageMutator. -func (in *ImageMutator) DeepCopy() *ImageMutator { - if in == nil { - return nil - } - out := new(ImageMutator) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ImageSpec) DeepCopyInto(out *ImageSpec) { *out = *in diff --git a/controllers/imagehub/config/crd/bases/imagehub.sealos.io_datapacks.yaml b/controllers/imagehub/config/crd/bases/imagehub.sealos.io_datapacks.yaml index a47511786..158564441 100644 --- a/controllers/imagehub/config/crd/bases/imagehub.sealos.io_datapacks.yaml +++ b/controllers/imagehub/config/crd/bases/imagehub.sealos.io_datapacks.yaml @@ -110,6 +110,8 @@ spec: - size type: object type: array + type: + type: string type: object description: Datas in datapack status type: object diff --git a/controllers/imagehub/config/crd/bases/imagehub.sealos.io_images.yaml b/controllers/imagehub/config/crd/bases/imagehub.sealos.io_images.yaml index b6420efcf..34c53143c 100644 --- a/controllers/imagehub/config/crd/bases/imagehub.sealos.io_images.yaml +++ b/controllers/imagehub/config/crd/bases/imagehub.sealos.io_images.yaml @@ -80,6 +80,8 @@ spec: type: object name: type: string + type: + type: string type: object status: description: ImageStatus defines the observed state of Image diff --git a/controllers/imagehub/deploy/manifests/deploy.yaml b/controllers/imagehub/deploy/manifests/deploy.yaml index 67a2fd6c2..d53fafcda 100644 --- a/controllers/imagehub/deploy/manifests/deploy.yaml +++ b/controllers/imagehub/deploy/manifests/deploy.yaml @@ -132,6 +132,8 @@ spec: - size type: object type: array + type: + type: string type: object description: Datas in datapack status type: object @@ -241,6 +243,8 @@ spec: type: object name: type: string + type: + type: string type: object status: description: ImageStatus defines the observed state of Image